How to Install the NGINX Web Server on Ubuntu

NGINX brings outstanding performance to Ubuntu 18.04

NGINX Web Server logo

NGINX

NGINX offers a powerful, flexible, and lightweight web server that you can easily install on the Linux platform. If your Linux distribution of choice is Ubuntu, that process is even easier. Walk through the process of installing the stable version of NGINX (version 1.14.0) on the latest Long-Term Support (LTS) release of Ubuntu Server (version 18.04).

Instructions in this article apply to NGINX version 1.14.0 and Ubuntu Server LTS version 18.04.

How to Install NGINX

You might be asking yourself, “Why install NGINX when Apache HTTP Server (Apache) has been the default Linux web server for years?” Well, Apache does an amazing job of handling multiple requests per second, but its performance falters when requests increase. So, when Apache server visits spike, page load times can suffer. In contrast, NGINX is optimized to provide consistent, predictable performance. Even when page visits spike, NGINX won’t falter. So, if performance is what you’re looking for, NGINX is your server.

To install NGINX, complete the following steps.

  1. Stop and disable Apache.

    A screenshot of the commands to stop the Apache server.

    If Apache is running on your Ubuntu server, you won't need to uninstall Apache to install and run NGINX, but the NGINX installer won't work until you stop Apache.

    To find out if Apache is running, open a terminal window, and then run the following command:

    sudo systemctl status apache2

    If Apache is listed as running, stop it by running the following command:

    sudo systemctl stop apache2

    Run the following command to disable Apache so that it doesn’t restart in the event of a server reboot:

    sudo systemctl disable apache2
  2. Install NGINX

    A screenshot of commands while installing the NGINX web server.

    With Apache disabled, you can install NGINX by running a single command:

    sudo apt-get install nginx

    Once installed, start and enable NGINX by running the following commands:

    sudo systemctl start nginx
    sudo systemctl enable nginx

    The NGINX web server is now installed and running.

  3. View the NGINX welcome page.

    With NGINX 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, run the following command:

    ip a

    The output of this command displays your server address.

    You could run into a problem when you try to view the default NGINX index.html page—namely, if Apache was installed first, by default, NGINX serves up the Apache index.html page. To see the NGINX welcome page (index.nginx-debian.html), you must rename the Apache welcome page.

    To do so, in the terminal window, run the following command:

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

    Now, go back to your web browser and reload the page. You should see the NGINX welcome page.

  4. Get to know the NGINX website structure.

    With NGINX installed and running, you're ready to set up your first web page. You configure NGINX web pages much like you do Apache web pages. First, you need to know the directories that house the files you'll use to create a site:

    • /var/www/html. This is the NGINX document root and houses all your website directories and pages.
    • /etc/nginx/sites-available. This directory houses all the configuration files for each site.
    • /etc/nginx/sites-enabled. This directory tells NGINX which sites are enabled for the server.

    There are two major difference between sites-available and sites-enabled:

    • 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's a link in sites-enabled, NGINX won't be aware of a site in sites-available.
  5. Create a new website.

    Out of the box, /etc/nginx/sites-available contains a single file called default. You must create a new (bare minimum) site.

    First, create a directory within the NGINX document root to house your website. Name this site test. Then, in the terminal window, run the following command:

    sudo mkdir /var/www/html/test

    Next, create an index.html file by running the following command:

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

    Within that document, add the following text:

    <H1>HELLO LIFEWIRE!</H2>

    Save and close the file by running the command Ctrl-x.

  6. Give the directory the necessary permissions by running the following commands:

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

  7. Create a configuration file for your new site in /etc/nginx/sites-available by running the following command:

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

    In that file, type 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 the test file.

  8. Test the NGINX configuration by running the following command:

    sudo nginx -t

    The test should be successful.

  9. To make sure NGINX can display the new test site, restart the web server by running the following command:

    sudo systemctl restart nginx

  10. Open your browser. In the address bar, type http://SERVER_IP/test (where SERVER_IP is the IP address of your server) to see your newly created index.html file.