如何在 Ubuntu 16.04 LTS 上使用 Nginx 安装 Let’s Encrypt SSL

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

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

在 Ubuntu 16.04 LTS 上安装 Let’s Encrypt SSL with Nginx

步骤 1. 首先,通过运行以下命令确保所有系统包都是最新的 apt-get 终端中的命令。

sudo apt-get update sudo apt-get upgrade

步骤 2. 在 Ubuntu 16.04 上安装 Let’s Encrypt SSL。

第一步是安装 certbot,这是一个软件客户端,它将自动执行该过程中的几乎所有事情:

add-apt-repository ppa:certbot/certbot apt-get update apt-get install certbot

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

apt-get install nginx systemctl start nginx

在 Ubuntu Linux 上安装让我们加密 SSL 的第一步是在 Nginx 服务器块配置中添加一个简单的配置。 将此行添加到您的服务器块配置中:

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

Save 并退出以应用更改:

### nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful

重启 Nginx:

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 服务器上配置 SSL/TLS。

首先,通过 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 服务器块配置可能如下所示:

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

Save 和 close 完成后的文件。

步骤 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 Web 服务器以应用更改。 如果需要,输出将记录到 /var/log/ssl-renew.log 文件中以供进一步分析。

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

Save

Save

Save