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
- Update the package list:
sudo apt update
- Install necessary dependencies:
sudo apt install -y curl wget unzip git
Setting Up Nginx
- Install Nginx:
sudo apt install -y nginx
- Start and enable Nginx:
sudo systemctl start nginx
sudo systemctl enable nginx
- Adjust the firewall to allow HTTP and HTTPS traffic:
sudo ufw allow ‘Nginx Full’
Installing PHP 8.2 and PHP-FPM
- Add the PHP repository:
sudo add-apt-repository ppa:ondrej/php
sudo apt update
- 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
- Edit PHP-FPM configuration:
sudo nano /etc/php/8.2/fpm/php.ini
- Set the timezone and other necessary settings:
date.timezone = UTC
- Restart PHP-FPM:
sudo systemctl restart php8.2-fpm
Setting Up Composer
- Download and install Composer:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
- Verify the installation:
composer –version
Installing Node.js
- Install Node.js and npm:
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash –
sudo apt install -y nodejs
- Verify the installation:
node -v
npm -v
Deploying Your Laravel Application
- Clone your Laravel project from your repository:
git clone https://github.com/yourusername/your-laravel-app.git
cd your-laravel-app
- Install PHP dependencies:
composer install
- Install Node.js dependencies:
npm install
- Generate application key:
php artisan key:generate
Managing Assets with Vite
- Build your assets for production:
npm run build
Configuring Laravel for AWS
- Edit the .env file to set the correct environment variables for your AWS setup, including database credentials and other necessary configurations.
- 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
- Edit the Nginx configuration file:
sudo nano /etc/nginx/sites-available/default
- 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;
}
}
- Test the Nginx configuration:
sudo nginx -t
- 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