centos nginx申请免费https证书

手机游戏开发者 2024-10-4 18:47:47 30 0 来自 中国
前言

各大厂有免费证书申请渠道,但是必要设置DNS,某些情况下无法掌控域名的DNS剖析,Let's Encrypt可以很好的办理这个题目,只要你有网站的管理权就能搞定,不外证书的有用期是三个月,必要搞一个定时使命隔一段时间申请一次。
利用步调

1.安装certbot

##安装前置依赖yum install -y epel-releaseyum install python2-certbot-nginx##安装certbotyum install -y certbot2.设置nginx

不管你的nginx是否已投入使用(投入使用的意思是已经在生产情况运行)均必要设置以下内容
### 如果已经设置了root,请使用alias,不外alias与root的剖析方式有差异,留意文件夹的层级location ^~/.well-known/acme-challenge/{        default_type "text/plain";        root /opt/ylbzj/nginx/html;}如果你的nginx是负载状态,你只必要在此中一台呆板上安装certbot,另一台呆板设置转发就行,假设你安装certbot的呆板ip为10.10.0.1,另一台呆板的nginx设置为
location ^~/.well-known/acme-challenge/{        proxy_pass http://10.10.0.1/.well-known/acme-challenge/;}3.天生key

## --nginx表现天生nginx的key## -w指的是根目次,即root指向的目次## -d是域名## -m是邮箱certbot certonly --nginx -w /opt/nginx/html -d yhwch.com -m zhiaiyahong@yhwch.com4.定时使命天生key

单机的nginx使用下面这个脚本就够用了,使用方式是:先手动天生一次证书,然后手动实行sh certbotCron.sh(假设你将脚本定名为这个,而且存储在根目次下),会主动将定时使命加到crontab中,而且不会影响现有的crontab使命,背面等着定时使命自己实行就行,有条件的最好加个关照。证书更换完成后必要重启nginx否则新证书不会收效!
#!/bin/bashecho $(date +"%Y-%m-%d %H:%M:%S")"证书革新开始实行======================="result=$(certbot renew)#result="- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Processing /etc/letsencrypt/renewal/ylbzj.hebei.gov.cn.conf - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - The following certificates are not due for renewal yet: /etc/letsencrypt/live/ylbzj.hebei.gov.cn/fullchain.pem expires on 2022-07-10 (skipped) No renewals were attempted. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"echo "------------重置证书周期效果为---------"echo $resultecho "-----------效果数据竣事------------"renewisok=$(echo ${result#*are})renewisok2=$(echo ${renewisok%yet*})echo "------------字符分割效果为----------"echo $renewisok2echo "------------分割竣事------------"if [ "${renewisok2}" = "not due for renewal" ];thendata1=$(echo ${renewisok#*on})data2=$(echo ${data1%(skipped*})echo "还没到期,不能更新,到期日期为"$data2crondata=$(date -d "$data2 -25 day" +%Y-%m-%d)echo "crontab 实行日期为"$crondatayue=$(date -d "$crondata" +%m)ri=$(date -d "$crondata" +%d)cronstr=$(echo "0 0 "$ri" "$yue"* sh /root/certbotCron.sh >> ~/log_certbot_"$crondata".log")echo $cronstr > ~/cronconf$(sed -i 's/*/ */g' ~/cronconf)$(crontab -l |grep -v $crondata >> ~/cronconf)$(crontab ~/cronconf)echo "crontab 使命列表如下:"crontab -lelseecho "重启1号服务器nginx"$(/opt/ylbzj/nginx/sbin/nginx -s reload)echo "到期了,已经更新,开始拷贝文件至2号服务器,假设呆板为10.10.0.2而且做好了免密登录"suffix=$(date +"%Y%m%d")zipfilename="letsencrypt_"$suffix".tar.gz"echo "压缩文件名称为:"$zipfilenamecd /etczipcommand=$(tar -czvf $zipfilename letsencrypt)kaobei=$(scp -r $zipfilename root@10.10.0.2:/etc)ssh root@172.20.191.55 "cd /etc;tar -zxvf $zipfilename -C /etc;/opt/ylbzj/nginx/sbin/nginx -s reload"echo "2号服务器文件拷贝完成,再次实行脚本添补下一个周期的定时使命"sh ~/certbotCron.shfiecho $(date +"%Y-%m-%d %H:%M:%S")"证书革新实行竣事======================="
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-10-18 16:48, Processed in 0.151279 second(s), 32 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表