前言
之前一直想在nginx中配置网站使用https协议访问,
第一、https协议更加安全,
第二、自己也想学习配置一下。
一、letsencrypt介绍及安装过程
1、letsencrypt官方地址
https://letsencrypt.org/
2、下载脚本
git clone https://github.com/certbot/certbot.git
3、安装命令
进入到certbot解压的文件夹
./certbot-auto certonly --standalone --email xxxxx@qq.com -d xxx.com -d www.xxx.com -d xxx.xxx.com -d xxx.xxx.com
-d :参数为你要生成证书的域名,支持一次性为多个域名生成
还有其它的安装方式,可参考官网上的 https://github.com/certbot/certbot.git
4、证书生成位置
/etc/letsencrypt/live/xxx.com/
5、证书生成后,可如下配置nginx即可
server{
listen 443 ssl;
server_name blog.lifefun.in;
proxy_read_timeout 60;
proxy_send_timeout 60;
ssl_certificate /etc/letsencrypt/live/xxx.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/xxx.com/privkey.pem;
access_log /root/xxx/solo.lifefun.com_access.log main;
error_log /root/xxx/solo.lifefun.com_error.log;
location /
{
proxy_pass xxx.xxxx.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
client_max_body_size 100m;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
二、过程中的trouble
我是在cent os 6.9上面安装的
1、在执行安装命令的时候,提示certbot-auto 不存在
解决方式:安装certbot-auto
2、提示找不到依赖库
可能我这里是因为yum源的问题,所以我这里的解决方式;替换为阿里的yum源
步骤:
(1)备份原来的:mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
(2) 下载:wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
(3) yum clean all,yum makecache 生成缓存
三、certbot证书过期问题
由于certbot证书有个过期时间(90天),一般是到期前一个月才允许我们续期,因此我们需要手动更新或者脚本自动更新
1、手动:重新执行一次命令
./certbot-auto certonly --no-self-upgrade --standalone --email xxxxx@qq.com -d xxx.com -d www.xxx.com -d xxx.xxx.com -d xxx.xxx.com
2、定时:采用了linux自带的crontab,一般默认会自带
(1)我是直接执行 crontab -e
在里面编辑,添加需要执行的任务:
2 1 2 * * /root/xxxxxx/xxxxx/certbot-auto --no-self-upgrade renew --pre-hook "/root/nginx/xxxxxxxx/sbin/nginx -s stop" --post-hook "/root/nginx/xxxxxxxx/sbin/nginx -c /root/nginx/xxxxxxxxx/conf/nginx.conf" > /root/logs/xxxxxxxx/crontab-encrypt.log 2>&1 &
我这里是每月2号凌晨1点2分执行,并且将日志写入crontab-encrypt.log
(2)也可以在/etc/crontab 里面编辑,添加(1)中的执行命令即可
(3)必须记得重启定时服务
重启:
service cronb restart
#如果是centos7 版本,
/bin/systemctl restart crond.service
启动:
service cronb start
#如果是centos7 版本,
/bin/systemctl start crond.service
停止:
service cronb stop
#如果是centos7 版本,
/bin/systemctl stop crond.service
(4)完成,到这里证书就会自动续期了
四、期间问题:
(1)没有启动定时服务,导致到点未执行
(2)没有记录日志,不知道是否执行成功
(3)必须先暂停nginx,然后执行更新命令,在重启nginx。(我这里碰到的问题是,执行更新时,端口nginx被占用)