如何為你的服務(wù)器申請免費的SSL證書

一、前戲

在文章《利用微信小程序和Kubernetes打造簡易私有云(一、開篇)》中,本人計劃用微信小程序作為K8S私有云的前端。由于之前對微信小程序也不了解,就不得不走一遍流程來學習實踐,才發(fā)現(xiàn)搭一個微信小程序遠沒有想象的那么容易啊。

如果你要搭建微信小程序的后端服務(wù),首先,你的服務(wù)器必須有個域名,而且域名還必須要備案。另外,小程序和服務(wù)器之間是通過https交流,你還得申請SSL證書,如官方文檔提到如下:

每個微信小程序需要事先設(shè)置一個通訊域名,小程序可以跟指定的域名與進行網(wǎng)絡(luò)通信。包括普通 HTTPS 請求(request)、上傳文件(uploadFile)、下載文件(downloadFile) 和 WebSocket 通信(connectSocket)

小程序必須使用 HTTPS 請求。小程序內(nèi)會對服務(wù)器域名使用的 HTTPS 證書進行校驗,如果校驗失敗,則請求不能成功發(fā)起。由于系統(tǒng)限制,不同平臺對于證書要求的嚴格程度不同。為了保證小程序的兼容性,建議開發(fā)者按照最高標準進行證書配置,并使用相關(guān)工具檢查現(xiàn)有證書是否符合要求。

所以,開發(fā)小程序我們得先把基礎(chǔ)環(huán)境搭好,本篇文章主要就是討論如何給自己的服務(wù)器申請SSL證書。當然,你得提前有個服務(wù)器,而且有個域名。本人使用的是阿里云ECS。

本文可以算是《利用微信小程序和Kubernetes打造簡易私有云》系列的前傳,歡迎看官不吝賜教,走你~

二、高潮

簡介

了解SSL證書的都知道,絕大部分都是收費的,而且還很貴。之前阿里云有段時間可以申請免費的證書,但現(xiàn)在木有了,只提供收費的,價格也貴的離譜(反正我沒錢):


image.png

若仔細挖掘,其實也可發(fā)現(xiàn)很多免費的SSL證書申請服務(wù),適合個人開發(fā)使用。但我個人不推薦使用國內(nèi)的免費SSL證書,總感覺不靠譜。這里,我強烈推薦——Let's Encrypt ,大家可以自行谷歌,這是一個很受歡迎并永久免費的SSL項目,而且到期后可以免費續(xù)簽(有效期90天),非常方便,請戳官網(wǎng)。

安裝

本人使用阿里云的CentOS 7親自嘗試安裝,發(fā)現(xiàn)官方推薦的安裝方式不太靠譜,會出現(xiàn)各種python相關(guān)的依賴問題。這里,我們直接使用官方另一種安裝方式,親測可行。

1.下載certbot-auto

執(zhí)行:

user@webserver:~$ wget https://dl.eff.org/certbot-auto
user@webserver:~$ chmod a+x ./certbot-auto

這里,我們無需關(guān)心Certbot是什么,只需要知道,證書的申請和生成是通過Certbot完成的,而certbot-auto腳本封裝了Certbot

2.申請證書

執(zhí)行:

./certbot-auto certonly --standalone --email abc@163.com -d ainizhi.xin

如上,certbot-auto命令會自動下載Certbot所需的依賴,并且為ainizhi.xin域名申請并生成證書。請更換--email-d后的參數(shù),分別表示自己郵箱和域名。

另外,證書90后就會到期,到時我們只需要使用certbot-auto renew命令免費續(xù)簽即可(建議配合使用linux的crontab機制),可參考官方文檔。

3.證書位置

生成后的證書位置如下:

[root@iz2ze2tzqe1llll9ow0vxoz ~]# ll /etc/letsencrypt/live/ainizhi.xin/
total 4
lrwxrwxrwx 1 root root  35 May  9 09:12 cert.pem -> ../../archive/ainizhi.xin/cert1.pem
lrwxrwxrwx 1 root root  36 May  9 09:12 chain.pem -> ../../archive/ainizhi.xin/chain1.pem
lrwxrwxrwx 1 root root  40 May  9 09:12 fullchain.pem -> ../../archive/ainizhi.xin/fullchain1.pem
lrwxrwxrwx 1 root root  38 May  9 09:12 privkey.pem -> ../../archive/ainizhi.xin/privkey1.pem
-rw-r--r-- 1 root root 682 May  9 09:12 README

