专注于WEB前端开发, 追求更好的用户体验, 更好的开发体验 [长沙前端QQ群:234746733]

使用letsencrypt自动生成和续期HTTPS证书

相信一些同学注意到了, 本站启用了https, 域名改成了xhl.me, 服务也早就全都改用docker了.
顺带介绍下之前搞的两个个docker项目: alpine-tengine, docker-backup.

*本文主要介绍letsencrypt证书的创建和通配符HTTPS证书生成, 还有基于letsencrypt docker镜像实现证书自动续期.

Let's Encrypt 是一个自动签发免费HTTPS证书的非营利机构, 生成的证书有3个月有效期, 到期可以免费续期.
Certbot 是 Let's Encrypt 官方推荐的生成证书的客户端工具, 文档.

使用letsencrypt-dns生成证书(支持通配符域名证书), 并自动续期

letsencrypt-dns 集成了: certbot(证书生成命令行工具), Lexicon(DNS记录修改工具, 主流DNS服务商都支持).
另外还自带了crontab 定时任务, 只要服务启动, 就会自动续期域名了.

1. 创建, /etc/letsencrypt/domains.conf, 内容类似这样:
*.domnain1.com domnain1.com
*.domnain2.com domnain2.com
2. 根据DNS PROVIDER信息验证, 并自动创建和续期证书

下面的例子是拿dnspod举例. LEXICON支持主流的DNS服务, 所有支持的列表.

docker run -it --rm --name letsencrypt2 \
    -v /etc/letsencrypt:/etc/letsencrypt \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -e 'LETSENCRYPT_USER_MAIL=yourmail@gmail.com' \
    -e 'LEXICON_PROVIDER=dnspod' -e 'LEXICON_DNSPOD_USERNAME={dnspod_api_id}' -e 'LEXICON_DNSPOD_TOKEN={dnspod_api_token}' \
    adferrand/letsencrypt-dns

如果是域名在godaddy或cloudflare, 可以这样:

  • godaddy API:
    -e 'LEXICON_PROVIDER=godaddy' -e 'LEXICON_GODADDY_KEY={{godaddy_key}}' -e 'LEXICON_GODADDY_SECRET={{godaddy_secret}}'
  • Cloudflare API:
    -e 'LEXICON_PROVIDER=cloudflare' -e 'LEXICON_CLOUDFLARE_USERNAME={{cloudflare_email}}' -e 'LEXICON_CLOUDFLARE_TOKEN={{cloudflare_api_key}}'
3. 进阶: 续期后执行其他命令(比如重启Nginx)

/etc/letsencrypt/domains.conf, 每行末尾可以加autocmd命令, 在续期后执行某个容器的shell命令, 比如:
*.domnain1.com domnain1.com autocmd-containers=nginx:nginx -s reload
续期后, 在容器名为nginx的容器中会执行: nginx -s reload
另外, domains.conf最好保持最后一行有个换行(空行), 有时发现最后一行的域名没读到.

4. 进阶: 使用docker-compose

创建 /compose/letsencrypt/docker-compose.yml, 例子(配置是godaddy, 按情况修改):

version: '3'
services:
  letsencrypt:
    container_name: letsencrypt
    image: adferrand/letsencrypt-dns
    volumes:
      - /etc/letsencrypt:/etc/letsencrypt
      - /var/run/docker.sock:/var/run/docker.sock
    environment:
      - LETSENCRYPT_USER_MAIL={{your_mail}}
      - LEXICON_PROVIDER=godaddy
      - LEXICON_GODADDY_KEY={{godaddy_key}}
      - LEXICON_GODADDY_SECRET={{godaddy_secret}}

启动服务执行: cd /compose/letsencrypt; docker-compose up -d --force-recreate -V;

5. 进阶: 开机自启服务

创建 /etc/systemd/system/letsencrypt.service, 内容:

[Unit]
Description=Letsencrypt Container
After=docker.service
Requires=docker.service

[Service]
Type=oneshot
RemainAfterExit=yes
TimeoutStartSec=0
ExecStart=/usr/local/bin/docker-compose up -d --force-recreate
ExecReload=/usr/local/bin/docker-compose up -d --force-recreate
ExecStop=/usr/local/bin/docker-compose stop
WorkingDirectory=/composes/letsencrypt

[Install]
WantedBy=multi-user.target

执行下, 下面的命令就可以成为自启的服务了:

systemctl daemon-reload;
systemctl enable letsencrypt; systemctl restart letsencrypt;

同理, 也可以把其他docker服务做成自启, 比如Nginx.

证书生成后 nginx 的配置

server {
  listen 80;
  listen 443 ssl;
  server_name domnain1.com www.domnain1.com;
  ssl_certificate /etc/letsencrypt/live/domnain1.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/domnain1.com/privkey.pem;
  ssl_session_cache shared:SSL:50m;
  ssl_session_timeout 5m;
  ssl_stapling on;
  ssl_stapling_verify on;

  ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
  ssl_prefer_server_ciphers on;
  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';
  ...
}

可能用到的debug命令

telnet domnain1.com 443
openssl s_client -connect domnain1.com:443
curl -vv -k https://domnain1.com # 测试https
docker exec -it letsencrypt sh # 进入容器执行shell命令
docker logs letsencrypt -f # 查看letsencrypt输出的日志信息

tools

另外, 也可以使用certbot 生成证书, 但是没有letsencrypt-dns方便, 就不介绍了.

/ 分类: 工具,实践 / TrackBackhttps://xhl.me/archives/letsencrypt-ssl/trackback标签: docker, https

添加新评论 »