A guide to deploying Laravel on Ubuntu 20.04 LTS Based Azure VM Server

Akshay Krison
12 min readMar 24, 2022

“Developer Friendly Deployment Step’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 Azure Console

Deploy the Laravel APP to Azure VM

Login to the AZURE console and search or Navigate to the Create Resource in that you can see Ubuntu Server 20.04 LTS below shown image and click on the create instance for creating a server.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

Azure Service Page
Create a VM Resouce

Create a new resource group first for that click on the create new and Name it your resource group.Then next you have to input the Virtual Machine Name use you project name as VM name then Choose the region you want to deploy the server (Use the region based on the application location where you were implementing by this you can reduce the latency issues)

Next you have to choose the Size of our Instance needed choose the appropriate size of the instance you need

When you scroll down you can saw the administrator account option from that you can choose the SSH Authentication type here am using the Password authentication with Username and Password (if you need pem methord you can go with that and download the pem in last step) Next you have to set The Inbound Port Rule that HTTP, HTTPS & SSH and click on Next

Here you have to choose the disk size for your VM by make it by Default and click on Next (if you need you can choose your on Disk size, disk type and encryption)

The next part is you have to setup the Networking Section, here you have to choose default or create new one, the Vnet, subnet, Public IP,Select the Inbound ports HTTP, HTTPS & SSH after that click next

You will navigate to management screen make the default one do not need to change any conf then click on the Next button

You will Navigate to advanced page and make the default one do not need to change any conf then click on the Next button

You will Navigate to Tags page and make the default one do not need to change any conf then click on the Next button to Review and Create

Check all the details and type the necessary details for the TERMS and click on the Create Button

Wait until the resources are yet to create…After creating click on the Go to resource button and you will navigate to the Resource Page

Here you can saw the detailed information of the resource you have created

  1. The Public IP of your server
  2. To connect the Server

Get in To the Server Through SSH

Since we have already created username and password on the first step in authentication type so we can ssh to our server through that username and password

since my username is akshay and the server ip 52.136.126.97 can get from the above screen and together we can,

ssh your_user_name@your_server_ip

After successful login, you will get a console.

Next, you have to update your server packages by running the command on the terminal, in which the server automatically look for the new version packages to be updated and update them automatically

sudo apt-get update

Here we are using Nginx as Our Webserver and On System Mysql Configuration (Not Managed DB)

The next step is to install the Nginx for that you have to run the below command in your terminal console:

sudo apt-get install nginx

Which automatically installs all the data needed for the Nginx, after successful installation just update the server again using “server package updation”.

Next Change the Uncomplicated Firewall in your Ubuntu server by running the below command

sudo ufw allow 'Nginx HTTP'

After that enter your IP in the Web Browser and you can see an Nginx landing page then the installation and testing is completed for the web browsers .

After that Navigate to the root directory of your server by running the below command

cd /var/www/html

Installing PHP

The next step is to Install the PHP version you need here am going with the PHP version php7.4 copy the below command and run it on your terminal which will install the PHP and all PHP extensions as we mentioned in the command (note you can change the version name according to your project and can avoid the extension which is not used)

sudo apt -y install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt -y install php7.4
php -v
sudo apt-get install -y php7.4-cli php7.4-json php7.4-common php7.4-mysql php7.4-zip php7.4-gd php7.4-mbstring php7.4-curl php7.4-xml php7.4-bcmath

After the successful installation, you can check the current version of the project by typing and it will output the PHP version being used:

php -v

Next we have to check the all the Extension’s that are installed, for that run the below command

php -m

Installing MySQL

Here we are using MySQL as database so for installing. MySQL need several steps.Type the below command to install the MySQL and Click “Y” to Install

sudo apt-get update
sudo apt install mysql-server

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

All done!! for the MYSQL Installation.

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

sudo mysql
mysql> exit

After ogin to the mysql console and next we have to list the available databases using the mysql query command for that run the below command and next create a new database of name ProjectX using the below commands

mysql> show database;
mysql> create database projectx;

Next we have to Secure our mysql for that we need to Authentication for accessing our database so run the below command by chaining the Password Section as you like

ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'Password@123'

After all these exit from mysql by simply type exit in the mysql console

exit

Setup GitHub SSH KEY with Remote Server [For GitHub Users only]

Use the below link to setup your Azure Server and Your GitHub Pass wordless SSH Authentication these will redirect to another tutorial do the procedure and come back to do below steps to complete the deployment

Clone the Project in to the root folder

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

First you have to navigate the html path by running the below command and list the files inside the folder. 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

cd /var/www/html
git clone <your repo url>

After the successful clone list the cloned folder using the ls command, then navigate to the project folder by running the below command (Since my Project name is “Project_X”)

ls
cd project_X/

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/project_x

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

composer install

Wait until the installation complete….

After successfully installing the composer we have to create our environment file .env for that do the below commands which copy the .env.example to .env file

sudo cp .env.example .env

Next open the .env file in your project by running the below command, Which open the nano terminal editor for you change the mysql details in the .env files and save,

sudo nano .env

Here we have to change the DB Credentials as we created in our previous step and save the .env file

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

After that run the migration command to migrate the tbales in to newly created database

php artisan migrate

Then do the php cache clearing by running the below commands

sudo php artisan config:cache
sudo php artisan config:clear

After that run the Laravel Key generation and Storage Link for our Project

sudo php artisan key:generate
sudo php artisan storage:link

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

Create 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;
}
}

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

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

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 giving the proper permission to your project

cd /var/www/html/projectx

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…!!!!

--

--