0
0
LaravelHow-ToBeginner · 4 min read

How to Deploy Laravel to AWS: Step-by-Step Guide

To deploy a Laravel app to AWS, launch an EC2 instance, install PHP and dependencies, clone your app, and configure the web server. Use RDS for your database and S3 for file storage. Finally, set environment variables and run php artisan migrate to complete deployment.
📐

Syntax

Deploying Laravel to AWS involves these main steps:

  • EC2 Instance Setup: Create a virtual server to host your app.
  • Install Dependencies: PHP, Composer, web server (Apache/Nginx), and database client.
  • Clone Laravel App: Pull your code from GitHub or upload files.
  • Configure Environment: Set .env with AWS RDS and S3 details.
  • Run Migrations: Use php artisan migrate to set up database tables.
  • Serve App: Start the web server to serve your Laravel app.
bash
aws ec2 run-instances --image-id ami-0abcdef1234567890 --count 1 --instance-type t2.micro --key-name MyKeyPair --security-groups MySecurityGroup

sudo apt update && sudo apt install php php-mbstring php-xml php-bcmath php-curl php-mysql nginx git composer -y

git clone https://github.com/yourusername/your-laravel-app.git
cd your-laravel-app
composer install
cp .env.example .env
php artisan key:generate

# Edit .env to add AWS RDS and S3 credentials

php artisan migrate

sudo systemctl restart nginx
💻

Example

This example shows how to deploy a Laravel app on an Ubuntu EC2 instance with Nginx and MySQL RDS.

It demonstrates installing PHP, cloning the app, setting environment variables, and running migrations.

bash
# Connect to EC2 instance
ssh -i MyKeyPair.pem ubuntu@ec2-xx-xx-xx-xx.compute-1.amazonaws.com

# Update and install dependencies
sudo apt update && sudo apt install php php-mbstring php-xml php-bcmath php-curl php-mysql nginx git composer -y

# Clone Laravel app
git clone https://github.com/yourusername/your-laravel-app.git
cd your-laravel-app

# Install PHP packages
composer install

# Setup environment
cp .env.example .env
nano .env
# Edit DB_HOST, DB_DATABASE, DB_USERNAME, DB_PASSWORD with RDS info
# Add AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_DEFAULT_REGION for S3

# Generate app key
php artisan key:generate

# Run migrations
php artisan migrate

# Configure Nginx (example)
sudo nano /etc/nginx/sites-available/laravel
# Add server block pointing to /home/ubuntu/your-laravel-app/public

sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
sudo systemctl restart nginx

# Visit your EC2 public IP in browser to see Laravel app
Output
Laravel application homepage loads successfully in browser
⚠️

Common Pitfalls

Common mistakes when deploying Laravel to AWS include:

  • Not setting correct permissions on storage and bootstrap/cache folders, causing errors.
  • Forgetting to configure .env with correct AWS RDS and S3 credentials.
  • Not running php artisan migrate after deployment, so database tables are missing.
  • Incorrect Nginx or Apache configuration, leading to 404 or 500 errors.
  • Security group rules blocking HTTP/HTTPS traffic to EC2 instance.

Example of wrong and right permission setup:

bash
# Wrong: No write permission
sudo chown -R ubuntu:ubuntu storage bootstrap/cache

# Right: Give web server write permission
sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R 775 storage bootstrap/cache
📊

Quick Reference

Summary tips for Laravel AWS deployment:

  • Use EC2 for hosting your Laravel app server.
  • Use RDS for managed MySQL/PostgreSQL database.
  • Use S3 for storing user files and backups.
  • Always set correct permissions on storage and bootstrap/cache.
  • Configure security groups to allow HTTP/HTTPS traffic.
  • Run php artisan migrate after deployment.
  • Use environment variables in .env for AWS credentials and DB connection.

Key Takeaways

Launch an EC2 instance and install PHP, Composer, and a web server to host Laravel.
Configure your .env file with AWS RDS database and S3 storage credentials before running migrations.
Set correct permissions on storage and cache folders to avoid runtime errors.
Ensure AWS security groups allow web traffic to your EC2 instance.
Run php artisan migrate to create database tables after deployment.