
当下的互联网,包括网站、APP、小程序、API接口等都被要求HTTPS协议加密!同时,浏览器厂家对于依旧使用的http的网站直接标注为不安全网页!这会导致用户对网站的不信任,导致用户的流失。所以安装证书还是很有必要的,认证级别越高、覆盖范围越广的证书,价格越贵。今天给大家推荐一个免费的HTTPS证书 –
Let's Encrypt
,非常适合个人网站、以及个人博客。
安装Let’s Encrypt证书
获取certbot-auto
下载certbot-auto
wget https://dl.eff.org/certbot-auto
授权
chmod a+x certbot-auto
申请证书
关闭web服务器
生成证书前需要暂时关闭web服务器
service nginx stop
或
./nginx -s stop
注:Certbot 会启动一个临时服务器来完成验证(会占用80端口或443端口,因此需要暂时关闭 Web 服务器)
申请证书
常规申请
./certbot-auto certonly
快速申请
./certbot-auto certonly --standalone --email xxx@xxx.com --agree-tos -d xxx.com -d www.xxx.com
注:
--email
后面跟证书申请人的邮箱
-d
后面跟域名,多个域名请用多个-d,参上
配置证书
Let’s Encrypt的证书链文件和私钥文件默认是在/etc/letsencrypt/live
目录下,并且按照申请的域名进行分类。
Nginx中配置SSL证书的配置文件参考如下:
server {
listen 443 ssl;
server_name xxx.com;
location / {
# ....
}
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 / {
# ...
}
#如果需要把http强制转换为https,需要配置以下内容
if ($host = xxx.com) {
return 301 https://$host$request_uri;
}
}
配置证书自动续期
Let’s Encrypt证书的有效期为3个月,手动续期太麻烦,所以可配置自动续期的定时任务。
操作linux系统自带的定时任务
sudo crontab -e
直接配置如下:
以下是每月的1号凌晨1点执行
0 1 1 * * /home/application/certbot-auto renew --renew-hook "sudo nginx -s reload"
查看是否配置成功
sudo crontab -l
当然,对于技术小白用户可能有点难度,需要安装证书,可联系本人QQ(768394818),有偿服务。