How to install Nginx on Ubuntu Server with HTTPS
Install Nginx on Ubuntu Server or Debian and add a free SSL certificate in about 15 minutes. At the end, your site runs on HTTPS with a certificate that renews by itself.
This guide starts where Secure Ubuntu Server and Debian in 5 steps ends: you have your own user with sudo rights, and UFW is on.
Before you start
- A server with Ubuntu Server or Debian
- A user with sudo rights, and UFW turned on
- A domain name, and access to its DNS settings at your registrar
- About 15 minutes
In this guide, the domain is example.com. Replace it with your own domain everywhere.
Step 1: Install Nginx
Nginx is a fast and light web server. Install it, and check that it runs:
~ $ sudo apt update && sudo apt install nginx -y
~ $ systemctl status nginxYou see active (running). Press q to leave the status screen.
Step 2: Open the firewall
Allow web traffic through UFW. The Nginx Full profile opens port 80 (HTTP) and port 443 (HTTPS):
~ $ sudo ufw allow 'Nginx Full'
~ $ sudo ufw statusThe status now shows Nginx Full ALLOW Anywhere, next to OpenSSH.
Step 3: Point your domain to your server
Log in at your domain registrar and add two A records:
| Type | Name | Value |
|---|---|---|
| A | example.com (or @) | your-server-ip |
| A | www | your-server-ip |
Check it from your server or your own computer:
~ $ dig +short example.comYou see the IP address of your server. DNS changes can take a while, from a few minutes up to a few hours. Wait until this works before step 5, because Let’s Encrypt checks your domain.
Step 4: Add your site
Make a folder for your site, with a simple test page:
~ $ sudo mkdir -p /var/www/example.com
~ $ echo '<h1>It works</h1>' | sudo tee /var/www/example.com/index.htmlMake a config file for your site:
~ $ sudo nano /etc/nginx/sites-available/example.comPaste this server block:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
root /var/www/example.com;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}Save with Ctrl+O, Enter, and close with Ctrl+X. Then link the config, test it, and reload Nginx:
~ $ cd /etc/nginx/sites-enabled
~ $ sudo ln -s ../sites-available/example.com .
~ $ sudo nginx -t && sudo systemctl reload nginxnginx -t shows syntax is ok and test is successful. Open http://example.com in your browser: you see “It works”.
Step 5: Turn on HTTPS with Let’s Encrypt
Let’s Encrypt gives free SSL certificates. Certbot gets the certificate and changes your Nginx config for you:
~ $ sudo apt install certbot python3-certbot-nginx
~ $ sudo certbot --nginx -d example.com -d www.example.comCertbot asks for your email address, for renewal notices, and asks you to accept the terms. When it is done, open https://example.com. You see the lock icon in your browser.
Certbot also sends all HTTP traffic to HTTPS. Visitors who type http:// land on the secure version.
Bonus: Test auto-renewal
Let’s Encrypt certificates last 90 days. Certbot renews them for you, with a timer that runs twice a day. Test it once:
~ $ sudo certbot renew --dry-runIf the dry run succeeds, renewal works. You can check the timer with systemctl list-timers | grep certbot.
Troubleshooting
| Problem | Likely cause | Fix |
|---|---|---|
| Browser shows the default “Welcome to nginx” page | Your site config is not linked, or the default site wins | Check the link in /etc/nginx/sites-enabled/. Remove the default with sudo rm /etc/nginx/sites-enabled/default and reload Nginx |
nginx -t gives an error | Typo in the server block, often a missing ; | Read the line number in the error, fix it, and test again |
| Certbot fails with a DNS or challenge error | DNS still points elsewhere, or port 80 is closed | Check dig +short example.com and sudo ufw status |
| Site loads, but HTTPS shows a warning | The certificate covers only one name | Run Certbot again with both -d example.com -d www.example.com |
| Locked out of SSH | Firewall or SSH setting | Use the KVM console in your client area |
FAQ
Yes. All commands in this guide work on current versions of Ubuntu Server and Debian.
Yes. Let’s Encrypt is a non-profit certificate authority. Browsers trust its certificates in the same way as paid ones.
Certbot renews it for you before the 90 days are over. Test it once with sudo certbot renew --dry-run.
Yes. Repeat step 3 to 5 for each domain, with its own folder and server block.
Both work well. Nginx uses less memory and handles many visitors at once, which makes it a common choice for new servers.
Quick checklist
- Nginx installed and running
- UFW allows Nginx Full
- A records for example.com and www point to your server
- Server block linked,
nginx -tpasses - HTTPS works, with the lock icon in your browser
certbot renew --dry-runsucceeds
Need a server for your site? Dedicated servers from €49.99 a month, with root access and a KVM console.
