During this initial setup I've been using plain HTTP for the Dev and Test sites since it removes a layer of complication should something interesting occur. As the live site will be using HTTPS connections we need to set this up on Test too, just to be sure.

First a little gripe. The last of the Secure Sockets Layer (SSL) protocols were officially deprecated in 2015 and are now being disabled or completely removed from browsers. Its replacement, Transport Layer Security (TLS) has been around since 1999 and even the first two versions of that have been officially been deprecated (as of 2023 anyway). But for some reason the name SSL lingers on when talking about encrypting data in transit between clients and servers. Whenever I refer to SSL here it's because the that's the parameter still being used, so I have no choice if we want things to actually work.

The live site is hosted with Mythic Beasts and they automatically configure LetsEncrypt certificates via the control panel. For the Test site I have two problems:

  1. I don't have the Mythic Beasts web control panel
  2. LetsEncrypt certificates need fiddly workarounds for use on internal sites

Some time ago I realised this was going to be an issue I on other projects so I created my own internal certificate authority. The full details of this will be in a future article but to put it in a nutshell I use XCA v1 as a nice easy to use program to generate and manage certificates and I deploy my root certificate on all hosts and devices on my network so they trust my internal CA as well. I just have to remember to keep an eye on expiry dates to avoid any embarrassments.

Generate Internal Certificate

Since the full setup and use of my certificate authority will be a future article I'll link to I'm not going to repeat that here so I'm going to skip a bit and continue from the point where I have generated my private key and public certificate and uploaded them to the the server.

Best practice is the private key should be generated on the host and a certificate signing request created to obtain the certificate which is the process you'd go through with any public certificate authority, and yes I can do that with XCA if I want. But not only is this is a test server and I'm doing the whole process entirely privately, having a backup of the private key in the XCA database will also make it easier to recover from errors.

Key points:

  • Certificate is valid for 2 years at most
  • The Subject commonName field contains the fully-qualified hostname
  • The Extensions X509v3 Subject Alternate Name has DNS entries for both simple and fully-qualifed hostname
  • Key usage permits Digital Signature, Non Repudiation, Key Encipherment and TLS Web Server Authentication permissions

The certificate should be exported as a PEM Chain so the intermediate and root certs are included as well. This keeps browsers happy as they don't have to muck about with the AIA records which a lot of browsers refuse to do.

Upload

The first step is to upload the key and certificate and also the root certificate to the host. I use scp since my desktop is Linux Mint, any file transfer program will do. The target should be a private area on the server such as my home directory rather than /tmp

me@desktop ~/Desktop $ scp *.pem user@examplehost
examplehost
joomla-cert.pem                          100% 5977     5.6MB/s   00:00    
joomla-key.pem                           100% 1675     2.1MB/s   00:00

The private key needs to go to the /etc/ssl/private directory with 640 permissions and root:root ownership

root@joomlatest:~# cd /etc/ssl/private
root@joomlatest:/etc/ssl/private# mv /home/user/joomla-key.pem .
root@joomlatest:/etc/ssl/private# chmod 640 joomla-key.pem 
root@joomlatest:/etc/ssl/private# chown root:ssl-cert joomla-key.pem 
root@joomlatest:/etc/ssl/private# ls -l
total 8
-rw-r----- 1 root ssl-cert 1675 Mar 13 21:23 joomla-test-key.pem
-rw-r----- 1 root ssl-cert 1704 Mar  3 16:51 ssl-cert-snakeoil.key

The certificate needs to go to the /etc/ssl/certs directory, it should also be owned by root but with 644 permissions so anyone can read it.

root@joomlatest:/etc/ssl/private# cd ../certs
root@joomlatest:/etc/ssl/certs# mv /tmp/joomla-test-cert.pem .
root@joomlatest:/etc/ssl/certs# chmod 644 joomla-test-cert.pem 
root@joomlatest:/etc/ssl/certs# chown root:root joomla-test-cert.pem 
root@joomlatest:/etc/ssl/certs# ls -l
-rw-r--r-- 1 root root   5977 Mar 13 21:23  joomla-test-cert.pem

