letsencrypt: Certificados SSL en nginx con renovación automática
Mediante letsencrypt podemos generar certificados SSL y renovarlos automáticamente. Vamos a ver cómo hacerlo para un nginx
Primero deberemos hacer un clone del repositorio:
cd /opt/ git clone https://github.com/letsencrypt/letsencrypt
Y apagaremos el nginx para realizar la petición inicial:
service nginx stop
Mediante letsencrypt-auto podemos generar el certificado indicando el dominio mediante la opción -b, por ejemplo:
# /opt/letsencrypt/letsencrypt-auto certonly --standalone -d ejemplo.systemadmin.es Updating letsencrypt and virtual environment dependencies...... Requesting root privileges to run with virtualenv: /root/.local/share/letsencrypt/bin/letsencrypt certonly --standalone -d ejemplo.systemadmin.es Version: 1.1-20080819 Version: 1.1-20080819 IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/ejemplo.systemadmin.es/fullchain.pem. Your cert will expire on 2016-05-03. To obtain a new version of the certificate in the future, simply run Let's Encrypt again. - If you like Let's Encrypt, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le
Una vez generado prepararemos la configuración del nginx tanto de HTTP como de HTTPS de tal forma que /.well-known sea un directorio accesible desde fuera:
server { listen 80 default; server_name localhost; root /usr/share/nginx/html; location ~ /.well-known { allow all; } location / { proxy_pass http://127.0.0.1:7990; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } } server { listen 443 ssl default; server_name localhost; root /usr/share/nginx/html; ssl_certificate /etc/letsencrypt/live/ejemplo.systemadmin.es/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/ejemplo.systemadmin.es/privkey.pem; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH'; location ~ /.well-known { allow all; } location / { proxy_pass http://127.0.0.1:7990; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Host $host; proxy_cache_bypass $http_upgrade; } }
Finalmente, arrancamos el nginx con el certificado de letsencrypt:
service nginx start
Para configurar la renovación automática deberemos genera un fichero de configuración para el dominio, por ejemplo /etc/ejemplo.conf con el contenido:
rsa-key-size = 4096 email = ejemplo@systemadmin.es domains = ejemplo.systemadmin.es webroot-path = /usr/share/nginx/html
Descargamos el script letsencrypt-nginx-autorenew desde github
wget https://raw.githubusercontent.com/jordiprats/letsencrypt-nginx-autorenew/master/renew.cert.sh -O /usr/local/bin/renew.cert.sh
Lo podemos probar con la configuración:
# /usr/local/bin/renew.cert.sh /etc/ejemplo.conf certificate up to date, remainig days: 89
Cuando falten menos de 10 días para caducar, renovará el certificado ejecutando el script de letencrypt. Podemos dejarlo en el cron diariamente para asegurarnos que no nos caduque el certificado:
0 0 * * * /usr/local/bin/renew.cert.sh /etc/ejemplo.conf
Deja un comentario: