欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

免费获取HTTPS证书的工具Certbot

最编程 2024-08-12 13:41:24
...

HTTPS是保证一个网站的安全和可靠的基础,一般情况下,要使用HTTPS协议,需要有一张被信任的 CA ( Certificate Authority )也就是证书授权中心颁发的 SSL 安全证书,并且将它部署到你的网站服务器上。一旦部署成功后,当用户访问你的网站时,浏览器会在显示的网址前加一把小锁,表明这个网站是安全的,当然同时你也会看到网址前的前缀变成了 https 。

怎么获得 SSL 安全证书呢?

理论上,我们自己也可以签发 SSL 安全证书,但是我们自己签发的安全证书不会被主流的浏览器信任,所以我们需要被信任的证书授权中心( CA )签发的安全证书。而一般的 SSL 安全证书签发服务都比较贵,比如 Godaddy 、 GlobalSign 等机构签发的证书一般都需要20美金一年甚至更贵,不过为了加快推广 HTTPS 的普及, EEF 电子前哨基金会、 Mozilla 基金会和美国密歇根大学成立了一个公益组织叫 ISRG ( Internet Security Research Group ),这个组织从 2015 年开始推出了 Let’s Encrypt 免费证书。这个免费证书不仅免费,而且还相当好用,所以我们就可以利用 Let’s Encrypt 提供的免费证书部署 HTTPS 了。

获取SSL证书

安装EPEL环境

#因为 Certbot 打包在EPEL中,所以在安装 Certbot 之前要先安装EPEL。    
yum install epel-release -y
#安装nginx
yum install nginx -y

获取certbot客户端

wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto
#验证安装是否成功
./certbot-auto --help

生成证书

cd /usr/bin
./certbot-auto certonly --standalone --email `你的邮箱地址` -d `你的域名地址`

当前网站有多个域名时需在后面增加,例如

./certbot-auto certonly --standalone --email `你的邮箱地址` -d `你的域名1` -d `你的域名2`

生成的证书路径

cd /etc/letsencrypt/live/  

Nginx中配置SSL证书的配置文件参考如下

server {
listen  443 ssl;
server_name xxx.com;
location / {
    # 本地tomcat的代理路径....
    proxy_pass http://localhost:8080;
}
ssl_certificate /etc/letsencrypt/live/xxx.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/xxx.com/privkey.pem;
}
server {
listen  80;
server_name xxx.com;
 location / {
            # ...
        proxy_pass http://localhost:8080;
    }
  #如果需要把http强制转换为https,需要配置以下内容
  if ($host = xxx.com) {
      return 301 https://$host$request_uri;
  } 
}

配置完成后,启动Nginx,浏览器中查看效果。

#保证80端口没有被占用
service nginx start

手动续签

#关闭nginx
systemctl stop nginx

./certbot-auto renew
# 如果提示未到期,cert not due for renewal,可以强制更新如下
./certbot-auto renew --force-renew
# 看到success表示成功了

#开启nginx
systemctl start nginx

自动续签

写一个renew-cert.sh

#!/bin/bash
# 停止nginx
nginx -s stop

# 续签
/root/certbot-auto renew --force-renew

# 重启nginx
nginx -s reload

给所有的用户添加执行renew-cert.sh这个文件的权限

chmod a+x renew-cert.sh

自动更新https证书

0 4 1 */2 * /root/renew-cert.sh >/root/crontab.log 2>&1

当更新证书./certbot-auto renew --force-renew报Couldn’t download https://raw.githubusercontent.com/certbot/certbot/v1.3.0/letsencrypt-auto-source/letsencrypt-auto.发现是阿里云的ECS访问不到github,估计又是GFW的锅,只能修改hosts

1、使用命令打开etc文件夹下面的hosts文件:

vim /etc/hosts

2.增加内容

199.232.4.133 raw.githubusercontent.com

3.重启网络

service network restart

或者

service network restart

或者

/etc/init.d/network restart

参考
https://zhuanlan.zhihu.com/p/80909555
https://www.jianshu.com/p/d88e19c8963b

推荐阅读