How to Deploy The Laravel APP on AWS EC2

“Developer Friendly Deployment Doc’s”

This article is focused on the deployment of Laravel Application in AWS EC2 using Ubuntu 20.04 LTS Image instance in simple developer-friendly configurations.

Prerequisites to Proceed Further

  1. Basic in Larval commands
  2. Basic in Linux Commands
  3. Basic in AWS Console

Deploy the Laravel APP to AWS EC2

Login to the AWS console and search or Navigate to the EC2 console below shown image and click on the create instance for creating a server.

Next you have to create an EC2 Instance for that click on the Launch Instance Button as below image show’s

Next you have to select the image you want, here am using Ubuntu current latest 20.4 LTS (as 2021 latest) so click on the select button as the below image shows

Next you have to choose an Instance type here am going with the free tier plan you can choose as per your requirements and click on the Next Configuration button

Next you have to configure the Instance details by default AWS provide all the information as we need for advance users can change as per your requirements else keep these same and click to Add Storage button

Then next you can choose the Storage type for your project need here am using 15GB of General Purpose SSD as Storage. Then click on the Add Tag button as like the below image shows.

Next we can add Tags for the Instance we are creating to understand the instance easily (this is not mandatory if you have n number of instance you have to put tags and you can easily understand the instance with the tag name) then click on the Configure Security Group button

Next we have to configure the security group here,

  1. Security Group Name: You can Name the Security group
  2. Type: These are the inbound and outbound rules for our instance
  3. Add Rule: We can add new rules by clicking on this
  4. Port Range: These are the ports we have used for the rules make it by default or you can input your on
  5. Source: In there we can set the individual IP or Any IP which can accept the rule (choose Anywhere to access the rules from any IP if you have static IP try to put ssh with that IP then ssh can be accessed from that IP only)

Next We can review the configurations that we have review all then click on Launch button for launching

Then you will ask to create a key pair change t create a new pair and name it that

For my case the below image shows,

  1. Switch to Create New Key Pair
  2. Key Pair Name: here i have given the key pair name as my project name you can input any name
  3. Download the Key Pair: Be sure The Key Pair is safe in your local machine because you can enter to the Server initially with using this key pair only
  4. Then Click on the Launch Icon

Wait until the instance creation and click on the instance name that created to get the instance details.

The below image shows the detailed instance overview, and click on the instance ID,

  1. Instance Status
  2. Name: Can add any name to the instance

Which will navigate you to the Instance details from that,

  1. Public IP of the instance
  2. Instance State: which shows the status of the instance
  3. Connect button to connect the instance in different method

click on the connect button to get the connection details,

In that section Navigate to SSH Client and copy down the below commands i have marked in the image

You can use Putty for this but I prefer ubuntu terminal for ubuntu users and Gitbash For Windows Users and run the copied command where your pem file located

The first command running will look like this this will give the 400 permission to the pem file

The next command is used for login in to the server, if you run this will ask for a confirmation type YES and click on the enter button will login server console

Juts update the server initially by using the below command

sudo apt update

Here we are using the Nginx as webserver so we need to install the Nginx for that run the below command and type Y and click enter.

sudo apt install nginx

Next we want to update the Nginx rule for HTTP for that run the below command

sudo ufw allow 'Nginx HTTP'

Then please check and verify the server IP in to your browser and it will run thé Nginx default page as like the below image shows

Next we want to install the other PHP extension and Database

Installing MySQL

Here we are using MySQL as database so for installing. MySQL need several steps

Type the below command to install the MySQL

sudo apt install mysql-server
sudo apt-get update

After the complete run of above command you can start the secure mysql installation using the below command

sudo mysql_secure_installation

Press Y which will allow the Password validity component for MySQL

Next you have to Enter the password for your MySQL root user then type Y and click enter

Next will ask for removing Anonymous users here i wont remove any users so i type N and click Enter, for production level we have to remove these users

Next you have to make sure the disallow access is not given for MySQL root login for that type N and click enter to proceed

Then type Y and click enter for removing the Test Database

And finally you have to reload the privilege tables for that Type Y and click on the enter button this will give a successful message of MySQL installation the next you have to test the mysql is working or not and create databases for our project for that do follow the below commands

Type the below command for login in to the MySQL console as the above image shows

sudo mysqlmysq> exit

Next we have to install the latest php version to our ubuntu environment for that follow the below procedure

Installing PHP

For installing the php and its extension. Run the below commands

sudo apt install php-fpm php-mysql

Now you can install the required packages with:

sudo apt install php-mbstring php-xml php-bcmath

Next you have to Clone the Project to your new server, since i was using the GitHub which will removed the password Authentication so we have to create ssh key and add it to our github account, some might use gitlab,private git etc.. can proceed directly without following the below steps

Setup GitHub SSH KEY with Remote Server

After completing the all procedure you have to clone your project to the server root path for that do the below Procedures

Clone the Project in to the root folder

For demo project https://github.com/gothinkster/laravel-realworld-example-app Author:

