首页 > Linux操作系统 > Linux操作系统 > Certbot的使用指南
1.certbot安装和配置
$ wget
$ chmod a+x ./certbot-auto
$ ./certbot-auto
这个小工具会自动下载并安装相关依赖和 Python 包。稍等一下就完成了。
2. 生成证书
生成证书过程中需要鉴权。有多种方式,比如 webroot 、 standalone 、 apache、 nginx 、 manual 等。我使用过前两种。 这两种中,简单一点的是 standalone。不过,这种方式需要把现有的 WebServer 停掉,因为这种方式下 certbot 需要占用 80 端口。
./certbot-auto certonly --text --agree-tos --email XXX@qq.com --standalone -d evtrust.com -d
-d 参数指定域名,可多个。一般第一个是主域名。
webroot 方式稍微繁琐一些,但好处是不需要关停现有的 WebServer 。此方法需要在域名对应的根目录下新建 .well-known
目录并写入若干文件供验证服务访问。 因此需要配置 WebServer 允许外部访问 路径。配置方法请参考相应 WebServer 的
文档。Nginx 的默认配置应该不用修改,Apache 就不知道了。 另外,不同的域名的根路径可能不同,下面的例子中可以看
到为不同的域名指定不同的根路径。
# ./certbot-auto certonly --text --agree-tos --email webmaster@excample.com --webroot -w /var/www
/example -d example.com -d -w /var/service/example -d evtrust.com
若是报错: Could not bind to IPv4 or IPv6.
就先执行 lsof -i:80 把80端口的占用进程杀掉,再生成证书
3. 修改 WebServer 配置以提供 HTTPS 服务
这里以 Nginx 作例子吧。
打开 Nginx 的配置文件(默认为: /etc/nginx/nginx.conf ),在需要提供 HTTPS 的 server 下做如下配置,并把 listen 80;
删掉:
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /etc/letsencrypt/live/oevtrust.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/evtrust.com/privkey.pem;
location / {
proxy_pass
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
root html;
index index.html index.htm;
}
}
具体的参数定义,参考上一篇博文 nginx的安装和配置
4. 定期 renew
Let's Encrypt 的证书有效期为 90 天,所以需要在到期前 renew 一下证书。 使用以下命令即可。
# ./certbot-auto renew --text --agree-tos --email webmaster@excample.com --webroot -w /var/www
/example -d example.com -d -w /var/service/example -d evtrust.com
或者直接运行以下命令,此时 certbot 会使用默认参数(此例为: /etc/letsencrypt/renewal/example.com.conf ):
# ./certbot-auto renew
注意:若是更新不成功,可以使用强更:
./certbot-auto renew --force-renew
如果希望将其锁定到特定版本并且不接收自动更新,只需在命令后加 --no-self-upgrade 即可。即:
./certbot-auto renew --no-self-upgrade
又或者在 crontab 里加入定时任务,每隔 80 天的凌晨 4 点执行一次 renew:
0 4 */80 * * ~/certbot-auto renew &>> /dev/null
sudo service cron restart
重启完定时生效。大部分浏览器都是会信任的证书。
启用 HTTPS 之后,会增加一点对服务器计算资源的占用,但是这非常值得。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/31546910/viewspace-2940732/ ,如需转载,请注明出处,否则将追究法律责任。