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

Come configurare Let's Encrypt certificato SSL con Nginx su CentOS 8 / RHEL 8 e CentOS 7 / RHEL 7

Let's Encrypt è un'autorità di certificazione che fornisce certificati SSL gratuiti per i siti Web per abilitare la crittografia TLS. È stato lanciato nell'aprile 2016.

Let's Encrypt automatizza il processo di creazione, convalida, firma, implementazione e rinnovo dei certificati per siti Web sicuri.

Attualmente, Let's encrypt supporta l'emissione di certificazioni automatizzate per Apache, Nginx, Plex e Haproxy.

Prerequisiti

Dovresti avere uno stack LEMP configurato sul tuo sistema RHEL / CentOS.

LEGGI :Come installare lo stack LEMP su CentOS 8 / RHEL 8

LEGGI: Come installare lo stack LEMP su CentOS 7 / RHEL 7

Installa Certbot

Per generare un certificato Let's Encrypt per il tuo dominio, dovresti avere l'accesso al terminale e il client Certbot ACME installato sul tuo sistema. Gestisce l'emissione di certificati e aggiorna la configurazione di Nginx per utilizzare il certificato creato senza tempi di inattività.

Certbot è disponibile sul repository EPEL solo per CentOS 7 / RHEL 7. Quindi, dobbiamo scaricare manualmente il programma di installazione di Certbot per CentOS 8 / RHEL 8 dal suo sito ufficiale.

### CentOS 8 / RHEL 8 ###

curl -O https://dl.eff.org/certbot-auto

mv certbot-auto /usr/local/bin/certbot-auto

chmod 0755 /usr/local/bin/certbot-auto

### CentOS 7 ###

rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

yum install -y certbot python2-certbot-nginx

### RHEL 7 ###

rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

subscription-manager repos --enable rhel-7-server-optional-rpms

yum install -y certbot python2-certbot-nginx

Crea host virtuale

Creeremo ora un file di configurazione dell'host virtuale (blocco server) per il dominio www.itzgeek.net.

Questo blocco server serve la versione HTTP del tuo dominio.
vi /etc/nginx/conf.d/www.itzgeek.net.conf

Usa le informazioni di seguito.

server {
   server_name www.itzgeek.net;
   root /opt/nginx/www.itzgeek.net;

   location / {
       index index.html index.htm index.php;
   }

   access_log /var/log/nginx/www.itzgeek.net.access.log;
   error_log /var/log/nginx/www.itzgeek.net.error.log;

   location ~ \.php$ {
      include /etc/nginx/fastcgi_params;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   }
}

Crea una radice del documento per posizionare i tuoi file HTML.

mkdir -p /opt/nginx/www.itzgeek.net

Modifica l'autorizzazione della directory.

chown -R nginx:nginx /opt/nginx/www.itzgeek.net

Inserisci il file HTML di prova nella radice del documento del tuo dominio web.

echo "This is a test site @ www.itzgeek.net" > /opt/nginx/www.itzgeek.net/index.html

Riavvia il servizio Nginx.

systemctl restart nginx

Crea / Aggiorna record DNS

Accedi al tuo strumento di gestione DNS o registrar di domini e crea un record A/CNAME per il dominio. Es:www.itzgeek.net.

Attendi un po' di tempo per consentire la propagazione del record.

Controlla la propagazione DNS con l'utilità Nslookup yum install -y bind-utils.

Installa il certificato Let's Encrypt

Usa il comando certbot per creare e installare il certificato Let's Encrypt.

### CentOS 8 / RHEL 8 ###

/usr/local/bin/certbot-auto --nginx

### CentOS 7 / RHEL 7 ###

certbot --nginx

Segui il prompt interattivo e genera e installa il certificato.

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): [email protected]  << Email Address to receive renewal/security notification

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A  << Access Terms and Conditions

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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 our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y  << Subscribe to Newsletter

Which names would you like to activate HTTPS for?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: www.itzgeek.net
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1  << Choose domain name to generate certificate
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for www.itzgeek.net
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/conf.d/www.itzgeek.net.conf

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): 2  << Redirect traffic from HTTP to HTTPS
Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/www.itzgeek.net.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://www.itzgeek.net

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=www.itzgeek.net
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/www.itzgeek.net/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/www.itzgeek.net/privkey.pem
   Your cert will expire on 2019-11-02. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot-auto
   again with the "certonly" option. 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

