Taking an nginx site live: HTTP first, issue the certificate, switch to HTTPS
The right order for a new site: DNS and ports, nginx version, a stage-1 HTTP-only config, certbot certonly --webroot, stage-2 HTTPS, renewal checks, rolling back bundles and Cloudflare notes.
DNS and ports
Point the A / AAAA records for the domain (and www) at the server, and open ports 80 and 443 in both the OS firewall and the cloud security group. Issuance uses port 80, so it must be reachable.
dig +short example.com A dig +short www.example.com A sudo ufw allow 'Nginx Full' # Ubuntu/Debian ufw sudo firewall-cmd --permanent --add-service={http,https} && sudo firewall-cmd --reload # RHEL familyInstall nginx and note the version
The nginx.org repo is current; distro packages can be old (Debian 12 ships 1.22, Ubuntu 24.04 ships 1.24). Below 1.25.1 pick “< 1.25.1” in the generator, otherwise http2 on fails as an unknown directive.
curl -fsSL nginx.cc/install | sudo bash # nginx.org repo, supports --dry-run nginx -v
Know where configs live
nginx.org packages and Docker read /etc/nginx/conf.d/*.conf; Debian/Ubuntu apt packages read conf.d and sites-enabled. Everything generated here goes into conf.d. The apt package's sites-enabled/default claims default_server on port 80; remove it before using the catch-all HTTPS redirect.
sudo nginx -T 2>/dev/null | grep -E '^# configuration file' sudo rm -f /etc/nginx/sites-enabled/default # Debian/Ubuntu apt packages only
Stage 1: deploy the HTTP-only config
Before the certificate exists, a config that references ssl_certificate fails nginx -t. For HTTPS scenarios the generator also gives a stage-1 config: port 80 only, with a webroot for /.well-known/acme-challenge/. Deploy it and confirm a test file is reachable from outside.
sudo mkdir -p /var/www/_letsencrypt/.well-known/acme-challenge echo ok | sudo tee /var/www/_letsencrypt/.well-known/acme-challenge/ping sudo nginx -t && sudo systemctl reload nginx curl -s http://example.com/.well-known/acme-challenge/ping # should print ok
Issue the certificate
Prefer certbot certonly --webroot: it only fetches the certificate, never edits nginx files, and renewals stay hands-off. --cert-name pins the directory name the generated config expects. certonly --nginx also works. Wildcards need DNS validation.
sudo apt install certbot # or sudo snap install --classic certbot sudo certbot certonly --webroot -w /var/www/_letsencrypt \ -d example.com -d www.example.com --cert-name example.com \ --email you@example.com --agree-tos --no-eff-email \ --deploy-hook "systemctl reload nginx"
Avoid plain certbot --nginx (without certonly) on generated files: it injects its own ssl options that clash with the TLS profile.
Stage 2: switch to the full HTTPS config
Overwrite the same file with the full config, test, reload. The port-80 server keeps the acme-challenge location, so renewals keep working.
sudo nginx -t && sudo systemctl reload nginx curl -sI https://example.com | head -20 curl -sI http://www.example.com | grep -i location
Check automatic renewal
certbot installs a systemd timer or cron job. --dry-run runs the full flow without replacing the certificate.
sudo certbot renew --dry-run systemctl list-timers | grep -i certbot
Installing and rolling back a split bundle
Split output puts shared snippets in /etc/nginx/nginxcc/ and sites in conf.d. Back up the whole directory first and restore it if nginx -t fails.
sudo cp -a /etc/nginx /etc/nginx.bak.$(date +%Y%m%d-%H%M%S) curl -fsSL 'https://nginx.cc/api/gen/bundle.tar.gz?…' | sudo tar -xzf - -C /etc/nginx sudo nginx -t && sudo systemctl reload nginx # rollback: sudo rm -rf /etc/nginx && sudo mv /etc/nginx.bak.<timestamp> /etc/nginx
Behind Cloudflare
Use SSL/TLS mode Full (strict), never Flexible, or the origin's HTTP→HTTPS redirect loops. Tick “Behind Cloudflare” in the generator so logs and rate limits see the real client IP.