記一次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 xx.vip www.xx.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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM