NewCode CLOUD — 20% off your first month on all cloud VPSClaim

Install Nginx with a Free Let's Encrypt SSL Certificate on Ubuntu

This guide takes you from a clean Ubuntu server to a website served over HTTPS with a free, automatically renewing certificate from Let’s Encrypt. It works the same on Debian 12. Hosting in Hong Kong, Japan, Singapore or the US means no ICP filing is needed — your site can go live as soon as DNS resolves.

Before you start

  • A server with Ubuntu 22.04 or 24.04 and a sudo user (see the initial setup checklist).
  • A domain name you control.
  • Ports 80 and 443 open in your firewall: sudo ufw allow 80,443/tcp.

1. Point your domain at the server

At your DNS provider, create two records:

Type Name Value
A @ your server IP
A www your server IP

Check that they resolve before continuing:

dig +short example.com
dig +short www.example.com

Both should print your server’s IP. DNS can take a few minutes; if you lowered the TTL beforehand it will be quick.

2. Install Nginx

sudo apt update
sudo apt install -y nginx
sudo systemctl enable --now nginx

Open http://YOUR_SERVER_IP in a browser — you should see the “Welcome to nginx” page.

3. Create a site directory

sudo mkdir -p /var/www/example.com/html
sudo chown -R $USER:$USER /var/www/example.com
echo '<h1>It works</h1>' > /var/www/example.com/html/index.html

4. Create a server block

sudo nano /etc/nginx/sites-available/example.com

Paste:

server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;

    root /var/www/example.com/html;
    index index.html index.php;

    location / {
        try_files $uri $uri/ =404;
    }
}

Enable it and reload:

sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

http://example.com should now show “It works”.

5. Install Certbot

The snap package is the version recommended by the Certbot project:

sudo apt install -y snapd
sudo snap install --classic certbot
sudo ln -sf /snap/bin/certbot /usr/bin/certbot

If you prefer not to use snap, sudo apt install -y certbot python3-certbot-nginx also works.

6. Get the certificate

sudo certbot --nginx -d example.com -d www.example.com

Certbot asks for an email address (for expiry warnings), verifies that you control the domain over port 80, installs the certificate and rewrites your server block to serve HTTPS and redirect HTTP to HTTPS.

Visit https://example.com — you should see a padlock.

7. Confirm automatic renewal

Let’s Encrypt certificates last 90 days. Certbot installs a timer that renews them automatically. Test it:

sudo certbot renew --dry-run
systemctl list-timers | grep certbot

Add these inside the server { listen 443 ... } block Certbot created:

    # Compress text responses
    gzip on;
    gzip_types text/css application/javascript application/json image/svg+xml;

    # Cache static files in the browser
    location ~* \.(css|js|png|jpg|jpeg|webp|svg|woff2)$ {
        expires 30d;
        add_header Cache-Control "public";
    }

    # Basic security headers
    add_header X-Content-Type-Options nosniff;
    add_header Referrer-Policy strict-origin-when-cross-origin;

Run sudo nginx -t && sudo systemctl reload nginx after every change.

Hosting several sites

Repeat steps 3–6 with a new directory and a new server block per domain. On a server with many IPs, such as a Server.HK SEO server, you can bind each site to its own IP with listen 203.0.113.25:443 ssl; — see how to add IP addresses on Linux.

Troubleshooting

  • Certbot says the challenge failed: DNS does not point to this server yet, or port 80 is blocked by a firewall.
  • “Too many certificates already issued”: Let’s Encrypt has rate limits. Use --dry-run while testing.
  • Site loads over HTTP but not HTTPS: check that 443/tcp is allowed in UFW and that the server block includes listen 443 ssl.

Prefer a graphical panel? aaPanel and 1Panel do all of this with a few clicks.