Install and Configure Nginx on Ubuntu Linux 18.04 LTS

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

First, login into your server using the ssh command:
$ ssh user@server
$ ssh

Step 2. Find out your Ubuntu Linux server IP address

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

Run the following apt command to install security updates on Ubuntu 18.04:
$ sudo apt update
$ sudo apt upgrade

Sample outputs:

Hit:1 bionic InRelease
Get:2 bionic-updates InRelease [88.7 kB]
Get:3 bionic-backports InRelease [74.6 kB]
Get:4 bionic-security InRelease [88.7 kB]     
Fetched 252 kB in 1s (503 kB/s)                                
Reading package lists... Done
Building dependency tree       
Reading state information... Done
All packages are up to date.

How to install Nginx on Ubuntu 18.04

Finally install Nginx weber server:
$ sudo apt install nginx

Step 4. commands to start/stop/restart Nginx server on Ubuntu

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:

                This is a test page for
                <small>Powered by Nginx and Ubuntu 18.04 LTS</small>

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.

This entry is 1 of 2 in the Linux, Nginx, MySQL, PHP (LEMP stack) in Ubuntu 18.04 LTS Tutorial series. Keep reading the rest of the series:
  1. Install and Configure Nginx on Ubuntu Linux 18.04 LTS
  2. Secure Nginx with Let’s Encrypt on Ubuntu 18.04 with DNS Validation

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 1 comment so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

Comments on this entry are closed.

Comments are closed. Still have questions? Post it on our forum