Certbot配置Let's Encrypt的https_ssl證書以及過程中出現的問題(2021更新)


起因是有個小程序需要鑲嵌H5,而小程序的請求必須都是https,所以我們需要給域名綁定https

之前一直用letsencrypt-auto也就是certbot-auto來進行自動安裝,但是今天進行安裝的時候出現了一些問題,有關於之前的步驟以及Let’s Encrypt的介紹可以查看之前的https://www.cnblogs.com/shook/p/12790532.html的第二部分

一、未成功安裝起因

[root@j certbot]# ./letsencrypt-auto
Skipping bootstrap because certbot-auto is deprecated on this system.
Upgrading certbot-auto 1.12.0 to 1.13.0...
Replacing certbot-auto...
Your system is not supported by certbot-auto anymore.
Certbot cannot be installed.
Please visit https://certbot.eff.org/ to check for other alternatives.

去certbot官方github上https://github.com/certbot/certbot/releases查了一下原因

起因是官方從1.9.0版本在所有基於Debian或RHEL的系統上,certbot-auto開始棄用,原因是certbot-auto是基於python2編寫的,python 2去年已經GG,certbot團隊又懶得移植到python3(我胡說的),具體看下面官方解釋

在Certbot 1.11.0這個版本我們看到官方給出了解釋https://community.letsencrypt.org/t/certbot-auto-no-longer-works-on-debian-based-systems/139702/7

那么我們現在如何來安裝Certbot

在上面官方也給出啊了解決方案,使用snap來進行安裝

那么什么是snap,簡單介紹一下snap

什么是snap,snap安裝包是Canonical公司發布的全新的軟件包管理方式,它類似一個容器擁有一個應用程序所有的文件和庫,各個應用程序之間完全獨立。所以使用snap包的好處就是它解決了應用程序之間的依賴問題,使應用程序之間更容易管理。但是由此帶來的問題就是它占用更多的磁盤空間。

二、安裝 let'sencrypt

我這里是放在centos下來進行的

先進行安裝依賴等配置

[root@j certbot]# yum install epel-release                 # 安裝epel
[root@j certbot]# yum install snapd                        # 安裝snapd
[root@j certbot]# systemctl enable --now snapd.socket      # 啟用snapd.socket
[root@j certbot]# ln -s /var/lib/snapd/snap /snap          # 創建軟鏈接
[root@j certbot]# snap install --classic certbot           # 安裝certbot
[root@j certbot]# ln -s /snap/bin/certbot /usr/bin/certbot # 創建certbot軟鏈接

如果之前安裝過Certbot出現了問題,可以進行重裝

[root@j certbot]# yum remove certbot                      # 卸載certbot
[root@j certbot]# rm /usr/local/bin/certbot-auto          # 刪除安裝文件
[root@j certbot]# rm -rf /opt/eff.org/certbot

三、證書生成

我這里環境是nginx

使用https一定要先在nginx開啟http_ssl_module模塊,不然會出現nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf 有的話可以跳過

如果沒有的話需要重新編譯安裝一下


[root@j sbin]# cd /usr/local/nginx/sbin/           # nginx現有目錄
[root@j sbin]# ./nginx -V                          # nginx現有模塊
nginx version: nginx/1.19.5
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_flv_module 

可以看到configure arguments:后面的安裝模塊

所以我們在新的安裝目錄需要帶上原有的

[root@j nginx-1.19.5]# cd /usr/local/src/nginx-1.19.5    # 進入新目錄   
[root@j nginx-1.19.5]# ./configure --prefix=/usr/local/nginx --with-http_flv_module  --with-http_ssl_module   #安裝ssl模塊
[root@j nginx-1.19.5]# make # make安裝,這里不要像以前一樣 make&make install 否則會覆蓋原有目錄
[root@j nginx-1.19.5]# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak  #nginx做原有備份
[root@j nginx-1.19.5]# cp ./objs/nginx /usr/local/nginx/sbin/  # nginx如運行 先停止
[root@j nginx-1.19.5]# /usr/local/nginx/sbin/nginx -V    #查看模塊是否安裝成功
[root@j nginx-1.19.5]# cd /usr/local/nginx/sbin/ # 運行nginx
[root@j sbin]# cd /usr/local/nginx/sbin/ 
[root@j sbin]# ./nginx 

執行之前我們先把nginx先停止,否則會占用80端口

接下來我們執行下方命令來進行執行,我這里*是做通配符

certbot certonly --nginx --email youremail@email.com -d x.域名.com -d *.域名.com

如果提示:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Could not choose appropriate plugin: The nginx plugin is not working; there may be problems with your existing configuration.
The error was: NoInstallationError("Could not find a usable 'nginx' binary. Ensure nginx exists, the binary is executable, and your PATH is set correctly.")

是因為沒有將nginx放到環境變量中,設置nginx軟連接

ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx
ln -s /usr/local/nginx/conf/ /etc/nginx

生成之后我們會在/etc/letsencrypt/live/x.域名.com目錄下找到我們生成的證書

那么我們該怎么進行nginx配置

我這里簡單寫一個conf,需要http和https並存以及ssl調優可另外搜索查看

server
{
    listen 80;
    listen 443 ssl;
    server_name x.域名.com;
    charset utf8;
    
     #ssl on;
        ssl_certificate /etc/letsencrypt/live/x.域名.com/fullchain.pem; 
        ssl_certificate_key /etc/letsencrypt/live/x.域名.com/privkey.pem;
        ssl_protocols TLSv1.3 TLSv1.2;

        ssl_prefer_server_ciphers on;    # Forward Secrecy
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 30m;
    #REWRITE-END
    location / {
      proxy_pass http://127.0.0.1:9999;
    }
    #禁止訪問的文件或目錄
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
    

    access_log  /usr/local/nginx/logs/x.域名.com.log;
    error_log  /usr/local/nginx/logs/x.域名.com.log;
}

重載nginx,更新證書certbot renew

然后我們訪問域名,查看下域名

四、certbot自動定時續期證書

certbot renew #手動測試,查看證書過期時間

certbot renew --force-renewal #忽略證書過期時間,直接重置證書時間

crontab -e #定時任務

0 0 1 * * /usr/bin/certbot renew --force-renewal #編輯文件


我這里由於nginx沒有關閉所以80端口還在占用所以就不演示了,更新證書一定要先停止nginx


免責聲明!

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



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