Install and Configure Nginx on Ubuntu Linux 18.04 LTS nixCraft

I am a new Ubuntu Linux user and a web developer by profession. How do I install and configure Nginx on Ubuntu Linux 18.04 LTS?

Introduction: Nginx is a free and open source web server. Nginx has a service for sending web pages over the Internet. You can send static or dynamic web pages generated by PHP. Nginx is well known for high-performance HTTP, HTTPS and reverse proxy server. It provides a simple configuration and uses low resources on the server. It is an excellent alternative to Apache server.

Install and Configure Nginx on Ubuntu Linux 18.04

The procedure to install Nginx on Ubuntu 18.04 LTS is as follows:

  1. Update the system using apt command
  2. Install Nginx on Ubuntu using apt install nginx
  3. Configure Nginx server
  4. Enable and restart Nginx server

Let us see all steps in details to install Nginx on Ubuntu Linux 18.04 LTS server.

Step 1. Login to your server using the ssh command

$ ssh user@server
$ ssh

Type the following ip command:
$ ip show
$ ip addr show

Note down the IP address You might have a public IPv4 address. It all depends upon your setup.

Step 3. Install Nginx on Ubuntu Linux 18.04 LTS

$ sudo apt update
$ sudo apt upgrade
$ sudo apt install nginx

Enable Nginx server at boot time using the systemctl command:
$ sudo systemctl enable nginx
Start Nginx server using the systemctl command:
$ sudo systemctl start nginx
Restart Nginx server using the systemctl command:
$ sudo systemctl restart nginx
Stop Nginx server using the systemctl command:
$ sudo systemctl stop nginx
Reload Nginx server using the systemctl command:
$ sudo systemctl reload nginx
Get status of Nginx server using the systemctl command:
$ sudo systemctl status nginx

Step 5. Open port 80 and 443 using UFW on Ubuntu Linux (firewall config)

UFW is an acronym for uncomplicated firewall. It is used for managing a Linux firewall and aims to provide an easy to use interface for the user. To open port 80 (HTTP) and HTTPS (443), run:
$ sudo ufw allow https comment 'Open all to access Nginx port 443'
$ sudo ufw allow http comment 'Open access Nginx port 80'
$ sudo ufw allow ssh comment 'Open access OpenSSH port 22'
$ sudo ufw enable

Verify it:
$ sudo ufw status

Step 6. Verify Nginx is working on Ubuntu 18.04 LTS

Your web server is up and running. It is time to test it. Use the IP address gathered in step # 2. Fire a web browser and type the URL:
OR use public IP address:

Nginx in action with a default page

The default Nginx page indicates that the Ubuntu and Nginx server is running fine on your system.

Step 7. Configure Nginx server

Let us set up our public domain (e.g., or and so on ) with a directory.

Create a user to store web pages

Add a new Linux user named www-pubcms using the useradd command
$ sudo useradd -s /usr/sbin/nologin -m -d /home/lighttpd/ -c ' user' www-pubcms
Lock down the Linux user account using the passwd command:
$ sudo passwd -l www-pubcms
passwd: password expiry information changed.

Make a directory to store web pages using the mkdir command

$ sudo mkdir -v /home/lighttpd/http/

Create a new sample web page

Use a text editor such as nano command or vim command:
$ sudo nano /home/lighttpd/http/index.html
$ sudo vim /home/lighttpd/http/index.html
Append the following HTML code:

<html> <head> <title>CMS.CYBERCITI.BIZ</title> </head> <body> <h1>Welcome</h1> This is a test page for <hr> <small>Powered by Nginx and Ubuntu 18.04 LTS</small> </body>

Set permission for the dir:
$ sudo chown -vR www-pubcms:www-pubcms /home/lighttpd/

Create virual domain configuration for your domain

$ sudo vim /etc/nginx/sites-available/
$ sudo nano /etc/nginx/sites-available/
Append the config:

# our first viraul host
server { listen 80; # port server_name; # dns server name # log files access_log /var/log/nginx/cms.cyberciti.biz_access.log; error_log /var/log/nginx/cms.cyberciti.biz_error.lg; # document root where files stores for domain root /home/lighttpd/http; index index.html index.htm;

Save and close the file. Create a new soft link using ln command in the sites-enabled directory to enable domain:
$ cd /etc/nginx/sites-enabled/
$ sudo ln -v -s /etc/nginx/sites-available/ .

Test and gracefully reload nginx server

$ sudo nginx -t

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

$ sudo systemctl reload nginx
Make sure you map domain name to your public IP address such as One can use the host command or dig command to verify A record for
$ host has address has IPv6 address 2600:3c00:1::68c8:17e8

Fire a browser and type your domain name:

Import Nginx log files

  • /var/log/nginx/ – Nginx server log files.
  • /etc/nginx/ – Nginx server config files directory. All active site config can be found in /etc/nginx/sites-enabled/ directory linked from actual config file directory at /etc/nginx/sites-available/
  • /etc/nginx/nginx.conf – Your main nginx config file.

Use the tail command or more command or grep command or cat command to view server log files:
$ tail -f /var/log/nginx/access.log
$ more /var/log/nginx/error.log
$ grep 'something' /var/log/nginx/cms.cyberciti.biz_access.log
$ cat /var/log/nginx/cms.cyberciti.biz_access.log

Check Nginx logs file to make sure everything working properly for all domains

Verify that Nginx ports are open on Ubuntu Linux with the ss command or netstat command:
$ ss -tulpn
$ ss -tulpn | grep :80
$ netstat -tulpn


And there you have it, Nginx server installed and configured on an Ubuntu Linux 18.04 LTS server. In the second part of the series, you would learn about configuring HTTPS (SSL/TLS certificates) server for security reasons. For more info see nginx wiki here.

Get 15% off on Linux Foundation certified SysAdmin, Progamming, Kubernetes/Containers and Open Stack certification & course. Use “CYBER15” coupon code.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Spread the love

Posted by News Monkey