Ubuntu also likes to have a link in the certs directory named for the certificate hash, f0847b85 in this case, suffixed with .0 that points to the actual certificate. We can determine the hash using openssl

root@joomlatest:/etc/ssl/certs# openssl x509 -hash -noout -in joomla-test-cert.pem
f0847b85
root@joomlatest:/etc/ssl/certs# ln -s joomla-test-cert.pem f0847b85.0
root@joomlatest:/etc/ssl/certs# ls -lrt
...
-rw-r--r-- 1 root root   5977 Mar 13 21:23  joomla-test-cert.pem
lrwxrwxrwx 1 root root     20 Mar 13 22:18  f0847b85.0 -> joomla-test-cert.pem

Make sure the certificate for the internal root CA is also deployed to the certs directory too. I really should add that to my server configuration playbook.

Apache2 Configuration

The final step is to enable TLS on the Joomla site in Apache. We're going to add this as a second config for now as Joomla has some configuration options of it's own to enforce HTTPS for everything or only where it's really needed.

The first task is to enable the SSL module if it hasn't been done already, the module should have been included by default when Apache2 was installed

root@joomlatest:~# a2enmod ssl
Considering dependency setenvif for ssl:
Module setenvif already enabled
Considering dependency mime for ssl:
Module mime already enabled
Considering dependency socache_shmcb for ssl:
Enabling module socache_shmcb.
Enabling module ssl.

The next task is to copy the site definition file which we can then modify to enable TLS. Assuming your definition is in joomla.conf:

root@joomlatest:~# cd /etc/apache2/sites-available
root@joomlatest:/etc/apache2/sites-available# cp joomla.conf joomla-tls.conf

We then modify the new joomla-tls .conf file to enable TLS, or SSL as the config still calls it. The config to add or change is shown in bold

<VirtualHost *:443>
     ServerAdmin This email address is being protected from spambots. You need JavaScript enabled to view it.
     DocumentRoot /srv/joomla/test/
     ServerName joomlatest.example.com

     ErrorLog ${APACHE_LOG_DIR}/joomla-test-error.log
     CustomLog ${APACHE_LOG_DIR}/joomla-test-access.log combined

     # Requires a2enmod ssl
     SSLEngine On
     SSLCertificateFile "/etc/ssl/certs/joomla-test-cert.pem"
     SSLCertificateKeyFile "/etc/ssl/private/joomla-test-key.pem"

     <Directory /srv/joomla/test/>
            Options FollowSymlinks
            AllowOverride All
            Require all granted
     </Directory>
</VirtualHost>

Once you're happy then enable the configuration

root@joomlatest:/etc/apache2/sites-available# a2ensite joomla-tls.conf

Restart apache2 and then check it's running

root@joomlatest:/etc/apache2/sites-available# systemctl restart apache2
root@joomlatest:/etc/apache2/sites-available# systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Tue 2023-03-14 20:02:40 GMT; 4s ago
       Docs: https://httpd.apache.org/docs/2.4/

And then finally connect to your test site via browser using HTTPS instead and hopefully get your home page with a nice little padlock icon next to it.

Browser with padlock icon

This assumes of course that you have added the public key for your internal certificate authority to your browser.

Troubleshooting Tips

If it's not worked then hopefully it's something silly like a typo or missed step so the first thing is of course to look at the browser error and hope that gives some clue, then check the modified config file. Most likely errors are:

  • Didn't restart apache2, or didn't check it came back
  • Didn't enable the TLS config file
  • Didn't enable the SSL module
  • Didn't specify https:// when testing.
  • Something not right with your internal CA config.

I've done all of those at various times. Other than that check the apache2 ErrorLog and CustomLog files and then start searching the interwebs for clue. It's usually something quite trivial though.