Let's Encrypt SSL Certificate
Let's Encrypt is a non-profit certificate authority run by Internet Security Research Group that provides X.509 certificates for Transport Layer Security encryption at no charge. The certificate is valid for 90 days, during which renewal can take place at any time.
This section will describe how to obtain a certificate and automate the renewal process.
Prerequisites
- A registered domain name such as mydomain.com
- Two Host A records on your authoritative DNS server for your domain such as:
Code: Select all
Type A, mydomain.com, 216.70.70.70
Type A, mysite.mydomain.com, 216.70.70.70
- Web server (such as Apache)
- A Virtual Host configuration file
Install Certbot
The Ubuntu package repository has Certbot available but tends to be several versions behind what is available by the Cerbot developers. We can get the latest stable version of Certbot by adding the developers package repository.
Code: Select all
sudo add-apt-repository ppa:certbot/certbot
sudo apt update
sudo apt install python-certbot-apache
LetsEncrypt Permission Fix
While testing version 0.31.0, I found that I kept getting a "client lacks sufficient authorization" error message. The problem is that during the authorization phase, it re-directs the Virtual Host configuration to /var/lib/letsencrypt/http_challenges but the parent folder of /var/lib/letsencrypt has too restrictive of permissions set. The owner is set to root:root and permissions of 700 which means the web service (www-data) cannot view anything under that directory. The solution is to fix the permission on that folder so "other" users (www-data) can see the contents. NOTE: This folder only exists once you try to obtain your 1st SSL certification in the next section below.
Obtain the SSL Certificate
Code: Select all
sudo certbot --apache -d mydomain.com -d mysite.mydomain.com
We use -d to specify each name we would like the certificate to be valid for.
Upon the first time creating the certificate, you will be prompted for various information.
Once the information gathering is complete, Certbot will ask how to configure the HTTPS settings such as not forcing a redirect of HTTP to HTTPS or letting it add a redirect to force all HTTP traffic to HTTPS.
You can answer however you like but I like to do my own redirection (especially if this is already handled on a load balancer).
The new files will be placed here by default:
Code: Select all
/etc/letsencrypt/live/mydomain.com/fullchain.pem
/etc/letsencrypt/live/mydomain.com/privkey.pem
Look at the modified Virtual Host configuration file:
Code: Select all
sudo vi /etc/apache2/sites-available/mysite.mydomain.com.conf
The modified file now looks something like this:
Code: Select all
<IfModule mod_ssl.c>
<VirtualHost _default_:443>
ServerName mysite.mydomain.com
ServerAlias mysite.mydomain.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/mysite.mydomain.com
ErrorLog ${APACHE_LOG_DIR}/mysite.mydomain.com-error.log
CustomLog ${APACHE_LOG_DIR}/mysite.mydomain.com-access.log combined
SSLEngine on
<FilesMatch "\.(cgi|shtml|phtml|php)$">
SSLOptions +StdEnvVars
</FilesMatch>
<Directory /usr/lib/cgi-bin>
SSLOptions +StdEnvVars
</Directory>
<IfModule mod_headers.c>
Header always set Strict-Transport-Security "max-age=15768000; includeSubDomains; preload"
</IfModule>
BrowserMatch "MSIE [2-6]" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
BrowserMatch "MSIE [17-9]" ssl-unclean-shutdown
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/mysite.mydomain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mysite.mydomain.com/privkey.pem
</VirtualHost>
</IfModule>
Schedule For SSL Certificate Auto-Renew
Let's Encrypt certificates are only valid for 90 days. This encourages admins to automate their certificate renewal process.
You can force a simulated update to ensure the process will work using the dry-run option below. If you see "success" messages, then you should be OK when it comes time for the renew to run for real:
Starting with Ubuntu 18.04 LTS, we now have a systemd timer. When enabled, it will check for renewals twice per day.
Code: Select all
sudo systemctl enable certbot.timer
sudo systemctl start certbot.timer
Check the status with this command (which also shows when it last ran and when the next run will be):
Example Output:
Code: Select all
● certbot.timer - Run certbot twice daily
Loaded: loaded (/lib/systemd/system/certbot.timer; enabled; vendor preset: en
Active: active (waiting) since Tue 2019-10-29 09:14:23 CDT; 6h ago
Trigger: Tue 2019-10-29 20:05:09 CDT; 4h 20min left
Oct 29 09:14:23 srv-apache systemd[1]: Started Run certbot twice daily.
You can check the journal logs for certbot-related events:
Example Output:
Code: Select all
Oct 29 09:14:23 srv-apache systemd[1]: Started Run certbot twice daily.
Oct 29 09:16:47 srv-apache sudo[5618]: root : TTY=pts/1 ; PWD=/etc/apache2/sites-available ; USER=root ; COMMAND=/usr/bin/certbot --apache -d mysite.mydomain.com
Oct 29 09:18:12 srv-apache sudo[5690]: root : TTY=pts/1 ; PWD=/etc/apache2/sites-available ; USER=root ; COMMAND=/usr/bin/certbot --apache -d mysite.mydomain.com
Oct 29 12:00:01 srv-apache CRON[6946]: (root) CMD (test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(43200))' && certbot -q renew)