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.
Need help with this?
Our team handles this kind of work daily. Let us take care of your infrastructure.
Related Articles
Why MySQL Fails to Start: A Guide to Common Errors and Solutions on Ubuntu
A practical walkthrough for diagnosing and resolving MySQL and MariaDB startup failures on Ubuntu, covering service status checks, log analysis, permission issues, and configuration repairs.
LaravelFrom 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.
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.