
Nginx is an open-source web server that delivers static and dynamic web applications or services. It functions as a web server, load balancer, reverse proxy, or HTTP cache, integrating with existing applications or delivering web services via your server's IP or domain.
This article outlines the steps to install Nginx on Ubuntu 20.04 and configure sample web applications for optimal performance.
Prerequisites
Before you begin:
Have an Ubuntu 20.04 server.
Set up a new A record for your domain that points to the server IP address.
Access the server using SSH as a non-root user with sudo privileges.
Install Nginx on Ubuntu 20.04
The latest Nginx package is available in the default APT repositories for Ubuntu 20.04. Follow the steps below to update your server packages and install Nginx.
Update the server package index.
console$ sudo apt update
Install Nginx.
console$ sudo apt install nginx -y
View the installed Nginx version on your server.
console$ nginx -v
Your output should be similar to the one below:
nginx version: nginx/1.18.0 (Ubuntu)
Manage the Nginx System Service
Nginx uses the nginx
systemd service profile to manage the web server's runtime and processes. Follow the steps below to enable the Nginx service and control the web server processes on your server.
Enable the Nginx web server to start automatically at boot time.
console$ sudo systemctl enable nginx
Output:
Synchronizing state of nginx.service with SysV service script with /usr/lib/systemd/systemd-sysv-install. Executing: /usr/lib/systemd/systemd-sysv-install enable nginx
Start the Nginx service.
console$ sudo systemctl start nginx
Stop the Nginx service.
console$ sudo systemctl stop nginx
Restart the Nginx service.
console$ sudo systemctl restart nginx
View the Nginx service status and verify that it's running.
console$ sudo systemctl status nginx
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 2025-04-06 07:47:16 UTC; 3s ago Docs: man:nginx(8) Process: 3705 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Process: 3714 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS) Main PID: 3715 (nginx) Tasks: 5 (limit: 9415) Memory: 5.3M CGroup: /system.slice/nginx.service ├─3715 nginx: master process /usr/sbin/nginx -g daemon on; master_process on; ├─3716 nginx: worker process ├─3717 nginx: worker process ├─3718 nginx: worker process └─3719 nginx: worker process
Based on the output value
Active: active (running)
, Nginx is running on your server. If the status showsActive: active (failed)
, stop any processes using HTTP port80
and restart thenginx
service.
Create a New Nginx Virtual Host
Nginx virtual hosts include configurations that allow the web server to serve web application files from a specific directory using a domain on your server. Follow the steps below to create a new sample Nginx virtual host configuration for secure file delivery.
Create a new Nginx virtual host configuration in the
/etc/nginx/sites-available
directory. For example,app.example.com.conf
.console$ sudo nano /etc/nginx/sites-available/app.example.com.conf
Add the following configurations to the file.
iniserver { listen 80; listen [::]:80; server_name app.example.com; root /var/www/app.example.com; index index.html; location / { try_files $uri $uri/ =404; } }
Save and close the file.
The above Nginx virtual host configuration listens for incoming connections using your
app.example.com
domain and serves your web application files from the/var/www/app.example.com
web root directory on your server.Test the Nginx configuration for errors.
console$ 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
Link the configuration to the
/etc/nginx/sites-enabled
directory to activate the virtual host on your server.console$ sudo ln -s /etc/nginx/sites-available/app.example.com.conf /etc/nginx/sites-enabled/
Create a new web root directory
/var/www/app.example.com
to store your web application files.console$ sudo mkdir -p /var/www/app.example.com
Create a new HTML application file within the directory. For example,
index.html
.console$ sudo nano /var/www/app.example.com/index.html
Add the following HTML contents to the file.
html<html> <head></head> <body> <h1>Greetings from Vultr</h1> </body> </html>
Save and close the file.
The above HTML application displays a
Greetings from Vultr
heading when accessed in a web browser.Restart Nginx to apply the new virtual host configurations on your server.
console$ sudo systemctl restart nginx
Access your domain to verify that Nginx correctly delivers the virtual host web application files on your server. For example, use the Curl utility to test access to your domain.
console$ curl http://app.example.com
Output:
<html> <head></head> <body> <h1>Greetings from Vultr</h1> </body> </html>
Secure the Nginx Web Server
SSL certificates provide encrypted communication between a user's browser and the Nginx web server via HTTPS. Nginx listens for incoming connections on the insecure HTTP port 80
by default. Follow the steps below to generate trusted Let's Encrypt SSL certificates and secure Nginx to enable encrypted HTTPS connections.
Install the Certbot Let's Encrypt client package using Snap.
console$ sudo snap install --classic certbot
View the installed Certbot version on your server.
console$ certbot --version
Output:
certbot 3.3.0
Allow network connections on the HTTP port
80
for Certbot ACME verification.console$ sudo ufw allow 80/tcp
Generate a new SSL certificate for your domain. Replace
app.example.com
with the actual domain in your Nginx virtual host configurations.console$ sudo certbot --nginx -d app.example.com --agree-tos
Set Up Firewall Rules
Uncomplicated Firewall (UFW) is enabled by default on Ubuntu 20.04. Follow the steps below to configure the firewall with rules that allow Nginx to listen for incoming HTTP and HTTPS connections on your server.
Allow connections on the HTTPS port
443
.console$ sudo ufw allow 443/tcp
View the UFW table and verify that the new connection rules are active.
console$ sudo ufw status
Output:
Status: active To Action From -- ------ ---- 22/tcp ALLOW Anywhere 80/tcp ALLOW Anywhere 443/tcp ALLOW Anywhere 22/tcp (v6) ALLOW Anywhere (v6) 80/tcp (v6) ALLOW Anywhere (v6) 443/tcp (v6) ALLOW Anywhere (v6)
Conclusion
You have successfully installed Nginx on your Ubuntu 20.04 server and set it up to serve web applications. Nginx supports various virtual host configurations for secure delivery of applications. It can also integrate with other technologies like MySQL and PHP to power dynamic web applications.
No comments yet.