Nginx is a high-performance web server and reverse proxy. This guide installs Nginx, configures a virtual host for a domain, and secures it with a free Let's Encrypt SSL certificate.
Prerequisites
- Ubuntu 22.04 server with a public IP
- A domain name pointing to your server's IP (A record)
- Ports 80 and 443 open in your firewall
Step 1 — Install Nginx
$apt update$apt install -y nginx$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 Thu 2026-04-24 10:00:00 UTC; 5s agoStep 2 — Configure UFW for Nginx
$ufw allow 'Nginx Full'$ufw statusStatus: active
To Action From
-- ------ ----
OpenSSH ALLOW IN Anywhere
Nginx Full ALLOW IN AnywhereStep 3 — Create a Virtual Host
$nano /etc/nginx/sites-available/example.comserver {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
access_log /var/log/nginx/example.com.access.log;
error_log /var/log/nginx/example.com.error.log;
}Create the web root and a test page:
$mkdir -p /var/www/example.com/html$echo "<h1>Welcome to example.com</h1>" > /var/www/example.com/html/index.html$chown -R www-data:www-data /var/www/example.comEnable the site and test the configuration:
$ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/$nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful$systemctl reload nginxStep 4 — Install Certbot and Obtain SSL Certificate
$apt install -y certbot python3-certbot-nginx$certbot --nginx -d example.com -d www.example.comFollow the interactive prompts. Certbot will automatically update your Nginx configuration to redirect HTTP to HTTPS.
Step 5 — Verify Auto-Renewal
$certbot renew --dry-runCongratulations, all renewals succeeded:
/etc/letsencrypt/live/example.com/fullchain.pem (success)Step 6 — Configure Nginx as a Reverse Proxy (Optional)
To proxy requests to a backend application running on port 3000:
server {
listen 443 ssl;
server_name app.example.com;
ssl_certificate /etc/letsencrypt/live/app.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/app.example.com/privkey.pem;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache_bypass $http_upgrade;
}
}[!TIP] Always run
nginx -tbefore reloading Nginx to catch configuration syntax errors.
