Instalación de certificados SSL con Let's Encrypt y Nginx en Debian
Cómo emitir certificados SSL con Let's Encrypt y habilitarlos en servidores Nginx para ofrecer conexiones seguras entre cliente y servidor.
Requerimientos
- Sistema Operativo Debian versión 9 (Stretch)
- Tener instalado Nginx
- Tener un dominio apuntando a la IP del Servidor.
1. Instalación de Certbot
Edita el archivo sources.list
.
sudo nano /etc/apt/sources.list
y añade las siguientes líneas al final.
deb http://deb.debian.org/debian stretch-backports main contrib non-free
deb-src http://deb.debian.org/debian stretch-backports main contrib non-free
Ejemplo:
sources.list
deb http://cdn-aws.deb.debian.org/debian stretch main
deb http://security.debian.org/debian-security stretch/updates main
deb http://cdn-aws.deb.debian.org/debian stretch-updates main
deb http://deb.debian.org/debian stretch-backports main contrib non-free
deb-src http://deb.debian.org/debian stretch-backports main contrib non-free
Actualiza el índice de paquetes.
sudo apt update
Instala Certbot desde los repositorios stretch-backports
.
sudo apt install python-certbot-nginx -t stretch-backports
Comprueba la versión instalada.
certbot --version
certbot 0.28.0
2. Creación de un nuevo dominio
Sigue la guía Creación y configuración de un nuevo dominio con Nginx. Con el dominio activo (por ejemplo, http://your_domain.com), continúa con los pasos siguientes.
3. Creación del certificado SSL
Consulta los certificados instalados.
sudo ls -l /etc/letsencrypt/live
Emite un nuevo certificado para el dominio y su versión www
.
sudo certbot --nginx -d your_domain.com -d www.your_domain.com
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.
-------------------------------------------------------------------------------
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
-------------------------------------------------------------------------------
Select the appropriate number [1-2] then [enter] (press 'c' to cancel):
Elige la opción 2
para que Certbot configure las redirecciones automáticamente.
Reinicia el servicio de Nginx.
sudo service nginx restart
Verifica los cambios desde el navegador o mediante curl
.
curl https://your_domain.com
<html>
<head>
<title>Welcome to your_domain.com!</title>
</head>
<body>
<h1>Success! The your_domain.com server block is working!</h1>
</body>
</html>
4. Añade un dominio a un certificado existente
Agrega el subdominio blog.your_domain.com
a un certificado ya emitido.
sudo certbot -d blog.your_domain.com --expand
Reinicia el servicio de Nginx.
sudo service nginx restart
5. Elimina un dominio de un certificado
Elimina el dominio blog.your_domain.com
de un certificado existente.
sudo certbot delete --cert-name blog.your_domain.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Deleted all files relating to certificate blog.your_domain.com.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Reconfigura manualmente las redirecciones en los archivos de configuración de Nginx.
sudo nano /etc/nginx/sites-available/blog.your_domain.com
Por ejemplo, comenta o elimina las líneas marcadas como # managed by Certbot
. Si es necesario, vuelve a crear el archivo con la configuración HTTP por defecto.
blog.your_domain.com
server {
root /var/www/blog.your_domain.com/html;
index index.html index.htm index.nginx-debian.html;
server_name blog.your_domain.com www.blog.your_domain.com;
location / {
try_files $uri $uri/ =404;
}
#listen [::]:443 ssl; # managed by Certbot
#listen 443 ssl; # managed by Certbot
#ssl_certificate /etc/letsencrypt/live/blog.your_domain.com/fullchain.pem; # managed by Certbot
#ssl_certificate_key /etc/letsencrypt/live/blog.your_domain.com/privkey.pem; # managed by Certbot
#include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
#ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
#if ($host = blog.your_domain.com) {
# return 301 https://$host$request_uri;
#} # managed by Certbot
listen 80;
listen [::]:80;
server_name blog.your_domain.com www.blog.your_domain.com;
#return 404; # managed by Certbot
}
Reinicia el servicio de Nginx.
sudo service nginx restart
6. Configura la renovación automática
Los certificados emitidos por Let's Encrypt son válidos durante 90 días, así que debes renovarlos con regularidad.
Comando para renovar los certificados:
sudo certbot renew
Saving debug log to /var/log/letsencrypt/letsencrypt.log
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/your_domain.com.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not yet due for renewal
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The following certs are not due for renewal yet:
/etc/letsencrypt/live/your_domain.com/fullchain.pem expires on 2020-09-04 (skipped)
No renewals were attempted.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Simula la renovación para comprobar que todo funciona.
sudo certbot renew --dry-run
...
Congratulations, all renewals succeeded. The following certs have been renewed:
/etc/letsencrypt/live/yourdomain.com/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates above have not been saved.)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Programa la renovación automática con un cron o timer del sistema.
sudo certbot renew --quiet --no-self-upgrade
Referencias
Publicado: 6 de junio de 2020