Let's Encrypt证书安装

申请让我们加密泛域名证书及Nginx / Apache证书配置

让我们加密基础知识

  • 让我们加密是一个认证机构(证书颁发机构= CA)
  • 想使用HTTPS需要认证机构颁发的电子证书
  • 让我们加密和其他认证机构的区别(或者说是卖点):
    • 免费,Let's Encrypt提供期限是90天的免费电子证书
    • 提供工具certbot自动生成电子证书文件
  • 让我们加密为了自动生成电子证书搞了一个ACME协议
    • 自动证书管理环境= ACME,自动认证管理环境协议
    • 协议草案已经提交IETF
    • ACME协议的基本思路是:
      • 在你服务器上生成一次性的随机特征数据(随机数)
      • 然后通过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.com 为A记录并且指向当前服务器IP(这条原本不成问题,因为国外的服务商的DNS根域名只能添加甲记录,但是国内的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的/网站可用,修改泛域名配置文件(这里以000-default.conf为例),添加SSL配置,将下面配置中的SSL证书地址,替换为之前成功获取的证书地址(如直接使用以下配置,请修改的DocumentRoot和目录目录为泛域名指向的目录)

<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连接标志。

总结

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

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

 

转载于:https://www.cnblogs.com/hxqxiaoqi/p/10948735.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值