How To Install Nginx on Ubuntu 20.04

In this article, we want to teach How to Install Nginx on Ubuntu 20.04.

Nginx is open-source software for web serving, reverse proxying, caching, load balancing, media streaming, and more. It started out as a web server designed for maximum performance and stability. In addition to its HTTP server capabilities, Nginx can also function as a proxy server for email (IMAP, POP3, and SMTP) and a reverse proxy and load balancer for HTTP, TCP, and UDP servers. And it is more flexible than the Apache HTTP server.

How To Install Nginx on Ubuntu 20.04

To install Nginx on Ubuntu 20.04 you need to log in to your server as a non-root user with sudo privileges. To do this you can check the Initial server setup for Ubuntu 20.04.

Install Nginx web server on Ubuntu 20.04

By default, Nginx is available in Ubuntu’s default repository. First, update the APT packages with the following command:

sudo apt update

Now install the Nginx on Ubuntu 20.04 with the following command:

sudo apt install nginx

When your installation is finished you need to adjust the firewall.

First, you need to enable the UFW firewall in Ubuntu 20.04 with the following command:

sudo ufw enable

Check that your firewall is active or not:

sudo ufw status
Output
Status: active

You need to list the available application through the firewall with the following command:

sudo ufw app list

In your output you will see:

Output
Available applications:
Nginx Full
Nginx HTTP
Nginx HTTPS
OpenSSH

Here you only need to allow traffic on port 80. you should enable the Nginx HTTP with the following command:

sudo ufw allow 'Nginx HTTP'

Now verify the changes with the command below:

sudo ufw status

Your output should similar to this:

Output
Status: active
To Action From
-- ------ ----
Nginx HTTP ALLOW Anywhere
Nginx HTTP (v6) ALLOW Anywhere (v6)

Let’s see how to check your Nginx web server.

How to check the Nginx webserver

At this point, the Nginx web server on Ubuntu 20.04 should be up and running.

To make sure that your service is up and running run the following command:

systemctl status nginx

In your output you will see:

Output
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 Sun 2021-09-26 11:35:49 CEST; 18min ago
Docs: man:nginx(8)
Main PID: 1864 (nginx)
Tasks: 3 (limit: 2282)
Memory: 6.1M
CGroup: /system.slice/nginx.service
├─1864 nginx: master process /usr/sbin/nginx -g daemon on; master_process >
├─1865 nginx: worker process
└─1866 nginx: worker process

As you can see the service is active and running on your server.

Also, you can access the default Nginx landing page. for this, you need your server’s public IP address.

To get your IP address you can use the following command:

hostname -I

Or you can use the curl tool to get your IP:

curl -4 icanhazip.com

Now that you have your server IP address, type it into your web browser to access the default Nginx landing page on Ubuntu 20.04.

http://your_server_ip

Nginx default land page

If you see this page your Nginx web server is working correctly on Ubuntu 20.04 and you can start to manage your web server.

How to manage the Nginx process

Now that you have your Nginx web server up and running, let’s review some basic management commands.

To stop the Nginx on Ubuntu 20.04 run the following command:

sudo systemctl stop nginx

You can start your service when it is stopped:

sudo systemctl start nginx

Also, you can stop and start your service again with the following command:

sudo systemctl restart nginx

If you have made configuration changes you need to reload your service after that:

sudo systemctl reload nginx

Nginx is configured to start automatically at boots by default. if you don’t want that to happen run the following command:

sudo systemctl disable nginx

To re-enable the service to start up at boot, use the following command:

sudo systemctl enable nginx

At this point, you can set up Nginx server blocks for your domain on Ubuntu 20.04.

Let’s see how it works.

How to Set up Nginx server blocks

The Nginx web server, server blocks (similar to virtual hosts in Apache) can be used to encapsulate configuration details and host more than one domain from a single server.

Here our domain name is nginx.orcacore.net. remember to replace it with your own domain name in the commands.

First, create a directory for your domain with the following command:

sudo mkdir -p /var/www/nginx.orcacore.net/html

Now you need to assign ownership of the directory with the $USER environment variable b using the following command:

sudo chown -R $USER:$USER /var/www/nginx.orcacore.net/html

Make sure the permissions of your web roots are correct with the following command:

sudo chmod -R 755 /var/www/nginx.orcacore.net

Here you need to create a sample index.html page. we use vi text editor to create that file you can use your favorite text editor:

sudo vi /var/www/nginx.orcacore.net/html/index.html

Then, add the sample HTML code into the file:

<html>
<head>
<title>Welcome to nginx.orcacore.net!</title>
</head>
<body>
<h1>Success! The nginx.orcacore.net server block is working!</h1>
</body>
</html>

save and close the file when you are finished.

Note: If you are interested, you can learn more about the vi by visiting the vi text editor article.

At this point, you need to create a server block with the correct directives with the following command:

sudo vi /etc/nginx/sites-available/nginx.orcacore.net

Paste the following configuration block into the file:

server {
listen 80;
listen [::]:80;

root /var/www/nginx.orcacore.net/html;
index index.html index.htm index.nginx-debian.html;

server_name nginx.orcacore.net www.nginx.orcacore.net;

location / {
try_files $uri $uri/ =404;
}
}

Now you need to enable the file by creating a link from it to the sites-enabled directory with the following command:

sudo ln -s /etc/nginx/sites-available/nginx.orcacore.net /etc/nginx/sites-enabled/

Also, you need to adjust a single value in the /etc/nginx/nginx.conf file.

Open the file with the following command:

sudo vi /etc/nginx/nginx.conf

Then, find the server_names_hash_bucket_size line and uncomment it by removing the “#” from it.

...
http {
...
server_names_hash_bucket_size 64;
...
}
...

When you are finished save and close the file.

Test for no syntax error with the following command:

sudo nginx -t
Output
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Then, restart the Nginx to enable your changes on Ubuntu 20.04:

sudo systemctl restart nginx

At this step, Nginx should now be serving your domain name. type your domain name in your web browser:

http://your-domain-name

You should see something like this:

Nginx server blocks on Ubuntu 20.04

Conclusion

At this point, you learn How to install Nginx on Ubuntu 20.04. and also, you can set up the Nginx server blocks.

Hope you enjoy it.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!