GNU/Linux >> Tutoriels Linux >  >> Cent OS

Comment sécuriser Nginx avec Letsencrypt sur Rocky Linux/Centos 8

Il a été développé par l'Internet Security Research Group (ISRG) et approuvé par tous les principaux navigateurs. Il est utilisé pour automatiser le processus de création, de validation, de signature, de mise en œuvre et de renouvellement des certificats pour les sites Web sécurisés.

Le certificat n'est valable que 90 jours, vous devrez donc le renouveler manuellement ou configurer le système de renouvellement automatique,

Let's encrypt prend en charge l'émission automatisée de certifications pour Apache, Nginx, Plex et HAproxy. Nous couvrirons nginx dans ce guide.

Contenu associé

  • Comment sécuriser Nginx avec Letsencrypt sur Ubuntu 20.04
  • Comment installer et configurer Nginx, WordPress et Mysql 8 dans Rocky Linux/Centos 8
  • Comment installer Nginx et configurer l'hôte virtuel dans Ubuntu 20.04

Prérequis :

  • Serveur CentOS 8 avec accès Internet et IP publique
  • Un nom de domaine valide avec un DNS pointant vers le serveur
  • Accès root au serveur Centos 8

Installation du client Certbot Let's Encrypt

Connectez-vous au serveur à l'aide de ssh [email protected] -p port :

ssh [email protected]

Mettez à jour tous vos packages vers leurs dernières versions disponibles.

sudo dnf update -y

Installer Nginx

sudo dnf install -y nginx

Démarrer et activer nginx

systemctl start nginx
systemctl enable nginx

Pour cela, créons une configuration nginx :
Configuration Nginx mise à jour :

server {
    listen 80;
    server_tokens off;
    client_max_body_size 10M;
    server_name site1.citizix.com;

    access_log /var/log/nginx/site1.citizix.com/access.log;
    error_log /var/log/nginx/site1.citizix.com/error.log;
    ignore_invalid_headers off;

    ## Deny illegal Host headers
    if ($host !~* ^(site1.citizix.com)$ ) {
        return 444;
    }

    root /var/www/site1.citizix.com;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header   Host $host;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Scheme $scheme;
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
    }

}

Installer le client Certbot

Le Certbot est un outil en ligne de commande utilisé pour simplifier le processus d'obtention et de renouvellement des certificats SSL Let's Encrypt pour votre site Web.

sudo dnf install epel-release
sudo dnf install certbot python3-certbot-nginx

Si vous utilisez firewalld, activez http et https dans le pare-feu :

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https

sudo firewall-cmd --reload

# Verify
sudo firewall-cmd --permanent --list-all

Obtenir un certificat

Arrêtez nginx :

sudo systemctl stop nginx
sudo certbot --nginx --non-interactive --agree-tos --email [email protected] -d site1.citizix.com

Sortie

# sudo certbot --nginx --non-interactive --agree-tos --email [email protected] -d site1.citizix.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Account registered.
Requesting a certificate for site1.citizix.com
Performing the following challenges:
http-01 challenge for site1.citizix.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/conf.d/site1.citizix.com.conf
Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/site1.citizix.com.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled
https://site1.citizix.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/site1.citizix.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/site1.citizix.com/privkey.pem
   Your certificate will expire on 2021-11-05. 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"
 - 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

Démarrez nginx :

sudo systemctl start nginx

Configuration Nginx mise à jour :

server {
    server_tokens off;
    client_max_body_size 10M;
    server_name site1.citizix.com;

    access_log /var/log/nginx/site1.citizix.com/access.log;
    error_log /var/log/nginx/site1.citizix.com/error.log;
    ignore_invalid_headers off;

    ## Deny illegal Host headers
    if ($host !~* ^(site1.citizix.com)$ ) {
        return 444;
    }

    root /var/www/site1.citizix.com;

    location / {
        proxy_pass http://127.0.0.1:8096;
        proxy_set_header   Host $host;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Host $server_name;
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   X-Scheme $scheme;
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/site1.citizix.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/site1.citizix.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = site1.citizix.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

    listen 80;
    server_name site1.citizix.com;
    return 404; # managed by Certbot
}

Cent OS
  1. Comment installer Mysql 8 sur Rocky Linux/Centos 8

  2. Comment installer Erlang sur Rocky Linux/Alma Linux/CentOS 8

  3. Comment installer le client FreeIPA sur Rocky Linux/Alma Linux/CentOS 8

  4. Comment migrer de Centos 8 vers Rocky Linux 8

  5. Comment sécuriser Nginx avec Letsencrypt sur Ubuntu 20.04

Comment installer Nginx avec ngx_pagespeed sur CentOS

Comment installer Nginx avec PHP-FastCGI sur CentOS 6

Comment installer Magento avec Nginx sur CentOS 7

Comment installer LetsEncrypt SSL avec Nginx sur CentOS 6

Comment migrer de CentOS 8 vers Rocky Linux 8

Comment sécuriser Nginx avec Let's Encrypt sur CentOS 8