Securing VPS/Servers SSL Certificates

Install a Commercial SSL Certificate on Apache on Ubuntu 24.04

From private key and CSR to a working HTTPS virtual host: install a CA-issued certificate and its intermediate chain in Apache on Ubuntu 24.04.

This guide walks you through installing a certificate issued by a commercial certificate authority (CA) on Apache on Ubuntu 24.04 LTS: you create a private key and a certificate signing request (CSR), order the certificate, combine it with the CA's intermediate certificates and configure the HTTPS virtual host. The steps are the same on Ubuntu 22.04.

Older tutorials on this topic targeted Ubuntu 10.10, which has been unsupported for more than a decade. Besides the release, two things have changed that matter here: certificates must list their host names in the subjectAltName extension, and Apache 2.4 reads the intermediate chain from the same file as the server certificate, so the old SSLCertificateChainFile directive is no longer needed.

If you only need a free, automatically renewed certificate, use the Let's Encrypt guide on this site instead. A purchased certificate makes sense when you need organisation or extended validation, a wildcard without DNS automation, or a certificate you can also install on appliances and other servers.

Requirements

  • Ubuntu 24.04 or 22.04 with Apache installed (sudo apt install apache2) and a user with sudo rights.
  • A working HTTP virtual host for your domain. The examples use example.com and www.example.com.
  • Access to the domain's DNS, an administrative mailbox or the web root, because the CA will ask you to prove control of the domain.

Step 1: Create the private key and CSR

Generate a 2048-bit RSA key and the signing request in one command. The key goes into /etc/ssl/private, which is only accessible to root and the ssl-cert group:

sudo openssl req -new -newkey rsa:2048 -noenc \
  -keyout /etc/ssl/private/example.com.key \
  -out /root/example.com.csr \
  -subj "/C=US/ST=New York/L=New York/O=Example Inc/CN=example.com" \
  -addext "subjectAltName = DNS:example.com,DNS:www.example.com"

Replace the subject fields with your own details. For a domain-validated certificate only the names matter; for organisation or extended validation the organisation fields must match your registered company data exactly. -noenc stores the key without a passphrase so Apache can restart unattended.

sudo chmod 600 /etc/ssl/private/example.com.key

Check the request before you send it:

sudo openssl req -in /root/example.com.csr -noout -text -verify

The output must show self-signature verify OK (older OpenSSL releases print verify OK), the correct subject and both names under "Subject Alternative Name". Print the CSR to copy it into the CA's order form:

sudo cat /root/example.com.csr

Paste everything including the -----BEGIN CERTIFICATE REQUEST----- and -----END CERTIFICATE REQUEST----- lines. The CSR is not secret. The key is: it never leaves the server and you never send it to the CA.

Step 2: Complete validation and download the files

The CA verifies that you control the domain, usually by one of three methods: a DNS record, a file served from the website, or an email to an administrative address. After validation you receive:

  • your server (leaf) certificate, for example example_com.crt;
  • one or more intermediate certificates, often delivered as a "CA bundle" or "chain" file.

If the CA offers downloads per server type, choose "Apache" or "PEM". Upload the files to the server, for example with scp into your home directory.

Step 3: Build the certificate chain file

Apache expects the server certificate first, followed by the intermediates in order from leaf to root. The root certificate itself is not needed, because clients already have it. Assuming the uploaded files are example_com.crt and ca-bundle.crt:

cat example_com.crt ca-bundle.crt | sudo tee /etc/ssl/certs/example.com.fullchain.crt > /dev/null

Open the result once and make sure every -----END CERTIFICATE----- line is followed by a line break before the next -----BEGIN CERTIFICATE-----. Files without a trailing newline are a common cause of "bad end line" errors.

Now run two checks. First, the certificate must belong to your key. Both commands must print the same hash:

openssl x509 -in example_com.crt -noout -pubkey | openssl sha256
sudo openssl pkey -in /etc/ssl/private/example.com.key -pubout | openssl sha256

Second, the intermediates must link your certificate to a root that Ubuntu trusts:

openssl verify -untrusted ca-bundle.crt example_com.crt

