在 GCP (Windows Server) 使用 Apache Server + Flask + Python + LINE BOT (Part 3 - 申請有效憑證 + 設置 Apache Server 為 SSL (https) 讓 LINE 可以訪問)

泥膩泥膩
男友說我是宅包
6 min readJan 22, 2020
https://www.salesforce.com/blog/2018/08/chatbots-lead-gen-sales-crm.html

因為 LINE Webhook URL 必須是 HTTPS URL,所以需要做一些處理QQ

一、申請免費的憑證 (SSL for Free)

二、架設一個 HTTPS SERVER

三、修改 LINE BOT Webhook URL

四、測試 LINE BOT

一、申請免費的憑證 (SSL For Free)

試過用 openssl 自己製作憑證,但是憑證是無效的…..

因此 LINE 不能使用 QQ 因此要申請一個有效的 SSL 憑證

SSL For Free : https://www.sslforfree.com/

注意,使用 SSL For Free 要使用 Domain Name,不能直接用 IP

打上 Domain Name 後,再點 Create Free SSL Certificate

選擇中間的 Manual Vertification 後,再點下面的 Manually Vertify Domain

讓它 Loading 一下,會出現下面這個頁面

先下載第一點的檔案 Download File #1

然後第五點它說要驗證你的 http://<your domain name>/.well-known/acme-challenge/2UuxxxxxxxxxSzr2A

所以這邊的做法,我是直接把 File #1 用記事本打開,你會看到裡面有一串文字(下圖),再把這串文字複製到 Python Flask 中

複製到 .py

@app.route('/.well-known/acme-challenge/<id>')
def acme_challenge(id):
return "jxxxxxxxxxxxxxxxxxxxxx6E"

重啟 Flask 後,就可以自行點那個連結,看是不是成功了

http://<your domain name>/.well-known/acme-challenge/2UuxxxxxxxxxSzr2A

完成後回到剛剛的 SSL For Free 頁面,點最下面的 Download SSL Certificate,它就會去驗證那個網頁,成功的話就可以下載憑證了!!!!

下載下來的憑證有三個檔案(ca_bundle.crt、certificate.crt、private.key)

這樣憑證的申請就好了

二、架設一個 HTTPS SERVER

把憑證申請完後,就要開始架一個 HTTPS SERVER 了

首先打開 Apache Server 的 config 檔

C:\Apache24\conf\httpd.conf
  1. 新增 Listen 443

https 預設的 port 是 443

2. 打開 LoadModule ssl_module modules/mod_ssl.so,移除 #

3. 在 httpd.conf 下面,新增<VirtualHost *:80> 和 <VirtualHost *:433>

# 用來轉址,http 轉到 https
<VirtualHost *:80>
Redirect permanent / https://<你的 Domain Name>
</VirtualHost>
<VirtualHost *:443>        ServerName <你的 Domain Name>
WSGIScriptAlias / C:\...\test\main.wsgi
# SSL
SSLEngine on
SSLCertificateFile "C:\...\sslforfree\certificate.crt"
SSLCertificateKeyFile "C:\...\sslforfree\private.key"
SSLCACertificateFile "C:\...\sslforfree\ca_bundle.crt"

</VirtualHost>

再到 C:\Apache24\bin 下開啟 cmd,執行 httpd

打開瀏覽器,輸入你的 domain name,就可以看到成功惹!!

憑證是已建立安全連線,且憑證(有效)

三、修改 LINE BOT Webhook URL

LINE Developers : https://developers.line.biz/zh-hant/

選擇你的 Channel →Message API Setting →Webhook Setting

打上你的 Domain Name,再按 Vertify,顯示 Success 代表成功了

四、測試 LINE BOT

打開你的 LINE 機器人測試一下

!!!!!!!!!!!!!! 有回應,這樣就成功惹

在 GCP (Windows Server) 上使用 Apache Server 和 SSL For Free 的憑證架設一個擁有有效憑證的 https (port: 443),並且讓 LINE Developers / LINE Bot 可以驗證成功自己架設的 Webhook URL

--

--