GNU/Linux >> Tutoriels Linux >  >> Ubuntu

Comment configurer le certificat SSL Let's Encrypt avec Nginx sur Ubuntu 18.04 et Ubuntu 16.04

Let's Encrypt est une autorité de certification largement connue qui fournit des certificats SSL X.509 gratuits pour le cryptage TLS. Il a été lancé en avril 2016.

Let's Encrypt automatise le processus de création, de validation, de signature, de mise en œuvre et de renouvellement des certificats.

Actuellement, Apache, Nginx, Plex et Haproxy sont pris en charge pour le processus automatisé.

Prérequis

Avant de continuer, configurez la pile LEMP pour avoir Nginx sur votre système Ubuntu.

LIRE :Comment installer la pile LEMP sur Ubuntu 18.04

LIRE :Comment installer la pile LEMP sur Ubuntu 16.04

Installer Certbot

Pour générer un certificat pour votre domaine, vous devez disposer d'un accès terminal/shell et d'un client Certbot ACME. Il gère l'émission et l'installation des certificats sans temps d'arrêt.

Pour installer le client Certbot, nous devons configurer Certbot PPA sur Ubuntu.

sudo apt update

sudo apt install -y software-properties-common

sudo add-apt-repository universe

sudo add-apt-repository ppa:certbot/certbot

sudo apt update

Maintenant, installez le client certbot.

sudo apt install -y certbot python-certbot-nginx

Créer un hôte virtuel Nginx

Nous allons maintenant créer un fichier de configuration d'hôte virtuel pour le domaine www.itzgeek.net.

Cet hôte virtuel sert la version HTTP de votre domaine.
sudo nano /etc/nginx/conf.d/www.itzgeek.net.conf

Utilisez les informations ci-dessous.

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;
   }
}

Créez une racine de document pour contenir vos fichiers HTML.

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

Modifier l'autorisation du répertoire.

sudo chown -R nginx:nginx /opt/nginx/

Placez le fichier HTML de test à la racine du document de votre domaine.

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

Redémarrez le service Nginx.

sudo systemctl restart nginx

Mettre à jour / Modifier l'enregistrement DNS

Accédez à votre outil de gestion DNS ou à votre registraire de domaine et créez un enregistrement A/CNAME pour le domaine. Ex :www.itzgeek.net.

Attendez un moment pour laisser l'enregistrement se propager.

Vérifiez la propagation DNS avec l'utilitaire Nslookup sudo apt install -y dnsutils.

Installer le certificat SSL Let's Encrypt

Utilisez la commande certbot pour créer manuellement un certificat Let's Encrypt.

sudo certbot --nginx

Suivez l'invite interactive et générez le certificat requis.

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]  << Enter email address to receive notifications

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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  << Agree to Terms of Service

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
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  << Select domain to install Let's Encrypt SSL 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 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-04. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. 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

Redirection des requêtes HTTP non-www vers www HTTPS avec Nginx (facultatif)

Nous allons maintenant configurer le serveur Nginx pour rediriger le trafic provenant du site HTTP non-www vers le site WW HTTPS, c'est-à-dire http://itzgeek.net>> https://www.itzgeek.net .

Ici, nous utiliserons le même fichier de configuration que nous avons créé pour la version HTTP du site pour effectuer la redirection du site HTTP non-www vers WW HTTPS.

sudo nano /etc/nginx/conf.d/www.itzgeek.net.conf

Ajoutez les informations ci-dessous à la fin du fichier.

# 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;

}

Redémarrez le service Nginx.

sudo systemctl restart nginx

Vérifier le certificat SSL Let's Encrypt

Vérifiez le certificat Let's Encrypt en visitant la version HTTPS de votre site Web.

http://votre-http-site-web

OU

https://votre-https-site-web

Vous devriez obtenir la version HTTPS de votre site maintenant.

Tester le certificat SSL Let's Encrypt

Testez votre certificat SSL pour tout problème et ses cotes de sécurité en accédant à l'URL ci-dessous.

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

Renouveler le certificat SSL Let's Encrypt

Les certificats Let's Encrypt ont une validité de 90 jours, et il est fortement conseillé de renouveler vos certificats avant leur expiration.

Grâce à l'entrée de planificateur cron intégrée fournie avec le client Certbot. Cette tâche cron s'exécute deux fois par jour pour renouveler les certificats qui vont expirer.

Vous pouvez simuler le processus de renouvellement du certificat avec la commande ci-dessous pour vous assurer que le renouvellement se déroule sans heurts.

sudo certbot renew --dry-run

Sortie :

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.

Le résultat ci-dessus confirme que le renouvellement fonctionnera comme prévu.

Conclusion

C'est tout. J'espère que vous avez appris à configurer le certificat SSL Let's Encrypt avec Nginx sur Ubuntu 18.04 et Ubuntu 16.04. Veuillez partager vos commentaires dans la section des commentaires.


Ubuntu
  1. Sécurisez Nginx avec Lets Encrypt sur Ubuntu 20.04

  2. Comment configurer Let's Encrypt (Certbot) sur Ubuntu 20.04

  3. Comment installer Let's Encrypt SSL sur Ubuntu 18.04 avec Nginx

  4. Comment installer Let's Encrypt sur Ubuntu 20.04 avec Apache

  5. Comment sécuriser Nginx avec Let's Encrypt sur Ubuntu 20.04

Comment installer Let's Encrypt SSL pour Nginx sur Ubuntu 18.04 LTS

Comment installer Nginx avec Let's Encrypt SSL sur Ubuntu 20.04 LTS

Comment installer Apache avec Let's Encrypt SSL sur Ubuntu 20.04 LTS

Sécurisez Nginx avec le certificat SSL Let's Encrypt sur Ubuntu 18.04

Comment sécuriser Nginx avec le certificat SSL Let's Encrypt

Comment sécuriser Nginx avec Let's Encrypt sur Ubuntu 20.04