Let's Encrypt是一個免費的SSL證書發行項目,自動化發行證書,證書有90天的有效期,適合我們個人或者臨時使用。certbot是Let's Encrypt發布的新的工具,生成證書的使用方法和證書續簽就變得更加簡單了。但是目前看certbot在一些老版本的Linux發行版上的兼容性還是有問題的,特別是在CentOS 5上因為python版本過低是無法用的,CentOS 6上需要先安裝epel才行,當然也有很多第三方的工具你也可以自己去嘗試一下。
運行環境centos7.4 nginx 1.14.0
1、安裝certbot客戶端
[root@izuf68g6a94fj32w0afx00z nginx]# yum install certbot
Loaded plugins: fastestmirror
base | 3.6 kB 00:00:00
epel | 3.2 kB 00:00:00
extras | 3.4 kB 00:00:00
nginx | 2.9 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/4): extras/7/x86_64/primary_db | 156 kB 00:00:00
(2/4): epel/x86_64/updateinfo | 937 kB 00:00:00
(3/4): updates/7/x86_64/primary_db | 1.3 MB 00:00:00
(4/4): epel/x86_64/primary | 3.6 MB 00:00:00
Loading mirror speeds from cached hostfile
epel 12738/12738
Package certbot-0.27.1-1.el7.noarch already installed and latest version
Nothing to do
我已經安裝過了。
2、獲取證書
certbot certonly --webroot -w /home/zengfp/www/blog/public -d zengfanping.com -d www.zengfanping.com
這個命令會為 zengfanping.com 和 www.zengfanping 這兩個域名生成一個證書。
使用 --webroot
模式會在 /home/zengfp/www/blog/public 中創建.well-know文件夾。
這個文件夾里面包含了一些驗證文件,certbot 會通過訪問 example.com/.well-known/acme-challenge
來驗證你的域名是否綁定的這個服務器。
證書生成完畢后,我們可以在/etc/letsencrypt/live/
目錄下看到對應域名的文件夾,里面存放了指向證書的一些快捷方式。
3、standalone模式
上面--webroot這個命令在大多數情況下都可以滿足需求。
但是有些時候我們的一些服務並沒有根目錄,例如一些微服務,這時候使用 --webroot 就走不通了。
certbot 還有另外一種模式--standalone
, 這種模式不需要指定網站根目錄,他會自動啟用服務器的443端口,來驗證域名的歸屬。
我們有其他服務(例如nginx)占用了443端口,就必須先停止這些服務,在證書生成完畢后,再啟用。
certbot certonly --standalone -d example.com -d www.example.com
4、獲取證書出錯
類似的如下:
IMPORTANT NOTES: The following errors were reported by the server: Domain: www.zengfanping.com Type: connection Detail: Fetching http://www.zengfanping.com/.well-known/acme-challenge/xOiPmxEoy3bm8SZkNOskBj83OPOCmRHN6vvIQFSL-8I: 2 connection refused To fix these errors, please make sure that your domain name was entered correctly and the DNS A/AAAA record(s) for that domain contain(s) the right IP address. Additionally, please check that your computer has a publicly routable IP address and that no firewalls are preventing the server from communicating with the client. If you’re using the webroot plugin, you should also verify that you are serving files from the webroot path you provided.
這個時候你就需要檢查自己的項目目錄是否正確,域名解析是否正確,如果你在你的server_name處寫了多個域名,就需要對每個域名都進行解析。還有你的項目路徑一定要正確
server { listen 80 default_server; server_name zengfanping.com www.zengfanping.com; rewrite ^/(.*) https://www.zengfanping.com/$1 permanent; root /home/zengfp/www/blog/public; index index.html; location ~ /.well-known { allow all; } # location / { # root /home/zengfp/www/blog/public; # index index.html index.htm; # } access_log /var/log/nginx/host.access.log main; error_log /var/log/nginx/host.error.log; }
在進行--webroot這個模式獲取證書的時候,我把我的nginx服務關了,一直獲取不來證書,而且一直報上面那個錯誤。報錯之后還是需要點耐心的,因為如果你一直重復上面那個獲取證書的命令,就會提示你獲取證書太頻繁,耐心啊。最后我把nginx服務啟動之后,再次獲取證書,就成功了。本人的一次坑,希望伙伴們不要走。
5、啟用https
本人配置:
server { listen 80 default_server; server_name zengfanping.com www.zengfanping.com; rewrite ^/(.*) https://www.zengfanping.com/$1 permanent; root /home/zengfp/www/blog/public; index index.html; location ~ /.well-known { allow all; } # location / { # root /home/zengfp/www/blog/public; # index index.html index.htm; # } access_log /var/log/nginx/host.access.log main; error_log /var/log/nginx/host.error.log; } server { listen 443 ssl; ssl on; server_name www.zengfanping.com; root /home/zengfp/www/blog/public; index index.html; ssl_certificate /etc/letsencrypt/live/www.zengfanping.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/www.zengfanping.com/privkey.pem; location ~ /\.ht { deny all; } }
上面的配置中有個rewrite,就是讓輸入上面域名中的任何一個,都讓其指向 https://www.zengfanping.com 這個地址。
最終效果:
6、跨過每一個小坑,讓自己每天都進步一點點!您的點贊是我前進的鼓勵!希望你不要吝嗇哦。個人博客網址:https://www.zengfanping.com。不吝賜教