A complete walkthrough of deploying a Laravel application on AWS EC2 using Laravel Forge, covering instance setup, Forge configuration, environment variables, SSL, and production best practices.
Step-by-Step Guide to Deploying a Laravel App on AWS with Laravel Forge
Deploying a Laravel application on AWS using Laravel Forge simplifies server management while providing a reliable and scalable hosting solution. This guide walks through the process of setting up an EC2 instance, configuring Laravel Forge, and deploying the application seamlessly.
Overview
We will cover:
- Launching an AWS EC2 instance for the Laravel app.
- Configuring Laravel Forge to manage the server.
- Deploying the Laravel application.
Prerequisites
- An AWS account.
- A Laravel Forge subscription.
- A Laravel application ready for deployment (e.g., in a GitHub repository).
Setting Up AWS
Step 1: Launch an EC2 Instance
In the AWS Management Console:
- Navigate to EC2 > Launch Instance.
- Select Ubuntu 22.04 LTS as the AMI.
- Choose an instance type (e.g.,
t3.mediumfor moderate workloads). - Configure the security group to allow inbound traffic on ports 22 (SSH), 80 (HTTP), and 443 (HTTPS).
- Create or select a key pair for SSH access.
- Launch the instance and note its public IP address.
Step 2: Connect to the EC2 Instance
ssh -i /path/to/key.pem ubuntu@your-ec2-instance-ip
Configuring Laravel Forge
Step 1: Connect AWS to Laravel Forge
In Laravel Forge:
- Navigate to Servers and click Create Server.
- Choose Custom VPS.
- Enter the EC2 instance's IP address and SSH key information.
- Click Install Forge to configure the server.
Step 2: Configure Your Server
Once Laravel Forge connects to the EC2 instance:
- Set up a default site.
- Enable SSL (via Let's Encrypt) for secure connections.
- Install any necessary software (e.g., PHP, MySQL).
Deploying Your Laravel Application
Step 1: Add the Laravel Repository
In Forge:
- Navigate to the server and click Sites.
- Click Create Site and enter the domain or subdomain.
- Under Deployment, add the GitHub repository URL.
- Provide the SSH key to authenticate with GitHub.
Step 2: Configure Environment Variables
Add the Laravel environment variables in the Forge UI, including database credentials and application keys:
APP_NAME=Laravel
APP_ENV=production
APP_KEY=base64:your-key
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=forge
DB_USERNAME=forge
DB_PASSWORD=your-password
Step 3: Deploy the Application
Click Deploy in Forge to start the deployment process. Forge will:
- Clone the Laravel repository.
- Run
composer installandphp artisan migrate. - Start serving the application.
Testing the Laravel Application
After deployment:
- Visit the domain in a browser to confirm the application is live.
- Check logs using the Forge interface for any errors.
- Run additional tests to verify application functionality.
Best Practices for Production
- Enable Automatic Backups: Use Forge's backup feature to store database snapshots.
- Set Up Monitoring: Monitor server metrics like CPU and memory usage in Forge.
- Optimize Performance: Use
php artisan config:cacheandphp artisan route:cachefor optimized configuration and routing. - Secure the Server: Regularly update the instance and use a strong SSH key.
By following this guide, we have deployed a Laravel application on AWS using Laravel Forge. This setup simplifies server management while providing scalability and robust deployment workflows. With Forge handling provisioning, SSL, and deployments, we can focus on building features rather than managing infrastructure.
Frequently asked questions
What do I need to deploy a Laravel app on AWS with Laravel Forge?
Deploying a Laravel app on AWS with Forge takes an AWS account, a Laravel Forge subscription and a Laravel application ready for deployment, for example in a GitHub repository. The app runs on an EC2 instance launched from Ubuntu 22.04 LTS, which Forge manages over SSH once it is added as a Custom VPS.
Which EC2 instance settings work for a Laravel Forge server?
Launch an instance from the Ubuntu 22.04 LTS AMI and pick an instance type such as t3.medium for moderate workloads. Configure the security group to allow inbound traffic on ports 22 for SSH, 80 for HTTP and 443 for HTTPS, create or select a key pair, and note the public IP address after launch.
How do I connect an AWS EC2 instance to Laravel Forge?
In Forge, go to Servers, click Create Server and choose Custom VPS. Enter the EC2 instance's IP address and SSH key information, then click Install Forge so Forge configures the server. After it connects, set up a default site and install any software the app needs, such as PHP and MySQL.
How do I add a Laravel GitHub repository to a Forge site?
Open the server in Forge, click Sites, then Create Site and enter the domain or subdomain. Under Deployment, add the GitHub repository URL and provide the SSH key Forge uses to authenticate with GitHub. Forge deploys the site from that repository each time Deploy is clicked.
Where do I set Laravel environment variables on Forge?
Laravel environment variables go in the Forge UI, including database credentials and application keys. A typical production set has APP_ENV=production, an APP_KEY with a base64 key, DB_CONNECTION=mysql, DB_HOST=127.0.0.1 and DB_PORT=3306, with DB_DATABASE and DB_USERNAME both set to forge and DB_PASSWORD set to the real password.
What does Laravel Forge do when I click Deploy?
Forge clones the Laravel repository, runs composer install and php artisan migrate, and starts serving the application. Afterwards, visit the domain in a browser to confirm the app is live, check the logs in the Forge interface for errors, and run further tests to verify the application works.
How do I secure a Laravel server on AWS managed by Forge?
Limit the EC2 security group to inbound ports 22, 80 and 443, and enable SSL through Let's Encrypt in Forge once the server is connected. Update the instance regularly and use a strong SSH key for access.
What production settings should a Laravel app on Forge use?
Turn on Forge's backup feature to store database snapshots, and watch server metrics such as CPU and memory usage in Forge. Run php artisan config:cache and php artisan route:cache to optimize configuration loading and routing. Make sure APP_ENV is set to production in the Forge environment variables.
Read how we handle it in AWS Cloud Management.
Related Articles
From Code to Production: A Guide to Automating Laravel Deployments with GitHub Actions
Learn how to build a fully automated CI/CD pipeline for Laravel using GitHub Actions, covering SSH key setup, workflow configuration, testing, and deployment to production servers.
LaravelLaravel Octane Performance Optimization Guide
Boost Laravel throughput 10x with Octane using Swoole and RoadRunner, connection pooling, memory management, and production deployment strategies.
LaravelMastering Laravel Workers: A Step-by-Step Guide to Setting Up a Laravel Worker Server on Ubuntu
A comprehensive guide to setting up Laravel queue workers on Ubuntu, covering Redis configuration, job creation, Supervisor process management, Horizon monitoring, scaling, and troubleshooting.