ubuntu 14.04 Let's Encrypt永久免费SSL证书安装教程

一、Let’s Encrypt简介
  • 如果要启用HTTPS,我们就需要从证书授权机构(以下简称CA) 处获取一个证书,Let’s Encrypt 就是一个 CA。 Let’s Encrypt 最初是由EEF 电子前哨基金会、 Mozilla 基金会和美国密歇根大学成立了一个公益组织叫 ISRG ( Internet Security Research Group ),为了加速https推广,在2015 年开始推出的免费证书。后来又有思科(全球网络设备制造商执牛耳者)、 Akamai 加入,甚至连 Linux 基金会也加入了合作,这些大牌组织的加入保证了这个项目的可信度和可持续性。
二、官方客户端certbot
  • 为了方便获取https证书,ISRG 的发起者 EFF (电子前哨基金会)为 Let’s Encrypt 项目发布了一个官方的客户端 Certbot ,利用它可以完全自动化的获取、部署和更新安全证书。根据web server和操作系统选择对应的教程
三、客服端certbot的两种工作方式:standalone和webroot
  • standalone 方式: certbot 会自己运行一个 web server 来进行验证。如果我们自己的服务器上已经有 web server 正在运行 (比如 Nginx 或 Apache ),用 standalone 方式的话需要先关掉它,以免冲突。
  • webroot 方式: certbot 会利用既有的 web server,在其 web root目录下创建隐藏文件, Let’s Encrypt 服务端会通过域名来访问这些隐藏文件,以确认你的确拥有对应域名的控制权。
四、安装cerbot命令
$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:certbot/certbot
$ sudo apt-get update
$ sudo apt-get install certbot
五、standalone方式获取ssl证书
sudo service haproxy stop
sudo certbot certonly --standalone -d www.domain.com
sudo service haproxy start
六、webroot方式获取ssl证书
  • haproxy将访问静态文件.well-known,转到nginx配访问
  • 采用haprox插件haproxy-acme-validation-plugin访问静态文件,大概是使用lua开启一个web 服务器来访问静态文件。
  • haproxy配置
frontend http_80_in
    acl app_letsencrypt path_beg /.well-known/acme-challenge/
    use_backend letsencrypt_backend if app_letsencrypt
backend letsencrypt_backend
    http-request set-header Host letsencrypt.requests
    dispatch 127.0.0.1:8000
  • nginx配置
# nginx关闭监控80端口,防止和haproxy冲突
server {
    listen 8000;
    server_name letsencrypt.requests;
    root /var/www/live;
}
  • 生成ssl证书
sudo mkdir /etc/haproxy/ssl
sudo mkdir /var/www/live
sudo service nginx start
sudo certbot certonly --webroot -w /var/www/live -d www.domain.com \
    --agree-tos \
    --email test@163.com

sudo chmod 644 /etc/letsencrypt/live   
sudo service nginx stop 
七、因为使用cerbot生成的ssl证书有效期是90天,需要定时更新ssl证书
#!/bin/bash

# Directory where the acme client puts the generated certs
LETSENCRYPT_CERT_OUTPUT=/etc/letsencrypt/live

# Create or renew certificate for the domain(s) supplied for this tool
certbot renew --dry-run --pre-hook "service nginx start" --post-hook "service nginx stop"

# Cat the certificate chain and the private key together for haproxy
# for path in $(find $LETSENCRYPT_CERT_OUTPUT/* -type d -exec basename {} \;); do
  # cat $LETSENCRYPT_CERT_OUTPUT/$path/{fullchain.pem,privkey.pem} > /etc/haproxy/ssl/${path}.pem
for path in $(ls -d $LETSENCRYPT_CERT_OUTPUT/* | grep -o -P "[\w\.-]*$"); do
  cat $LETSENCRYPT_CERT_OUTPUT/$path/fullchain.pem $LETSENCRYPT_CERT_OUTPUT/$path/privkey.pem > /etc/haproxy/ssl/${path}.pem
done
  • 设置定时任务更新,每周一凌晨2点执行
0 2 * * 1 sudo sh /etc/letsencrypt/letsencrypt-passworks.sh >> /var/log/letsencrypt/letsencrypt-passworks.log 2>&1

猜你喜欢

转载自blog.csdn.net/tianjiewang/article/details/79627941
今日推荐