Securing VPS/Servers SSL Certificates

Create a Self-Signed SSL Certificate for Apache on Ubuntu 24.04

Generate a self-signed certificate with a proper subjectAltName, enable mod_ssl and serve a site over HTTPS with Apache on Ubuntu 24.04.

This guide shows you how to create a self-signed TLS certificate with OpenSSL and use it for an HTTPS virtual host in Apache on Ubuntu 24.04 LTS. The same steps work on Ubuntu 22.04. At the end, Apache answers on port 443 with your own certificate and redirects plain HTTP to HTTPS.

Older tutorials on this topic were written for Ubuntu 10.04, which reached end of life many years ago. The idea is unchanged, but the details are not: current browsers and tools ignore the Common Name and require a subjectAltName, 1024-bit keys are rejected, and the Apache layout uses sites-available files with a .conf extension.

A self-signed certificate encrypts traffic but is not trusted by browsers, so visitors see a warning. That is fine for internal tools, staging sites, a connection between your own servers, or a placeholder until a real certificate arrives. For a public website use a certificate from a certificate authority instead: either a free one from Let's Encrypt or a commercial one. Both are covered in separate guides on this site.

Requirements

  • A server running Ubuntu 24.04 or 22.04 and a user with sudo rights (the examples use deploy).
  • Apache installed and serving your site over HTTP. If it is not installed yet, run sudo apt update && sudo apt install apache2.
  • The host name the site is reached by. The examples use example.com and the address 203.0.113.10; replace both with your own.

Step 1: Enable mod_ssl

The SSL module is part of the Apache packages on Ubuntu but is not enabled by default. Enable it and restart Apache so that the module is loaded:

sudo a2enmod ssl
sudo systemctl restart apache2

Step 2: Generate the key and certificate

Ubuntu keeps certificates in /etc/ssl/certs and private keys in /etc/ssl/private, which only root and the ssl-cert group can enter. One openssl req command creates both files:

sudo openssl req -x509 -newkey rsa:2048 -noenc -days 365 \
  -keyout /etc/ssl/private/example.com.key \
  -out /etc/ssl/certs/example.com.crt \
  -subj "/CN=example.com" \
  -addext "subjectAltName = DNS:example.com,DNS:www.example.com,IP:203.0.113.10"

What the options do:

  • -x509 writes a self-signed certificate instead of a certificate request.
  • -newkey rsa:2048 creates a new 2048-bit RSA key together with the certificate.
  • -noenc leaves the key without a passphrase, so Apache can start unattended. It replaces the older -nodes option, which is deprecated in OpenSSL 3.
  • -days 365 sets the validity period. Without it the certificate is valid for only 30 days.
  • -subj sets the subject without the interactive questions. Leave it out if you prefer to be asked for country, organisation and so on.
  • -addext "subjectAltName = ..." lists every name and IP address the certificate must be valid for. Clients check this list, not the Common Name, so include each name you will type into a browser. Remove the IP: entry if nobody connects by address.

Make sure the private key is readable by root only:

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

Check what you created:

sudo openssl x509 -in /etc/ssl/certs/example.com.crt -noout -subject -dates -ext subjectAltName

Step 3: Create the HTTPS virtual host

Create a new site file:

sudo nano /etc/apache2/sites-available/example.com-ssl.conf

Add the following, adjusting the names and DocumentRoot to match your existing HTTP site:

<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com/public_html

    SSLEngine on
    SSLCertificateFile /etc/ssl/certs/example.com.crt
    SSLCertificateKeyFile /etc/ssl/private/example.com.key

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

Enable the site, test the configuration and reload Apache:

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

configtest must print Syntax OK. If it reports a missing file, check the two paths in the virtual host.

Step 4: Redirect HTTP to HTTPS (optional)

To send all plain HTTP requests to the encrypted site, add one line to your existing port 80 virtual host (for example /etc/apache2/sites-available/example.com.conf):

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    Redirect permanent / https://example.com/
</VirtualHost>
sudo apache2ctl configtest
sudo systemctl reload apache2

Step 5: Open port 443 in the firewall

If UFW is active, allow HTTPS. The apache2 package installs application profiles for this:

sudo ufw allow 'Apache Full'
sudo ufw status

If the profile is not available on your system, sudo ufw allow 443/tcp does the same for HTTPS. Also allow port 443 in your provider's network firewall if you use one.

Step 6: Test the connection

From the server or any other machine, request the page and tell curl to accept the untrusted certificate with -k:

curl -kI https://example.com/

To see which certificate Apache actually sends, use openssl s_client:

openssl s_client -connect example.com:443 -servername example.com </dev/null 2>/dev/null | openssl x509 -noout -subject -issuer -dates

Subject and issuer are identical, which is exactly what "self-signed" means. In a browser you will see a warning page; after you accept the exception the connection is encrypted like any other HTTPS connection.

Trust the certificate on your own machines

You can remove the warning on machines you control by adding the certificate to their trust store. Copy example.com.crt (never the key) to the client. On an Ubuntu or Debian client:

sudo cp example.com.crt /usr/local/share/ca-certificates/example.com.crt
sudo update-ca-certificates

Command-line tools such as curl then accept the certificate without -k. Browsers may use their own certificate store; import the file through the browser's certificate settings if needed. For a single command you can also pass the file directly:

curl --cacert example.com.crt https://example.com/

Renewing and replacing the certificate

A self-signed certificate does not renew itself. Check the expiry date at any time:

sudo openssl x509 -in /etc/ssl/certs/example.com.crt -noout -enddate

To renew it, run the command from step 2 again and reload Apache. Clients that imported the old certificate must import the new one. When you later switch to a certificate from a certificate authority, only the two SSLCertificate... lines in the virtual host change.

Frequently asked questions

Ubuntu already has a "snakeoil" certificate. Can I use that?

Yes, for a quick test. The ssl-cert package generates /etc/ssl/certs/ssl-cert-snakeoil.pem and /etc/ssl/private/ssl-cert-snakeoil.key for the server's host name, and the stock default-ssl site uses them (sudo a2ensite default-ssl). It is issued for the system host name only, so creating your own certificate with the correct names is the better choice for a real site. If the host name changes, regenerate it with sudo make-ssl-cert generate-default-snakeoil --force-overwrite.

Is a self-signed certificate less secure than a paid one?

The encryption is the same. What is missing is identity: nobody has confirmed that the certificate belongs to your server, so a visitor cannot tell your certificate from one presented by an attacker in the middle unless they imported yours beforehand. That is why it is suitable for machines you control and not for public visitors.

Can I use one self-signed certificate for several sites?

Yes. Add every host name to the subjectAltName list, separated by commas, and reference the same two files in each virtual host. A wildcard entry such as DNS:*.example.com covers all direct subdomains.

How long should the certificate be valid?

You decide with -days. A long lifetime is convenient, but some clients refuse server certificates with very long validity periods even when they are trusted locally, so one year with a calendar reminder is a safe choice.

Share story:

Continue reading other guides on How VPS.