GNU/Linux >> Linux Esercitazione >  >> Cent OS

Come proteggere Nginx con Let's Encrypt su CentOS 8

In questo articolo, avremo spiegato i passaggi necessari per proteggere Nginx con Let's encrypt su CentOS 8. Prima di continuare con questo tutorial, assicurati di aver effettuato l'accesso come utente con sudo privilegi. Tutti i comandi in questo tutorial devono essere eseguiti come utente non root.

Let's Encrypt è un'autorità di certificazione che fornisce certificati SSL gratuiti per il sito Web, operativa da aprile 2016 e supportata da aziende e organizzazioni Internet di tutto il mondo come Mozilla, Cisco, Chrome , Akamai, ecc.

Prerequisito:

  • Sistema operativo con CentOS Linux
  • Indirizzo IPv4 del server con privilegi di superutente (accesso root)
  • Terminale Gnome per desktop Linux
  • Client PuTTy SSH per Windows o macOS
  • Powershell per Windows 10/11
  • Familiarità con i comandi DNF

Proteggi Nginx con Let's Encrypt su CentOS

Passaggio 1. Il primo comando aggiornerà gli elenchi dei pacchetti per assicurarti di ottenere l'ultima versione e le dipendenze.

sudo dnf install epel-release
sudo dnf update
sudo dnf install mod_ssl openssl

Prima di installare il dominio SSL Let's Encrypt dovrebbe essere ben accessibile e utilizzare l'host virtuale Nginx. Leggi il tutorial su come installare Nginx su CentOS 8.

Passaggio 2. Installa Certbot.

Il pacchetto certbot non è incluso nei repository CentOS 8 standard, ma può essere scaricato dal sito Web del fornitore:

sudo wget -P /usr/local/bin https://dl.eff.org/certbot-auto
sudo chmod +x /usr/local/bin/certbot-auto

Quindi, genera un nuovo set di parametri DH a 2048 bit usando il seguente comando:

sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048

Per ottenere un certificato SSL per il dominio, utilizzeremo il plugin Webroot che funziona creando un file temporaneo per convalidare il dominio richiesto in la directory ${webroot-path}/.well-known/acme-challenge:

sudo mkdir -p /var/lib/letsencrypt/.well-known
sudo chgrp nginx /var/lib/letsencrypt
sudo chmod g+s /var/lib/letsencrypt

Per evitare di duplicare il codice, crea i due frammenti seguenti che verranno inclusi in tutti i file di blocco del server Nginx:

sudo mkdir /etc/nginx/snippets
$ nano /etc/nginx/snippets/letsencrypt.conf

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

ssl_dhparam /etc/ssl/certs/dhparam.pem;

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

ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers off;

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=63072000" always;
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;

Una volta creati gli snippet, apri il blocco del server di dominio e includi lo snippet letencrypt.conf, come mostrato di seguito:

$ nano /etc/nginx/conf.d/example.com.conf

server {
  listen 80;
  server_name example.com www.example.com;

  include snippets/letsencrypt.conf;
}

Ricarica la configurazione di Nginx per rendere effettive le modifiche:

sudo systemctl reload nginx

Quindi, esegui questo comando per ottenere un certificato e fai in modo che Certbot modifichi automaticamente la tua configurazione Nginx per servirla, attivando l'accesso HTTPS in un unico passaggio:

sudo /usr/local/bin/certbot-auto certonly --agree-tos --email [email protected] --webroot -w /var/lib/letsencrypt/ -d your-domain.com -d www.your-domain.com

Infine, passaggi, modifica il blocco del server di dominio come segue:

$ nano /etc/nginx/conf.d/example.com.conf

server {
    listen 80;
    server_name www.your-domain.com your-domain.com;

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

server {
    listen 443 ssl http2;
    server_name www.your-domain.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
}

Ricarica il servizio Nginx per rendere effettive le modifiche:

sudo systemctl reload nginx

Passaggio 4. Verifica dello stato del certificato.

Puoi assicurarti che Certbot abbia creato il tuo certificato SSL correttamente utilizzando il test del server SSL della società di sicurezza cloud Qualys. Apri il seguente link nel tuo browser web preferito, sostituendo your-domain.com con il tuo dominio di base:

https://www.ssllabs.com/ssltest/analyze.html?d=your-domain.com

Congratulazioni, hai imparato a proteggere Nginx con Let's encrypt su CentOS 8. Se hai domande, lascia un commento qui sotto.


Cent OS
  1. Proteggi Nginx con Let's Encrypt su Ubuntu 18.04 - Come farlo?

  2. Come proteggere Nginx con Letsencrypt su Rocky Linux/Centos 8

  3. Come installare Let's Encrypt con Apache su CentOS 7

  4. Come installare Let's Encrypt su CentOS 8 con Nginx

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

Come configurare il blocco del server Nginx e proteggere Nginx con Let's Encrypt SSL su Rocky Linux 8 / CentOS 8

Come installare Let's Encrypt SSL con Nginx su CentOS 7

Come proteggere Nginx con Let's Encrypt certificato SSL

Come proteggere Apache con Let's Encrypt su CentOS 8

Come proteggere Nginx con Let's Encrypt su Ubuntu 20.04

Come proteggere Apache con Let's Encrypt su Ubuntu 20.04