COCO研究院

 找回密碼
 註冊
搜索
查看: 691|回復: 0

Sonic web server配置SSL證書 (以支援https)

[複製鏈接]
發表於 23-3-4 23:40 | 顯示全部樓層 |閱讀模式
本帖最後由 綠茶妹 於 23-3-5 20:31 編輯

前情提要:
https://www.coco-in.net/thread-156155-1-1.html

這個web server用Sonic寫的
為了要讓它有https比較安全,我去研究方法

以下是正文:
參考官方說明有詳細的步驟。
下面是開程式
  1. ssl = {
  2.     "cert": "/path/to/fullchain.pem",
  3.     "key": "/path/to/privkey.pem",
  4.     "password": "for encrypted privkey file",   # Optional
  5. }
  6. app.run(host="0.0.0.0", port=8443, ssl=ssl)
複製代碼


下面是申請憑證用的指令
  1. sudo certbot certonly --key-type ecdsa --preferred-chain "ISRG Root X1" -d mytrading.coco-in.net

複製代碼


申請時,注意它會問要哪一種方式,請選下面的第一種。(第2種我測試有點怪怪的,第1種的路徑是我要的)
root@Trading:~# sudocertbot certonly --key-type ecdsa --preferred-chain "ISRG Root X1" -dmytrading.coco-in.net
Saving debug log to/var/log/letsencrypt/letsencrypt.log

How would you liketo authenticate with the ACME CA?
- - - - - - - - - -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1:Spin up a temporary webserver (standalone)
2: Place files inwebroot directory (webroot)



以下是我的測試程式 hello.py
  1. from sanic import Sanic
  2. from sanic.response import json
  3. from sanic.exceptions import NotFound


  4. app = Sanic(name="pyapp")

  5. @app.route('/')
  6. async def test(request):
  7.     return json({'hello': 'world'})


  8. if __name__ == '__main__':
  9.     app.error_handler.add(
  10.         NotFound,
  11.         lambda r, e: sanic.response.empty(status=404)
  12.     )
  13.     ssl = {
  14.     "cert": "/etc/letsencrypt/live/mytrading.coco-in.net/fullchain.pem",
  15.     "key": "/etc/letsencrypt/live/mytrading.coco-in.net/privkey.pem",
  16.     }
  17.     app.run(host='0.0.0.0', port=443, ssl=ssl)
複製代碼


可以成功的在外網開啟網址
Let's Encrypt 每三個月會到期。
剛查網路上的資料,應該會自動續簽
https://caloskao.org/ubuntu-use- ... tificate-authority/


2023-03-05 20_30_40-Clipboard.png

補充很重要的點,記得要打開防火牆。不然外網會連不上。
請參考這裡的說明

像下面這樣子打開
  1. $ sudo ufw allow 80/tcp
  2. $ sudo ufw allow 443/tcp
複製代碼


下面這樣查詢
  1. sudo ufw status
複製代碼







評分

參與人數 1金錢 +2 收起 理由
tinyding + 2 感謝分享

查看全部評分

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

手機版|Archiver|站長信箱|廣告洽詢|COCO研究院

GMT+8, 24-4-19 22:21

Powered by Discuz! X3.4

Copyright © 2001-2023, Tencent Cloud.

快速回復 返回頂部 返回列表
理財討論網站 | AI繪圖AI超擬真美女AI beauty AI Stable DiffusionAI正妹AI Lookbook