GNU/Linux >> Linux Esercitazione >  >> Debian

Proteggi Nginx con Lets Encrypt su Debian 9

Let's Encrypt è un'autorità di certificazione gratuita e aperta sviluppata dall'Internet Security Research Group (ISRG). I certificati emessi da Let's Encrypt sono oggi considerati affidabili da quasi tutti i browser.

In questo tutorial, spiegheremo come utilizzare lo strumento Certbot per ottenere un certificato SSL gratuito per Nginx su Debian 9. Mostreremo anche come configurare Nginx per utilizzare il certificato SSL e abilitare HTTP/2.

Prerequisiti #

Assicurati che i seguenti prerequisiti siano soddisfatti prima di continuare con questo tutorial:

  • Acceduto come utente con privilegi sudo.
  • Disponi di un nome di dominio che punta all'IP del tuo server pubblico. Useremo example.com .
  • Installa Nginx seguendo queste istruzioni
  • Hai un blocco server per il tuo dominio. Puoi seguire queste istruzioni per i dettagli su come crearne uno.

Installa Certbot #

Certbot è uno strumento completo e facile da usare in grado di automatizzare le attività per ottenere e rinnovare i certificati SSL Let's Encrypt e configurare i server Web per utilizzare i certificati. Il pacchetto certbot è incluso nei repository Debian predefiniti.

Aggiorna l'elenco dei pacchetti e installa il pacchetto certbot:

sudo apt updatesudo apt install certbot

Genera Strong Dh (Diffie-Hellman) gruppo #

Lo scambio di chiavi Diffie–Hellman (DH) è un metodo per lo scambio sicuro di chiavi crittografiche su un canale di comunicazione non protetto. Genereremo un nuovo set di parametri DH a 2048 bit per rafforzare la sicurezza:

sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048
Se lo desideri, puoi modificare la dimensione fino a 4096 bit, ma in tal caso la generazione potrebbe richiedere più di 30 minuti a seconda dell'entropia del sistema.

Ottenere un certificato SSL Let's Encrypt #

Per ottenere un certificato SSL per il nostro dominio utilizzeremo il plugin Webroot che funziona creando un file temporaneo per la convalida del dominio richiesto nel ${webroot-path}/.well-known/acme-challenge directory. Il server Let's Encrypt effettua richieste HTTP al file temporaneo per convalidare che il dominio richiesto venga risolto nel server su cui viene eseguito certbot.

Mapperemo tutte le richieste HTTP per .well-known/acme-challenge in una singola directory, /var/lib/letsencrypt .

I seguenti comandi creeranno la directory e la renderanno scrivibile per il server Nginx.

sudo mkdir -p /var/lib/letsencrypt/.well-knownsudo chgrp www-data /var/lib/letsencryptsudo chmod g+s /var/lib/letsencrypt

Per evitare la duplicazione del codice, crea i seguenti due snippet che verranno inclusi in tutti i nostri file di blocco del server Nginx.

Apri il tuo editor di testo e crea il primo snippet, letsencrypt.conf :

sudo nano /etc/nginx/snippets/letsencrypt.conf
/etc/nginx/snippets/letsencrypt.conf
location ^~ /.well-known/acme-challenge/ {
  allow all;
  root /var/lib/letsencrypt/;
  default_type "text/plain";
  try_files $uri =404;
}

Crea il secondo snippet ssl.conf che include i cippatori consigliati da Mozilla, abilita OCSP Stapling, HTTP Strict Transport Security (HSTS) e applica poche intestazioni HTTP incentrate sulla sicurezza.

sudo nano /etc/nginx/snippets/ssl.conf
/etc/nginx/snippets/ssl.conf
ssl_dhparam /etc/ssl/certs/dhparam.pem;

ssl_session_timeout 1d;
ssl_session_cache shared:SSL:50m;
ssl_session_tickets off;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS';
ssl_prefer_server_ciphers on;

ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 30s;

add_header Strict-Transport-Security "max-age=15768000; includeSubdomains; preload";
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;

Al termine, apri il file di blocco del server di dominio e includi il letsencrypt.conf snippet come mostrato di seguito:

sudo nano /etc/nginx/sites-available/example.com.conf
/etc/nginx/sites-available/example.com.conf
server {
  listen 80;
  server_name example.com www.example.com;

  include snippets/letsencrypt.conf;
}

Abilita il nuovo blocco del server creando un collegamento simbolico a sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/

Riavvia il servizio Nginx per rendere effettive le modifiche:

sudo systemctl restart nginx

Ora puoi eseguire Certbot con il plug-in webroot e ottenere i file del certificato SSL emettendo:

sudo certbot certonly --agree-tos --email [email protected] --webroot -w /var/lib/letsencrypt/ -d example.com -d www.example.com

Se il certificato SSL viene ottenuto con successo, sul tuo terminale verrà stampato il seguente messaggio:

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/example.com/privkey.pem
   Your cert will expire on 2018-07-28. 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"
 - 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

Quindi, modifica il blocco del server di dominio come segue:

sudo nano /etc/nginx/sites-available/example.com.conf
/etc/nginx/sites-available/example.com.conf
server {
    listen 80;
    server_name www.example.com example.com;

    include snippets/letsencrypt.conf;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name www.example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;

    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl http2;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;

    # . . . other code
}

Con la configurazione sopra stiamo forzando HTTPS e reindirizzamento da www alla versione non www.

Ricarica il servizio Nginx per rendere effettive le modifiche:

sudo systemctl reload nginx

Certificato SSL Let's Encrypt con rinnovo automatico #

I certificati di Let's Encrypt sono validi per 90 giorni. Per rinnovare automaticamente i certificati prima della scadenza, il pacchetto certbot crea un cronjob che viene eseguito due volte al giorno e rinnova automaticamente qualsiasi certificato 30 giorni prima della sua scadenza.

Poiché stiamo utilizzando il plug-in webroot di certbot una volta rinnovato il certificato, dobbiamo anche ricaricare il servizio nginx. Aggiungi --renew-hook "systemctl reload nginx" al /etc/cron.d/certbot file in modo che assomigli a questo:

sudo nano /etc/cron.d/certbot
/etc/cron.d/certbot
0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew --renew-hook "systemctl reload nginx"

Testare il processo di rinnovo automatico, eseguendo questo comando:

sudo certbot renew --dry-run

Se non ci sono errori, significa che il processo di rinnovo è andato a buon fine.


Debian
  1. Come proteggere Nginx con Lets Encrypt su Ubuntu 20.04 / 18.04

  2. Proteggi Nginx con Lets Encrypt su Ubuntu 18.04

  3. Proteggi Nginx con Lets Encrypt su Ubuntu 16.04

  4. Proteggi Nginx con Let's Encrypt SSL su Debian 10/11

  5. Come proteggere Nginx con Let's Encrypt su Ubuntu 20.04

Proteggi Apache con Lets Encrypt su Debian 10

Installa Lets Encrypt e Secure Nginx con SSL/TLS in Debian 9

Come installare Drupal 9 con Nginx e Lets Encrypt SSL su Debian 10

Come installare Discourse Forum con Nginx e Free Lets Encrypt SSL su Debian 11

Come proteggere Nginx con Let's Encrypt su CentOS 8

Come proteggere Nginx con Let's Encrypt su Ubuntu 20.04