Nginx virtual host configuration on ubuntu18.04


If nginx is installed, all your virtual host configurations file will live in the directory below. Each website will have its own configuration file


For example, create a new configuration file for the site example.loc.

sudo nano example.conf

Now that you have created a virtual host file, you can copy and paste the code below into each file and save. First we write for Laravel projects.

server {
    listen 80;
    listen [::]:80;
    root /var/www/html/example/public;
    index  index.php index.html index.htm;
    server_name  example.loc;

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

    location ~ \.php$ {
       include snippets/fastcgi-php.conf;
       fastcgi_pass             unix:/var/run/php/php7.2-fpm.sock;
       fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;


And for standard projects, you can copy the default file that is by default, and change the rest there.

cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.loc

Next, let's enable the file by creating a link from it to the sites-enabled directory, which Nginx reads from during startup:

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

Next, test to make sure that there are no syntax errors in any of your Nginx files:

sudo nginx -t

After that, you need to write the host name into the /etc/hosts file with the Vim editor and at the end reload the nginx server.

sudo vim /etc/hosts       example.loc www.example.loc
sudo systemctl restart nginx