HTTPS for Apache on Debian and Ubuntu

Last Updated:

Verification

These commands were tested on default installations of Debian 11 and Ubuntu 20.04 in May 2022. If you experience issues, Contact Ryan and report an issue

(Optional) Upgrade Packages

Make sure your machine has the latest packages installed and reboot.

  
    sudo DEBIAN_FRONTEND="noninteractive" apt-get update;
    sudo DEBIAN_FRONTEND="noninteractive" apt-get --with-new-pkgs upgrade -y;
    sudo reboot;
  

Upload Site Files

For example purposes, we'll add one static file. You can download your own site's files instead if you have some.

  
    sudo mkdir -p /var/www/example.com/public;
    echo "

HTTPS Site

" | sudo tee /var/www/example.com/public/index.html;

Generate Diffie-Hellman Parameters

We generate a new set of parameters unique to your machines. This improves security because usually a default set of DH parameters are used by everyone that installs Apache. We'll use this file in a little bit.

  
    # Generate Diffie-Hellman Parameters
    sudo openssl dhparam -out /etc/ssl/dhparam.pem 2048;
  

Copy TLS Certificate Files

Here we copy the needed TLS key and certificate files from previous lessons. Then we ensure the permissions are correct on both files. You might need to upload your certificate files onto the machine if you bought a certificate or generated a free certificate on another machine.

  
    # Edit the file names to something that
    # makes sense in your situation.

    # Copy Your TLS Key
    cp MY_CERTIFICATE.key /etc/ssl/private/MY_CERTIFICATE.key;
    chmod 600 /etc/ssl/private/MY_CERTIFICATE.key;

    # Copy Your TLS Certificate
    cp MY_CERTIFICATE.pem /etc/ssl/certs/MY_CERTIFICATE.pem;
    chmod 644 /etc/ssl/certs/MY_CERTIFICATE.pem;
  

Installing Apache

We install Apache and remove one of its default site configuration files. We'll be adding our own site configuration soon, and don't need the default.

  
    # Install Apache
    sudo apt-get install -y apache2;

    # Remove Default Site
    sudo a2dissite 000-default;

    # Want to Check Apache Version?
    # apache2 -v;
    #
    # Debian
    # Server version: Apache/2.4.53 (Debian)
    # Server built:   2022-03-14T16:28:35
    #
    # Ubuntu
    # Server version: Apache/2.4.41 (Ubuntu)
    # Server built:   2022-03-16T16:52:53
  

Enabling Modules

We'll need to enable two modules for the rest of the tutorial, with "ssl" handling HTTPS calls and "headers" adding some custom headers to responses.

  
    sudo a2enmod ssl;
    sudo a2enmod headers;
  

Enhanced Security

Now we disable showing the Apache version and signatures when it responds. This is information that can be used to identify your site as vulnerable and can be used in a targeted attack.

  
    echo "ServerTokens ProductOnly" | sudo tee -a /etc/apache2/conf-enabled/security.conf;
    echo "ServerSignature Off" | sudo tee -a /etc/apache2/conf-enabled/security.conf;
  

HTTP to HTTPS Redirect

For our first site configuration, we redirect any insecure HTTP traffic on port 80 to HTTPS. Pay close attention to the highlighted lines and make changes so it matches your domain.

  
    # Filename: /etc/apache2/sites-available/example.com.http.conf
    <VirtualHost *:80>
      ServerName example.com
      DocumentRoot /var/www/example.com/public

      ServerAdmin webmaster@localhost
      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined

      Redirect 301 / https://example.com/
    </VirtualHost>
  

HTTPS Configuration

Now we add the main HTTPS site configuration. Here's the full configuration file, and we'll break it down section by section. Pay close attention to the highlighted lines and make changes so it matches your domain.

Line 16: We're specifying Apache should use the unique DH Parameters we generated earlier.

Lines 20-23: For enhanced security at the risk of your site breaking for some visitors, you can use the stronger ciphers instead.

Line 29: When you're sure everything is working, you can enable this line to increase your site's grade on Qualys' SSLLabs from an "A" to an "A+" rating.

  
    # Filename: /etc/apache2/sites-available/example.com.https.conf
    <IfModule mod_ssl.c>
      <VirtualHost *:443>
        ServerName example.com
        DocumentRoot /var/www/example.com/public

        ServerAdmin webmaster@localhost
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # TLS
        SSLEngine on
        SSLProtocol            +TLSv1.2 +TLSv1.3
        SSLCertificateFile     /etc/ssl/certs/MY_CERTIFICATE.pem
        SSLCertificateKeyFile  /etc/ssl/private/MY_CERTIFICATE.key
        SSLOpenSSLConfCmd      DHParameters "/etc/ssl/dhparam.pem"
        SSLCompression         off
        SSLSessionTickets      off
        SSLHonorCipherOrder    on
        SSLCipherSuite         ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256
        # For better security, you can use only the strong ciphers below instead
        # But beware, it might break the site for some of your visitors
        #SSLCipherSuite        ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256

        # Optional
        # Forces TLS for all visitors for 1 year
        # Only enable this when you are sure everything is working well
        # Required for an "A+" Grade
        #Header always set Strict-Transport-Security: "max-age=31536000; includeSubDomains; preload"

      </VirtualHost>
    </IfModule>
  

Enabling the Sites

Here we enable the site configurations with Apache's a2ensite command (using symlinks). If you're not familiar with symlinks, think of them like creating a shortcut to another file. This will create a symlink from source file "/etc/apache2/sites-available/example.com.https.conf" to "/etc/apache2/sites-enabled/"

  
    sudo a2ensite example.com.http;
    sudo a2ensite example.com.https;
  

Testing Our Configuration

It's always a good idea to check our configurations and see if there are any errors. We do this with the command "apache2ctl configtest".

  
    # Test the Apache configuration
    sudo apache2ctl configtest;

    # Output should be similar to:
    # AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
    # Syntax OK
  

Starting Apache

We now need to make sure Apache is running. First, we make sure Apache is "enabled" which means Apache will start on boot of the machine. Then, we "restart" Apache which will restart it if it's running or if it's not running, start it. We prefer "restart" because it works in either case.

  
    # Run Apache automatically at boot by "enabling" it
    sudo systemctl enable apache2;

    # Start or Restart Apache Now
    sudo systemctl restart apache2;
  

You're Done!

Your new site should be available and running. To test it in your browser, make sure you set up a DNS "A" Record on your domain that contains your machine's IP. When you visit the domain, the site should load correctly and should also be ready to test on Qualys SSLLabs where you'll get an "A" rating! (or "A+" if you enabled the setting in the configuration).

Tags: