facebook youtube pinterest twitter reddit whatsapp instagram

Installing and Configuring Nginx

Nginx or engine X is a free and open-source web server, reverse proxying, load balancer, caching, and more. Apache isn't the only web server for hosting web content (although, it is the most used), Nginx is also very powerful and stable.

Also, it can be used as a reverse proxy and load balancer for HTTP, TCP, and UDP servers.

Let's get to installing Nginx, make sure you don't have any other web server service running (e.g Apache), except you want to run both to together, and you know what you are doing.

To get started with Nginx, you run the following command:

sudo apt install nginx

Verify the installation:

sudo nginx -v

Enter the IP address of your server in a browser to test if Nginx is working, if it works, you a Welcome to Nginx page.

Just as Apache stores its default configuration file in the /etc/apache directory, Nginx stores its own in the /etc/nginx.

Nginx serves web pages from the Root, this is located in /var/www/html, this is where the default page is served from.

What I love about Apache and Nginx is the virtual host feature (virtualhost is called serverblock in Nginx, but I'll still use the virtualhost term to make thing less confusing), you can host more that one website on a server. The way it works is that it consists of an individual configuration file for different domains. For example, you can have a server with a single IP address that hosts three websites, e.g abc.com, devsrealm.com, classicpress.net, you get the idea.

I love to think of the server IP as an apartment, you know an apartment contains one or more rooms, so, the virtual hosts are the rooms. For example, website1.com is a room under my server IP (apartment), website2.com is also another room under my server IP.

Applying this to Nginx: You create a separate configuration file for each website and store them in your Nginx configuration directory.

Each configuration file would include a configuration file, where you would place an identifier such as a name or IP address that differentiates one website from the other.

When a user requests a web page, Nginx will serve the website the user is requesting, just the way a user would go into a specific room.

To set up a virtual host, we need to create a file in /etc/nginx/sites-enabled/ directory, confirm if you have sites-enabled and sites-available directory created under your Nginx directory using ls -l /etc/nginx

If they aren't created, you might need to create them, although they are not needed, but using this is easier to add new sites, disabling sites, and the likes. Rather than having one large config file.

To get the format in place, create a site-available and sites-enabled under the Nginx directory:

sudo mkdir sites-available sites-enabled

This would create the two directories, so, the next thing you would want to do is to edit the http block inside /etc/nginx/nginx.conf and add this line:

include /etc/nginx/sites-enabled/*;

made sure you include it within the block (add it before the block closure)

http {

...........

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

}

Save the file by using Ctrl + X and enter.

The next thing you need to do is to create the site config inside sites-available, and you'd create a symlink for them inside sites-enabled for those you want enabled.

Let's assume you have created the following file:

/etc/nginx/sites-availabe/myfirstwebsite.com

To enable that site, we would need to create a symbolic link for it and store that link in the /etc/nginx/sites-enabled directory:

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

Don't get the symbolic link term twisted, it is just like a shortcut to points to the original file, it is that simple. It does not contain the data in the target file, all it does is point another entry somewhere in the file system.

To apply changes, you simply reload Nginx:

sudo systemctl reload nginx

If Nginx created the sites-enabled and sites-available directory for you, then a site configuration file named default exists in /etc/nginx/sites-available and a symbolic link to it is already present in /etc/nginx/sites-enabled.

If all you want to do is host a single site, we only need to replace the default content that NGINX serves which is located in the /var/www/html directory (the same as Apache) with the content for your site.

If you want to host multiple websites on your server, then you can make use of the default file as a template, for example, if you want to make a config for website.com, you can copy it as follows:

sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/website.com

This would automatically copy the content in the default file into website.com, and now you can edit this file and change it to serve additional content.

This is the default config file if you need it:

# You should look at the following URL's 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;
#       }
#}

Note: Only one site can be referred to as a default site. A default site in NGINX is one that answers if none of the other sites match a request. Therefore, make sure you remove both occurrences of default_server from your newly copied config.

Open up your newly copied config, and change these lines:

listen 80 default_server;
listen [::]:80 default_server;

To:

listen 80;
listen [::]:80;

Adjust the server name option to refer the name of your new site, e.g

server_name website.com www.website.com;

Change the Document Root to the directory that will store the files for your new site.

Find this line:

root /var/www/html;

And change it to this:

root /var/www/website.com;

The Final look should be similar to something like this:

server {
        listen 80; 
        listen [::]:80;

        root /var/www/acmesales.com;

        index index.html index.htm index.nginx-debian.html;

        server_name acmesales.com www.acmesales.com;

        location / { 
                try_files $uri $uri/ =404; 
        }
}

It is as easy as that, if you are used to configuring Apache, then this should a breeze.

Related Post(s)

  • Installing WP-CLI In a GNU/Linux Server

    WP-CLI is a command-line interface for WordPress. It can also be used with ClassicPress, as they are no differences in their usage, maybe just minimal if you are updating or downloading new ClassicPr

  • Installing and Running Rclone Mount As a Windows Service

    I previously wrote a guide on how to mount and unmount rclone in Linux, in this guide, I’ll walk you through on how to do the same on a windows system. Step 1: Download Rclone First, go to rclone d

  • Mounting and Unmounting Cloud Storage With (Rclone) in GNU/Linux

    Ever wondered if you could mount your preferred cloud storage as a virtual drive on your system? Thanks to Rclone, you can mount and access different kinds of cloud storage from a file manager, I'll

  • How To Configure and Enable Syntax Highlighting in Nano Text Editor

    nano is one of the most popular, and friendly editor in GNU/Linux, it has several features that can boost the usability and easiness of the program. In the guide, you'll learn how to configure and en

  • Installing and Using NCDU (NCurses Disk Usage)

    ncdu is a curses-based version of the well-known 'du' command and provides a fast way to see what directories are using your disk space. What I love about this utility is that it can traverse the re

  • Installing Nginx Server as Reverse-Proxy for Apache [Multiple Websites]

    Nginx or engine X is a free and open-source web server, reverse proxying, load balancer, caching, and more. Apache isn’t the only web server for hosting web content (although, it is the most used),