Applications Azure

NGINX with SSL (Certbot) on Ubuntu 24.04 on Azure User Guide

| Product: NGINX SSL Certbot on Ubuntu 24.04 LTS on Azure

Overview

This guide covers deploying and using NGINX with SSL (Certbot) on Ubuntu 24.04 LTS on Microsoft Azure, from cloudimg's Azure Marketplace image. The image pairs the NGINX web server with Certbot, the Let's Encrypt client, and Certbot's NGINX plugin, so that once your domain points at the VM, one Certbot command obtains a browser-trusted certificate, installs it into your NGINX server block and redirects HTTP to HTTPS. Certbot's renewal timer is already enabled.

What's included in this VM image:

  • NGINX 1.30 from the official nginx.org APT repository (nginx.service, enabled at boot)
  • Certbot 2.9 with the NGINX plugin (python3-certbot-nginx) from Ubuntu 24.04
  • certbot.timer enabled: it runs certbot renew twice a day
  • A default server on port 80 that serves /var/www/html and the ACME HTTP-01 challenge path /.well-known/acme-challenge/
  • A default server on port 443 with a self-signed placeholder certificate, so HTTPS answers before you have issued your own
  • A Mozilla Intermediate TLS snippet at /etc/nginx/snippets/ssl-params.conf (TLS 1.2 and 1.3) with HSTS, X-Content-Type-Options and X-Frame-Options headers
  • Ubuntu 24.04 LTS with unattended security upgrades enabled
  • Azure Linux Agent (waagent) and cloud-init for Azure provisioning
  • cloudimg's support team on hand 24 hours a day by email (support@cloudimg.co.uk), with a guaranteed response within 24 hours

Platform: Microsoft Azure (Gen2 Hyper-V, UEFI) Login user: the administrator username you choose when you deploy (azureuser in the examples below), with sudo NGINX configuration: /etc/nginx/nginx.conf, server blocks in /etc/nginx/conf.d/ Web root: /var/www/html Certbot configuration and certificates: /etc/letsencrypt/

Prerequisites

Before deploying this image, make sure you have:

  1. An active Microsoft Azure subscription
  2. Access to the Azure Portal or the Azure CLI
  3. An SSH key pair for Linux VM access
  4. A domain name you control, where you can create an A record that points at the VM's public IP address
  5. A network security group that allows TCP 22 (SSH) from your own network, and TCP 80 and 443 from the Internet. Let's Encrypt checks your domain over port 80 (the HTTP-01 challenge), so port 80 must be reachable from the Internet when you issue and renew a certificate.

Recommended VM size: Standard_B2s (2 vCPU, 4 GB RAM) for a test or a small site; Standard_D2s_v3 (2 vCPU, 8 GB RAM) or larger for production.

Step 1: Deploy the Virtual Machine

Option A: Azure Portal

  1. Find NGINX SSL Certbot Ubuntu 24.04 by cloudimg in the Azure Marketplace and choose Get it now, then Create
  2. On the Basics tab, choose your subscription, resource group, region and VM size
  3. Under Administrator account, choose SSH public key and enter your username and key
  4. Under Inbound port rules, allow SSH (22), HTTP (80) and HTTPS (443). Limit SSH to your own network once the VM is created
  5. Choose Review + create, then Create

Option B: Azure CLI

Accept the image's Marketplace terms once per subscription:

az vm image terms accept --urn cloudimg1647283583153:nginx-ssl-certbot-ubuntu-24-04:standard:latest

Create the VM:

az vm create \
  --resource-group myResourceGroup \
  --name my-nginx-certbot-vm \
  --image cloudimg1647283583153:nginx-ssl-certbot-ubuntu-24-04:standard:latest \
  --size Standard_B2s \
  --admin-username azureuser \
  --ssh-key-values ~/.ssh/id_ed25519.pub \
  --public-ip-sku Standard

Open HTTP and HTTPS in the VM's network security group (az vm create opens SSH only):

az vm open-port --resource-group myResourceGroup --name my-nginx-certbot-vm --port 80,443 --priority 1010

Step 2: Connect via SSH

Find the VM's public IP address:

az vm show --resource-group myResourceGroup --name my-nginx-certbot-vm --show-details --query publicIps -o tsv

Connect as the administrator user you chose:

ssh azureuser@<public-ip-address>

Wait for first-boot provisioning to finish before you change anything:

sudo cloud-init status --wait
status: done

Step 3: Check NGINX and Certbot

NGINX and Certbot's renewal timer are both active from the first boot:

systemctl is-active nginx certbot.timer
active
active

Check the versions:

nginx -v
certbot --version
nginx version: nginx/1.30.0
certbot 2.9.0

