In a previous blog of this series we set up Apache httpd to forward traffic to Payara Server. However, this only covers forwarding HTTP and not HTTPS. This blog will demonstrate how to secure Payara Server with Apache over HTTPS on Ubuntu.
What is HTTPS?
HTTPS, or Secure HTTP encrypts the traffic to prevent anyone from tampering with the message or eavesdropping on it, although this only works as long as both endpoints maintain the secure channel. You will know when you are on a website using HTTPS as your browser will display a green padlock to the left of the address bar and the URL will start with https:// as shown in Firefox:
How Do I Set up Apache to Forward HTTPS Requests?
Presuming that you have already set up Apache httpd as in the previous blog there are several additional modules that must be enabled. To enable them, use the
a2enmod command as shown:
And then restart the server:
At this point if you go to https://localhost in a browser it will return a 502 error. This is because you do not have an SSL certificate configured for use in the Apache web server.
How do I get an SSL certificate?
Both domain1 and payaradomain (default domains shipped with most distributions) come with sample, pregenerated SSL certificates that use Payara Limited Services as its OU for the localhost domain. Also, when creating a new domain in Payara Server, a new SSL certificate is generated in the same way for the current hostname of the machine where the command is run. However, this should NOT be used in production, it is strongly recommended that you get a custom certificate from a trusted Certificate Authority for production usage. For instructions on how to use a custom SSL certificate with Payara Server, see our blog post on "Securing Payara Server with Custom SSL Certificate". Both the keystore and truststore for Payara Server can be found in the config directory of the relevant Payara Server domain. The default certificate for Payara can be found in keystore.jks within that directory. The keystore used in Payara Server is a JKS or Java KeyStore; different to Apache Server which uses the PKCS12 type. The Java Keytool can be used to convert a JKS keystore to a PKCS12 store with the following steps:
- Use the Keytool to export the public certificate as "public.cert"
- Use the Keytool to convert the keystore to pkcs12 format as "mystore.p12"
- Use openssl to
You will now have the files public.cert which is the SSL certificate and mystore.pem which is the key file and has the password of mysecret.
Editing the configuration
Now that you have the certificate and key you need to edit the Apache configuration file. If you followed the previous blog it will be under /etc/apache2/sites-available/payaraSite.conf. If the file does not exist, run the following command to create it:
Now, you should find the following section in the file, which starts with
<VirtualHost *:80> as below:
Add the following configuration fragment:
Note that the Payara Server port we are proxying to is still the HTTP port. This is because the Payara Server instance is local to the Apache webserver, so encryption is not needed at this point and would slow things down. Port 8080 should not be exposed to the internet and should be behind a firewall. For
The default certificate is only valid for localhost and will return a 502 error if you try accessing it via the 127.0.0.1 address or your hostname.
SSLProxyCheckPeerExpire makes sure that the certificate is not out of date.
SSLCertificateKeyFile are the paths for the both the certificate and private key files respectively.
Now restart the ApacheServer.
You will be required to type in the password for your key file, which will be set to the value used earlier of mysecret. You will be required to type it in every time the Apache server starts.
Storing the key password
It is very important to note that it is a security risk to store this password on the server. In production, this file must be encrypted, but it is better not to store it at all.
To avoid retyping the password for the key every time you restart Apache, you can create a bash script to automatically input the password. As the root user in the /etc/apache2 directory, create a file called "password.sh". Open it with your preferred text editor and write the following:
Where mysecret is the password of the key file if you set it to be something other than the default. Then make the file executable by running
Finally, add the following configuration element in your virtual host definition:
Now when you start Apache you will no longer be asked for the password to your keyfile. If you go to https://localhost you will see a security warning which should look something like this:
This is because you are using a self-signed certificate rather than an externally verified certificate issued by a Certificate Authority such as LetsEncrypt or Verisign. Click on Advanced and then Add Security Exception you will be able to reach the Payara Server home page.
Success! You have now reached Payara’s welcome screen using HTTPS.