Instalar un certificado SSL de Let’s Encrypt en Debian 7 (Wheezy) con Apache 2.2

En este post voy a explicar cómo instalar un certificado SSL de Let’s Encrypt en Debian 7 (Wheezy) con Apache 2.2. Si estás buscando cómo instalar un certificado SSL de Let’s Encrypt en Debian 8 (Jessie) con Apache 2.4 puedes encontrar un tutorial en este enlace.

Si dispongo de acceso por shell y permisos de administración, en la web de Let’s Encrypt recomiendan hacerlo a través del cliente Certbot. En esta web selecciono el servidor web y el sistema operativo donde voy a instalar el certificado y me dirige a una guía de instalación.

LetsEncrypt-Apache2-2-Debian7-Certbot

A continuación aparece una guía paso a paso de cómo llevar a cabo la instalación.

Dado que Debian 7 no dispone de un paquete de Certbot, tengo que usar el paquete certbot-auto.

Lo primero que hago es habilitar el módulo SSL para Apache

sudo a2enmod ssl

Si no lo tengo instalado, tengo que reiniciar Apache para cargar el módulo en Apache

sudo service apache2 restart

Creo un directorio para alojar el binario de Let’s Encrypt y accedo a ese directorio

mkdir -p /etc/letsencrypt/bin
cd /etc/letsencrypt/bin/

Obtengo el binario y añado permiso de ejecución

wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto

Suponiendo que la base del hosting está alojada en /home/midominio/wordpress/ , creo el directorio .well-known, donde el cliente de Let’s Encrypt tratará de validar el dominio escribiendo información y accediendo desde sus servidores a mi servidor

mkdir /home/midominio/wordpress/.well-known

Creo un archivo .htaccess

touch /home/midominio/wordpress/.well-known/.htaccess

con el siguiente contenido, que hace accesible desde el exterior al directorio .well-known

RewriteEngine On
Satisfy Any

Creo el certificado para el dominio, con y sin www

root@debian7:/etc/letsencrypt/bin# ./certbot-auto certonly --webroot -w /home/midominio/wordpress/ -d midominio.com -d www.midominio.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel):hello@midominio.com

-------------------------------------------------------------------------------
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.1.1-August-1-2016.pdf. You must agree
in order to register with the ACME server at
https://acme-v01.api.letsencrypt.org/directory
-------------------------------------------------------------------------------
(A)gree/(C)ancel: A

-------------------------------------------------------------------------------
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about EFF and
our work to encrypt the web, protect its users and defend digital rights.
-------------------------------------------------------------------------------
(Y)es/(N)o: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for midominio.com
http-01 challenge for www.midominio.com
Using the webroot path /home/midominio/wordpress for all unmatched domains.
Waiting for verification...
Cleaning up challenges

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at
 /etc/letsencrypt/live/midominio.com/fullchain.pem. Your cert will
 expire on 2017-08-07. To obtain a new or tweaked version of this
 certificate in the future, simply run certbot-auto again. To
 non-interactively renew *all* of your certificates, run
 "certbot-auto renew"
 - Your account credentials have been saved in your Certbot
 configuration directory at /etc/letsencrypt. You should make a
 secure backup of this folder now. This configuration directory will
 also contain certificates and private keys obtained by Certbot so
 making regular backups of this folder is ideal.
 - 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

Ya están creados los certificados. Para acceder a ellos se hace referenciando tres archivos (realmente son enlaces simbólicos) ubicados en /etc/letsencrypt/live/midominio.com

cert.pem -> ../../archive/midominio.com/cert1.pem
chain.pem -> ../../archive/midominio.com/chain1.pem
fullchain.pem -> ../../archive/midominio.com/fullchain1.pem
privkey.pem -> ../../archive/midominio.com/privkey1.pem

Cada vez que se renuevan los certificados, se crean nuevos archivos (se incrementa el último dígito en 1 unidad) y se cambia el enlace simbólico.

Lo siguiente que tengo que hacer es añadir la parte SSL al archivo de configuración de Apache, incluyendo los certificados. No voy a describir todos los elementos del archivo de configuración del virtualhost, ya que puede variar mucho entre varias configuraciones. Este archivo se encuentra normalmente en el directorio /etc/apache/sites-available/

<VirtualHost *:80>
 ServerAdmin hello@midominio.com
 ServerName www.midominio.com
 ServerAlias midominio.com *.midominio.com
 Redirect permanent / https://www.midominio.com/
