Deploy Laravel Application using Nginx, PHP, MySQL Lemp Stack Setup in Ubuntu

Deploying a Laravel application on Ubuntu with Nginx involves several steps. This blog will guide you step by step to deploy laravel application in ubuntu vps server. In whole blog we need to deal some technologies such as PHP, Nginx, MySQL, Composer etc.

Let's upgrade Ubuntu VPS Server
sudo apt update && sudo apt upgrade -y

For serving and handling HTTP request it's need to install Nginx inside ubuntu. Install Nginx using below command

sudo apt install nginx -y

As laravel is built on top of PHP.  So it's needed to install PHP. We are installing PHP 8.3 according to our requirements. You can install desired php version according to need

Install software Properties Common Transport
sudo apt install software-properties-common apt-transport-https -y
Add PHP Repository
sudo add-apt-repository ppa:ondrej/php -y
Now install PHP8.3
sudo apt install php8.3-fpm php8.3-common php8.3-mysql php8.3-xml php8.3-xmlrpc php8.3-curl php8.3-gd php8.3-imagick php8.3-cli php8.3-dev php8.3-imap php8.3-mbstring php8.3-opcache php8.3-soap php8.3-zip php8.3-intl php8.3-bcmath
sudo apt install php*-fpm php*-common php*-mysql php*-xml php*-xmlrpc php*-curl php*-gd php*-imagick php*-cli php*-dev php*-imap php*-mbstring php*-opcache php*-soap php*-zip php*-intl php*-bcmath

Read also: Host multiple laravel project on same port in nginx server

Be confirmed that, you have successfully installed PHP8.3 in ubuntu vps. 

sudo php -v
It's needed to install composer
sudo apt install wget php-cli php-zip unzip

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
HASH="$(wget -q -O - https://composer.github.io/installer.sig)"

php -r "if (hash_file('SHA384', 'composer-setup.php') === '$HASH') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
composer


Navigate to /var/www/html directory and clone project from github
cd /var/www/html
sudo git clone [email protected]:Programming-Mindset/demo-laravel-app.git project1
cd /var/www/html/project1
composer install

.env and Permission Setup

cp .env.example .env
php artisan key:generate
nano .env
chown -R www-data:www-data /var/www/html/project1/storage/
chown -R www-data:www-data /var/www/html/project1/bootstrap/
php artisan migrate
Install MySQL
sudo apt install mysql-server
sudo mysql_secure_installation 

#Add new Database and User
sudo mysql
CREATE DATABASE example_database;
CREATE USER 'username'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
GRANT ALL ON example_database.* TO 'username'@'%';
flush privileges;
mysql -u example_user -p
SHOW DATABASES;
Nginx Configuration

cd /etc/nginx/sites-available

nano default


Edit Nginx default file paste below content and finally save by ctrl + x, then press Y and finally press enter

server {
    server_name ip_address_here;
    root /var/www/html/project1/public; #project path. modify this line according to your project path

    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/php8.3-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

Check nginx configuration is ok

sudo nginx -t
sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/

Now restart nginx and php-fpm

sudo systemctl restart nginx
sudo systemctl restart php8.3-fpm

Get your public ip address and paste in in browser

curl icanhazip.com

You are good to go. Hope that this blog will help you a lot. Bookmark programmingmindset.com as your tech guide. This website will not disappoint you

Tags: