申请 Let’s Encrypt 泛域名证书 及 Nginx/Apache 证书配置 - 简书

2018年3月13日,Let’s Encrypt终于上线了在1月就应该上线的泛域名证书,这个证书类型为泛域名提供了可用的HTTPS方案。

let's encrypt基础知识

  • let's encrypt是一个认证机构(Certificate Authority = CA)
  • 想使用HTTPS需要认证机构颁发的电子证书
  • Let’s Encrypt和其他认证机构的区别(或者说是卖点):

    • 免费,Let’s Encrypt提供期限是90天的免费电子证书
    • 提供工具certbot自动生成电子证书文件
  • Let's Encrypt为了自动生成电子证书搞了一个ACME协议

    • Automatic Certificate Management Environment=ACME,自动认证管理环境协议
    • 协议草案已经提交IETF
    • ACME协议的基本思路是:

      • 在你服务器上生成一次性的随机特征数据(nonce)
      • 然后通过Let’s Encrypt的服务器核对这个数据
      • 核对成功发放证书
      • 有两种方式,HTTP和DNS,一般使用的是前者
  • certbot 官方安装文档

获取泛域名证书

安装Certbot

从官方源安装最新版certbot(最新版为0.22.0,从0.22.0版本才开始支持泛域名申请,不推荐从Debian源安装,常年不更新,还停留在0.10)

wget https://dl.eff.org/certbot-auto
chmod a+x ./certbot-auto

初始化

./certbot-auto

获取证书

因为目前大多数国内的DNS服务商不在API支持的列表里,所以以下使用手动方式进行DNS认证,只要将下方命令中的*.minirplus.com替换为自己的域名即可

./certbot-auto certonly --manual -d *.minirplus.com --agree-tos --no-bootstrap --manual-public-ip-logging-ok --preferred-challenges dns-01 --server https://acme-v02.api.letsencrypt.org/directory

注意!域名的 minirplus.com 解析记录必须以 A记录 方式指向当前运行命令的服务器IP,而不能使用CNAME记录。否则会报错,报错信息如下:

IMPORTANT NOTES:
 - The following errors were reported by the server:
 
   Domain: minirplus.com
   Type:   connection
   Detail: DNS problem: SERVFAIL looking up TXT for
   _acme-challenge.minirplus.com
 
   To fix these errors, please make sure that your domain name was
   entered correctly and the DNS A/AAAA record(s) for that domain
   contain(s) the right IP address. Additionally, please check that
   your computer has a publicly routable IP address and that no
   firewalls are preventing the server from communicating with the
   client. If you're using the webroot plugin, you should also verify
   that you are serving files from the webroot path you provided.

运行该命令后,会要求 输入邮箱,用于接收证书过期通知

Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel):

接着会出现一段广告,大意是收集客户邮箱给赞助商,YN 均可

-------------------------------------------------------------------------------
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 EFF and
our work to encrypt the web, protect its users and defend digital rights.
-------------------------------------------------------------------------------
(Y)es/(N)o:

接着重要的部分来了,在DNS记录中添加一个 _acme-challenge 前缀的域名 TXT记录,记录的内容为中间显示的随机码xVloe7V1kMEd2ZlOLlUxv-HltYfTDaMhrrwKjFU47DU

-------------------------------------------------------------------------------
Please deploy a DNS TXT record under the name
_acme-challenge.minirplus.com with the following value:
 
xVloe7V1kMEd2ZlOLlUxv-HltYfTDaMhrrwKjFU47DU
 
Before continuing, verify the record is deployed.
-------------------------------------------------------------------------------
Press Enter to Continue

接着确保当前域名的根记录 minirplus.comA记录 并且指向当前服务器IP(这条原本不成问题,因为国外的服务商的DNS根域名只能添加A记录,但是国内的DNSPOD则更加灵活,可以添加CNAME记录,所以会在认证的时候出现问题)

按回车,进行认证

等待片刻,出现如下信息,说明认证成功

申请操作成功后, 会在界面中输出证书的存放路径, 以及证书的到期时间 (90天)

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/minirplus.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/minirplus.com/privkey.pem
   Your cert will expire on 2018-06-19. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot-auto
   again. To non-interactively renew *all* of your certificates, run
   "certbot-auto 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
    • *

证书的存放路径

( 这里以example.com为例 )

生成证书中会创建 /etc/letsencrypt 文件夹, 证书文件默认存放在 /etc/letsencrypt/live/example.com 文件夹中, 其中 example.com 取自第一个域名
example.com 文件夹中包含 4 个文件 ./cert.pem ./chain.pem ./fullchain.pem ./privkey.pem

  • cert.pem 域名证书
  • chain.pem 根证书及中间证书
  • fullchain.pem 由 cert.pem 和 chain.pem 合并而成
  • privkey.pem 证书私钥

创建一个 2048 位的 Diffie-Hellman 文件
(nginx 默认使用 1024 位的 Diffie–Hellman 进行密钥交换, 安全性太低)

openssl dhparam -out /etc/letsencrypt/live/dhparams.pem 2048
    • *