</VirtualHost>

<VirtualHost _default_:443>
 ServerAdmin hello@midominio.com
 ServerName www.midominio.com
 ServerAlias midominio.com *.midominio.com
 SSLEngine On
 DocumentRoot /home/midominio/wordpress/

 # Ruta de los certificados Let's Encrypt
 SSLCertificateFile "/etc/letsencrypt/live/midominio.com/fullchain.pem"
 SSLCertificateKeyFile "/etc/letsencrypt/live/midominio.com/privkey.pem"
 SSLCACertificateFile "/etc/letsencrypt/live/midominio.com/fullchain.pem"

</VirtualHost>

Tras modificar este archivo, compruebo que la sintaxis es correcta

apache2ctl -t
Syntax OK

Y reinicio Apache

service apache2 restart
[ ok ] Restarting web server: apache2 ... waiting ..

Accedo a http://www.midominio.com para comprobar que se produce la redirección y la carga de https://www.midominio.com

Solo queda por configurar la renovación. La hago de forma automática mediante cron para poder despreocuparme.

Lo primero que voy a hacer es simular una renovación

/etc/letsencrypt/bin/certbot-auto renew --dry-run
Saving debug log to /var/log/letsencrypt/letsencrypt.log

-------------------------------------------------------------------------------
Processing /etc/letsencrypt/renewal/midominio.com.conf
-------------------------------------------------------------------------------
Cert not due for renewal, but simulating renewal for dry run
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for midominio.com
http-01 challenge for www.midominio.com
Waiting for verification...
Cleaning up challenges

-------------------------------------------------------------------------------
new certificate deployed without reload, fullchain is
/etc/letsencrypt/live/midominio.com/fullchain.pem
-------------------------------------------------------------------------------
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates below have not been saved.)

Congratulations, all renewals succeeded. The following certs have been renewed:
 /etc/letsencrypt/live/midominio.com/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates above have not been saved.)

IMPORTANT NOTES:
 - Your account credentials have been saved in your Certbot
 configuration directory at /etc/letsencrypt. You should make a
 secure backup of this folder now. This configuration directory will
 also contain certificates and private keys obtained by Certbot so
 making regular backups of this folder is ideal.

Para programar la renovación automática, añado la siguiente línea en el cron, mediante el comando

crontab -e
# Renovación automática de los certificados Let's Encrypt
15 2,14 * * * /etc/letsencrypt/bin/certbot-auto renew --no-self-upgrade

Este comando va a tratar de renovar 2 veces al día el certificado. Para comprobar este comando de renovación, lo ejecuto de forma manual:

/etc/letsencrypt/bin/certbot-auto renew --no-self-upgrade
Saving debug log to /var/log/letsencrypt/letsencrypt.log

-------------------------------------------------------------------------------
Processing /etc/letsencrypt/renewal/midominio.com.conf
-------------------------------------------------------------------------------
Cert not yet due for renewal

The following certs are not due for renewal yet:
 /etc/letsencrypt/live/midominio.com/fullchain.pem (skipped)
No renewals were attempted.

Enlace de interés:
http://www.alondiamant.com/2016-12-20-using-lets-encrypt-certificates-with-wordpress-on-amazon-lightsail

6 comments

  1. Hola! estoy siguiendo los pasos y al realizar wget https://dl.eff.org/certbot-auto linux me indica: -bash: /usr/bin/wget: cannot execute binary file.
    Soy novato en linux. Como podría solucionar este inconveniente?
    Gracias!

  2. Estás teniendo problemas para ejecutar el comando wget.

    Prueba a ejecutar todos estos comandos con «sudo» delante.

  3. Hola Estoy siguiendo todos los pasos y al ejecutar el comando: ./certbot-auto certonly –webroot -w /home/midominio/wordpress/ -d midominio.com -d http://www.midominio.com
    da varios errores , como que no encuentra archivos y finaliza con el mensaje
    : No se pudieron obtener algunos archivos, ¿quizás deba ejecutar «apt-get update» o deba intentarlo de nuevo con –fix-missing?

  4. Hola, Christian
    Ten en cuenta que Debian 7 es una versión que está sin soporte, por lo que te recomiendo, antes de nada, usar Debian 9 o Debian 10 y seguir los tutoriales correspondientes.
    Saludos

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.