When an HTTPS certificate is nearing expiry, simply overwriting the files and reloading the service can leave three common problems: a certificate that does not match its private key, missing intermediate certificates, or a configuration check that passes while the live service still presents the old certificate. Multiple sites on a dedicated server may also share a listening address, so acceptance checks must include the target hostname rather than just connect to the server’s address.
This guide uses Ubuntu Server 24.04 LTS, Nginx and OpenSSL. It covers an existing HTTPS site, not certificate issuance. Before starting, prepare the new leaf certificate, intermediate certificates and matching private key, and retain a usable copy of the previous version. Run the commands below in a Bash session with administrative privileges.
1. Establish the endpoints and rollback baseline
Record the certificate paths in the site configuration, the actual listening address and port, and the fingerprint of the leaf certificate currently served. If a proxy or load balancer is involved, test the origin and public endpoint separately; otherwise, you may mistake a certificate cached at the entry point for the one loaded by the origin.
Capture the actual parameters interactively. Do not assume the default listening port or use server login credentials for site testing:
read -r -p 'TLS hostname: ' TLS_NAME
read -r -p 'Origin IPv4: ' TLS_IP
read -r -p 'TLS port: ' TLS_PORT
export TLS_NAME TLS_IP TLS_PORT
umask 077
WORK=$(mktemp -d)
export WORK
The change record should also state whether the old certificate is still valid. If it has already expired, switching back to the old files can restore the configuration but not a trusted connection. Being able to roll back does not necessarily mean being able to restore service.
Never include private keys in ticket text, chat logs or archived command output. Certificates can be public; private keys must have restricted access. Keep rollback files under access control rather than copying them into a shared directory as attachments.
2. Validate the new certificate and private key offline
Place the files to be deployed in a restricted directory, named leaf.pem, intermediates.pem and privkey.pem. Run the following checks from that directory. Use the intermediate certificates supplied with the issued certificate, not a bundle assembled from unknown sources.
openssl x509 -in leaf.pem -noout \
-subject -issuer -dates -ext subjectAltName
openssl x509 -in leaf.pem -noout -checkhost "$TLS_NAME"
openssl x509 -in leaf.pem -noout -checkend 604800
openssl pkey -in privkey.pem -check -noout
openssl verify -purpose sslserver \
-CAfile /etc/ssl/certs/ca-certificates.crt \
-untrusted intermediates.pem leaf.pem
Seven days is only the minimum remaining-validity threshold used for this operation, not a renewal interval. checkend does not replace full validation: also check the not-before date, hostname match and system clock. For a private PKI, use a trust-root file distributed through an authorized channel. Do not suppress errors by disabling verification.
Compare public keys without assuming a key algorithm
Do not compare only RSA moduli; the certificate may use a different key algorithm. Export both public keys in the same DER format, then compare them byte for byte:
openssl x509 -in leaf.pem -pubkey -noout \
> "$WORK/cert-public.pem"
openssl pkey -pubin -in "$WORK/cert-public.pem" \
-outform DER > "$WORK/cert-public.der"
openssl pkey -in privkey.pem -pubout \
-outform DER > "$WORK/key-public.der"
cmp "$WORK/cert-public.der" "$WORK/key-public.der"
Every command must succeed. The keys match only if cmp produces no output and exits with status zero. Stop if any step fails; do not continue with deployment. For an encrypted private key, also confirm that the service’s existing unlocking mechanism works. Do not temporarily remove the passphrase and then forget to restore the file’s protection.
3. Switch files using complete version directories
Build the server’s full chain with the leaf certificate first, followed by the intermediate certificates in chain order. The root certificate is normally omitted. Whether a client trusts the certificate depends on its own trust store, not on whether the server sends the root certificate.
cat leaf.pem intermediates.pem > fullchain.pem
Store versions under /etc/nginx/tls/site/versions/, with a current symlink pointing to the active version. When introducing this layout for the first time, move the old files into a version directory and verify that setup first. Do not combine the directory restructuring and the new certificate deployment into a single change whose effects cannot be distinguished.
Change the two paths in the site’s existing HTTPS configuration to:
ssl_certificate /etc/nginx/tls/site/current/fullchain.pem;
ssl_certificate_key /etc/nginx/tls/site/current/privkey.pem;
Create versions/candidate and install the new full chain and private key. Restrict private-key read access to the account needed to load it for the service. The following example is suitable for a conventional deployment in which the root master process loads the certificates:
BASE=/etc/nginx/tls/site
install -d -m 700 "$BASE/versions/candidate"
install -m 644 fullchain.pem \
"$BASE/versions/candidate/fullchain.pem"
install -m 600 privkey.pem \
"$BASE/versions/candidate/privkey.pem"
OLD=$(readlink -f "$BASE/current")
test -d "$OLD"
ln -s "$BASE/versions/candidate" "$BASE/current.next"
mv -Tf "$BASE/current.next" "$BASE/current"
nginx -t
Check the exit status of each command, especially the check of the old path. The names candidate and current.next must be reserved exclusively for this change. If either already exists, investigate rather than overwrite it. Keeping the temporary link and destination in the same directory allows a rename-based switch, avoiding the mix of old and new files that can result from overwriting them individually.
4. Check the actual TLS handshake after reloading
If nginx -t fails, restore the old link immediately and do not reload. If it succeeds, run systemctl reload nginx, then check the service status and the logs for that reload. A running service only confirms that a process exists; it does not prove that the target site is serving the new certificate.
openssl s_client \
-connect "${TLS_IP}:${TLS_PORT}" \
-servername "$TLS_NAME" \
-verify_hostname "$TLS_NAME" \
-verify_return_error \
-CAfile /etc/ssl/certs/ca-certificates.crt \
-showcerts </dev/null > "$WORK/handshake.txt" 2>&1
First confirm that the command exited with status zero, then extract the first certificate:
openssl x509 -in "$WORK/handshake.txt" \
-out "$WORK/served.pem"
openssl x509 -in "$WORK/served.pem" \
-noout -serial -dates -fingerprint -sha256
openssl x509 -in "$BASE/current/fullchain.pem" \
-noout -serial -dates -fingerprint -sha256
The two leaf-certificate fingerprints should match, and the handshake log should show successful verification. Next, use an independent client to check the public hostname and make real application requests. If the origin serves the correct certificate but the public endpoint still serves the old one, trace the actual entry path layer by layer to identify where TLS terminates. Do not repeatedly reload the origin in the hope that it fixes the problem.
5. Keep rollback and acceptance evidence together
If the handshake fails, the hostname does not match or the application behaves abnormally, restore the link using the saved OLD value, then test the configuration and reload:
ln -s "$OLD" "$BASE/current.rollback"
mv -Tf "$BASE/current.rollback" "$BASE/current"
nginx -t && systemctl reload nginx
After rolling back, repeat the same SNI handshake and application requests. Confirm that the old version is being served and that verification succeeds. Do not declare service restored merely because the reload command succeeded.
Retain the old and new certificate fingerprints, validity dates, configuration test results, reload logs and endpoint test results, noting whether each test targeted the origin or the public endpoint. Delete the temporary directory once the checks are complete. Retain the old version according to the change procedure, and keep private-key access restricted at all times. A certificate rotation is complete when real clients receive the expected certificate, the chain validates and application requests work—not merely when the files have been copied.
Frequently asked questions
Does a successful Nginx configuration test mean the new certificate is live?
No. It validates the configuration before loading. After reloading, connect with the actual hostname as SNI, check certificate verification and compare the served leaf fingerprint with the target file.
Should the server send the root certificate too?
Usually not. Serve the leaf certificate first, followed by intermediate certificates in chain order. Root trust comes from the client trust store; sending a root does not establish trust.
Dedicated EPYC 7543P Server from $460.4/month
Choose from two ECC memory options. Confirm the configuration and amount due on the configuration page.