前言

自己做了个网站,由于增加了用户登录注册功能,想用https保护用户隐私。因此,研究如何启用https,本文即是介绍如何在ubuntu上配合Nginx使用CertBot。

Let’s Encrypt 及 Certbot 简介

Let’s Encrypt 是 一个叫 ISRG ( Internet Security Research Group ,互联网安全研究小组)的组织推出的免费安全证书计划。参与这个计划的组织和公司可以说是互联网顶顶重要的先驱,除了前文提到的三个牛气哄哄的发起单位外,后来又有思科(全球网络设备制造商执牛耳者)、 Akamai 加入,甚至连 Linux 基金会也加入了合作,这些大牌组织的加入保证了这个项目的可信度和可持续性。

安装Certbot

1
2
3
4
5
$ 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 python-certbot-nginx

配置

  • 如果你的域名下有服务在运行,请运行

$ sudo certbot --authenticator webroot --installer nginx

  • 如果没有,你可以先临时关掉nginx。当获取证书后再重启。

$ sudo certbot --authenticator standalone --installer nginx --pre-hook "nginx -s stop" --post-hook "nginx"

  • 过程中需要输入email, domain信息,还有webroot,这个就是你静态文件的地址(例如/var/www/html)

自动更新证书

$ sudo certbot renew --dry-run

  • 成功后效果:
1
2
3
4
5
6
7
8
9
10
-------------------------------------------------------------------------------
Processing /etc/letsencrypt/renewal/your.domain.com.conf
-------------------------------------------------------------------------------
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates below have not been saved.)

Congratulations, all renewals succeeded. The following certs have been renewed:
/etc/letsencrypt/live/your.domain.com/fullchain.pem (success)
** DRY RUN: simulating 'certbot renew' close to cert expiry
** (The test certificates above have not been saved.)
  • 可以使用crontab -e的命令来启用自动任务

    1
    2
    3
    sudo crontab -e
    添加配置:
    00 1 * * 1 /usr/bin/certbot renew >> /var/log/le-renew.log
  • 上面的执行时间为:每周一半夜1点执行renew任务。

  • 你可以在命令行执行/usr/bin/certbot renew >> /var/log/le-renew.log看看是否执行正常,如果一切OK,那么我们的配置到此结束