Ultimate Guide: How to Deploy Laravel 11/12 Application on AWS EC2 with MySQL (Step-by-Step)
๐ Introduction
Deploying your Laravel 11 or Laravel 12 application on AWS (Amazon Web Services) can feel overwhelming at first, but itโs one of the best decisions for scalability, security, and performance. AWS provides flexible cloud computing infrastructure that can handle everything from small projects to enterprise-grade applications.
In this complete step-by-step guide, weโll walk you through deploying a Laravel 11/12 project on AWS EC2 instance using MySQL as the database, along with best practices for optimization, SSL setup, and environment configuration.
๐งฉ What Youโll Learn in This Guide
โ
Launching an EC2 instance
โ
Installing PHP, Composer, and Laravel dependencies
โ
Setting up Nginx or Apache for Laravel
โ
Configuring MySQL on AWS
โ
Managing .env and Laravel configurations
โ
Running migrations and seeding the database
โ
Setting up domain and SSL (Letโs Encrypt)
โ
Troubleshooting deployment issues
โ๏ธ Step 1: Create and Configure AWS EC2 Instance
- Log in to your AWS Management Console.
- Navigate to EC2 โ Instances โ Launch Instance.
- Choose Ubuntu 22.04 LTS (recommended) as your operating system.
- Select an instance type โ for small Laravel projects,
t2.micro(free-tier) works fine. - Configure:
- Storage: 20โ30 GB SSD recommended
- Security Group: Open port 22 (SSH), 80 (HTTP), 443 (HTTPS)
- Add your key pair (download
.pemfile) for SSH access. - Click Launch Instance.
๐ Step 2: Connect to Your Instance via SSH
Open your terminal and connect using:
ssh -i /path/to/your-key.pem ubuntu@your-ec2-public-ipTip: Replace
your-key.pemwith the correct path and ensure correct permissions usingchmod 400 your-key.pem.
๐งฑ Step 3: Install Required Packages (PHP, Composer, Git)
Update packages and install essential dependencies:
sudo apt update && sudo apt upgrade -y
sudo apt install nginx git unzip curl -yNow, install PHP (Laravel 11/12 supports PHP 8.2+):
sudo apt install php8.2 php8.2-fpm php8.2-cli php8.2-mbstring php8.2-xml php8.2-curl php8.2-mysql php8.2-bcmath php8.2-zip -yCheck PHP version:
php -vThen install Composer globally:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer๐๏ธ Step 4: Clone Your Laravel Project from GitHub or GitLab
cd /var/www
sudo git clone https://github.com/yourusername/your-laravel-app.git
cd your-laravel-app๐ง Step 5: Set File Permissions
Laravel requires the storage and bootstrap/cache directories to be writable.
sudo chown -R www-data:www-data /var/www/your-laravel-app
sudo chmod -R 775 storage bootstrap/cache๐ Step 6: Set Up Your .env File
Copy the example file and edit your environment variables.
cp .env.example .env
nano .envUpdate these lines:
APP_NAME="Laravel AWS"
APP_ENV=production
APP_KEY=
APP_DEBUG=false
APP_URL=http://your-ec2-public-ip
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=StrongPassword123Generate Laravel app key:
php artisan key:generate๐๏ธ Step 7: Install and Configure MySQL Database
Install MySQL server:
sudo apt install mysql-server -y
sudo mysql_secure_installationLog in and create a database:
sudo mysql -u root -pCREATE DATABASE laravel_db;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'StrongPassword123';
GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;๐งฐ Step 8: Install Laravel Dependencies and Run Migration
Inside your project folder:
composer install --no-dev --optimize-autoloader
php artisan migrate --seed
php artisan config:cache
php artisan route:cache๐ Step 9: Configure Nginx for Laravel
Create a new Nginx configuration file:
sudo nano /etc/nginx/sites-available/laravel.confAdd this configuration:
server {
listen 80;
server_name your-domain.com;
root /var/www/your-laravel-app/public;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}Enable the configuration:
sudo ln -s /etc/nginx/sites-available/laravel.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx๐ Step 10: Set Up SSL with Letโs Encrypt (Optional but Recommended)
Install Certbot:
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your-domain.com -d www.your-domain.comAutomatic renewal:
sudo systemctl enable certbot.timerโ๏ธ Step 11: Setup Supervisor for Queue Workers (Optional)
If your Laravel app uses queues:
sudo apt install supervisor -y
sudo nano /etc/supervisor/conf.d/laravel-worker.confAdd this configuration:
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/your-laravel-app/artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=true
user=www-data
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/your-laravel-app/storage/logs/worker.logThen restart supervisor:
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*๐งน Step 12: Final Optimization & Health Check
Run:
php artisan optimize
sudo systemctl restart nginxVisit your EC2 public IP or domain, and your Laravel 11/12 app should load perfectly. ๐
๐ก๏ธ Step 13: Enable Firewall (UFW)
To protect your server:
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo ufw status๐ Step 14: Enable Logging and Monitoring
- CloudWatch Logs: For instance performance monitoring
- Laravel Telescope / Log Viewer: For debugging and request logs
- Automatic backups: Schedule MySQL dump scripts
Example backup command:
mysqldump -u laravel_user -p laravel_db > /var/backups/laravel_db_$(date +%F).sqlโ Common Deployment Issues
| Issue | Cause | Fix |
|---|---|---|
| 403 Forbidden | Wrong folder permissions | sudo chown -R www-data:www-data /var/www/your-laravel-app |
| 500 Internal Server Error | Missing .env or wrong PHP version | Check php -v and .env |
| Database Connection Error | Wrong credentials | Verify DB user and host |
| CSS/JS not loading | Missing symbolic link | Run php artisan storage:link |
๐ง Pro Tips for Production
- Use Redis for cache/session.
- Use S3 or CloudFront for static assets.
- Set
APP_DEBUG=falsefor security. Schedule cron jobs for tasks:
* * * * * php /var/www/your-laravel-app/artisan schedule:run >> /dev/null 2>&1
๐ Conclusion
Deploying Laravel 11/12 on AWS EC2 may look complex initially, but with this step-by-step guide, you can easily manage your entire infrastructure โ from web server setup to database configuration and SSL security. AWS offers unmatched scalability and reliability, making it ideal for Laravel developers aiming for professional-grade hosting.
