How To Install and Configure Nginx On Ubuntu 20
How To Install and Configure Nginx On Ubuntu 20
04
Introduction
Nginx is a free, open-source Linux application for web servers. It works as a reverse proxy
server by directing web traffic to specific servers.
Nginx is used for security and load-balancing, but can also function independently as a web
server.
This guide will help you install Nginx on Ubuntu 20.04 Linux (Focal Fossa).
Prerequisites
Note: If the system generates an error about the lock file, please see How To Fix Could Not
Get Lock /Var/Lib/Dpkg/Lock Error for suggestions.
nginx -v
If the status displays active (running), Nginx has already been started. Press CTRL+z to
exit the status display.
If Nginx is not running, use the following command to launch the Nginx service:
To set Nginx to load when the system starts, enter the following:
To grant Nginx access through the default Ubuntu firewall, enter the following:
Note: It is recommended that you only allow the bare minimum required traffic through the
firewall. For this process, only basic HTTP traffic is needed. Other configurations may
require HTTPS (encrypted) or other traffic. If the system uses a different firewall, it should
be configured to allow traffic on Port 80 (HTTP), Port 443 (HTTPS), or whatever ports are
required by the network.
http://127.0.0.1
Note: If the system has a specific hostname or IP address, that may be used instead.
If the system does not have a graphical interface, the Nginx Welcome page can be loaded in
the terminal using curl:
The system should display the HTML code for the Nginx Welcome page.
Step 7: Configure a Server Block (Optional)
In Nginx, a server block is a configuration that works as its own server. By default, Nginx has
one server block preconfigured.
It is located at /var/www/html. However, it can be configured with multiple server blocks for
different sites.
Note: This tutorial uses test_domain.com for the domain name. This may be replaced with
your own domain name.
Open index.html for editing in a text editor of your choice (we will use the Nano text editor):
<html>
<head>
<title>Welcome to test_domain.com!</title>
</head>
<body>
<h1>This message confirms that your Nginx server block is working.
Great work!</h1>
</body>
</html>
server {
listen 80;
root /var/www/test_domain.com/html;
index index.html index.htm index.nginx.debian.html;
server_name test_domain.com www.test_domain.com;
location / {
try_files $uri $uri/ =404;
}
}
5. Create Symbolic Link for Nginx to Read on Startup
Create a symbolic link between the server block and the startup directory by entering the
following:
The system should report that the configuration file syntax is OK, and that the configuration
file test is successful.
If you’re using a test domain name that isn’t registered or public, the /etc/hosts file may need
to be modified to display the test_domain.com page.
hostname –i
In an empty space just below the localhost information, add the following line:
Replace 127.0.1.1 with the IP address displayed above. Press CTRL+o to save the changes,
then CTRL+x to exit.
Open a browser window and navigate to test_domain.com (or the domain name you
configured in Nginx).
o
/var/www/html – Website content as seen by visitors.
/etc/nginx – Location of the main Nginx application files.
/etc/nginx/nginx.conf – The main Nginx configuration file.
/etc/nginx/sites-available – List of all websites configured through
Nginx.
/etc/nginx/sites-enabled – List of websites actively being served by
Nginx.
/var/log/nginx/access.log – Access logs tracking every request to your
server.
/var/log/ngins/error.log – A log of any errors generated in Nginx.
Conclusion
You should now have a working installation of Nginx on Ubuntu 20.04. As a bonus, you
should now have an introduction to setting up an Nginx server block.