How to install Nginx on Ubuntu 20.04 LTS

How do I install the Nginx on Ubuntu 20.04 LTS Linux server?

The Nginx is a high-performance web server for serving dynamic and static pages. It works in both standalone and reverse proxy mode. Nginx is free and open-source software released under the 2-clause BSD license. Let us see how to install and the Nginx on Ubuntu 20.04 LTS Linux server.
Tutorial requirements
Operating system/appUbuntu 20.04 LTS
Root privileges required Yes
Difficulty Easy (rss)
Estimated completion time 20m
Table of contents

How to install Nginx on Ubuntu 20.04 LTS

Make sure your system is up to date and patched. To do that, type the following apt command:
$ sudo apt update
$ sudo apt upgrade

Step 1 – Installing Nginx server

Now that Ubuntu updated with the latest patches, it is time to install Nginx software. In other words, type the following apt command and the press the [Enter] key:
$ sudo apt install nginx

Step 2 – Make sure Nginx service started at boot time

We are going to use the systemctl command as follows to enable the nginx.service:
$ sudo systemctl is-enabled nginx.service
If nginx.service not enabled, enable it, run:
$ sudo systemctl enabled nginx.service

Managing Nginx service on Ubuntu cloud server

To start, stop, restart and then find the service status again use the following commands as per your needs.

Start the Nginx service

$ sudo systemctl star nginx.service

Stop the Nginx service

$ sudo systemctl start nginx.service

Restart the Nginx service

$ sudo systemctl restart nginx.service

Reload the Nginx server gracefully

$ sudo systemctl reload nginx.service

Get status of the Nginx server

$ sudo systemctl status nginx.service
Sample session:

 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 Fri 2020-06-05 16:37:04 UTC; 7min ago
       Docs: man:nginx(8)
   Main PID: 68741 (nginx)
      Tasks: 3 (limit: 4660)
     Memory: 2.6M
     CGroup: /system.slice/nginx.service
             ├─68741 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             ├─68743 nginx: worker process
             └─68744 nginx: worker process

Jun 05 16:37:04 ip-172-26-9-151 systemd[1]: Starting A high performance web server and a reverse proxy server...
Jun 05 16:37:04 ip-172-26-9-151 systemd[1]: Started A high performance web server and a reverse proxy server.

Step 3 – Open the Nginx port 80 and 443 using UFW firewall

You need to type the following ufw command to open TCP port number 80 and 443 for everyone:
$ sudo ufw allow 80/tcp comment 'accept HTTP Nginx'
$ sudo ufw allow 443/tcp comment 'accept HTTPS/TLS Nginx connections'

Verify port status it:
$ sudo ufw status

Step 4 – Find your Ubuntu 20.04 LTS server’s static IP address

Execute any one of the following command:
$ hostname -I
$ ip a
$ ip a s eth0

My AWS EC2 IP address:

172.26.9.151

You can also use the dig command/host command as follows to find your public IPv4/IPv6 address from the CLI:
$ dig TXT +short o-o.myaddr.l.google.com @ns1.google.com

52.42.113.13

See How To Find My Public IP Address From Command Line On a Linux for more info.

Step 5 – Test your Nginx server installation on Ubuntu

At this stage you can use the curl command as follows:
$ curl -I http://172.26.9.151
$ curl -I http://52.42.113.13

If you have domain name mapped and A dns record set to 52.42.113.13, try:
$ curl -I http://your-domian-name-here
$ curl -I http://www.cyberciti.biz

HTTP/1.1 200 OK
Server: nginx/1.17.10 (Ubuntu)
Date: Fri, 05 Jun 2020 17:00:01 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 05 Jun 2020 16:37:04 GMT
Connection: keep-alive
ETag: "5eda74b0-264"
Accept-Ranges: bytes

Of course we can open a web browser such as Chrome or Firefox/Safari and type the URL as follows:
http://your-server-ip
http://172.26.9.151

Basic configuration

