SSL免費證書之Let’s Encrypt


官網:https://letsencrypt.org/zh-cn

 

官網建議使用Certbot的方式進行安裝,所以首先我們需要安裝Certbot

Certbot)

  官網:Certbot (eff.org)

 

 選擇運行的機器與系統的對應信息,選擇之后頁面會進行跳轉,我這邊使用的是Centos8+NGINX, 會跳轉到頁面:Certbot - Centosrhel8 Nginx (eff.org)

 1.需要登錄服務器

2.安裝snapd工具(Installing snap on CentOS | Snapcraft documentation

1)查看Centos版本

cat /etc/centos-release

2)添加epel源

$ sudo dnf install epel-release
$ sudo dnf upgrade

3)安裝snapd

sudo yum install snapd

#加入systemd管理進程
sudo systemctl enable --now snapd.socket

# 添加classic支持
sudo ln -s /var/lib/snapd/snap /snap

 

安裝完成之后, 需要新打開一個終端, 重新進入, 即可使用snapd相關命令

 

3.安裝snap之后,要升級到最新的版本

sudo snap install core; sudo snap refresh core

 

4.移除老的之前安裝過的Certbot工具刪除掉

sudo apt-get remove certbot

sudo dnf remove certbot

sudo yum remove certbot

 

5.安裝Certbot

sudo snap install --classic certbot

創建軟鏈接:

sudo ln -s /snap/bin/certbot /usr/bin/certbot

 

6.對nginx生成證書

certbot --nginx

#如果配置不是默認的位置 (/etc/nginx or /usr/local/etc/nginx),需要手動指定配置文件所在目錄
certbot certonly --nginx --nginx-server-root=/root/nginx/conf

 

正常的話,就順利生成了證書與Key:

 

 

進行nginx配置:

server    {
    listen                          80;
    server_name                     xx.xx.com;
    access_log                      /data/log/nginx/access_xx.xx.com_80.log main buffer=10k flush=5s;
    error_log                       /data/log/nginx/error_xx.xx.com_80.log warn;
   
    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP    $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

}

server {
    listen        443 ssl;
    server_name   xx.xx.com;
    access_log    /data/log/nginx/access_xx.xx.com_443.log main;
    error_log     /data/log/nginx/error_xx.xx.com_443.log warn;

    ssl_certificate     /etc/letsencrypt/live/xx.xx.com/fullchain.pem;
    ssl_certificate_key    /etc/letsencrypt/live/xx.xx.com/privkey.pem;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    
    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP    $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

}

然后重新加載配置文件即可:

nginx -t

nginx -s reload

 

7.測試證書到期自動更換

 

sudo certbot renew --dry-run

 

這個時候  就可以使用https訪問一下啦~~~

 

注意點:

1.nginx需要支持SSL

nginx -V

查看是否加載模塊: --with-http_ssl_module

 

 

 

 

 

 

參考:

 


免責聲明!

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



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