Deploying a Laravel application on AWS can be straightforward if you follow the proper steps. This guide will walk you through everything you need to know, from setting up your environment to configuring your application for optimal performance on AWS.

Overview

AWS provides a robust infrastructure for deploying applications, offering scalability, security, and reliability. This guide focuses on deploying a Laravel application, a popular PHP framework known for its elegant syntax and developer-friendly features.

Requirements and Setup

Before starting, ensure you have the following:

  • An AWS account
  • An EC2 instance running a Linux distribution (preferably Amazon Linux 2 or Ubuntu)
  • SSH access to your EC2 instance
  • Basic knowledge of Linux command-line operations

Preparation for PHP and Nginx Installation

  1. Update the package list:

    sudo apt update
  1. Install necessary dependencies:

    sudo apt install -y curl wget unzip git

Setting Up Nginx

  1. Install Nginx:

    sudo apt install -y nginx
  1. Start and enable Nginx:

    sudo systemctl start nginx

sudo systemctl enable nginx

  1. Adjust the firewall to allow HTTP and HTTPS traffic:

    sudo ufw allow ‘Nginx Full’

Installing PHP 8.2 and PHP-FPM

  1. Add the PHP repository:

    sudo add-apt-repository ppa:ondrej/php

sudo apt update

  1. Install PHP 8.2 and PHP-FPM:

    sudo apt install -y php8.2 php8.2-fpm php8.2-cli php8.2-mbstring php8.2-xml php8.2-mysql php8.2-zip php8.2-curl php8.2-gd

Configuring PHP-FPM

  1. Edit PHP-FPM configuration:

    sudo nano /etc/php/8.2/fpm/php.ini
  1. Set the timezone and other necessary settings:

    date.timezone = UTC
  1. Restart PHP-FPM:

    sudo systemctl restart php8.2-fpm

Setting Up Composer

  1. Download and install Composer:

    curl -sS https://getcomposer.org/installer | php

sudo mv composer.phar /usr/local/bin/composer

  1. Verify the installation:

    composer –version

Installing Node.js

  1. Install Node.js and npm:

    curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash –

sudo apt install -y nodejs

  1. Verify the installation:

    node -v

npm -v

Deploying Your Laravel Application

  1. Clone your Laravel project from your repository:

    git clone https://github.com/yourusername/your-laravel-app.git

cd your-laravel-app

  1. Install PHP dependencies:

    composer install
  1. Install Node.js dependencies:

    npm install
  1. Generate application key:

    php artisan key:generate

Managing Assets with Vite

  1. Build your assets for production:

    npm run build

Configuring Laravel for AWS

  1. Edit the .env file to set the correct environment variables for your AWS setup, including database credentials and other necessary configurations.
  1. Set up your database on AWS RDS and update the .env file accordingly:

    DB_CONNECTION=mysql

DB_HOST=your-rds-endpoint

DB_PORT=3306

DB_DATABASE=your-database-name

DB_USERNAME=your-username

DB_PASSWORD=your-password

Nginx Configuration for Laravel

  1. Edit the Nginx configuration file:

    sudo nano /etc/nginx/sites-available/default
  1. Configure the server block for your Laravel application:

    server {

    listen 80;

    server_name your_domain_or_IP;

    root /var/www/html/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 $document_root$fastcgi_script_name;

        include fastcgi_params;

    }

    location ~ /\.ht {

        deny all;

    }

}

  1. Test the Nginx configuration:

    sudo nginx -t
  1. Reload Nginx to apply the changes:

    sudo systemctl reload nginx

Final Thoughts

Deploying a Laravel application on AWS involves multiple steps, from setting up your server environment to configuring your application and web server. By following this comprehensive guide, you can ensure a smooth deployment process and leverage AWS’s robust infrastructure for your Laravel applications.

References

Deploying a Laravel application to Elastic Beanstalk

The serverless LAMP stack part 4: Building a serverless Laravel application