Nowadays, Nginx is among the most popular web servers in the world and is hosting many of the most visited websites on the Internet. Designed as a web server with utmost performance and stability, it is also used as a reverse proxy, a load balancer and a cache server.

Throughout this guide, we will look at how to install Nginx, configure the firewall, manage Nginx processes, and configure the server block to host multiple domains on one cloud instance running the Ubuntu 20.04 operating system.

Requirements

Before getting started, you will need a regular user (other than root) that has sudo access.

As an option, you may also need a domain to complete the last steps of the tutorial. You can find instructions regarding domain DNS registration and management in the DNS service guide.

Step One: Installing Nginx

As Nginx is available in Ubuntu’s default repositories, you can install it from those repositories using apt.

Considering this is our first time using apt in this guide, first, we will update our package list to gain access to the latest and most recent packages. Then, we can install Nginx:

sudo apt update
sudo apt install nginx

Upon accepting this process, Nginx will be installed along with all the required cloud dependencies.

Step Two: Configuring the firewall

Before we can do anything, we need to set up the instance’s firewall to access the Nginx service. Note that Nginx is a service that gets installed along with ufw, allowing you to easily configure access.

To see the services that ufw can work with, enter the following command.

sudo ufw app list

You should get a list of application profiles in the output, as shown in the below image:

There are three profiles for Nginx, as you can see in the output:

  • Nginx Full: This profile will open both port 80 (normal, unencrypted web traffic) as well as port 443 (TLS/SSL encrypted traffic).
  • Nginx HTTP: This profile only opens port 80 (normal and unencrypted web traffic).
  • Nginx HTTPS: This profile will only open port 443 (TLS/SSL encrypted traffic).

Next, we are going to allow traffic over port 80 in the firewall.

For this purpose, type the following command:

sudo ufw allow 'Nginx HTTP'

Ensure that the change becomes effective with the following command:

sudo ufw status

The output will show what HTTP traffic is allowed:

Step Three: Checking the Web Server

When the installation process is finished, the Nginx operating system will boot; which means that now the web server should be running.

We can use the system and the following command to confirm that the service is up and running correctly:

systemctl status nginx

The service has started successfully, as confirmed in the output. Nevertheless, the best way to check the performance of this web server is by sending a request to Nginx.

You can open the default Nginx page by typing your IP address into the browser and checking if the web server is running as it should. If you don’t know your server’s IP, it can be obtained from the user panel or by entering the following command in the instance:

curl ifconfig.io

After finding your server’s IP, type the following address into the browser:

http://INSTANCE_IP

You should see the default Nginx page in response:

Once you see this page, it indicates that the Nginx web server is properly running in your instance.

Step Four: Managing the Nginx Web Server Processes

Having your web server running now, we can run some basic commands.

To stop your web server, type the following command:

sudo systemctl stop nginx

To restart the web server after it has been stopped, enter the following command:

sudo systemctl start nginx

To stop the server and then restart it, type the following command:

sudo systemctl restart nginx

When you have only made configuration changes, Nginx may reload often with no interruption in connections. Use the following command to do this:

sudo systemctl reload nginx

Nginx is configured by default to start automatically whenever the instance boots. You can disable this by entering the following command if you do not wish it to do so:

sudo systemctl disable nginx

To enable the service again to start at boot time, type the following command:

sudo systemctl enable nginx

Having now covered the basic commands for Nginx management, you can now configure your website to have more than one domain hosted.

Step Five: Launching Server Block

While using the Nginx web server, server blocks (like virtual hosts in Apache) can be used to set up and host more than one domain on the same server. For this guide, we will be setting up a domain called your_domain, which should be replaced with your domain name.

In Ubuntu 20.04, Nginx has a server block enabled by default which is configured to deliver content to the var/www/html/ directory. Although this works well for a single website, when hosting multiple websites, using this setup will not suit your needs. Rather than changing var/www/html/, let’s create a directory structure in var/www/ for the your_domain website. We will leave var/www/html/ as the default directory to display content if the user request is not consistent with other websites.

Set up a directory for your_domain as follows (the -p flag will create the required parent directories):

sudo mkdir -p /var/www/your_domain/html

Next, specify the owner of the directory with the $USER environment variable:

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

You can run the following command to make sure the permissions are correct and to allow the owner to read, write, and execute the files:

sudo chmod -R 755 /var/www/your_domain

Then, create a sample index.html page with nano or your preferred editor:

sudo nano /var/www/your_domain/html/index.html

Insert the following HTML example into it:

Then save and close the file by pressing Ctrl+X, followed by Y, and then Enter.

To enable Nginx to deliver this content, you must create a server block using the correct instructions. However, rather than changing the default configuration file directly, let’s create a new file in /etc/nginx/sites-available/your_domain:

sudo nano /etc/nginx/sites-available/your_domain

Add the following configuration block, resembling the default block but altered for our new directory and domain name:

Remember that we have set root for the new directory and server_name for the domain name.

Next, we must activate this file by linking to sites-enabled, which Nginx would read on boot:

/sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled

Note: Nginx is using a common method called symlinks to follow which blocks are active on your server. Setting up a symbolic link here is kind of like creating a shortcut on disk, which means that you can remove the shortcut from the sites-enabled list, but leave it in sites-available to activate it anytime you want.

You now have two server blocks enabled and configured to respond to requests using the listen and server_name commands:

  • your_domain: will respond to your_domain and your_domain requests.
  • default: will respond to any request on port 80 not matching other block servers.

It is necessary to set a single value in the /etc/nginx/nginx.conf file to prevent potential problems caused by adding additional name servers. Open the file:

sudo nano /etc/nginx/nginx.conf

Search for the server_names_hash_bucket_size value and delete the # character at the start of this line to uncomment it.

After making the changes, close and save the file.

Next, to check if the Nginx configuration is error-free, use the command below:

sudo nginx -t

Assuming there are no issues, restart Nginx so the changes become effective:

sudo systemctl restart nginx

Now, Nginx should display your domain. You can verify this by going to http://your_domain. If you have completed the steps correctly and with no errors, you should see the following image:

Step Six: Getting To Know Key Nginx Files and Directories

Having learned how to install and manage Nginx, now you should familiarize yourself with the key directories and files of this service:

Content

  • /var/www/html: Web content, that by default contains only the default Nginx page you saw earlier, will be retrieved from this directory.

Server Configuration

  • /etc/nginx: Nginx configuration directory. This is where all Nginx configuration files are located.
  • /etc/nginx/nginx.conf: Main Nginx configuration file. You can modify this file to change the Nginx global configuration.
  • /etc/nginx/sites-available/: The directory where server blocks for each site can be saved. Note that Nginx won’t use configuration files in this directory unless they are linked to the sites-enabled directory. Usually, the entire configuration of server blocks is done in this directory and then they are enabled by being linked to another directory.
  • /etc/nginx/sites-enabled/: This is the directory where the active server blocks for each site are stored.

Server Logs

  • var/log/nginx/access.log/: All requests to your web server will be logged in this file, provided Nginx is not configured otherwise.
  • var/log/nginx/error.log/: All Nginx errors will be logged in this file.

Conclusion

Having now configured your web server, you will have many options available to you in terms of the content type and technologies you want to use to build a better experience.

Finally, if you want to set up HTTPS for your domain name using a free SSL certificate via Let’s Encrypt, you can refer to ArvanCloud’s certificate guide.