在 Nginx 上使用 Let’s Encrypt 加密(HTTPS)你的网站[简明教程]

MargotMccul 8年前

来自: http://www.appinn.com/use-letsencrypt-with-nginx/

前几天有个消息,只在特定的圈子里传播了一下: The PKI platform of StartSSL is now hosted by Qihoo 360 ,大意是老牌的免费证书提供商 StartSSL 将服务器放到了 Qihoo 360 的服务器上,于是担心证书已不再安全。从而转向免费证书新星 Let’s Encrypt

青小蛙曾经用过好几年的 StartSSL 服务,为此还付费使用过,一年 59.9 美金,你可以随意签发证书。不过近两年证书普遍降价,再加上免费的 Let’s Encrypt 和即将到来的 HTTP/2,互联网的全面 HTTPS 化将在不久的将来实现…届时不会再有劫持,不会再有干扰…总之看起来很美。

Let’s Encrypt 是一个将于2015年末推出的数字证书认证机构,将通过旨在消除当前手动创建和安装证书的复杂过程的自动化流程,为安全网站提供免费的SSL/TLS证书。

Let’s Encrypt 是由互联网安全研究小组(ISRG,一个公益组织)提供的服务。主要赞助商包括电子前哨基金会,Mozilla基金会,Akamai以及思科。2015年4月9日,ISRG与Linux基金会宣布合作。

那么,现在使用 Let’s Encrypt 则是一个非常经济实惠又安全的选择,于是青小蛙参考  How To Secure Nginx with Let’s Encrypt on Ubuntu 14.04 这篇文章,有了这篇教程。

必备条件:

  1. 有一台 VPS,推荐  HHVPS 产品;
  2. 有一个域名(可以在  HH 注册 );
  3. 想加密自己的网站

实现结果:

用户通过 TLS/SSL 加密访问你的网站,而后台实现自动续签 Let’s Encrypt 证书。

开始

教程基于 Ubuntu 14.04 系统,不够其他 Linux 版本都是类似的,用你熟悉的系统即可。

第 1 步:安装 Let’s Encrypt 客户端

装前准备,更新系统和安装 git & bc:

apt-get update
apt-get -y install git bc

克隆 Let’s Encrypt 到  /opt/letsencrypt:

git clone https://github.com/letsencrypt/letsencrypt /opt/letsencrypt

第 2 步:获取证书

首先关闭 Nginx:

service nginx stop

并且检查一下 80 端口没有被占用:

netstat -na | grep ‘:80.*LISTEN’

运行 Let’s Encrypt:

cd /opt/letsencrypt

./letsencrypt-auto certonly –standalone

</div>

注意:Let’s Encrypt 需要超级用户权限,如果你没有使用 sudo 命令,可能会让你输入密码。

之后会出现图形界面输入邮箱、条款、域名等信息:

支持多域名,只需要在第三张截图里用空格或者英文逗号分隔就好了。

目前 Let’s Encrypt 没有 EV 证书与 泛域名证书的计划。

IMPORTANT NOTES:

  • If you lose your account credentials, you can recover through e-mails sent to domain@appinn.com
  • Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/example.com/fullchain.pem. Your cert will expire on 2016-05-15. To obtain a new version of the certificate in the future, simply run Let’s Encrypt again.
  • Your account credentials have been saved in your Let’s Encrypt 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 Let’s Encrypt so making regular backups of this folder is ideal.
  • If like Let’s Encrypt, please consider supporting our work by:

Donating to ISRG / Let’s Encrypt: https://letsencrypt.org/donateDonating to EFF: https://eff.org/donate-le

看到这一段,就正常证书已经签发成功,位于 /etc/letsencrypt,注意备份。

如果使用国内 VPS,此处可能会由于 DNS 问题出错,可以尝试更换 VPS 的 DNS 为第三方,比如 8.8.8.8。

每一个域名都会自动生成四个文件,位于 /etc/letsencrypt/archive/domain 目录下:

cert.pem: 域名证书

chain.pem: The Let’s Encrypt 证书

fullchain.pem: 上面两者合体

