Nginx Configuration for Laravel App

Deploying laravel app on nginx server#

There are two most popular web server available in the world of open source

  1. Apache
  2. Nginx

Today we will discuss about nginx. As we know nginx works in many way and it is faster and lightier than apache.

we will create configuration for laravel app

for installing nginx use below command

sudo apt install nginx -y

After installing create a file name on your domain name like below in /etc/nginx/sites-available/ directory.

sudo nano /etc/nginx/sites-available/example.com

it will open a nano text editor

copy paste the below code

server {
    listen 80;
    root /var/www/html/example.com/public;
    index index.php index.html index.htm;
    server_name example.com www.example.com;

    location / {
        try_files $uri $uri/ /index.php?$query_string;

    }
    location ~ \.php {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    }
    location ~ /\.ht {
        deny all;
    }
    location ~ /\.env {
        deny all;
    }

}

as you can see we have used php7.4-fpm if you have installed any other version just changed the version number. for example php8.0-fpm;

now create a symbolic link file in sites-enabled directory using below command

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/

now test for error using nginx -t

sudo nginx -t

If everything is correct and no error found you can simply restart the nginx server.

sudo systemctl restart nginx

your laravel project files will be in /var/www/html/example.com directory.

comments powered by Disqus