如何在 CentOS 7 上安装 Let’s Encrypt SSL with Nginx

在本教程中,我们将向您展示如何在 CentOS 7 上使用 Nginx 安装 Let’s Encrypt SSL。对于那些不知道的人,LetsEncrypt 是一个免费的开放式证书颁发机构 (CA),它为网站和其他服务提供免费证书。 该服务由电子前沿基金会、Mozilla、Cisco Systems 和 Akamai 提供支持。 不幸的是,LetsEncrypt.org 证书目前的有效期为 3 个月。 这意味着您现在需要每季度更新一次证书。

本文假设您至少具备 Linux 的基本知识,知道如何使用 shell,最重要的是,您将网站托管在自己的 VPS 上。 安装非常简单,假设您在 root 帐户下运行,如果不是,您可能需要添加 ‘sudo‘ 到命令以获取 root 权限。 我将向您展示在 CentOS 7 服务器上使用 Nginx 加密 SSL 的分步安装。

先决条件

  • 运行以下操作系统之一的服务器:CentOS 7。
  • 建议您使用全新的操作系统安装来防止任何潜在问题。
  • 对服务器的 SSH 访问(或者如果您在桌面上,则只需打开终端)。
  • 一个 non-root sudo user或访问 root user. 我们建议充当 non-root sudo user,但是,如果您在充当 root 时不小心,可能会损害您的系统。

在 CentOS 7 上安装 Let’s Encrypt SSL with Nginx

第 1 步。首先,让我们首先确保您的系统是最新的。

yum clean all yum -y update

步骤 2. 在 CentOS 7 上安装 Let’s Encrypt SSL。

在 CentOS 7 中,您可以找到 证书机器人 在 EPEL 存储库上; 如果你启用它,只需安装你需要的:

yum install epel-release yum install certbot

您还需要安装并运行 Nginx。 当然,如果您将证书添加到先前配置的 Web 主机上,这将已经安装:

yum install nginx systemctl start nginx

在 CentOS Linux 上安装 let’s encrypt SSL 的第一步是在 Nginx 虚拟主机配置中添加一个简单的配置。 将此行添加到您的虚拟主机配置中:

location ~ /.well-known {   allow all;   }

Save 并退出以应用更改:

nginx -t systemctl restart nginx

使用 Certbot 获取证书:

运行如下所示的命令,将“idroot.us”替换为您的真实域名,然后 /var/www/idroot.us 使用您的真实 webroot 路径:

certbot certonly -a webroot --webroot-path=/var/www/idroot.us -d idroot.us -d www.idroot.us

结果:

[[email protected]:~]certbot certonly -a webroot --webroot-path=/var/www/idroot.us -d idroot.us -d www.idroot.us Saving debug log to /var/log/letsencrypt/letsencrypt.log Obtaining a new certificate Performing the following challenges: http-01 challenge for idroot.us Using the webroot path /var/www/html for all unmatched domains. Waiting for verification... Cleaning up challenges Generating key (2048 bits): /etc/letsencrypt/keys/0001_key-certbot.pem Creating CSR: /etc/letsencrypt/csr/0001_csr-certbot.pem IMPORTANT NOTES:  - Congratulations! Your certificate and chain have been saved at    /etc/letsencrypt/live/idroot.us/fullchain.pem. Your cert    will expire on 2017-07-16. 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"  - 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 [[email protected]:~]

步骤 3. 在 Nginx Web 服务器上配置 Let’s Encrypt TLS/SSL。

首先,通过 Certbot 编辑您在配置期间指定的虚拟主机文件并添加以下三个指令:

listen 443 ssl http2; ssl_certificate /etc/letsencrypt/live/idroot.us/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/idroot.us/privkey.pem;

完整的 Nginx vhost 配置可能如下所示:

server {      listen 80;      server_name idroot.us www.idroot.us;      rewrite ^(.*) https://idroot.us$1 permanent; }  server {      access_log off;      log_not_found off;      error_log  logs/idroot.us-error_log warn;      server_name  idroot.us;      root   /var/www/idroot.us;     index  index.php index.html index.htm;      listen 443 ssl http2;     ssl_certificate /etc/letsencrypt/live/idroot.us/fullchain.pem;     ssl_certificate_key /etc/letsencrypt/live/idroot.us/privkey.pem;    ## Stuff required by certbot      location ~ /.well-known {      allow all;      }    ## SSL    ssl_session_cache shared:SSL:20m;    ssl_session_timeout 10m;     ssl_prefer_server_ciphers On;    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;    ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS;     ssl_stapling on;    ssl_stapling_verify on;    resolver 8.8.8.8 8.8.4.4 valid=300s;    resolver_timeout 10s;     access_log /var/www/idroot.us/logs/access.log;    error_log /var/www/idroot.us/logs/error.log;     # php-script handler    location ~ .php$ {       fastcgi_index index.php;       fastcgi_pass 127.0.0.1:9000;       fastcgi_read_timeout 150;       root    /var/www/idroid.us/public_html;       fastcgi_param SCRIPT_FILENAME /var/www/idroot.us$fastcgi_script_name;       include /etc/nginx/fastcgi_params;    }  location  ~ /.ht {                deny  all;            }     }

步骤 5. 设置 Let’s Encrypt SSL 自动更新。

我们将添加一个 cronjob 来每周运行更新命令,运行这个命令:

 VISUAL=nano; crontab -e

粘贴以下行:

01 1 * * 0 /usr/bin/certbot renew >> /var/log/ssl-renew.log  06 1 * * 0 /usr/bin/systemctl nginx reload

Save 和 Exit 来自 crontab 表。

这将创建一个新的 cron 作业,该作业将在每周日凌晨 1 点执行,然后它将重新加载 Nginx 网络服务器以应用更改。 输出将登录到 /var/log/ssl-renew.log 如有需要,归档以供进一步分析。

恭喜! 您已成功安装 Let’s Encrypt。 感谢您使用本教程在 CentOS 7 系统上安装 Let’s Encrypt SSL。 如需更多帮助或有用信息,我们建议您查看 Let’s Encrypt 官方网站.

Save