使用 Let's Encrypt(Certbot) 配置 HTTPS,自己颁发SSL证书

6 篇文章 0 订阅

之前用的https,都是通过百度云购买的SSL证书。

今天自己查资料捯饬了一下,通过nginx配置http和https转发,还有websocket的转发。

nginx的安装步骤和转发步骤,详见 nginx的安装和配置

下面说一下certbot安装和配置

1. 安装

$ wget https://dl.eff.org/certbot-auto
$ 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 online.XX.top -d online2.XX.top  

-d 参数指定域名,可多个。一般第一个是主域名。

webroot 方式稍微繁琐一些,但好处是不需要关停现有的 WebServer 。此方法需要在域名对应的根目录下新建 .well-known 目录并写入若干文件供验证服务访问。 因此需要配置 WebServer 允许外部访问 http://example.com/.well-known 路径。配置方法请参考相应 WebServer 的文档。Nginx 的默认配置应该不用修改,Apache 就不知道了。 另外,不同的域名的根路径可能不同,下面的例子中可以看到为不同的域名指定不同的根路径。

# ./certbot-auto certonly --text --agree-tos --email webmaster@excample.com --webroot -w /var/www/example -d example.com -d www.example.com -w /var/service/example -d service.ulefa.com

无论使用那种方式,运行以上命令后都会在 /etc/letsencrypt 生成一堆东西,包括证书。

若是报错:  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/online.XX.top/fullchain.pem;
	ssl_certificate_key /etc/letsencrypt/live/online.XX.top/privkey.pem;
    location / {
	    proxy_pass http://online.XX.top:8080/;
	    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 www.example.com -w /var/service/example -d service.ulefa.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 之后,会增加一点对服务器计算资源的占用,但是这非常值得。

 

另外,还写了一篇证书可以长久使用的证书生成方式,有效期暂设置十年,但是浏览器会有不安全提示,参考链接:

https://blog.csdn.net/cocos2dGirl/article/details/92634197

参考链接:https://www.cnblogs.com/ly-radiata/articles/6119374.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值