nginx TSL 配置 (强制http重定向到https)

( 这里以example.com为例 )

首先对 http 协议进行 301 重定向到 https 协议

server {
  listen 80;
  server_name example.com www.example.com;
  return 301 https://example.com$request_uri;
}

nginx https 相关配置

( 这里以example.com为例 )

server {
    listen 443 ssl;
    server_name example.com www.example.com;
    
    # 配置站点证书文件地址
    ssl_certificate      /etc/letsencrypt/live/example.com/fullchain.pem;
    # 配置证书私钥
    ssl_certificate_key  /etc/letsencrypt/live/example.com/privkey.pem;
    
    # 配置 Diffie-Hellman 交换算法文件地址
    ssl_dhparam          /etc/letsencrypt/live/dhparams.pem;
    
    # 配置服务器可使用的加密算法
    ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS';

    # 指定服务器密码算法在优先于客户端密码算法时,使用 SSLv3 和 TLS 协议
    ssl_prefer_server_ciphers  on;
    
    # ssl 版本 可用 SSLv2,SSLv3,TLSv1,TLSv1.1,TLSv1.2 
    # ie6 只支持 SSLv2,SSLv3 但是存在安全问题, 故不支持
    ssl_protocols        TLSv1 TLSv1.1 TLSv1.2;
    
    # 配置 TLS 握手后生成的 session 缓存空间大小 1m 大约能存储 4000 个 session
    ssl_session_cache          shared:SSL:50m;
    # session 超时时间
    ssl_session_timeout        1d;
    
    # 负载均衡时使用 此处暂时关闭 详情见 https://imququ.com/post/optimize-tls-handshake.html 
    # 1.5.9 及以上支持
    ssl_session_tickets off;
    
    # 浏览器可能会在建立 TLS 连接时在线验证证书有效性,从而阻塞 TLS 握手,拖慢整体速度。OCSP stapling 是一种优化措施,服务端通过它可以在证书链中封装证书颁发机构的 OCSP(Online Certificate Status Protocol)响应,从而让浏览器跳过在线查询。服务端获取 OCSP 一方面更快(因为服务端一般有更好的网络环境),另一方面可以更好地缓存 以上内容来自 https://imququ.com/post/my-nginx-conf-for-wpo.html
    # 1.3.7 及以上支持
    ssl_stapling               on;
    ssl_stapling_verify        on;
    # 根证书 + 中间证书
    ssl_trusted_certificate    /etc/letsencrypt/live/example.com/fullchain.pem;
    
    # HSTS 可以告诉浏览器,在指定的 max-age 内,始终通过 HTTPS 访问该域名。即使用户自己输入 HTTP 的地址,或者点击了 HTTP 链接,浏览器也会在本地替换为 HTTPS 再发送请求 相关配置见 https://imququ.com/post/sth-about-switch-to-https.html
    add_header Strict-Transport-Security max-age=60;
    
    # 在此填写原本 http 协议中的配置
}

以上配置完成后, 重启 nginx 即可完成对 https 的切换
(如遇权限问题请使用sudo)

service nginx restart

或者

sudo systemctl reload nginx
    • *

更新证书

certbot生成的证书是有90天期限的。
使用以下命令即可进行 续期, 续期成功后需要服务器

./certbot-auto renew

该命令只会对快到期的证书才会进行更新, 如果希望强制更新, 可以增加 --force-renewal 参数

    • *

修改Apache配置文件

进入/etc/apache2/sites-available,修改泛域名配置文件(这里以000-default.conf为例),添加SSL配置,将下面配置中的SSL证书地址,替换为之前成功获取的证书地址(如直接使用以下配置,请修改DocumentRoot和Directory目录为泛域名指向的目录)

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/vps
    ServerSignature Off
    <Directory /var/www/vps >
        Options -Indexes
    </Directory>
</VirtualHost>
 
<IfModule mod_ssl.c>
    <VirtualHost *:443>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/vps
        ServerSignature Off
        <Directory /var/www/vps >
            Options -Indexes
        </Directory>
        SSLEngine on
        SSLCertificateFile /etc/letsencrypt/live/minirplus.com/fullchain.pem;
        SSLCertificateKeyFile /etc/letsencrypt/live/minirplus.com/privkey.pem
    </VirtualHost>
</IfModule>
    • *

效果

当用户访问任意域名,例如 https://xVloe7V1kMEd2ZlOLlUxv.minirplus.com

都会看到绿色的HTTPS连接标志。

HTTPS连接.png

总结

有了泛域名证书之后有几个好处

  1. 只要申请一次,所有子域名都可以使用,再也不用重复申请证书了。
  2. 用户随机生成的子域名也可以使用HTTPS访问了。

Know more

作为Let’s Encrypt方案的对比,Godaddy的SSL证书价格为

单域名:HK$493.00/年

泛域名:HK$1,944.00/年


Original url: Access
Created at: 2019-02-14 11:03:26
Category: default
Tags: none

请先后发表评论
  • 最新评论
  • 总共0条评论