Let’s Encrypt is a Certificate Authority (CA) that makes it easy to obtain free TLS/SSL certificates, enabling an encrypted connection between the client's browser and the remote server using the Hypertext Transfer Protocol Secure (HTTPS); the standard port for this protocol is 443 TCP.
Let's Encrypt recommends that most people with shell access use the ACME client called Certbot. It can automate certificate issuance and installation and also has expert modes for people who do not want automatic configuration.
If Certbot does not meet your needs, there are many other ACME clients to choose from. Once you have chosen an ACME client, consult its documentation to proceed.
In this tutorial, we will show you how to install Certbot to add certificates for NGINX, Apache and LiteSpeed as web servers, both automatically and manually, on different Linux distributions.
For a Windows system, you can follow our article Install a Let’s Encrypt certificate on Windows Server and if the server has a panel such as Plesk, cPanel or HestiaCP installed, use the corresponding procedure:
- How to configure Let’s Encrypt in HestiaCP
- How to configure Let's Encrypt in Plesk
- How to configure cPanel AutoSSL
Requirements and preliminary steps
You need shell access as root or with sudo privileges on the VPS server or servers with Nginx or Apache installed to follow this article. You must also have purchased a domain and pointed it to DNS servers such as the name servers from Clouding. Your domain must also have the necessary A records for the certificate creation process to work correctly (DNS record types).
In this article, we will specifically use the records ejemplo.com and www.ejemplo.com as examples. If you want to create a certificate for them, you need a record for each one.
For a Wildcard certificate, a record must be created during the process of obtaining it. For more information, see the corresponding section.
Installing Certbot
There are several methods for installing certbot; here we explain how to use apt and snapd. However, if you do not have snapd installed or do not plan to use it in the future, we recommend the second option with apt (Advanced Packaging Tool), which comes by default with GNU/Linux distributions, to avoid unnecessary resource consumption, or yum for CentOS.
Installation for NGINX:
Install the required packages:
# apt update && apt install certbot python3-certbot-nginx
For older versions such as Ubuntu 16 or Debian 8, the package is python-certbot-nginx.
Installation for Apache:
Install the required packages:
# apt update && apt install certbot python3-certbot-apache
For older versions such as Ubuntu 16 or Debian 8, the package is python-certbot-apache.
Installation for LiteSpeed or others:
Like some other web servers, LiteSpeed does not have an official package for automatic configuration. You therefore only need to install the Certbot package:
# apt update && apt install certbot
Installation for NGINX:
Install the required packages:
# yum update && yum install certbot python3-certbot-nginx
For versions earlier than CentOS 7, the package is python-certbot-nginx.
Installation for Apache:
Install the required packages:
# yum update && yum install certbot python3-certbot-apache
For versions earlier than CentOS 7, the package is python-certbot-apache.
Installation for LiteSpeed:
LiteSpeed does not have an official package for automatic configuration. You therefore only need to install the Certbot package:
# yum update && yum install certbot
If you do not already have snapd installed, you can run the following on GNU/Linux:
# apt update && apt install snapd && snap install core; snap refresh core
For CentOS, replace apt with yum and enable it as follows:
# yum update && yum install snapd && systemctl enable --now snapd.socket && snap install core; snap refresh core
Install Certbot with snap:
# snap install --classic certbot
Verify the installation of the symbolic link:
# ln -s /snap/bin/certbot /usr/bin/certbot
Obtain an SSL certificate for a domain or subdomain
Automatic configuration (recommended):
Obtain a certificate and automatically modify the Nginx configuration:
# certbot --nginx -d example.com -d www.ejemplo.com
Confirm that automatic configuration was successful by accessing your domain over HTTPS in a browser and checking that the certificate appears.
You can also use SSL Checker or an SSL Server Test to verify the Let's Encrypt certificate.
Manual configuration:
Obtain a certificate only (without modifying the Nginx configuration):
# certbot certonly --nginx -d example.com -d www.ejemplo.com
In this case, you must manually configure the Nginx files to add the certificate locations. Replace the following values in the https (443) protocol configuration with the following locations:
ssl_certificate /etc/letsencrypt/live/ejemplo.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/ejemplo.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
You can also add a redirect from port 80 TCP (HTTP) to 443 TCP (HTTPS) in the corresponding section if you wish. Example:
server {
if ($host = ejemplo.com) {
return 301 https://$host$request_uri;
}
listen 80 ;
listen [::]:80 ;
server_name ejemplo.com;
return 404;
}
Automatic configuration (recommended):
Obtain a certificate and automatically modify the Apache configuration:
# certbot --apache -d example.com -d www.ejemplo.com
Confirm that automatic configuration was successful by accessing your domain over HTTPS in a browser and checking that the certificate appears.
You can also use SSL Checker or an SSL Server Test to verify the Let's Encrypt certificate.
Manual configuration:
Obtain a certificate only (without modifying the Apache configuration):
# certbot certonly --apache -d example.com -d www.ejemplo.com
Example Apache configuration file for HTTPS:
<IfModule mod_ssl.c>
<VirtualHost *:443>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
ServerName ejemplo.com
SSLCertificateFile /etc/letsencrypt/live/ejemplo.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/ejemplo.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
Example HTTP to HTTPS redirect for an Apache site configuration:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =ejemplo.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
As mentioned in the installation section, Certbot does not provide an official package for automatic LiteSpeed configuration. You must configure it manually by obtaining a certificate using the webroot option for validation through an ACME file:
# certbot certonly --webroot -w /usr/local/lsws/ejemplo.com/html/ -d example.com -d www.ejemplo.com
Configuration through LiteSpeed Webadmin
Once this is done, specify the certificate locations for the domain in question in the configuration. If you have access to Webadmin, you can do this in the SSL section of the corresponding virtualhost for that domain:
| Private Key File | /etc/letsencrypt/live/ejemplo.com/privkey.pem |
| Certificate File | /etc/letsencrypt/live/ejemplo.com/fullchain.pem |
Also remember to enable the Listener on port 443 if it is not already enabled, map the domain and mark the connection as secure:
In the SSL section, add a certificate to configure SSL and support the required protocols, as shown in the following screenshots:
Then configure the certificate in Virtual Hosts:
You can also enable and create a rule for redirection:
Redirection rule:
rewriteCond %{HTTPS} !on
rewriteCond %{HTTP:X-Forwarded-Proto} !https
rewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
Manual LiteSpeed configuration
If you are not using CyberPanel, edit the web service configuration file to create or modify the Listener:
# vi /usr/local/lsws/conf/httpd_config.conf
An example of adding or modifying the Listener and specifying a certificate location with vhost mapping:
listener HTTPS {
address *:443
secure 1
keyFile /etc/letsencrypt/live/ejemplo.com/privkey.pem
certFile /etc/letsencrypt/live/ejemplo.com/fullchain.pem
certChain 1
sslProtocol 31
map ejemplo ejemplo.com
map ejemplo2 ejemplo2.com
}
You must then also modify the configuration of the vhost or vhosts (after creating the mapped listener):
# vi /usr/local/lsws/conf/vhosts/ejemplo.com/vhconf.conf
An example vhost configuration file with HTTP to HTTPS redirection:
docRoot $VH_ROOT/html/
rewrite {
enable 1
autoLoadHtaccess 1
rules <<<END_rules
rewriteCond %{HTTPS} !on
rewriteCond %{HTTP:X-Forwarded-Proto} !https
rewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
END_rules
}
vhssl {
keyFile /etc/letsencrypt/live/ejemplo.com/privkey.pem
certFile /etc/letsencrypt/live/ejemplo.com/fullchain.pem
}
Additional options and information
Check or configure automatic renewal
Certbot automatically configures certificate renewal with a timer, and no further action is required. Therefore, the following is for information only.
Simulate a test renewal:
# certbot renew --dry-runCheck the status of the Certbot timer service:
# systemctl status certbot.timerTo list the jobs:
# systemctl list-timersThe following should appear, configured for automatic renewal:
certbot.timer - certbot.serviceIf you installed Certbot with snapd, this will appear:
snap.certbot.renew.timer - snap.certbot.renew.serviceYou can also disable the certbot.timer service to renew certificates through a scheduled task. In that case, run crontab:
# crontab -eAdd the following:
00 00 */1 * * certbot renewWith this configuration, it will check every day at 00:00 and renew if necessary.
Obtain a Wildcard certificate with Certbot
Run the following command, replacing the email address with a valid one, to obtain a Wildcard certificate:
# certbot certonly --manual --preferred-challenges=dns --email usuario@ejemplo.com --server https://acme-v02.api.letsencrypt.org/directory --agree-tosYou will be prompted to enter the domain to certify. Specify it using *. followed by the domain you want to certify to obtain the Wildcard:
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator manual, Installer None
Please enter in your domain name(s) (comma and/or space separated) (Enter 'c'
to cancel): *.ejemplo.es
You will then be asked whether you agree to your IP address being stored in their logs; you must accept with "Y":
Obtaining a new certificate
Performing the following challenges:
dns-01 challenge for ejemplo.com
NOTE: The IP of this machine will be publicly logged as having requested this
certificate. If you're running certbot in manual mode on a machine that is not
your server, please ensure you're okay with that.
Are you OK with your IP being logged?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Finally, you will be asked to create an _acme-challenge TXT record with your name server provider, using the content specified:
Please deploy a DNS TXT record under the name
_acme-challenge.ejemplo.com with the following value:
3wkWcAfmf1NvzCgtVSsaSqAaiDf26y3xZLuegXm-uHkaA
Before continuing, verify the record is deployed.
Press Enter to Continue
Create the record with your DNS provider and wait for the changes to propagate. You can verify this with nslookup, dig or view the progress in DNS Checker for example. Once the changes have propagated, press Enter to continue:
Waiting for verification...
Cleaning up challenges
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/ejemplo.com-0001/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/ejemplo.com-0001/privkey.pem
Your cert will expire on 2021-08-04. 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: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le
In this case, you must also configure the certificate in the configuration. For more information, see the previous section on manually configuring the service you are using. You can also export it for use on other servers hosting subdomains of the same domain (it will not renew automatically).