免费永久HTTPS(ssl)证书——Let's Encrypt来了

大司徒 / 2024-10-17 / 原文

什么是Let's Encrypt

Let’s Encrypt是一个非盈利的,免费的CA,可以提供免费HTTPS认证服务。 提供了一套完整的工具,基于这套工具,我们可以免费来搭建HTTPS网站。 详情可参见它的官网:

https://letsencrypt.org/

 

为什么使用Let’s Encrypt

国内有许多机构可以提供免费的SSL证书,但是一般只有一年的免费服务。Let’s Encrypt可以基于cron可以实现定时更新证书,从而实现永久免费的目的。

如何使用Let's Encrypt配置HTTPS证书

查阅Let’s Encrypt官网可以发现我们应该安装Certbot工具来配置HTTPS证书。

介绍certbot安装方式的网站如下:

certbot.eff.org/instruction…

它的文档还是非常人性化的,选择好系统后,会自动展示对应系统的安装文档,这里我选的系统是:Nginx + CentOS 7

安装

#安装snapd软件
sudo yum install snapd
sudo systemctl start snapd
sudo systemctl enable --now snapd.socket
sudo ln -s /var/lib/snapd/snap /snap

#使用snapd安装certbot
snap install --classic certbot
ln -s /snap/bin/certbot /usr/bin/certbot

#使用certbot生成证书
sudo certbot certonly --nginx
#(1)输入邮箱 (Enter 'c' to cancel): 
abc123@foxmail.com
#(2) 注册服务  You must agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: y
#(3) 输入有生成的证书域名,多个用空格分开 Please enter the domain name(s) you would like on your certificate (comma and/or space separated) 
(Enter 'c' to cancel): www.xxx.cn

#生成成功后命令行会显示证书文件在:
/etc/letsencrypt/live/www.xxx.cn/fullchain.pem;
/etc/letsencrypt/live/www.xxx.cn/privkey.pem;

#卸载
#yum安装,使用下面命令进行移除
sudo yum remove certbot

配置nginx

server {
    listen       443 ssl;
    server_name  www.xxx.cn;

    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Headers X-Requested-With;
    add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

    ssl_certificate      /etc/letsencrypt/live/www.xxx.cn/fullchain.pem;
    ssl_certificate_key  /etc/letsencrypt/live/www.xxx.cn/privkey.pem;
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout  10m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    
    root         /usr/share/nginx/html;
}
    

更新证书

#测试续期
sudo certbot renew --dry-run

#为了尽量确保证书不失效,我们配置一下定时任务即可更新证书并重启nginx。
0 0 * 1 * sudo certbot renew && nginx -s reload

添加新的证书

sudo certbot certonly --nginx --domains xxx.xxxxx.com