Skip to main content

How to install Nginx on Ubuntu 20.04 LTS (2021 Tutorial)



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

Installing Nginx with apt

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
Install Nginx On Ubuntu 20.04 LTS Linux server

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

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.ows.codes

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

Verify and check your web server - welcome to nginx

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.ows.codes
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.ows.codes - welcome nginx website</title>
</head>
<body>
 <h2>www.ows.codes (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@ows.codes</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.
ows.codes

Comments

Popular posts from this blog

Authority Backlinks

Here is the commonly used Backlinks by Nigerian Bloggers Today.

OWS Auto Music + Youtube Video Grabber Blog Documentation

OWS Auto Blog Documentation Getting Started What is OWS Auto Blog? A simple and also the Best Plugin for WordPress websites when it comes to posting albums, tracks, and grabbing from RSS feeds without leaving a trace of the original poster or link back (credits) eg: The post title appeared first on website name... Compatible With any hosting plan (Shared*, Dedicated, VPS, etc.) cPanel, Apache, Nginx, Plesk, Webuzo, Aapanel (bt.cn), ZPanel, VestaCP, Sentora, Froxlor or ServerPilot. FFmpeg only for Voice tag. Features Auto Post from RSS feed and it tags the mp3 to your website with your music artwork (music cover), with unique content without credit to the original posters, it uses your default post content in feed settings or random post content (You can configure either random post or default post via settings), it automatically sets tags, category, and author according to the settings in OWS Auto Blog (Feed Settings), eg: let us say the title is "Drake - Gods Plan", it wil

OWS Mp3 Tagger Documentation

OWS MP3 TAGGER PHP Script Documentation Getting Started What is OWS Mp3 Tagger? A simple and Powerful PHP Script For Mp3 Uploads Minimum Requirements Works with any hosting plan (Shared*, Dedicated, VPS, etc.) Apache or Nginx or cPanel FFmpeg 1 MB Disk Space on Server (OWS Mp3 Tagger) file size * Note:   If you are using a shared hosting plan, please ensure that FFmpeg is installed (You can contact your web hosting). Features Block Virus in Mp3 Files Use Any Image or default image Protected against Attacks Easy Upload Songs Allows Voice Tag in Mp3 Files (Jingle) Add New songs Without reloading pages Validate Images before adding to Mp3 Add Artist, Title, Genre, Comment, Album, Release Year, Custom Image, or Default Image, Upload songs without consuming your data. Add Voice Tag in Mp3 Eg: Song beginning only, Song beginning and close to the ending, Beginning middle and ending, Middle only, Custom Eg: add after 30 secs, Custom Eg: add after 30 secs and 60 secs,  Clean and friendly code