1. 程式人生 > >Linux下certpot 免費搭建https協議

Linux下certpot 免費搭建https協議

centos配置Let's Encrypt並自動更新

假如就放在/home

wget https://github.com/certbot/certbot/archive/master.zip

unzip master.zip

cd certbot-master/

./certbot-auto --help

./certbot-auto certonly --webroot --agree-tos -v -t --email loonghereqq.com -w /var/www/vduok.com/mch/web -d mch.vduok.com

如果報錯,再執行一遍

email為郵箱,-w後面為網站目錄,-d後面為網站域名

然後生成的證書在/etc/letsencrypt/live/

編輯nginx配置檔案,不同框架,需要改不同的東西,大同小異

server {

listen 443;

server_name mch.vduok.com;

ssl on;

root /var/www/vduok.com/merchant/web;

index index.html index.php;

ssl_certificate "/etc/letsencrypt/live/mch.vduok.com/fullchain.pem";

ssl_certificate_key "/etc/letsencrypt/live/mch.vduok.com/privkey.pem"

;

ssl_session_cache shared:SSL:1m;

ssl_session_timeout  10m;

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;

ssl_prefer_server_ciphers on;

location / {

try_files $uri $uri/ /index.php$is_args$query_string;

}

location ~ \.php$ {

try_files $uri =404;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;

}

location /storage/ {

alias /var/www/vduok.com/storage/;

}

}

server {

server_name mch.vduok.com;

location / {

rewrite (.*) https://mch.vduok.com$1 permanent;

}

}

service nginx reload

即可完成SSL的配置,有效期3個月,快到期會自動往上面的郵箱發郵件,後臺renew續期即可

/home/certbot-master/certbot-auto renew

完成續期

加入定時任務,設定了每週一凌晨4點30自動更新證書,並自動重啟nginx服務,證書在到期前30天內才能更新,多餘的更新會自動忽略掉的,每週更新還有一個好處是更新可能會失敗,這樣最多還有4次的嘗試機會來保證不會過期.

建立指令碼 renew-cert.sh

#!/bin/bash

/home/certbot-master/certbot-auto renew

/sbin/service nginx reload

儲存指令碼,並給予可執行許可權

chmod a+x renew-cert.sh

寫入定時任務   crontab -e

30 4 * * 1 /home/renew-cert.sh >> /home/renew-cert.log 2>&1

儲存並重啟crontd

service crond restart

完成自動更新證書