Nginx is a high performance, free and an open-source HTTP and reverse proxy server. It can be used as a standalone web server, and as a reverse proxy for Apache and other web servers. This tutorial explains how to install Nginx on Ubuntu 20.04.
It is a more flexible and lightweight program than Apache HTTP Server that’s why it powers some of the largest sites on the Internet. Nginx can handle the bigger amount of connection than Apache and consuming smaller memory.
- You should logged in Ubuntu server with a non-root sudo enabled user account.
- Ensure that Apache or any other services are not running on port
Install Nginx on Ubuntu
Nginx package is available in the default Ubuntu repositories. Perform the following steps to install Nginx:
At first, you need to update packages index to the latest version by type:
sudo apt update
Now, run the below command to install Nginx:
sudo apt install nginx
Once the installation process is completed, Nginx service will start automatically. Verify it by running the following command:
sudo systemctl status nginx
● 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 Mon 2020-05-03 04:20:06 UTC; 16min ago Docs: man:nginx(8) Main PID: 423 (nginx) Tasks: 2 (limit: 4915) CGroup: /system.slice/nginx.service ├─423 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; └─424 nginx: worker process
If on your server firewall is enabled then you need to open both HTTP(
80) and HTTPS(
443) ports. Execute the following commands to open both HTTP(
80) and HTTPS(
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload
If your using iptables to filter connections to your system, you’ll need to open HTTP (
80) and HTTPS (
Open the necessary ports by issuing the following command:
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
You can verify your Nginx installation by visiting below URL in your browser:
It will show you default Nginx welcome page as shown below:
Managing Nginx Service
Finally, you installed Nginx HTTP server on your machine. Below are few basic commands to manage Nginx service:
You can stop Nginx service execute below command:
sudo systemctl stop nginx
To start it again by typing:
sudo systemctl start nginx
Restart (stop and start) Nginx service the Apache service by:
sudo systemctl restart nginx
If configuration file edited and want to Reload nginx service then you can do it by typing:
sudo systemctl reload nginx
For disable Nginx to auto start after boot execute below command:
sudo systemctl disable nginx
Again to do re-enable type:
sudo systemctl enable nginx
Nginx Configuration Files Structure
- All configuration files are located in the
- Nginx main configuration file is at
- It’s best practice to create a separate configuration file of each domain for better maintainability.
- New server blocks (configuration file) of each domain should be stored in
/etc/nginx/sites-availabledirectory. You need to make symlink of these configuration files at
/etc/nginx/sites-enabledto take in used by Nginx.
- Activating server blocks is done by creating a symlink (a pointer) from the configuration file sites in a
/etc/nginx/sites-enableddirectory to the sites-enabled directory.
- The default server web root directory is
- It’s best practice to to follow standard naming convention. Nginx server block files name should as domain name and must end with
.confextension. For example, your domain name is
example.comthen server block file name should
- Nginx log files (
error.log) are located in the
/var/log/nginx/directory. It’s also recommended to have a different access and error log files for each server block.
In this tutorial, you learned how to install Nginx on your Ubuntu 20.04 Focal Fossa. Now you can deploy your applications and use Nginx as a web or proxy server. To learn more visit Nginx official page.
If you have any question or suggestion, please leave comment below.