這里,我們使用的是fullchain.pemprivkey.pem,前者是證書,后者是私鑰。

啟用證書

本文使用Nginx來驗證我們生成的證書,當然,你得提前安裝好Nginx。首先,修改Nginx默認的配置文件,啟用https配置:

server {
        listen      8060 ssl http2 default_server;
        listen      [::]:8060 ssl http2 default_server;
        server_name  _;
        root        /usr/share/nginx/html;
        ssl_certificate "/etc/letsencrypt/live/ainizhi.xin/fullchain.pem";
        ssl_certificate_key "/etc/letsencrypt/live/ainizhi.xin/privkey.pem";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        root  /usr/share/nginx/html;
        index  index.html index.htm;
        }

        error_page 404 /404.html;
            location = /40x.html {
            root  /usr/share/nginx/html;
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
            root  /usr/share/nginx/html;
        }
    }

如上,我們主要修改了如下路徑:

        ssl_certificate "/etc/letsencrypt/live/ainizhi.xin/fullchain.pem";
        ssl_certificate_key "/etc/letsencrypt/live/ainizhi.xin/privkey.pem";

修改后重啟nginx即可。另外,可能有讀者會問到,為什么不使用默認的443接口——答曰:因為我的域名沒有備案 (灬? ?灬)。

測試

我們使用Chrome或其他瀏覽器訪問地址https://ainizhi.xin,如下圖所示,我們可以看到地址欄前面有了可愛的的“鎖”圖標:

image.png

如上,則表示我們申請的免費SSL證書啟用成功,也被各大瀏覽器廠商認可。

問題

安裝Certbot 時很可能會出現(xiàn)如下錯誤:

...省略...
Creating virtual environment...
Installing Python packages...
Traceback (most recent call last):
  File "/tmp/tmp.AT0iiLfl6l/pipstrap.py", line 184, in <module>
    exit(main())
  File "/tmp/tmp.AT0iiLfl6l/pipstrap.py", line 165, in main
    for path, digest in PACKAGES]
  File "/tmp/tmp.AT0iiLfl6l/pipstrap.py", line 120, in hashed_download
    response = opener(using_https=parsed_url.scheme == 'https').open(url)
  File "/usr/lib64/python2.7/urllib2.py", line 431, in open
    response = self._open(req, data)
  File "/usr/lib64/python2.7/urllib2.py", line 449, in _open
    '_open', req)
  File "/usr/lib64/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/usr/lib64/python2.7/urllib2.py", line 1258, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/usr/lib64/python2.7/urllib2.py", line 1214, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno 101] Network is unreachable>

一般是網(wǎng)絡(luò)問題,重試幾次即可。成功時如下所示:

...省略...
Creating virtual environment...
Installing Python packages...
Installation succeeded.
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator standalone, Installer None
-------------------------------------------------------------------------------
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v01.api.letsencrypt.org/directory
-------------------------------------------------------------------------------
(A)gree/(C)ancel: A
-------------------------------------------------------------------------------
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about EFF and
our work to encrypt the web, protect its users and defend digital rights.
-------------------------------------------------------------------------------
(Y)es/(N)o: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for ainizhi.xin
Waiting for verification...
Cleaning up challenges
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
  /etc/letsencrypt/live/ainizhi.xin/fullchain.pem
  Your key file has been saved at:
  /etc/letsencrypt/live/ainizhi.xin/privkey.pem
  Your cert will expire on 2018-08-07\. To obtain a new or tweaked
  version of this certificate in the future, simply run certbot-auto
  again. To non-interactively renew *all* of your certificates, run
  "certbot-auto renew"
- Your account credentials have been saved in your Certbot
  configuration directory at /etc/letsencrypt. You should make a
  secure backup of this folder now. This configuration directory will
  also contain certificates and private keys obtained by Certbot so
  making regular backups of this folder is ideal.
- If you like Certbot, please consider supporting our work by:
  Donating to ISRG / Let's Encrypt:  https://letsencrypt.org/donate
  Donating to EFF:  

睡覺

本文只是簡單的介紹了如何通過Let's Encrypt申請免費SSL證書并應(yīng)用之。本人水平有限,難免有錯誤或遺漏之處,望大家指正和諒解,歡迎評論留言。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容