First you have to navigate the html path by running the below command and list the files inside the folder

cd /var/www/html
ls

Then next you have to clone the project to the html folder for that use the below command, in that you have to paste your github project ssh url and click enter to start the clone operation

git clone <repo ssh url >

After the successful clone list the cloned folder using the ls command, then navigate to the project folder by running the below command

ls
cd projectx/

Next we have to install composer in our Ubuntu Environment for that run the below commands as like the image shows

cd ~
sudo apt install composer

After successfully installing the composer we have to again Navigate to our project using cd command

cd /var/www/html/projectx

Then install composer on that project by simply typing the composer install command

Wait until the installation complete….

After successfully installing the composer we have to create a mysql db for our project and put it into the .env file of the project

Create Mysql DB

Login to the mysql console by simply typing the command below

sudo mysql

And list the available databases using the mysql query command

show databases;

Next you have to create the database you have needed by running. The create query command

create database projectx;

Then type exit to exit from the mysql console

Next open the .env file in your project by running the below command

sudo nano /var/www/html/project_x/.env

Which open the nano terminal editor for you change the mysql details in the .env files and save,

For save you have to press

Ctrl+O and click Enter will save the changes

And for Exit you have to press

Ctrl+x and click enter

For Production. Deployment we have to set passwords for the MySQL root user for that do the below steps,

Set Passowrd to Mysql

Login to the mysql console and run the below. Command change the last password section

ALTER USER ‘root’@’localhost’ IDENTIFIED WITH caching_sha2_password BY ‘Password@123’

Now you wont able to login the MySQL console without entering the password for login the MySQL console run the below command and type password

sudo mysql -u root -p

Next you have to configure the Nginx Webserver according to the project path for that do the below. Procedures

Nginx Configuration Setup

Navigate to the nginx configuration folder by running the below command

cd /etc/nginx/sites-available

sudo ln -s /etc/nginx/sites-available/projectx /etc/nginx/sites-enabledCreate new Nginx configuration for your project here am giving the name of the configuration as my project name

sudo nano projectx

It will open an empty nano editor in your terminal copy the below Nginx conf and paste it to the empty editor and edit the configuration as per your project you have to edit the bold-italics part only and save it

root => input the path of the public folder for your project, since Laraval runs on public folder

Server_name => here you can input the domain if you have any one pointed to the IP or else you can put the IP address directly

server {
listen 80;
listen [::]:80;
root /var/www/html/projectx/public;
index index.php index.html index.htm;
server_name projectx.com www.projectx.com;

location / {
try_files $uri $uri/ =404;
}


# pass PHP scripts to FastCGI server
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}
}

After saving the file will we need to symlink the newly created configuration to Nginx Enabled folder, because. Nginx wont suggest any direct edit in the configuration so we have to create a configuration in sites-available then which has to be symlinked to sites-enabled by running the below command

sudo ln -s /etc/nginx/sites-available/projectx /etc/nginx/sites-enabled

After successfully complete the symlink operation we have to test the configuration so working prefect or not for that do the below commands

Run the below Command to test the newly. Created Nginx configuration

sudo nginx -t

Will show an alert the configuration is successful as like the below image shows else you have to verify the configuration script and make it successful, then you have to restart the Nginx Service for that run the below command

sudo systemctl restart nginx.service

After all the Configuration we can go back to the project Folder for migrating the db and artisan operations

cd /var/www/html/projectx

Migrate the Project

For migration of database you have to run the migration command for Laravel inside the project root location

sudo php artisan migrate

After successfully migrated run the artisan. commands for clearing config and cache

sudo php artisan optimize:clear

Then next you have to generate the APP Key using the below command

sudo php artisan key:generate

Next you have to give the permission. For the storage and bootstrap. Folder if used

sudo chown -R www-data:www-data /var/www/html/project_x/storage
sudo chown -R www-data:www-data /var/www/html/project_x/bootstrap

Then check and verify the domains you have pointed to the server IP and in the Nignx configuration in your browser…!!!!

--

--

--

DevOps Engineer / Solution Architect- Calicut

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

CS373 Spring 2022: Tanner Dreiling, Week 14

Create Table in Word Document Using Java

Learn to code in Java

Git Exposure To Git Init, Git Add , Git Status, Git Commit, Git Log , Git Branch

Wifi lenovo não funciona — Wifi lenovo does not work (Windows 10)

How to do a good refinement session?

Creating and Updating infrastructure from your Continous Deployment/Integration pipeline on GitLab…

Vulnhub Shenron-3 writeup

Akshay Krison

Akshay Krison

DevOps Engineer / Solution Architect- Calicut

More from Medium

BEGINNERS GUIDE TO LARAVEL 9 LUMEN : HOW TO CREATE RESTful APIs WITH LUMEN 9

Do I Need Docker? And how to install Docker in my Laravel Project.

Creating your own Oauth2 server using Laravel Passport —Implicit grant flow

RBAC with Laravel 9