Read in 5 minutes

last updated 

Secure Apache with Let's Encrypt on CentOS 7

Let’s Encrypt is a free, automated and open certificate authority developed by the Internet Security Research Group (ISRG). Certificates issued by Let’s Encrypt are valid for 90 days from the issue date and are trusted by almost all browsers today.

In this tutorial, we will cover the steps necessary to install a free Let’s Encrypt SSL certificate on a CentOS 7 server running Apache as a web server. We will use the certbot utility to obtain and renew Let’s Encrypt certificates.


Ensure that you have met the following prerequisites before continuing with this tutorial:

  • Have a domain name pointing to your public server IP. In this tutorial we will use
  • Have Apache installed and running on your server.
  • Have Apache virtual host for your domain.
  • Have port 80 and 443 open in your firewall.

Install the following packages which are required for an SSL encrypted web server:

yum install mod_ssl openssl

Install Certbot

Certbot is a tool that simplifies the process of obtaining SSL certificates from Let’s Encrypt and auto-enabling HTTPS on your server.

The certbot package is provided by EPEL. If the EPEL repository is not installed on your system, you can install it using the following command:

sudo yum install epel-release

Once the EPEL repository is enabled, install the certbot package by typing:

sudo yum install certbot

Generate Strong Dh (Diffie-Hellman) Group

Diffie–Hellman key exchange (DH) is a method of securely exchanging cryptographic keys over an unsecured communication channel. Generate a new set of 2048 bit DH parameters to strengthen the security:

sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048

You can change the size up to 4096 bits but in that case the generation may take more than 30 minutes depending on the system entropy.

Obtaining a Let’s Encrypt SSL certificate

To obtain an SSL certificate for our domain we’re going to use the Apache plugin that works by creating a temporary file for the requested domain in the ${webroot-path}/.well-known/acme-challenge directory and the Let’s Encrypt validation server makes HTTP requests to validate that the DNS for the requested domain resolves to the server where certbot runs.

To make it more simple we’re going to map all HTTP requests for .well-known/acme-challengeto a single directory, /var/lib/letsencrypt. The following commands will create the directory and make it writable for the Nginx server.

The following commands will create the directory and make it writable for the Apache server.

mkdir -p /var/lib/letsencrypt/.well-known
chgrp apache /var/lib/letsencrypt
chmod g+s /var/lib/letsencrypt

To avoid duplicating code create the following two configurations snippets:

Alias /.well-known/acme-challenge/ "/var/lib/letsencrypt/.well-known/acme-challenge/"
<Directory "/var/lib/letsencrypt/">
    AllowOverride None
    Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
    Require method GET POST OPTIONS
SSLProtocol All -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
SSLHonorCipherOrder On
Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
Header always set X-Frame-Options DENY
Header always set X-Content-Type-Options nosniff
# Requires Apache >= 2.4
SSLCompression off
SSLUseStapling on
SSLStaplingCache "shmcb:logs/stapling-cache(150000)"
# Requires Apache >= 2.4.11
SSLSessionTickets Off

The snippet above is using the chippers recommended by, enables OCSP Stapling, HTTP Strict Transport Security (HSTS) and enforces few security‑focused HTTP headers.

Reload the Apache configuration for changes to take effect:

sudo systemctl reload httpd

Now, we can run Certbot tool with the webroot plugin and obtain the SSL certificate files by typing:

sudo certbot certonly --agree-tos --email [email protected] --webroot -w /var/lib/letsencrypt/ -d -d

If the SSL certificate is successfully obtained, certbot will print the following message:

 - Congratulations! Your certificate and chain have been saved at:
   Your key file has been saved at:
   Your cert will expire on 2018-12-07. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:
   Donating to EFF:          

CentOS 7 ships with Apache version 2.4.6 which does not include the SSLOpenSSLConfCmd directive. This directive is only available on Apache 2.4.8 later and it is used for configuration of OpenSSL parameters such as Diffie–Hellman key exchange (DH).

We will have to create a new combined file using the Let’s Encrypt SSL certificate and the generated DH file. To do this, type:

cat /etc/letsencrypt/live/ /etc/ssl/certs/dhparam.pem >/etc/letsencrypt/live/

Now that everything is setup, edit your domain virtual host configuration as follows:

<VirtualHost *:80> 

  Redirect permanent /

<VirtualHost *:443>

  <If "%{HTTP_HOST} == ''">
    Redirect permanent /

  DocumentRoot /var/www/
  ErrorLog /var/log/httpd/
  CustomLog /var/log/httpd/ combined

  SSLEngine On
  SSLCertificateFile /etc/letsencrypt/live/
  SSLCertificateKeyFile /etc/letsencrypt/live/
  SSLCertificateChainFile /etc/letsencrypt/live/

  # Other Apache Configuration


With the configuration above we are forcing HTTPS and redirecting from www to non www version. Fell free to adjusts the configuration according to your needs.

Restart the Apache service for changes to take effect:

sudo systemctl restart httpd

You can now open your website using https:// and you’ll notice a green lock icon.

If you test your domain using the SSL Labs Server Test, you’ll get an A+ grade as shown bellow:

Auto-renewing Let’s Encrypt SSL certificate

Let’s Encrypt’s certificates are valid for 90 days. To automatically renew the certificates before they expire, we will create a cronjob which will runs twice a day and will automatically renew any certificate 30 days before its expiration.

Run the crontab command to create a new cronjob which will renew the certificate, create a new combined file including the DH key and restart apache :

sudo crontab -e
0 */12 * * * certbot renew --cert-name --renew-hook "cat /etc/letsencrypt/live/ /etc/ssl/certs/dhparam.pem >/etc/letsencrypt/live/ && systemctl restart httpd"

To test the renewal process, you can use the certbot --dry-run switch:

sudo certbot renew --cert-name --dry-run

If there are no errors, it means that the renewal process was successful.


In this tutorial, you used the Let’s Encrypt client, certbot to download SSL certificates for your domain. You have also created Apache snippets to avoid duplicating code and configured Apache to use the certificates. At the end of the tutorial you have set up a cronjob for automatic certificate renewal.

If you want to learn more about how to use Certbot, their documentation is a good starting point.

This post is a part of the Install LAMP Stack on CentOS 7 series.
Other posts in this series:

Secure Apache with Let's Encrypt on CentOS 7