Reindirizza le richieste HTTP non www a www HTTPS con Nginx (opzionale)

Ora configureremo il server Nginx per reindirizzare il traffico proveniente dal sito HTTP non www al sito HTTPS WW, ovvero http://itzgeek.net>> https://www.itzgeek.net .

Qui utilizzeremo lo stesso file di configurazione che abbiamo creato per la versione HTTP del sito per eseguire il reindirizzamento del sito HTTP non www al reindirizzamento HTTPS WW.

vi /etc/nginx/conf.d/www.itzgeek.net.conf

Aggiungi le seguenti informazioni alla fine del file.

# Redirect NON-WWW HTTP to WWW HTTPS

server {
    if ($host = itzgeek.net) {
        return 301 https://www.itzgeek.net$request_uri;
    }


   server_name itzgeek.net;
    listen 80;
    return 404;

}

Riavvia il servizio Nginx.

systemctl restart nginx

Firewall

Configura il firewall per consentire le richieste HTTPS.

firewall-cmd --permanent --add-port=443/tcp

firewall-cmd --reload

Verifica certificato Let's Encrypt

Verifica i dettagli del certificato Let's Encrypt andando alla versione HTTPS del tuo sito web.

http://il-tuo-http-sito-web

O

https://tuo-https-sito-web

Test del certificato SSL

Controlla il tuo certificato SSL per eventuali problemi e le relative valutazioni di sicurezza andando all'URL sottostante.

https://www.ssllabs.com/ssltest/analyze.html?d=www.itzgeek.net

Rinnova il certificato Let's Encrypt

Il certificato Let's Encrypt ha una validità di 90 giorni e deve essere rinnovato prima della scadenza.

Per impostazione predefinita, sia CentOS 7 che RHEL 7 sono configurati con la voce cron scheduler che viene eseguita due volte al giorno per rinnovare il certificato Let's Encrypt.

Sfortunatamente, per CentOS 8 / RHEL 8, è necessario configurare manualmente lo scheduler cron.

echo "0 0,12 * * * root python -c 'import random; import time; time.sleep(random.random() * 3600)' && /usr/local/bin/certbot-auto renew" | sudo tee -a /etc/crontab > /dev/null

Puoi anche simulare il processo di rinnovo del certificato con il comando seguente per assicurarti che il rinnovo proceda senza intoppi.

### CentOS 8 / RHEL 8 ###

/usr/local/bin/certbot-auto renew --dry-run

### CentOS 7 / RHEL 7 ###

certbot renew --dry-run

Risultato:

Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Processing /etc/letsencrypt/renewal/www.itzgeek.net.conf
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Cert not due for renewal, but simulating renewal for dry run
Plugins selected: Authenticator nginx, Installer nginx
Renewing an existing certificate
Performing the following challenges:
http-01 challenge for www.itzgeek.net
Waiting for verification...
Cleaning up challenges

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
new certificate deployed with reload of nginx server; fullchain is
/etc/letsencrypt/live/www.itzgeek.net/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/www.itzgeek.net/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.

Se l'output non segnala alcun problema, il rinnovo del certificato funzionerà come previsto.

Conclusione

È tutto. Spero che tu abbia imparato a configurare Let's Encrypt SSL Certificate con Nginx su CentOS 8 / RHEL 8 e CentOS 7 / RHEL 7. Condividi il tuo feedback nella sezione commenti.


Cent OS
  1. Come installare SSL su RHEL 8 / CentOS 8

  2. Proteggi Nginx con Lets Encrypt su CentOS 7

  3. Installa WordPress con Nginx su CentOS 7 / RHEL 7

  4. Come installare phpMyAdmin con Nginx su CentOS 7 / RHEL 7

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

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

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

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

Come installare Nginx con Let's Encrypt SSL su Fedora 35

Come proteggere Apache con Let's Encrypt SSL Certificate su CentOS 8

Come proteggere Nginx con Let's Encrypt certificato SSL