07.06.2023

How to Install SSL Certificate on Apache for CentOS 8

In this tutorial, we will install an SSL certificate on the Apache web server for CentOS 8. This will ensure that website traffic is encrypted and HTTPS is used. First, you need a web server. How to install Apache on CentOS 8 was discussed earlier. An SSL certificate is required too. We recently looked at how to set up Apache virtual hosts on CentOS 8. It may be useful to learn it before proceeding to installing an SSL certificate.

In the Serverspace you can create a server with already installed app "Apache".

Checking mod_ssl

First, you need to make sure that mod_ssl is installed in the system. To check this use this command:

rpm -qa | grep mod_ssl

If you see no output, install mod_ssl:

dnf install mod_ssl

Place the SSL certificate on the server

You need to create a complete chain of your SSL certificate from its public key and the chain of certificate authorities.

cat pub-key.pem ca-chain.pem > full-chain.pem

Now place it in the /etc/pki/tls/certs/ folder.
Place the private key to the /etc/pki/tls/private/ folder and make it inaccessible to other users.

chmod -R 600 /etc/pki/tls/private/

Apache configuration

Create a configuration file for your domain name if you don't have one. Put it in the /etc/httpd/conf.d/ folder and give it a name with the .conf extension. Here and further replace domain-name.com with your own.

nano /etc/httpd/conf.d/domain-name.com.conf

Insert the following configuration there:

<VirtualHost *:443>
SSLEngine on
# The path to the complete chain of your SSL certificate
SSLCertificateFile /etc/pki/tls/certs/full-chain.pem
# The path to the private key
SSLCertificateKeyFile /etc/pki/tls/private/privkey.pem
# The path to the content of your website.
<Directory /var/www/domain-name.com>
AllowOverride All
</Directory>
# The path to the content of your website
DocumentRoot /var/www/domain-name.com
# Domain name of your website
ServerName domain-name.com
ServerAlias www.domain-name.com
</VirtualHost>

Create the /var/www/domain-name.com directory and put the content of your site there, if you haven't already done so.

Redirect from HTTP to HTTPS

To redirect requests from unprotected port 80 (HTTP) to encrypted port 443 (HTTPS), add the following lines to the /etc/httpd/conf.d/domain-name.com.conf file.

<VirtualHost *:80>
ServerName domain-name.com
ServerAlias www.domain-name.com
Redirect "/" "https://domain-name.com/"
</VirtualHost>

Save and close the file and restart Apache.

systemctl restart httpd

Now you have a configured SSL certificate on Apache with a working HTTPS protocol on your site.