Render是一個雲端託管平台, 可以部屬網站、程式、資料庫, 只要將程式放在GitHub或是Gitlab, 再使用Render連結到GitHub或是Gitlab網址, 就可以把程式或網站放在雲端執行。 使用Render+GitHub建立一個伺服器的步驟說明: 1. 在GitHub建立的repository, 將程式及需要的資料放在repository 2. 使用Render連結GitHub repository, 部屬伺服器 <br/> ## 在GitHib建立repository 在GitHub的Dashboard左邊欄位點選"Create"或是直接在中間欄位新增, repository可設置為私密或公開 ![image](https://hackmd.io/_uploads/ryfGnXQnC.png) repository這裡我放了兩個檔案 1. requirements.txt:內容是執行主程式前需要安裝的程式 2. app. py:主要執行程式 ![image](https://hackmd.io/_uploads/ryRKuu73R.png) https://github.com/tz7013/my_linebot <br/> ## 使用Render部屬伺服器 到Render註冊會員 https://render.com/ ![image](https://hackmd.io/_uploads/H1yZUW2j0.png) 點選Web Services ![image](https://hackmd.io/_uploads/SytKP-nsR.png) 第一次進來的畫面如下, 我們選第一個連結到GitHub repository ![image](https://hackmd.io/_uploads/SJKs5GXnC.png) 如果有2個以上的專案, 點選右邊下拉式選單的Configure GitHub, 或是點選Public Git repository, 貼上網址 ![image](https://hackmd.io/_uploads/Sy9I9MX20.png) 出現以下視窗, 點選"Select repositories" --> 選目標專案 --> 再點Save ![image](https://hackmd.io/_uploads/Bk1l6zmn0.png) 然後在deploying頁面就可以看到剛剛連結的repository了, 點選目標repository --> Connect ![image](https://hackmd.io/_uploads/SJTu1QQ20.png) 部屬Web Service設定 ![image](https://hackmd.io/_uploads/Hy1sjIQ2C.png) * **Build Command: pip install -r requirements.txt** (在每次部屬前執行這個命令-安裝相關模組) * **Start Command: gunicorn -w 4 -b 0.0.0.0:000 app:app** 1. 使用gunicorn運行程式, gunicorn是 Python 的一個 WSGI HTTP 伺服常來部署 Flask 或 Django 等應用程式。 2. -w 4: 指定使用 4 個 worker 進程來處理請求。Worker 進程數越多,伺服器處理並發請求的能力就越強,但也會消耗更多的系統資源。 3. -b 0.0.0.0:10000: 指定伺服器綁定的地址和端口號。0.0.0.0 表示伺服器將監聽所有可用的網絡接口, 而 10000 是程式將使用port。 4. app:app: 指定 Flask 應用程式的入口點。第一個 app 是指程式名稱(不需要 .py 後綴),第二個 app 是 Flask 應用的實例名稱。 ![image](https://hackmd.io/_uploads/Bkkub843C.png) 建立環境變數(例如: OPENAI API KEY、LINE BOT的USER ID) --> 點選Deploy Web Service部屬伺服器 ![image](https://hackmd.io/_uploads/BkdE_XX3C.png) 出現以下畫面開始執行 在logs看到"Your service is live", 恭喜部屬成功 左上角的網址就可以複製起來貼到line bot的messaging api ![image](https://hackmd.io/_uploads/ryr4SEXhA.png) ![image](https://hackmd.io/_uploads/rJXzI4QhC.png) **注意:Python本身並不提供直接引用環境變數的語法。必須通過標準庫中的os模組來實現。** <br/> ## 使用Render部屬個人網站 我也是使用python Flask架設本地伺服器的程式, 再透過Reder來執行, 步驟與上面一樣。 ![image](https://hackmd.io/_uploads/Bkg9TUXhR.png) > 注意:我在電腦上執行python程式時, index.html、script.js和style.css都放在templates目錄下, 可正常執行, 但是放到github上使用render執行時有顯示網頁但是沒有格式, 後來把script.js和style.css放到static目錄下就OK了, 問題如下面說明。 > ### Flask 框架的文件夾結構 >* templates 目錄:這裡用來存放 HTML 模板文件。Flask 會自動尋找這個目錄中的模板文件來渲染網頁。 >* static 目錄:這裡用來存放靜態資源,例如 CSS、JavaScript 和圖片。這些靜態資源需要通過 URL 路徑來訪問,Flask 會自動服務這些文件。 >Flask 會自動將 static 目錄中的文件映射到 /static/ 路徑下。 例如,如果 style.css 文件放在 static 目錄中,可以在 HTML 模板中這樣引用: ```html= <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"> <script src="{{ url_for('static', filename='script.js') }}"></script> ``` [感謝chatgpt大神] 我的openai聊天機器人網頁版, 歡迎聊聊 https://myopenai-41g7.onrender.com/ GitHub連結 https://github.com/tz7013/myopenai_web <br/> ## Render Web Service免費限制: 1. 超過15分鐘沒有請求的話會進入休眠模式, 再度訪問重啟最多約一分鐘。 2. 每個月可運行750hrs。 如果只是架設個人網頁, 或是Demo作品非常好用, 代替Ngrok另外一個好選擇。 <br/> ## 參考資料 1. https://medium.com/@King610160/render%E9%83%A8%E7%BD%B2-web-service-2779497b8d39 1. https://israynotarray.com/other/20221213/3036227586/