To setup Nginx on Ubuntu server

# First update the Ubuntu
sudo apt-get update
sudo apt-get upgrade

# Then install nginx
sudo apt-get install nginx

Now enable nginx at system startup:

sudo systemctl enable nginx
# To check the status of nginx 
sudo systemctl status nginx.service
# Or
sudo service nginx status
# To stop Nginx server
sudo service nginx stop
# To reload and restart
sudo service nginx reload
sudo service nginx restart

After successfully doing the above steps, then it’s time to install php. To install php it’s need to install php fpm to work with nginx. FPM stands for Fast-CGI Process Manager. To install PHP and PHP fpm it needs to run the following command:

sudo apt-get install php8.1-fpm
# Ubuntu will install the latest PHP and php-fpm from the apt repository

You could install php php-fpm but that installs Apache also, which we don’t want. This command will install php8.1-fpm along with dependencies with PHP regular. After successful installation of PHP and php-fpm to check the php-fpm status:

sudo service php8.1-fpm

Typically php-fpm will reside under /var/run/php/ folder. Now at this point, we have Nginx and PHP up and running on the Ubuntu machine. Now it’s time to run a website on Nginx. The Nginx configuration file resides in /etc/nginx folder. And site configuration resides under /etc/nginx/sites-available/default file. The content of the file looks like the following:

##
# You should look at the following URLs in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# <http://wiki.nginx.org/Pitfalls>
# <http://wiki.nginx.org/QuickStart>
# <http://wiki.nginx.org/Configuration>
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: <https://bugs.debian.org/773332>
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: <https://bugs.debian.org/765782>
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                # proxy_pass <http://localhost:8080>;
                # proxy_http_version 1.1;
                # proxy_set_header Upgrade $http_upgrade;
                # proxy_set_header Connection 'upgrade';
                # proxy_set_header Host $host;
                # proxy_cache_bypass $http_upgrade;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \\.php$ {
        # include snippets/fastcgi-php.conf;
        #
        #       # With php7.0-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php7.0-fpm:
        #       fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\\.ht {
        #       deny all;
        #}
}

# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
#       listen 80;
#       listen [::]:80;
#
#       server_name example.com;
#
#       root /var/www/example.com;
#       index index.html;
#
#       location / {
#              try_files $uri $uri/ =404;
#       }
#

By default, nginx will look at the file in the /var/www/html folder.

        location ~ \\.php$ {
				        include snippets/fastcgi-php.conf;
        #
        #       # With php7.0-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php7.0-fpm:
               fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        }

Must uncommnet the above line without this php script will not run on nginx. Also remeber to add index.php on index directive like bellow

 # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

After saving this then run the following commands:

sudo systemctl reload nginx
sudo systemctl restart php8.1-fpm

If you want vhost your site follow the following steps:

Suppose we have a site name site1.com which resides under /var/www/. So the full path of the site is /var/www/site1.com. Now create a file under /etc/nginx/sites-available/site1.com.conf and then copy the following line:

server {
		# Avoid port 80 because it's the default 
    listen 8000;
    server_name site1.com;  # Replace with your domain names

    root /var/www/site1.com;  # Replace with the public directory of your Laravel project

    index index.php index.html;

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

    location ~ \\.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;  # Replace with the path to your PHP-FPM socket
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ /\\.ht {
        deny all;
    }
}

Save and exit. Now create a symbolic link to /etc/nginx/sites-enables by writting the following the