Deploying Laravel to AWS EC2
LaravelDeploying a Laravel application to production on AWS EC2 with Nginx and PHP-FPM — the same stack used in real production environments.
Server Requirements
| Component | Version |
|---|---|
| OS | Amazon Linux 2023 / Ubuntu 22.04 |
| PHP | 8.2+ |
| Nginx | Latest stable |
| MySQL | 8.0+ (or RDS) |
| Composer | Latest |
1. Server Setup (Amazon Linux 2023)
SSH — Server Setup
# Update system
sudo dnf update -y
# Install PHP 8.2 and extensions
sudo dnf install -y php8.2 php8.2-fpm php8.2-mysqlnd \
php8.2-mbstring php8.2-xml php8.2-zip \
php8.2-bcmath php8.2-curl php8.2-redis
# Install Nginx
sudo dnf install -y nginx
# Install MySQL client
sudo dnf install -y mysql
# Install Composer
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
# Install Git
sudo dnf install -y git2. Configure Nginx
/etc/nginx/conf.d/laravel.conf
server {
listen 80;
server_name your-domain.com www.your-domain.com;
root /var/www/html/public;
index index.php;
# Laravel URL rewriting
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# PHP-FPM
location ~ \.php$ {
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
# Deny .htaccess access
location ~ /\.ht {
deny all;
}
# Cache static assets
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
}3. Deploy Your Laravel App
SSH — Deploy
# Clone your repo
cd /var/www
sudo git clone https://gitlab.com/yourname/myapp.git html
cd html
# Install dependencies
sudo composer install --optimize-autoloader --no-dev
# Set up .env
sudo cp .env.example .env
sudo php artisan key:generate
# Edit .env with your production values
sudo nano .env
# Set permissions
sudo chown -R nginx:nginx /var/www/html
sudo chmod -R 755 /var/www/html/storage
sudo chmod -R 755 /var/www/html/bootstrap/cache
# Run migrations
sudo php artisan migrate --force
# Optimize
sudo php artisan config:cache
sudo php artisan route:cache
sudo php artisan view:cache
# Start services
sudo systemctl start php-fpm
sudo systemctl start nginx
sudo systemctl enable php-fpm
sudo systemctl enable nginx4. Environment File (.env)
.env (Production)
APP_NAME="My Laravel App"
APP_ENV=production
APP_DEBUG=false
APP_URL=https://your-domain.com
DB_CONNECTION=mysql
DB_HOST=your-rds-endpoint.amazonaws.com
DB_PORT=3306
DB_DATABASE=myapp
DB_USERNAME=admin
DB_PASSWORD=your-secure-password
CACHE_DRIVER=redis
SESSION_DRIVER=redis
QUEUE_CONNECTION=redis
REDIS_HOST=your-redis-endpoint.amazonaws.com
REDIS_PORT=6379
MAIL_MAILER=ses
AWS_ACCESS_KEY_ID=your-key
AWS_SECRET_ACCESS_KEY=your-secret
AWS_DEFAULT_REGION=ap-northeast-15. Auto-Deploy with GitLab CI/CD
.gitlab-ci.yml
deploy:
stage: deploy
only:
- main
script:
- ssh -i $EC2_KEY ubuntu@$EC2_IP "
cd /var/www/html &&
git pull origin main &&
composer install --no-dev --optimize-autoloader &&
php artisan migrate --force &&
php artisan config:cache &&
php artisan route:cache &&
php artisan view:cache &&
sudo systemctl reload php-fpm
"6. SSL with Certbot (HTTPS)
Terminal
# Install Certbot
sudo dnf install -y certbot python3-certbot-nginx
# Get free SSL certificate
sudo certbot --nginx -d your-domain.com -d www.your-domain.com
# Auto-renew (add to crontab)
0 12 * * * certbot renew --quiet💡
This is exactly what you do at work with ms-cms! AWS EC2 + Nginx + PHP-FPM + RDS MySQL is the standard production stack. You already have hands-on experience with this — that's a huge advantage.
🎉
Course Complete! You've finished all PHP & Laravel lessons — from variables to production deployment. You're now a full-stack PHP/Laravel developer. Keep building projects and applying what you've learned!
Test your knowledge!
Take the Laravel quiz.