Installing nginx:

Caddy webserver

Currently I am diving more in to Caddy webserver which seems to have easier syntax than nginx. You can read more about it here

To install nginx, run the following command in terminal:

$ sudo apt install nginx

Setting up nginx as webserver for nextjs/gatsby

Example config for reverse proxy: (/etc/nginx/sites-enabled/gatsby)

# example script for reverse proxy
server {
	server_name example.xopun.com;
	location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                proxy_pass http://localhost:5000;
                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;
        }
}

Warning:

If you get any errors stating Can't start Nginx - Job for nginx.service failed then especially check the line endings. Every line should end with a ";" otherwise this error will popup

Setting up nginx as webserver for php

Edit the following in nginx (/etc/nginx/sites-enabled/default):

server {

    server_name example.xopun.com;
    listen 80;
    root /var/www/example.xopun.com;

    index index.php;

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


    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    }

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

Setting up nginx as webserver for Symfony framework

server {

    server_name example.com;
    listen 80;
    root /var/www/symfony/public;

    location / {
        # try to serve file directly, fallback to index.php
        try_files $uri /index.php$is_args$args;
    }

    # optionally disable falling back to PHP script for the asset directories;
    # nginx will return a 404 error when files are not found instead of passing the
    # request to Symfony (improves performance but Symfony's 404 page is not displayed)
    # location /bundles {
    #     try_files $uri =404;
    # }

    location ~ ^/index\.php(/|$) {
        fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;

        # optionally set the value of the environment variables used in the application
        # fastcgi_param APP_ENV prod;
        # fastcgi_param APP_SECRET <app-secret-id>;
        # fastcgi_param DATABASE_URL "mysql://db_user:db_pass@host:3306/db_name";

        # When you are using symlinks to link the document root to the
        # current version of your application, you should pass the real
        # application path instead of the path to the symlink to PHP
        # FPM.
        # Otherwise, PHP's OPcache may not properly detect changes to
        # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
        # for more information).
        # Caveat: When PHP-FPM is hosted on a different machine from nginx
        #         $realpath_root may not resolve as you expect! In this case try using
        #         $document_root instead.
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        # Prevents URIs that include the front controller. This will 404:
        # http://domain.tld/index.php/some-path
        # Remove the internal directive to allow URIs like this
        internal;
    }

    # return 404 for all other php files not matching the front controller
    # this prevents access to other php files you don't want to be accessible.
    location ~ \.php$ {
        return 404;
    }

We are using the php8.0-fpm as the fastcgi gateway in order to serve the php files.

Restart the nginx server via sudo systemctl restart nginx. You can also test the nginx configuration by sudo nginx -t.

Running multiple server blocks

If you need to run multiple server bloks, then run each virtual server on each file. e.g.

/etc/nginx/sites-enabled/gatsby

# example script for reverse proxy
# /etc/nginx/sites-enabled/nginx

server {
	server_name example1.xopun.com;
	location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        proxy_pass http://localhost:5000;
        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;
        }
}

/etc/nginx/sites-enabled/laravel

server {

    server_name example.xopun.com;
    listen 80;
    root /var/www/example.xopun.com;

    index index.php;

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


    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    }

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

Restart the server with sudo systemctl restart nginx after any modification you do in the configuration.

Retreiving errors

$ sudo tail /var/log/nginx/error.log -n 200

Permission

IT could be a bit tricky to setup the permission for nginx.

Step 1: Add user to group www-data

$ sudo adduser {USER-NAME-HERE} {GROUP-NAME-HERE}

# e.g. 

$ sudo adduser itachi www-data

Step 2: ownership of the folder

$ sudo chown -R $USER:www-data /var/www/html

TIP

change chown permission for every folder that is in the path. e.g. In the above case, www-data will have to have access to /var/www too and not just /var/www/html folder

Give the execute permission

$ sudo chmod +x /var
$ sudo chmod +x /var/www
$ sudo chmod +x /var/www/html

[... to be continued.]

Last Updated: