How to Install the NGINX Web Server On Ubuntu 18.04

HTML displayed on a screen.

NGINX is a very powerful, flexible, and lightweight web server that can easily be installed on the Linux platform. If your Linux distribution of choice is Ubuntu, that process is even easier. Here we’ll walk you through the process of installing the stable version of NGINX (1.14.0) on the latest LTS (Long Term Support) release of Ubuntu Server (18.04).

Why Install NGINX?

You might be asking yourself, “Why install NGINX when Apache has been the default web server for years?” The answer is a bit more complicated than you might expect. To simplify that answer, consider Apache is a web server that does an amazing job of handling a large number of requests per second. However, Apache performance does start to struggle when requests increase. This means, when Apache server visits spike, page load times can suffer. On the other hand, NGINX is optimised to provide consistent and predictable performance. So even when page visits spike, NGINX won’t falter. On the flip side, Apache is also far more flexible than NGINX. With a great deal of modules and support for other services and servers, Apache can (at the moment) do more than NGINX. So if performance is what you’re looking for NGINX is your server. If flexibility is on top of your list, Apache should be considered first.

Stopping and Disabling Apache

A screenshot of the commands to stop the Apache server.

The first thing to be done is stopping and disabling Apache. If Apache is running on the server, NGINX cannot be installed. Apache can remain installed the same server as NGINX, but unless Apache is stopped, NGINX will refuse to install. To find out if Apache is running, issue the command:

sudo systemctl status apache2

If Apache is listed as running, you must stop and disable it, so NGINX can be installed. To stop the Apache web server, issue the command:

sudo systemctl stop apache2

Disable the Apache server (so it doesn’t restart in the event of a server reboot) with the command:

sudo systemctl disable apache2

Installing NGINX

A screenshot of commands while installing the NGINX web server.

With Apache taken care of, the installation of NGINX can be accomplished with a single command:

sudo apt-get install nginx

Once installed, start and enable NGINX with the following commands:

sudo systemctl start nginx
sudo systemctl enable nginx

The NGINX web server is now installed and running.

Viewing the NGINX Welcome Page

Now that NGINX is installed, you can point your web browser to the IP address of the hosting server to see the NGINX Welcome Page. If you’re not sure of your server’s IP address, issue the command:

ip a

The output of the above command will display your server address.

Chances are, you’ll run into a problem when trying to view the default NGINX index.html page. The issue is that, if Apache was installed first, NGINX will serve up the Apache index.html page by default. In order to see the NGINX Welcome Page (index.nginx-debian.html), the Apache Welcome Page must be renamed. Back at the terminal window, issue the command:

sudo mv /var/www/html/index.html /var/www/html/index.html.old

Go back to your web browser and reload that page. The Apache Welcome Page should now be replaced by the NGINX Welcome Page.

How to Configure a Web Site

With NGINX installed and running, it is now time to setup your first web page. How NGINX sites are configured is done very much like Apache. Let’s walk through the basics of setting up a barebones site. The first thing you need to know are the necessary directories that house the files used in creating a site. These directories are:

  • /var/www/html — this is the NGINX document root, where all of your website directories and pages will be housed.
  • /etc/nginx/sites-available — this is the directory that houses all of the configuration files for each of your sites.
  • /etc/nginx/sites-enabled — this is the directory that instructs NGINX which sites are actually enabled for the server.

The difference between sites-available and sites-enabled is twofold:

  • sites-available are actual files for every site you have created for the server.
  • sites-enabled are links to the files in sites-available. Unless there is a link in sites-enabled, NGINX will not be aware of a site in sites-available.

Out of the box, there will be a single file in /etc/nginx/sites-available. That file is default. What we’re going to do is create a new (bare minimum) site. The first thing that must be done is to create a directory (within the NGINX document root) to house our website. We’ll call that site test. From the terminal window, issue the command:

sudo mkdir /var/www/html/test

Create an index.html file with the command:

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

Inside that document, add the following:


Save and close the file with the command Ctrl-x. Give the directory the necessary permissions with the commands:

sudo chown www-data:www-data -R /var/www/html/test
sudo chmod -R 755 /var/www/html/test

Now create a configuration file for our new site in /etc/nginx/sites-available with the command:

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

Inside that file, add the following content:

server {

      listen 80;

      listen [::]:80;

      root /var/www/html/test;

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

      server_name _;

      location / {

              try_files $uri $uri/ =404;



Save and close that file.

Test the NGINX configuration with the command:

sudo nginx -t

The test will display as successful. In order to make sure NGINX can display the newly crafted test site, restart the web server with the command:

sudo systemctl restart nginx

You can now point your browser to http://SERVER_IP/test (Where SERVER_IP is the IP address of your server) to see the newly created index.html file displayed.