NGINX listens on ports 80 and 443, on IPv4 and IPv6:

sudo ss -Htlnp '( sport = :80 or sport = :443 )' | awk '{print $4}'
0.0.0.0:443
0.0.0.0:80
[::]:443
[::]:80

Certbot's NGINX plugin is installed. It both answers the Let's Encrypt challenge and installs the certificate:

sudo certbot plugins 2>/dev/null | grep -A1 '^\* nginx'
* nginx
Description: Nginx Web Server plugin

The configuration is valid:

sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Until you issue your own certificate, HTTPS answers with the self-signed placeholder certificate, over HTTP/2. Browsers warn about it, so use -k to test it with curl:

curl -s -o /dev/null -w 'HTTP %{http_code}\n' http://localhost/
curl -sk -o /dev/null -w 'HTTP %{http_code}, HTTP/%{http_version}\n' https://localhost/
HTTP 200
HTTP 200, HTTP/2

Step 4: Point Your Domain at the VM

At your DNS provider, create an A record for each name you want on the certificate (for example example.com and www.example.com), pointing at the VM's public IP address. From your workstation, check that it resolves before you continue:

dig +short example.com
203.0.113.10

The answer must be the VM's public IP address. Let's Encrypt cannot issue a certificate for a name that does not resolve to the VM yet.

Step 5: Create a Server Block for Your Domain

The shipped default servers answer any name (server_name _;). Certbot looks for the server block whose server_name matches your domain, and adds the HTTP to HTTPS redirect to that block. If there is no matching block, Certbot still installs the certificate, but it reports No matching insecure server blocks listening on port 80 found. and adds no redirect. So give your site its own server block first.

Create /etc/nginx/conf.d/example.com.conf, replacing the names with your own:

sudo tee /etc/nginx/conf.d/example.com.conf > /dev/null <<'EOF'
server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    root /var/www/html;
    index index.html;

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

Check the configuration and reload NGINX:

sudo nginx -t && sudo systemctl reload nginx

Step 6: Issue a Let's Encrypt Certificate

Run Certbot with the NGINX plugin, naming every domain in your server block. Use your own email address: Let's Encrypt sends expiry warnings and account notices to it.

sudo certbot --nginx -d example.com -d www.example.com \
  -m you@example.com --agree-tos --redirect --non-interactive

Certbot answers the HTTP-01 challenge through NGINX on port 80, obtains the certificate, and then edits your server block. After it finishes, /etc/nginx/conf.d/example.com.conf has two server blocks: your site, now on port 443 with the certificate, and a port 80 block that redirects to HTTPS. The lines Certbot adds are marked # managed by Certbot:

server {
    server_name example.com www.example.com;
    root /var/www/html;
    index index.html;

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

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    return 404; # managed by Certbot

}

The option is --nginx, with two dashes. sudo certbot nginx without them fails with certbot: error: unrecognized arguments: nginx.

Step 7: Check HTTPS with Your Certificate

List the certificates Certbot manages, with their domains, expiry date and file paths:

sudo certbot certificates

From your workstation, plain HTTP now redirects, and HTTPS answers with your certificate:

curl -sI http://example.com/ | head -1
curl -sI https://example.com/ | head -1
HTTP/1.1 301 Moved Permanently
HTTP/1.1 200 OK

Step 8: Turn On HTTP/2 and the Security Headers for Your Site

Certbot's block uses Certbot's own TLS settings (/etc/letsencrypt/options-ssl-nginx.conf, TLS 1.2 and 1.3). HTTP/2 and the HSTS, X-Content-Type-Options and X-Frame-Options headers of the shipped default server are not carried over to it, so add them under the server_name line of your HTTPS server block (the first block in the file):

    http2 on;
    add_header Strict-Transport-Security "max-age=63072000; includeSubDomains" always;
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-Frame-Options "SAMEORIGIN" always;

Do not include /etc/nginx/snippets/ssl-params.conf in the same block: it repeats directives that Certbot's file already sets, and nginx -t refuses a duplicate. Check and reload, then test again from your workstation:

sudo nginx -t && sudo systemctl reload nginx
curl -sI https://example.com/ | grep -iE '^(HTTP|strict-transport|x-content|x-frame)'
HTTP/2 200
strict-transport-security: max-age=63072000; includeSubDomains
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN

Only send HSTS once you are sure the whole site, and every subdomain, will stay on HTTPS: browsers remember it for the max-age (two years here).

Step 9: Automatic Renewal

Let's Encrypt certificates are valid for 90 days. certbot.timer runs certbot renew twice a day, and Certbot renews any certificate that is within 30 days of expiry, then reloads NGINX. Check when the timer runs next:

systemctl list-timers certbot.timer --no-pager

Rehearse a renewal against the Let's Encrypt staging service (port 80 must still be open to the Internet):

sudo certbot renew --dry-run

Step 10: Publish Your Site

Your site's files live in the server block's root, /var/www/html in the example above. Copy your site there (or point root at another directory), then reload NGINX:

sudo cp -r ./my-site/* /var/www/html/
sudo nginx -t && sudo systemctl reload nginx

To put NGINX in front of an application instead, replace the location / block in your server block with a proxy to it. For an application listening on 127.0.0.1:8080:

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

Server Components

Component Version Source
NGINX 1.30.0 nginx.org APT repository (http://nginx.org/packages/ubuntu noble nginx)
Certbot 2.9.0 Ubuntu 24.04 (certbot)
Certbot NGINX plugin 2.9.0 Ubuntu 24.04 (python3-certbot-nginx)
OpenSSL 3.0.13 Ubuntu 24.04
Operating system Ubuntu 24.04 LTS Canonical

Filesystem Layout

Path What it is
/ The OS disk (about 29 GB usable)
/boot, /boot/efi Boot and EFI partitions
/mnt The Azure temporary disk: its contents are lost when the VM is deallocated, so never keep data there
/etc/nginx/nginx.conf NGINX main configuration; it includes /etc/nginx/conf.d/*.conf
/etc/nginx/conf.d/default.conf The shipped default servers on ports 80 and 443
/etc/nginx/snippets/ssl-params.conf The Mozilla Intermediate TLS snippet and security headers
/etc/ssl/cloudimg/ The self-signed placeholder certificate used by the default 443 server
/var/www/html The default web root, and the ACME challenge root
/etc/letsencrypt/ Certbot's accounts, certificates (live/<domain>/) and renewal configuration
/var/log/nginx/ NGINX access.log and error.log
/var/log/letsencrypt/ Certbot's log

Managing the Services

sudo systemctl status nginx --no-pager
sudo systemctl reload nginx        # apply configuration changes without dropping connections
sudo systemctl restart nginx
sudo nginx -t                      # always check the configuration before a reload
systemctl status certbot.timer --no-pager

Log Files

  • /var/log/nginx/access.log and /var/log/nginx/error.log: NGINX
  • /var/log/letsencrypt/letsencrypt.log: every Certbot run, including the timer's renewals
  • journalctl -u nginx and journalctl -u certbot: the services' journal

On Startup

At every boot, systemd starts nginx.service and certbot.timer. Ubuntu's apt-daily and unattended-upgrades timers install security updates automatically. To turn automatic upgrades off, run sudo dpkg-reconfigure unattended-upgrades and answer No.

Troubleshooting

Certbot cannot obtain a certificate (challenge failed, timeout or connection refused). Check that your domain's A record resolves to the VM's public IP (dig +short example.com), and that the network security group allows TCP 80 from the Internet. Let's Encrypt validates over port 80 even when you only want HTTPS.

Certbot says No matching insecure server blocks listening on port 80 found. The certificate was installed but no redirect was added, because no server block has your domain in server_name. Create one (Step 5), then run the Step 6 command again: Certbot reuses the certificate it already has and adds the redirect.

certbot: error: unrecognized arguments: nginx. Use --nginx, with two dashes.

nginx -t fails after an edit. It names the file and line. Fix it before you reload: a reload with a broken configuration leaves the old configuration running, and a restart stops NGINX.

The browser warns about the certificate. The VM is still serving the self-signed placeholder certificate. Issue your own (Steps 4 to 6) and browse to the domain name, not the IP address.

Renewal fails. Run sudo certbot renew --dry-run and read /var/log/letsencrypt/letsencrypt.log. The usual cause is port 80 closed, or DNS no longer pointing at the VM.

Security Recommendations

  • Issue your own certificate (Steps 4 to 6) before you serve real traffic. The default 443 server's self-signed certificate is a placeholder only.
  • Allow SSH (22) only from your own network. Allow 80 and 443 from the networks your visitors use; port 80 must stay reachable from the Internet for Let's Encrypt validation and renewal.
  • Hide the NGINX version from response headers: add server_tokens off; in a file such as /etc/nginx/conf.d/00-server-tokens.conf, then sudo nginx -t && sudo systemctl reload nginx.
  • Keep unattended security upgrades on, and reboot when /var/run/reboot-required exists.
  • Use an email address you read with -m, so you see Let's Encrypt's expiry warnings if renewal ever fails.

Support

cloudimg's support team is on hand 24 hours a day by email (support@cloudimg.co.uk), with a guaranteed response within 24 hours. Contact support@cloudimg.co.uk.