privkey.pem: 证书密钥

</div>

第 3 步:配置 Nginx

有了域名证书,就开始配置 Nginx 了,这部分比较略。

打开对应网站的配置文件,一般在 /etc/nginx/sites-available/default 或者 /usr/local/nginx/conf/ 中,试你自己的情况。

server {

listen 443 ssl;

server_name appinn.com ;

ssl on;

ssl_certificate /etc/letsencrypt/live/ appinn.com /fullchain.pem;

ssl_certificate_key /etc/letsencrypt/live/

appinn.com

/privkey.pem;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

ssl_prefer_server_ciphers on;

ssl_ciphers ‘EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH’;

}

</div>

注意修改红色的部分为你的域名

如果你想开启全站 https,需要将 http 转向到 https,再添加一个 server 就好了:

server {

listen 80;

server_name appinn .com;

return 301 https://$host$request_uri;

}

</div>

注意以上配置文件需要根据实际情况修改。

保存,重启 Nginx

service nginx restart

此时,打开你的域名比如 https:// 就能看到绿色的地址栏了。

第 4 步:自动续签证书

Let’s Encrypt 证书只有 90 天的有效期,这和之前按年使用的商业证书有些区别,所以我们还需要设置自动续签,好让证书一直有效。

安装 Webroot 插件

这是一个可以不用停止 Web 服务就能让 Let’s Encrypt 验证域名的插件,再次打开 Nginx 配置文件,在 ssl 下面添加:

location ~ /.well-known {

allow all;

}

</div>

保存。

使用命令行续签证书

cd /opt/letsencrypt

./letsencrypt-auto certonly -a webroot –agree-tos –renew-by-default –webroot-path= /usr/share/nginx/html -d example.com -d www.example.com

</div>

注意修改 webroot-path 参数,这是你的网站路径。

service nginx reload

重新加载 Nginx 配置文件。

创建  Let’s Encrypt 续签配置文件

cp /opt/letsencrypt/examples/cli.ini /usr/local/etc/le-renew-webroot.ini

我们将直接编辑示例配置文件:

vi /usr/local/etc/le-renew-webroot.ini

修改以下几行:

rsa-key-size = 4096

email = you@example.com

domains = example.com, www.example.com

webroot-path = /usr/share/nginx/html

</div>

保存。

现在就可以使用配置文件来续签证书了:

cd /opt/letsencrypt

./letsencrypt-auto certonly -a webroot –renew-by-default –config /usr/local/etc/le-renew-webroot.ini

</div>

创建自动续签脚本

下载脚本并设置权限:

curl -L -o /usr/local/sbin/le-renew-webroot https://gist.githubusercontent.com/thisismitch/e1b603165523df66d5cc/raw/fbffbf358e96110d5566f13677d9bd5f4f65794c/le-renew-webroot

chmod +x /usr/local/sbin/le-renew-webroot

</div>

注意:确保上一步创建的 续签配置文件 /usr/local/etc/le-renew-webroot.ini 存在,否则脚本将无法运行。

运行脚本:

le-renew-webroot

正常情况下,你将得到当前证书的剩余时间:

Checking expiration date for example.com…The certificate is up to date, no need for renewal (82 days left).

创建定时任务:

crontab -e

添加下面一行,让每周一早上 2 点 30 分运行一次,并记录到日志文件中。

30 2 * * 1 /usr/local/sbin/le-renew-webroot >> /var/log/le-renewal.log

结束

至此,在 Nginx 上使用 Let’s Encrypt 的配置与续签全部完成,今后就完全不需要打理这一部分了,HTTPS 将默默工作。目前 善用佳软  已经在使用 Let’s Encrypt,可以围观一下: https://xbeta.info

小众软件还有一枚有效期内的泛域名证书,目前还没有使用 Let’s Encrypt,是的,你可以用 https://www.appinn.com 来访问小众软件,不过由于历史以及 CDN 遗留问题,还没有全站切换过去,等待 HTTP/2 吧。

不过发现频道( https://faxian.appinn.com )已经 HTTPS 了哦。有疑问欢迎留言反馈。

</div>