Install Nginx on Ubuntu 18.04

Filed Under: NGINX

NGINX pronounced as engine-x is an open source and popular HTTP server and can be configured to act as a reverse proxy and a load balancer.

In comparison with Apache, NGINX is less resource hungry and can serve a large number of concurrent request due to its event-based and asynchronous architecture.

Although Apache has a lead in the market share NGINX is steadily gaining the ground over the last few years. In this article, we will explore how to install NGINX on Ubuntu 18.04 along with configuring a virtual host.


You can SSH into the Ubuntu 18.04 system using the root or a sudo enabled user. If you are logged in as root, then you can run the commands as it is. If you are using a sudo enabled used, please prefix the commands with “sudo”.

Update the system

Before you start installing NGINX, it is always recommended to upgrade your Ubuntu 18.04 to the latest. The following apt-get commands will do it for you.

# apt-get update 
# apt-get upgrade

The first command will update the list of available packages and their versions and the second one will actually install the newer versions of the packages that you have.

Once you are done with upgrading the system, check the release version of your Ubuntu system with the following command.

# lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.2 LTS
Release:        18.04
Codename:       bionic

Install NGINX on Ubuntu

NGINX is available in the default repositories of Ubuntu and can be installed with a single line command as shown below.

# apt-get install nginx

Once NGINX has been installed, it will run automatically. You can verify that by using following systemctl command.

 # systemctl status nginx
 ● 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 Tue 2019-02-12 09:12:08 UTC; 11s ago
     Docs: man:nginx(8)
  Process: 17726 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 17714 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
 Main PID: 17729 (nginx)
    Tasks: 2 (limit: 1152)
   CGroup: /system.slice/nginx.service
           ├─17729 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
           └─17732 nginx: worker process

The output of the above command verifies that NGINX is loaded and active with PID of 17729.

Configuring Firewall Access

You have probably enabled the default firewall manager UFW in your Ubuntu 18.04 system earlier. Therefore, you need to unblock the port number 80 which is used by NGINX to serve the web pages using HTTP.

# ufw allow 80/tcp
# ufw reload

If you are planning to use HTTPS by installing an SSL certificate at a later stage then open up port number 443 as well.

# ufw allow 443/tcp
# ufw reload

Now point your favorite web browser to https://SERVER_IP, you will be greeted with NGINX welcome page.

Install NGINX on Ubuntu Welcome Page

Nginx Welcome Page

Alternatively, you can also verify the same using a CURL command. If Nginx is up and running then the following curl command will fetch the headers of index page otherwise it will display an error message. Adjust the IP address that is used with the following curl command as per yours.

# curl -I
cURL Nginx Ubuntu Server IP address

cURL Nginx Server IP

Managing NGINX Service

Managing NGINX service is pretty easy like you do it for other systemd services. The following section will list each of the NGINX systemd commands one by one.

1. Stop NGINX Server

To stop the NGINX service use the following systemd command:

# systemctl stop nginx

2. Start NGINX Server

Start NGINX again using:

# systemctl start nginx 

3. Restart NGINX Server

To stop and start the NGINX service use:

# systemctl restart nginx

4. Reload NGINX Server Configurations

To reload the NGINX service without dropping the connections use:

# systemctl reload nginx

5. Disable NGINX Auto Start at Boot

By default, NGINX is configured to start automatically during reboot as we have seen it earlier. To disable this behavior use the following command:

# systemctl disable nginx

5. Enable NGINX Auto Start at Boot

To enable the NGINX to start automatically during reboot again use:

# systemctl enable nginx

Add a virtual host to NGINX Server

The configuration file for default page served by NGINX can be found in the location /etc/nginx/sites-available, and the filename must be default. Edit this file to add more configuration options for your default site. But we are interested in adding a virtual host.

To add a virtual host, navigate to the /etc/nginx/sites-available folder. This is the default location for NGINX server blocks. Use your favorite editor to create one such server block i.e a virtual host in the said location.

# cd /etc/nginx/sites-available
# cat wordpress.conf
 server {
        listen 80;
        root /var/www/html/wordpress;
        index index.html;

The above virtual host uses server name as, change this name to your domain. Now create the root for the site as per above server block and change the ownership of the folder to NGINX owner which is www-data.

# mkdir -p /var/www/html/wordpress
# chown -R www-data:www-data /var/www/html/wordpress

To activate the server block create a symbolic link of the above virtual host configuration file inside /etc/nginx/sites-enabled folder.

# cd /etc/nginx/sites-enabled
# ln -s ../sites-available/wordpress.conf .

Finally create an index page:

# cd /var/www/html/wordpress
# echo "This page is served from the domain" > index.html 

Reload the new settings of NGINX:

# systemctl reload nginx

Use the curl command to verify if NGINX is serving the index page for the new domain:

# curl
This page is served from the domain 

The output of the above CURL command verifies that NGINX is serving the index page for the domain and thus validates the correctness of above virtual host configuration.


The installation of NGINX in Ubuntu 18.04 is complete. Apart from installing NGINX, we have also checked various systemd options of NGINX and how to add a virtual host through NGINX server blocks. You can now proceed with installing an SSL certificate for your domain possibly from Lets Encrypt by creating another server block and this will enable NGINX to serve pages using HTTPS.


  1. Curtis Underwood says:

    Thanks for the knowledge

  2. Shahrukh says:

    The title of this post is really bad, and yes I am a real person writing this review, you could have used a title like may be NGINX – zero to hero

    1. Pankaj says:

      The title is meant to convey a brief idea about the article. That’s why it’s named like this.

Leave a Reply

Your email address will not be published. Required fields are marked *

Generic selectors
Exact matches only
Search in title
Search in content