Deploy Multiple Laravel in Nginx Virtualhost LEMP Stack

Deploying multiple laravel based website using nginx virtual host is so much easy. This article will give you a-z guideline how to setup and run your multiple laravel projects using nginx virtual host under LEMP stack in ubuntu server. Follow the whole article step by step. Hope that it will give you clear idea. 

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

Read also: Deploy laravel application using nginx php mysql lemp stack setup in ubuntu

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 project1 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

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

.env and Permission Setup(both projects)

cp .env.example .env
php artisan key:generate
nano .env
sudo chown -R www-data:www-data /var/www/html/project1/storage/
sudo chown -R www-data:www-data /var/www/html/project1/bootstrap/
sudo chown -R www-data:www-data /var/www/html/project2/storage/
sudo chown -R www-data:www-data /var/www/html/project2/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
Project1 Nginx Configuration
sudo nano /etc/nginx/sites-available/site1.conf

Edit project1.conf file paste below content and finally save by ctrl + x, then press Y and finally press enter

server {
    server_name project1.com www.project1.com;
    root /var/www/html/project1.com/public; #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;
    }
}
Project2 Nginx Configuration
sudo nano /etc/nginx/sites-available/project2.conf

Edit project2.conf file paste below content and finally save by ctrl + x, then press Y and finally press enter

server {
    server_name project2.com www.project2.com;
    root /var/www/html/project2.com/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/project1.conf /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/project2.conf /etc/nginx/sites-enabled/

Now restart nginx and php-fpm

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

Read also: Real time notification using reverb laravel 11

Read also: Laravel 11 sanctum api login registration

Read also: Laravel pdf download and render using dompdf


visit your domains on individual tabs in browser. You will see output


You are good to go. Hope that this article has helped you a lot. Bookmark programmingmindset.com as your tech guide. This website is regularly updated with new contents and articles. 

Tags: