DEV Community

Quang Nam Nguyen
Quang Nam Nguyen

Posted on

Deploy Laravel project to Amazon EC2

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>
Enter fullscreen mode Exit fullscreen mode

2.2. Copy source code to EC2 instance

  • Clone from github repository
  git clone <link github repository>
Enter fullscreen mode Exit fullscreen mode
  • (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
Enter fullscreen mode Exit fullscreen mode

2.4. Install mysql client to connect RDS instance

sudo apt install mysql-client-core-8.0
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • Create your database name based on the project
  CREATE DATABASE <databasename>;
  EXIT;
Enter fullscreen mode Exit fullscreen mode

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>
...
Enter fullscreen mode Exit fullscreen mode

2.6. Install Composer

sudo apt-get install -y composer
Enter fullscreen mode Exit fullscreen mode

2.7. Go to project directory and install dependencies

composer update 
composer install
Enter fullscreen mode Exit fullscreen mode

2.8. Generate application key

php artisan key:generate
Enter fullscreen mode Exit fullscreen mode

2.9. Copy directory to /var/www/

cp <foldername> /var/www/
Enter fullscreen mode Exit fullscreen mode

2.10. Update permissions

sudo chown -R www-data:www-data /var/www/<foldername>
sudo chmod -R 755 <foldername>
Enter fullscreen mode Exit fullscreen mode

3. Setting up Nginx

3.1. Configure Nginx to serve the content

sudo vi /etc/nginx/sites-available/<foldername>
Enter fullscreen mode Exit fullscreen mode
  • Then you can enter the following setting:
server {
    listen 80;
    server_name <server_domain_or_IP>;
    root /var/www/<foldername>/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;
    }
}
Enter fullscreen mode Exit fullscreen mode

3.2. To activate the new virtual host configuration file, create a symbolic link to foldername in sites-enabled

sudo ln -s /etc/nginx/sites-available/<foldername> /etc/nginx/sites-enabled/
Enter fullscreen mode Exit fullscreen mode

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

sudo nginx -t
Enter fullscreen mode Exit fullscreen mode

3.4. To apply the changes, reload Nginx with

sudo systemctl reload nginx
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Check that package PHP-FPM is exist

sudo vi /etc/php/<version>/fpm/pool.d/www.conf
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Restart the service

sudo service php<version>-fpm restart
Enter fullscreen mode Exit fullscreen mode

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

Select SG

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

Create database for our application

Choose your DB instance type

Choose your DB instance type

Check the option connection to the EC2 instance

Check the option connection to the EC2 instance

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

Top comments (1)

Collapse
 
ivanpeace85 profile image
🚩Ivan Amado🚩

very good!!!