The expected output is example_com.crt: OK.

Step 4: Configure the Apache virtual host

Enable the SSL module:

sudo a2enmod ssl

Create the HTTPS site:

sudo nano /etc/apache2/sites-available/example.com-ssl.conf
<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html

    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/example.com.fullchain.crt
    SSLCertificateKeyFile /etc/ssl/private/example.com.key
    SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1

    ErrorLog ${APACHE_LOG_DIR}/example.com-ssl-error.log
    CustomLog ${APACHE_LOG_DIR}/example.com-ssl-access.log combined
</VirtualHost>

SSLCertificateFile points to the combined file from step 3. Loading intermediates from this file is supported since Apache 2.4.8 and makes SSLCertificateChainFile obsolete; if a CA's instructions still mention that directive, you can ignore it. The SSLProtocol line limits connections to TLS 1.2 and 1.3.

To redirect visitors from HTTP, add this line inside your existing port 80 virtual host:

Redirect permanent / https://example.com/

Enable the site, test the configuration and restart Apache so the newly enabled module is loaded:

sudo a2ensite example.com-ssl
sudo apache2ctl configtest
sudo systemctl restart apache2

If UFW is active, allow HTTPS:

sudo ufw allow 'Apache Full'

Step 5: Verify the installation

Request the site without any option that disables verification. A correct installation returns headers and no certificate error:

curl -I https://example.com/

Inspect the chain the server sends:

openssl s_client -connect example.com:443 -servername example.com </dev/null

Under "Certificate chain" you should see your certificate at position 0 and the intermediates after it, and near the end Verify return code: 0 (ok). If a desktop browser accepts the site but curl, mobile devices or API clients fail, the intermediate certificates are almost always missing from the file.

Renewal and housekeeping

  • Expiry: commercial certificates are not renewed automatically unless your CA offers an ACME or API integration. Check the date with sudo openssl x509 -in /etc/ssl/certs/example.com.fullchain.crt -noout -enddate and set a reminder well ahead of it. Validity periods are set by the CA and industry rules keep shortening them, so check what your CA issues.
  • Renewing: order the renewal with a new CSR (repeat step 1; a fresh key is good practice), rebuild the chain file, then run sudo systemctl reload apache2.
  • Backups: keep a copy of the key in a safe place. If it is lost, the certificate is useless and must be reissued; if it is exposed, ask the CA to revoke the certificate.
  • Clean up: delete the uploaded copies in your home directory and /root/example.com.csr once the site works.

Troubleshooting

  • Apache does not start: run sudo apache2ctl configtest and read /var/log/apache2/error.log. A key that does not match the certificate is reported there as a key values mismatch; repeat the hash comparison from step 3.
  • Browser shows the wrong certificate: another virtual host is answering. List them with sudo apache2ctl -S and check ServerName and ServerAlias.
  • Name mismatch warning: the name in the address bar is not in the certificate's subjectAltName list. The certificate has to be reissued with the missing name.

Frequently asked questions

Can I use an ECDSA key instead of RSA?

Yes, if your CA accepts it, and most do. Create the key with sudo openssl ecparam -genkey -name prime256v1 -noout -out /etc/ssl/private/example.com.key, then generate the CSR from it with openssl req -new -key and the same -subj and -addext options. ECDSA keys are smaller and faster to use; RSA 2048 remains the most widely compatible choice.

Can I install the same certificate on a second server?

Technically yes: copy the key and the chain file over a secure channel and use the same virtual host settings. Check your CA's licence terms first, because some products are licensed per server. Every additional copy of the key is also one more place it can leak from.

Should the private key have a passphrase?

On a web server usually not. Apache would ask for the passphrase on every start, so an unattended reboot leaves the site offline. Strict file permissions on the key and full-disk or volume encryption are the usual alternatives.

What is the difference between DV, OV and EV certificates?

The encryption is identical. Domain validation (DV) proves control of the domain only. Organisation validation (OV) and extended validation (EV) additionally verify the company behind it and put its name into the certificate. Installation on Apache is the same for all three.

Share story:

Continue reading other guides on How VPS.