Security, Tech & Programming
Laravel Deploy on nGinx LEMP

Laravel Deploy on nGinx LEMP

This tutorial explains how to publish a laravel project website on LEMP server (running nginx & mysql) on ubuntu. However, it should work the same on any other linux distribution based vps.

Will PHP artisan serve work?

php artisan serve command is not supposed to be used in production environment.

The production environment doesn’t need any running process of the project itself. It simply works as a regular php project.

How to deploy Laravel app on Nginx

Deploying Laravel App on nginx requires these steps:

  • Setting up a VPS with LEMP stack
  • Copying laravel files to server directory
  • Setting up nginx site configuration for laravel app
  • Install ufw to open port for your laravel app

Setting up VPS with LEMP stack

Setting up a LEMP stack on ubuntu is a straight forward process. You can follow steps highlighted here:

  • Setup latest ubuntu vps
  • Install nginx
  • install mysql or mariadb
  • Install latest (or required) php-fpm and related packages.

Setup a generic nginx website pointing to a folder for your laravel project. Copy the following nginx configurations for your project.

Note, change the with your domain name. Also change the root directory path. Make sure that you point towards the /public folder inside the laravel project.

server {
    listen 80;
    listen [::]:80;
    # if needed:
    listen 443 ssl;
    root /var/www/;

    index index.php;

    location / {
         try_files $uri $uri/ /index.php$is_args$args;

    # pass the PHP scripts to FastCGI server listening on /var/run/php8.1-fpm.sock
    location ~ \.php$ {
            try_files $uri /index.php =404;
            fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
    location ~ /\.ht {
        deny all;


This nginx conf is important, as it also fixes several errors, e.g. when nginx laravel homepage works but inner pages give error 404.

UFW config for laravel

We need to enable required port for laravel app.

To do that, install ufw:

sudo apt-get install ufw

Enable port 8000 for laravel project and for nginx http and https.

Note, make sure to enable default ssh port 22, otherwise you will be stuck out of ssh terminal login in future. You should change port 22 to any custom port if you set that for ssh.

sudo ufw allow 22
sudo ufw allow 8000
sudo ufw allow 'Nginx HTTPS'
sudo ufw allow 'Nginx HTTP'

Check the open ports by running:

sudo ufw status

The output should be like:

To                         Action      From
--                         ------      ----
Nginx HTTP                 ALLOW       Anywhere                  
Nginx HTTPS                ALLOW       Anywhere                  
22                         ALLOW       Anywhere                  
8000                       ALLOW       Anywhere                  
Nginx HTTP (v6)            ALLOW       Anywhere (v6)             
Nginx HTTPS (v6)           ALLOW       Anywhere (v6)             
22 (v6)                    ALLOW       Anywhere (v6)             
8000 (v6)                  ALLOW       Anywhere (v6)   

Composer install

Goto the root files directory for your laravel project and run:

sudo composer install

Please note, that while running composer install, it will give you errors regarding which packages are missing for laravel. you need to install them for your server. E.g: if curl is missing, then you need to first install php8.1-curl (or any other php version that you’re using), by running:

sudo apt-get install php8.1-curl

and so on.

Generate app key

php artisan key:generate

Link Laravel Storage

php artisan storage:link

File permissions & ownership

If you get any errors, you might have to setup file permissions & ownership for your project. For that, run these commands while outside the root directory of your project:

chown -R www-data:www-data domain-folder/

Leave a Reply

Your email address will not be published. Required fields are marked *

Hire Me!