Table of Contents
Introduction
Deploying a Laravel application can be a complex task, especially when moving from a local development environment to a production server. AWS EC2 provides a flexible and scalable solution for hosting your applications. This guide will walk you through the process of deploying a Laravel application on an AWS EC2 instance.
Prerequisites
Before you begin, ensure you have the following:
- An AWS account.
- Basic knowledge of Linux commands.
- Your Laravel application ready for deployment (e.g., pushed to a Git repository).
Step 1: Launch an EC2 Instance
- Log in to AWS Management Console: Navigate to the EC2 dashboard.
- Launch Instance: Click on “Launch Instance”.
- Choose an Amazon Machine Image (AMI): Select an Ubuntu Server AMI (e.g., Ubuntu Server 22.04 LTS).
- Choose an Instance Type: Select an appropriate instance type based on your application’s needs (e.g.,
t2.microfor testing). - Configure Instance Details: Keep the defaults for most settings. Ensure “Auto-assign Public IP” is enabled.
- Add Storage: Default storage (8GB) is usually sufficient for a basic Laravel app.
- Add Tags: (Optional) Add tags like
Name: LaravelAppServer. - Configure Security Group: Create a new security group or select an existing one. Ensure the following ports are open:
- SSH (Port 22): For connecting to your instance.
- HTTP (Port 80): For web traffic.
- HTTPS (Port 443): For secure web traffic (recommended).
- Review and Launch: Review your settings and click “Launch”.
- Create a new key pair: Download the
.pemfile. This key is crucial for SSH access.
Step 2: Connect to Your EC2 Instance
Open your terminal and use the following command to connect to your instance. Replace your-key.pem with your key pair file and your-instance-ip with your EC2 instance’s public IP address.
chmod 400 your-key.pem
ssh -i "your-key.pem" ubuntu@your-instance-ip
Step 3: Install Web Server (Nginx), PHP, and Composer
Once connected, update your package list and install necessary software.
sudo apt update
sudo apt upgrade -y
# Install Nginx
sudo apt install nginx -y
# Install PHP and extensions (adjust version if needed, e.g., php8.1-fpm)
sudo apt install php8.1-fpm php8.1-mysql php8.1-mbstring php8.1-xml php8.1-bcmath php8.1-curl php8.1-zip php8.1-gd -y
# Install Composer
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
Step 4: Install MySQL Server
sudo apt install mysql-server -y
sudo mysql_secure_installation
Follow the prompts to secure your MySQL installation. Remember the root password.
Step 5: Deploy Your Laravel Application
- Clone your repository: Navigate to
/var/www/and clone your Laravel project.
sudo mkdir -p /var/www/your-laravel-app
cd /var/www/your-laravel-app
sudo git clone https://github.com/your-username/your-laravel-app.git .
- Set permissions: Ensure Nginx and PHP-FPM have proper permissions.
sudo chown -R www-data:www-data /var/www/your-laravel-app
sudo chmod -R 775 /var/www/your-laravel-app/storage
sudo chmod -R 775 /var/www/your-laravel-app/bootstrap/cache
- Install Composer dependencies: From your project root (
/var/www/your-laravel-app).
composer install --no-dev --optimize-autoloader
- Configure
.envfile: Copy.env.exampleto.envand update your database credentials.
sudo cp .env.example .env
sudo nano .env
Update DB_DATABASE, DB_USERNAME, DB_PASSWORD.
- Generate Application Key:
php artisan key:generate
- Run Migrations and Seeders:
php artisan migrate --seed
Step 6: Configure Nginx
Create a new Nginx configuration file for your Laravel application.
sudo nano /etc/nginx/sites-available/your-laravel-app
Add the following content:
server {
listen 80;
server_name your-instance-ip your-domain.com;
root /var/www/your-laravel-app/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
Enable the site and restart Nginx:
sudo ln -s /etc/nginx/sites-available/your-laravel-app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
Step 7: Configure Database
Connect to MySQL and create a database and user for your Laravel application.
sudo mysql -u root -p
CREATE DATABASE your_database_name CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_username'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Remember to update your .env file with these new credentials.
Conclusion
You have successfully deployed your Laravel application on an AWS EC2 instance. This guide covered the essential steps from launching an instance to configuring your web server and database. For production environments, consider adding SSL, a domain name, and more robust security measures.