DEPLOY LARAVEL PROJECT TO AMAZON EC2

Deploy Laravel project to Amazon EC2

Hello guys, today i'll show you how to deploy Laravel project to Amazon EC2

1. Prerequisite

  • EC2 instance (installed Ubuntu, Nginx)
  • VPC setup for EC2 instance can access ssh
  • RDS for mysql (check to the option "Connect to exist EC2 instance")

2. Install environment for Laravel

2.1. Connect to EC2 instance via ssh

ssh -i "<examplekey.pem>" <username>@<EC2 Address>

2.2. Copy source code to EC2 instance

  • Clone from github repository

    git clone <link github repository>
  • (Optional) Copy source from local using scp

    • Install zip, unzip from local machine

      sudo apt install zip
    • Compress source files using zip command:

      zip -r <zipfilename.zip> <folderpath>
    • Copy source from local to EC2 instance using scp command:

      scp -i "<examplekey.pem>" <zipfilename.zip> <username>@<EC2 Address>:<pathtosave>
    • Extract source files using unzip command:

      unzip <zipfilename.zip>

2.3. Install PHP dependencies based on your project. For example

sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install -y php7.3

2.4. Install mysql client to connect RDS instance

sudo apt install mysql-client-core-8.0
  • Connect to RDS instance from EC2 via mysql client by following command and type your password

    mysql -h <RDS Address Instance> -P <port | 3306> -u <masteruser> -p
    • Create your database name based on the project
    CREATE DATABASE ;
    EXIT;

2.5. Config your environment file to match your RDS server

...
APP_URL=<URL of your EC2 instance>
APP_DEBUG=false

DB_CONNECTION=mysql
DB_HOST=<RDS Address link>
DB_PORT=3306
DB_DATABASE=<databasename>
DB_USERNAME=<master username>
DB_PASSWORD=<password>
...

2.6. Install Composer

sudo apt-get install -y composer

2.7. Go to project directory and install dependencies

composer update 
composer install

2.8. Generate application key

php artisan key:generate

2.9. Copy directory to /var/www/

cp <foldername> /var/www/

2.10. Update permissions

sudo chown -R www-data:www-data /var/www/<foldername>
sudo chmod -R 755 <foldername>

3. Setting up Nginx

3.1. Configure Nginx to serve the content

sudo vi /etc/nginx/sites-available/<foldername>
  • Then you can enter the following setting:
server {
    listen 80;
    server_name ;
    root /var/www//public;

    // Focus this log if you see error
    access_log /var/log/nginx/laravel-access.log;
    error_log /var/log/nginx/laravel-error.log;


    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 = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}
sudo ln -s /etc/nginx/sites-available/<foldername> /etc/nginx/sites-enabled/

3.3. To confirm that the configuration doesn’t contain any syntax errors, you can use

sudo nginx -t

3.4. To apply the changes, reload Nginx with

sudo systemctl reload nginx

4. (Optional) Fix some error when configuring

4.1. By default, EC2 ubuntu not enabled php-fpm, run following command to enable

sudo a2enmod proxy_fcgi setenvif
sudo service nginx restart

Check that package PHP-FPM is exist

sudo vi /etc/php/<version>/fpm/pool.d/www.conf

Then you find this line and check that path is correct (file .sock must exists in the folder /var/run/php/)

listen = /var/run/php/php<version>-fpm.sock

Restart the service

sudo service php<version>-fpm restart

Setting EC2, RDS

1. Create EC2

  • AMI: Ubuntu
  • Instance type: t2.micro (free tier)

Create EC2

  • Select existing security group or you can change later

Create EC2

2. Create Security Group

We allow SSH, ICMP and TCP from anywhere and in Outbound we allow anything

Create Security Group

3. Create Route table

Route the traffic to the internet gateway (igw)

Create Route table

4. Create RDS

Create database for our application

Route table

Choose your DB instance type

Select RDS engine type

Check the option connection to the EC2 instance

Check connection

After all you can access your application by enter your public IP EC2 address in the browser. Enjoy it 😉