记一次nginx使用Let's Encrypt配置HTTPS

Let’s Encrypt是一个免费,自动化,和 公开 的证书颁发机构,并且可以通过脚本实现证书的更新。

 

本次使用的是阿里云的centos服务器

前提条件

服务器需要有python和git

检查python 

python --version 

检查git

git --version

生成证书

1、从git上clone encrypt

git clone https://github.com/certbot/certbot

2、进入到certbot的目录生成证书

./letsencrypt-auto certonly --standalone --email xiaoweiv@yeah.net -d xx.vip -d www.xx.vip

Let's Encrypt可以支持多域名的配置

执行成功的情况下,会在/etc/letsencrypt/live/xx.vip目录下生成四个文件 cert.pem  chain.pem  fullchain.pem  privkey.pem

cert.pem - Apache服务器端证书
chain.pem - Apache根证书和中继证书
fullchain.pem - Nginx所需要ssl_certificate文件
privkey.pem - 安全证书KEY文件

配置nginx

1、进入nginx配置文件目录

/usr/local/nginx/conf

2、修改nginx.conf文件

 1 # HTTPS server
 2     #
 3     server {
 4         listen       443;
 5         server_name  iloveqing.vip www.iloveqing.vip;
 6  ssl on; 7 8 ssl_certificate "/etc/letsencrypt/live/xx.vip/fullchain.pem"; 9 ssl_certificate_key "/etc/letsencrypt/live/xx.vip/privkey.pem"; 10 11  ssl_session_cache shared:SSL:1m; 12  ssl_session_timeout 5m; 13 14 ssl_ciphers HIGH:!aNULL:!MD5; 15  ssl_prefer_server_ciphers on; 16 17 location / { 18 root html/love; 19  index index.html index.htm; 20  } 21 location /zuimeidenvren { 22  root html; 23  index index.html index.htm; 24  } 25 26 }

至此,我们就完成了HTTPS的配置工作。重新加载nginx的配置使其生效

/usr/local/nginx/sbin/nginx -s reload

 

设置自动更新证书

1、编写更新的shell脚本

 1 #!/bin/sh
 2 
 3 #先停止nginx
 4 . /etc/init.d/functions
 5 
 6 NGINX_DIR="/usr/local/nginx"
 7 NGINX="${NGINX_DIR}/sbin/nginx"
 8 NGINX_CONF="${NGINX_DIR}/conf/nginx.conf"
 9 PROG=$(basename $NGINX)
10 
11 if [ ! -x ${NGINX} ]; then
12     echo -n $"${NGINX} does not exists."; warning; echo
13     exit 5
14 fi
15 echo $PROG
16 echo "stop nginx"
17 pkill $PROG
18 echo "ngix stop success and update ssl "
19 #更新证书 20 /data/git/certbot/certbot-auto renew --force-renew 21 #启动nginx 22 echo "start nginx" 23 $NGINX -c $NGINX_CONF 24 echo "nginx status" 25 pids=`ps -ef|grep nginx` 26 27 if [ "$pids" = "" ] 28 then 29 echo "no nginx pid!" 30 else 31 echo "nginx Id list :$pids" 32 fi 33 exit 0;

2、修改crontab进行定时更新

0 0 10 * * root /etc/letsencrypt/live/xx.vip/updatessl.sh

3、从证书的信息中看,let`s Encrypt的有效期为90天

遇到的问题

1、生成证书时报 Problem binding to port 80: Could not bind to IPv4 or IPv6.. Skipping

  这是因为原来nginx启动的情况下进行证书的生成时,80端口被使用了。停止nginx的服务

2、配置了nginx后,有错误 unknown directive ssl, 提示

  这是在我第一次安装编译nginx时没有配置SSL模块导致的

  (1)进入到nginx的解压目录  cd /data/software/nginx-1.15.0

  (2)执行命令 ./configure --with-http_ssl_module  //重新添加这个ssl模块

  (3)执行make命令(不要执行make install,因为make是用来编译的,而make install是安装,不然你整个nginx会重新覆盖的)。

  (4)备份之前的nginx 

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

    拷贝最新的(如果失败,可以使用 cp -rpf)  

 cp objs/nginx /usr/local/nginx/sbin/nginx

  (5)在nginx的安装目录下 /usr/local/nginx/sbin下执行命令 ./nginx -V

 

参考

https://www.jianshu.com/p/eaad77ed1c1b

转载于:https://www.cnblogs.com/xiaoweiv/p/11022345.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值