Edit the /etc/nginx/nginx.conf file which is the main server config file:
$ sudo nano /etc/nginx/nginx.conf
Increase worker connections:
worker_connections 1024;
Save and close the file. Other important details for Nginx:

  1. Default HTTP port – 80
  2. Default HTTPS port – 443
  3. User and group for Nginx server – www-data
  4. Nginx DocumentRoot – /var/www/html/
  5. Nginx config directory – /etc/nginx/
  6. Global config file – /etc/nginx/nginx.conf
  7. Virtual domain/host config directory – /etc/nginx/sites-available/
  8. Currently enabled domain/host config directory – /etc/nginx/sites-enabled/
  9. Various config options – /etc/nginx/snippets/
  10. Server log file – /var/log/nginx/access.log
  11. Server error log file – /var/log/nginx/error.log

Step 6 – Configuring Nginx virtual hosts

Create a config file for your domain as follows:
$ sudo nano /etc/nginx/sites-available/your-domain-name.conf
Append the following config:

# Replace your-domain-name-here with your actual domain name such as www.cyberciti.biz
server {
	listen 80;
	listen [::]:80;
 
	server_name your-domain-name-here www.your-domain-name-here;
 
	root /home/your-domain-name-here/html;
	index index.html;
        access_log  /var/log/nginx/www.your_domain_name_access.log;
        error_log  /var/log/nginx/www.your_domain_name_error.log;
 
	location / {
		try_files $uri $uri/ =404;
	}
}

Create a new Ubuntu 20.04 user for your domain

Run the following useradd command $ sudo useradd -d /home/your-domain-name-here -m -k /dev/null -s /usr/sbin/nologin usernamehere
Where,

  • -d /home/your-domain-name-here : Set home directory of the new account to serve DocumentRoot.
  • -m : Make sure we create the user’s home directory set by the -d option.
  • -k /dev/null : Avoid creating dot files for Nginx virtual domain DocumentRoot which can expose sensitive information by using the /dev/null as alternative skeleton directory.
  • -s /usr/sbin/nologin : Set login shell of the new account to /usr/sbin/nologin, so that web server user can not login into our system using the ssh or any other method. Again this is a security feature.
  • usernamehere : User name that will store files for our virtual domain.

Make sure we lock the Linux user account, enter:
$ sudo passwd -l usernamehere
passwd: password expiry information changed.

Create html folder using the mkdir command:
$ sudo mkdir -pv /home/your-domain-name-here/html/
Create a sample html page as follows:
$ sudo nano /home/your-domain-name-here/html/index.html

<html>
<head>
 <title>www.cyberciti.biz - welcome nginx website</title>
</head>
<body>
 <h2>www.cyberciti.biz (virtual host)</h2>
 <p>This is a test page running on:</p>
 <ul>
	 <li>Ubuntu Linux 20.04 LTS</li>
	 <li>Nginx server</li>
 </ul>
 <hr>
 <small>Got a feedback? Email us @ webmaster@cyberciti.biz</small>
</body>
</html>

Set permission using the chown command:
$ sudo chown -R usernamehere:usernamehere /home/your-domain-name-here/
Turn on newly created virtual domain, run:
$ sudo ln -v -s /etc/nginx/sites-available/your-domain-name.conf /etc/nginx/sites-enabled/
'/etc/nginx/sites-enabled/your-domain-name.conf' -> '/etc/nginx/sites-available/your-domain-name.conf'

Test nginx virtual host config:
$ sudo nginx -t

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

You must get “Syntax is OK” message and then restart the Nginx server on Ubuntu Linux:
$ sudo systemctl reload nginx.service
Set your domains’s A and AAAA records to server’s public IPv4/IPv6 address and test it:
http://your-domain-name-here
http://www.cyberciti.biz

Conclusion

Now you have your Nginx web server installed on Ubuntu Linux 20.04 LTS Linux server. You can configure PHP/Python/Perl/Ruby apps or install TLS/SSL certificates to secure traffic. I strongly urge that you read Nginx documentation here.

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig 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 jobs killall kill pidof pstree pwdx time
Searchinggrep 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
0 comments… add one

Leave a Reply

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

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.