How To Install Nginx on Ubuntu 18.04 Server

Nginx is the second most used web server in the world. According to the Netcraft survey, Nginx is used by approx 25% of the busiest sites on the web. In this tutorial, we’ll install Nginx on Ubuntu 18.04 LTS server.

install-nginx-on-ubuntu-18.04

Nginx is mainly known for its low resource consumptions and high performance. Nginx can be used as a web server or reverse proxy with apache in the frontend.

Prerequisites

Before you follow this tutorial, you should have a non-root user with sudo privilege.

Step 1 – Install Nginx on Ubuntu

Ubuntu’s default repositories are having Nginx. So, we just need to install Nginx using the default repositories.

Above all, we have to update the list of available packages in the repository. After that, we will install Nginx on Ubuntu 18.04 by using the below command.

sudo apt-get update
sudo apt-get install nginx

Step 2 – Configure Firewall

UFW (Uncomplicated Firewall) is the default firewall configuration tool for Ubuntu. By default UFW is disabled. Use the below command to enable the UFW.

sudo ufw enable

During installation, Nginx registers itself with UFW. Therefore, allowing Nginx in UFW is very simple. You can see the list of registered application in UFW using the below command

sudo ufw app list
Output:
Available applications:
   Nginx Full
   Nginx HTTP
   Nginx HTTPS
   OpenSSH

Here we have three different profiles for Nginx.

  • Nginx Full – For allowing both port 80 (HTTP) and port 443 (HTTPS).
  • Nginx HTTP – For allowing only port 80 (HTTP)
  • Nginx HTTPS – For allowing only port 443 (HTTPS)

For better security, you should only allow those ports which are used. Because we do not configure SSL in this tutorial, we only allow port 80 (HTTP) for now. Allow Nginx HTTP using the below command.

sudo ufw allow 'Nginx HTTP'

After that, check the firewall status by using the below command.

sudo ufw status
Output:
Status: active
 To                         Action      From
 --                         ------      ----
 OpenSSH                    ALLOW       Anywhere                  
 Nginx HTTP                 ALLOW       Anywhere                  
 OpenSSH (v6)               ALLOW       Anywhere (v6)             
 Nginx Full (v6)            ALLOW       Anywhere (v6)             
 Nginx HTTP (v6)            ALLOW       Anywhere (v6)

In the above output, you can see Nginx HTTP is allowed in firewall now.

Step 3 – Enable and Verify Nginx Service

By default, Nginx Service is enabled and started during installation. In case if it not enabled and started automatically. You can Enable and Start the Nginx service on Ubuntu by using the below command.

sudo systemctl enable nginx
sudo systemctl start nginx

After that, you can see the current status of the service by using the below command.

sudo systemctl status nginx
Output: ● nginx.service - A high performance web server and a reverse proxy server     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)     Active: active (running) since Wed 2020-02-12 07:17:55 UTC; 2h 20min ago       Docs: man:nginx(8)   Main PID: 11664 (nginx)      Tasks: 2 (limit: 1108)     CGroup: /system.slice/nginx.service             ├─11664 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;             └─11667 nginx: worker process  Feb 12 07:17:55 demo.linuxbots.com systemd[1]: Starting A high performance web server and a reverse proxy server…

To verify, If you enter the domain or IP of the server in a web browser you can see the Nginx welcome page on it.

nginx-welcome-page
Nginx Welcome Page

Step 4 – Understanding Configuration File’s Structure

As a result, now you know how to install Nginx on Ubuntu and how to add the UFW rule for it. But you should understand the configuration files structure to configure Nginx Server.

Config Files

Directory or FileDescription
/etc/nginx/All the nginx configuration files are located in this directory.
/etc/nginx/nginx.confThe main configuration file for Nginx. Changes in this file applied globally.
/etc/nginx/sites-available/Config files of all the available Server Blocks (Virtual Hosts) are located here.
/etc/nginx/sites-enabled/Softlink’s of all the enabled virtual hosts are located here. You have to create a soft link of the server blocks config file to enable it.
/etc/nginx/snippets/Frequently used configurations are can be saved as snippets and can be used repeatedly easily.

Content Files

Directory or FileDescription
/var/www/html/By default, All the contents of web application are stored in this directory. This can be changed by configuration.

Log Files

Directory or FileDescription
/var/log/nginx/All the log files of Nginx are located in this directory.
/var/log/nginx/access.logDefault Access log file for the Nginx web server.
/var/log/nginx/error.logDefault Error log file for the Nginx web server.

Step 5 – Configure a Server Block (Virtual Host)

Server Blocks are used when we want to serve multiple domains from a single server. In Apache web server they known as Virtual Hosts.

For this tutorial, we configure a Server Block for the domain demo.linuxbots.com, remember to replace it with your own domain name.

Meanwhile, the default configuration of Nginx is serve the /var/www/html/ directory contents. The default config is working well with a single site but it can not be used with multiple site setup.

We will create a new directory and a new server block config file for our domain. So let’s start by creating the directory structure for our example domain by using the below command.

sudo mkdir -p /var/www/demo.linuxbots.com/html

After that, change the ownership of the directories by using the below command.

sudo chown -R $USER:www-data /var/www/demo.linuxbots.com

Now, change the permission of the folder. The default permission is okay if you did not change anything. Otherwise, change the permission by using the below command.

sudo chmod -R 755 /var/www/demo.linuxbots.com

In case you want to make the directory writable by the webserver. Use the below permissions.

sudo chmod -R 775 /var/www/demo.linuxbots.com

After that, create an example index file in the HTML directory created in the previous step.

vim /var/www/demo.linuxbots.com/html/index.html

Enter some example HTML code and save the file. You can also use the below HTML code.

<html>
    <body>
        <h1>Welcome the Linuxbots.com</h1>
	<h3>The exmaple Config for nginx is working...</h3>
    </body>
</html>

Finally, create a server block in /etc/nginx/sites-available/ by using the below command.

sudo vim /etc/nginx/sites-available/demo.linuxbots.com

Enter the below contents in the file and replace demo.linuxbots.com with your own domain name. After that save the file.

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

        root /var/www/demo.linuxbots.com/html;
        index index.html;

        server_name demo.linuxbots.com;

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

We have changed the server name and root directory in this configuration.

Now, Enable this configuration by creating a soft-link to /etc/nginx/sites/enabled/ directory. Use the below command for this.

sudo ln -s /etc/nginx/sites-available/demo.linuxbots.com /etc/nginx/sites-enabled/demo.linuxbots.com

After that, you check the configuration by using the below command.

sudo nginx -t

You should see the following output.

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart or Reload Nginx on Ubuntu

Now, restart or reload the Nginx service for applying the changes by using the below commands.

sudo systemctl restart nginx

or

sudo systemctl reload nginx

Nginx should now be serving your server block. To verify it point your browser to the server block domain name. You should see the content which we write in the index file.

example-server-block-nginx
Example Server Block Nginx

Conclusion

As a result, we have successfully install Nginx on Ubuntu 18.04 Sever. You can share your views in the comment section.

Leave a Comment