Nginx is a free and open-source web server that can deliver both static and dynamic applications or services. It is commonly used as a web server, load balancer, reverse proxy, or HTTP cache, and integrates seamlessly with existing applications or serves content directly via a server's IP or domain.
This guide explains the steps required to set up Nginx on Ubuntu 20.04 and configure a sample web application for best performance.
Prerequisites
Before you begin, ensure that you have the following in place:
- An Ubuntu 20.04 server.
- A new A record for your domain pointing to the server’s IP address.
- SSH access as a non-root user with sudo privileges.
- The server updated with the latest packages.
Matching infrastructure at centron
Ubuntu servers without your own hardware: ccloud³ VMs with full root access from €3.12 per month, billed by the hour and ready in seconds. Rent a cloud server →
Install Nginx on Ubuntu 20.04
The current version of Nginx is available in the default APT repositories of Ubuntu 20.04. Follow these steps to update your server packages and install Nginx.
Update the package index
$ sudo apt updateInstall Nginx
$ sudo apt install nginx -yVerify the installed version
$ nginx -vYour output should look similar to:
nginx version: nginx/1.18.0 (Ubuntu)
Manage the Nginx Service
Nginx runs as a systemd service, allowing you to control its processes. Use the following commands to manage it.
Enable Nginx at boot
$ sudo systemctl enable nginxStart Nginx
$ sudo systemctl start nginxStop Nginx
$ sudo systemctl stop nginxRestart Nginx
$ sudo systemctl restart nginxCheck Nginx status
$ sudo systemctl status nginxIf the output shows Active: active (running), then Nginx is running correctly. If it shows Active: active (failed), stop any processes using port 80 and restart Nginx.
Create a New Nginx Virtual Host
A virtual host configuration allows Nginx to serve files from a specific directory under a given domain. Follow the steps to set up a sample configuration.
Create a new configuration file
$ sudo nano /etc/nginx/sites-available/app.example.com.confAdd the configuration
server {
listen 80;
listen [::]:80;
server_name app.example.com;
root /var/www/app.example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}This configuration listens on port 80 for app.example.com and serves files from /var/www/app.example.com.
Test configuration for errors
$ sudo nginx -tActivate the site
$ sudo ln -s /etc/nginx/sites-available/app.example.com.conf /etc/nginx/sites-enabled/Create the web root
$ sudo mkdir -p /var/www/app.example.comCreate an HTML test page
$ sudo nano /var/www/app.example.com/index.htmlAdd the following HTML to the file:
Greetings from centron
Restart Nginx
$ sudo systemctl restart nginxVerify access
Use curl or a browser to confirm your setup:
$ curl http://app.example.comExpected output:
Greetings from centron
Secure the Nginx Web Server
SSL certificates enable encrypted communication between a user’s browser and the Nginx server over HTTPS. By default, Nginx listens on the unsecured HTTP port 80. Follow these steps to obtain trusted Let's Encrypt SSL certificates and secure Nginx for encrypted HTTPS connections.
Install Certbot
Use Snap to install the Let's Encrypt Certbot client package.
$ sudo snap install --classic certbotCheck Certbot version
$ certbot --versionExample output:
certbot 3.3.0
Allow HTTP traffic for ACME verification
Certbot requires access to port 80 to verify domain ownership.
$ sudo ufw allow 80/tcpGenerate an SSL certificate
Replace app.example.com with your actual domain configured in the Nginx virtual host.
$ sudo certbot --nginx -d app.example.com --agree-tosSet Up Firewall Rules
Ubuntu 20.04 comes with Uncomplicated Firewall (UFW) enabled by default. Use the following steps to configure firewall rules so that Nginx can accept both HTTP and HTTPS traffic.
Allow HTTPS traffic
$ sudo ufw allow 443/tcpVerify firewall rules
$ sudo ufw statusExpected 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 now successfully installed Nginx on Ubuntu 20.04 and configured it to serve web applications. With virtual hosts, you can securely deliver applications, and by integrating Nginx with other technologies such as MySQL and PHP, you can also host dynamic web applications.
Testen Sie Ihr Setup auf ccloud³
Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.