[不做怎麼知道系列之Android開發者的30天後端養成故事 Day17] - 終於要上雲端了嗎?! #Django #CloudHosting #GAE

哈囉,我們又見面了,昨天我們體驗過在本地端架 Django Server 了,可是要自己處理的事情太多了,今天我們來照著 Google Cloud Platform(簡稱 GCP) Python 官方文件中的,Running Django on the App Engine standard environment,一起把 Django 放到雲端上執行吧 !

整體的概念就是,先上 GCP 網頁,創立一個專案,然後安裝 Cloud SDK,這東西就是把專案放到雲端的核心,然後再到我們本地的 Django 專案資料夾,多加三個檔案 app.yamlrequirements.txtmain.py,再用 $ gcloud app deploy,燈愣,你的網站就已經在雲端上架好了,想看看網站長什麼樣子就用 $ gcloud app browse,就會跳到瀏覽器開新分頁來顯示,是不是超讚。

我們上述所說的,是使用 Google Cloud Platform(GCP) 的 Google App Engine(GAE) 服務,在你註冊之後會有免費的時限和額度,可以參考 Google Cloud Platform 免費方案,雖然是免費,可是還是要注意一下其中 GAE 的計費方式,是用多少算多少,也就是說你的流量越大,費用就越高。

為了避免像口罩地圖一樣的悲劇 XD,你在創立 GCP Project 的時候,就順便把 預算 給定下來是最保險的作法 !

創立 GCP Project

建立 預算 的方法:點入左側邊攔的 帳單預算與快訊設定預算

安裝 Cloud SDK (安裝流程可參考 官方文件)

打開 Windows PowerShell (可以從 Windows 搜尋裡面找),輸入

1
2
3
(New-Object Net.WebClient).DownloadFile("https://dl.google.com/dl/cloudsdk/channels/rapid/GoogleCloudSDKInstaller.exe", "$env:Temp\GoogleCloudSDKInstaller.exe")

& $env:Temp\GoogleCloudSDKInstaller.exe

然後照著提示做完,等全部跑完,輸入 $ gcloud,確認一下是不是有安裝 gcloud 成功,gcloud 安裝完後會自動更新環境變數,如果沒有,那就自己新增吧 XD

登入 gcloud

$ gcloud auth application-default login

下載範例程式

$ git clone [https://github.com/GoogleCloudPlatform/python-docs-samples.git](https://github.com/GoogleCloudPlatform/python-docs-samples.git)

$ cd python-docs-samples\appengine\standard_python37\django

創一個新的 Django 專案

用一個完全乾淨的專案,才知道哪些檔案是 Google App Engine 的,哪些是純 Django 的。

  • 進到虛擬環境中
    如果還沒創建,可以參考 Day4 中,創立虛擬環境的過程、安裝 django 的過程

    $ yourvirtualenv\Scripts\activate (Windows)

    $ source yourvirtualenv/bin/activate (Mac/Linux)

  • 創 Django project 和 app

    (venv)$ django-admin startproject django_on_gae

    (venv)$ cd django_on_gae && django-admin startapp shop

  • 替首頁加點東西,比較好確認真的是我們的網頁,修改的細節可以參考 Day5

    views.py

    1
    2
    3
    4
    5
    6
    from django.shortcuts import render
    from django.http import HttpResponse

    # Create your views here.
    def home_view(request):
    return HttpResponse("Hi Django on GAE")

urls.py

1
2
3
4
5
6
7
8
9
from django.contrib import admin
from django.urls import path

from shop.views import home_view

urlpatterns = [
path('', home_view),
path('admin/', admin.site.urls),
]

settings.py

1
2
3
4
5
6
7
# 接受各個瀏覽網頁的 host
ALLOWED_HOSTS = ['*']

...

# 告訴 GAE 我們的靜態檔案(img, css, js)路徑
STATIC_ROOT = 'static'
  • 先在本地端跑跑看 (細節可以參考 Day4, Day5)

    $ python manage.py runserver

    瀏覽 127.0.0.1:8000,確認一下首頁是不是有出現

部屬前的作業

需要新增下列三個檔案

  • app.yaml
  • requirements.txt
  • main.py

這三個檔案,可以從範例程式借鏡 XD,也就是剛剛從 GCP 的 Github 下載下來的 python-docs-samples\appengine\standard_python37\djangodjango 這個資料夾中有包含這三個檔案,而我把最基本的給截出來,所以現在這三個檔案會長這樣,有興趣可以自己去比較一下差別

  • app.yaml

    這部分我是完全照抄,GAE 會依照 app.yaml 這個檔案,來建置執行的環境,詳細的資訊可以參考 官方文件,詳細的可以有時間再來 K,實際上是等到環境架不起來才來 K 文件哈哈

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    # [START django_app]
    runtime: python37

    handlers:
    # This configures Google App Engine to serve the files in the app's static
    # directory.
    - url: /static
    static_dir: static/

    # This handler routes all requests not caught above to your main app. It is
    # required when static routes are defined, but can be omitted (along with
    # the entire handlers section) when there are no static files defined.
    - url: /.*
    script: auto
    # [END django_app]
  • main.py

    從註解可以看到,GAE 是從根目錄的 main.py 檔,作為切入點,然後再將封包透過 python 的 WSGI (Web Server Gateway Interface) 轉發到 django app 裡面,概念是這樣。

可是實際上 GAE 是將 wsgi 轉成 GAE 看得懂的物件,所以我們才不需要特別設定,就可以把封包導入到 django app,註解的最下面有寫到,可以自訂 entrypoint(切入點),不過這個暫時還用不到。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
'''
`django_on_gae` 這個名字,要看你創立的 django project 名稱來跟著改,
原本範例程式是 `mysite`,我改成 `django_on_gae`
'''
from django_on_gae.wsgi import application

# App Engine by default looks for a main.py file at the root of the app
# directory with a WSGI-compatible object called app.
# This file imports the WSGI-compatible object of your Django app,
# application from mysite/wsgi.py and renames it app so it is discoverable by
# App Engine without additional configuration.
# Alternatively, you can add a custom entrypoint field in your app.yaml:
# entrypoint: gunicorn -b :$PORT mysite.wsgi
app = application
  • reqirements.txt

    GAE 會在建立環境的時候,用 $ pip install -r reqirements.txt 來安裝相依套件。

    1
    Django==3.0.3

部屬(Deploy) 到 Google App Engine(GAE)上

$ gcloud app deploy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Services to deploy:

descriptor: [C:\Users\shrim\Downloads\django_on_gae\app.yaml]
source: [C:\Users\shrim\Downloads\django_on_gae]
target project: [your-project-id]
target service: [default]
target version: [your-version-code]
target url: [https://your-project-id.appspot.com]

Do you want to continue (Y/n)? y

Beginning deployment of service [default]...
#============================================================#
#= Uploading 1 file to Google Cloud Storage =#
#============================================================#
File upload done.
Updating service [default]...done.
Setting traffic split for service [default]...done.
Deployed service [default] to [https://your-project-id.appspot.com]

You can stream logs from the command line by running:
$ gcloud app logs tail -s default

To view your application in the web browser run:
$ gcloud app browse

來看看到底有沒有成功 !

$ gcloud app browse

就會自動開啟瀏覽器,新增一個分頁來開啟你的網頁

單日心得總結

其實我原本的心路歷程是這樣的,想說之前看過 FirebaseHosting,那不然試試看好了,結果他不支援 Python 的執行環境,只好改向 AWS Lambda,但是看了一下,一開始就玩 Serverless 還要多做很多事,有點麻煩,然後再看到 Django學習紀錄 20.部署django至heroku平台,可是我之前在用 Heroku 的時候還不太懂,有些不好的回憶。

直到最後考慮到 GCP,看到 開始使用 Django 的說明頁我就定下來了,有中文,很棒, 有多種選項,很棒,加上之前用 GCPCloud Speech-to-Text 的經驗,還不知道自己做了些什麼,就已經設定好了 XD,只能說 Google 提供的服務,是真的挺好用的,而且有些說明是看得懂的中文,不是機器翻譯的那種中文,聽說 AWS 也是不錯,有機會再回去試試看。

今天早上跟了好想工作室的籃球團,去當個雷雷,洗個澡、想個文章主題,到工作室已經快中午了,順便吃午餐,覺得這種生活方式,跟上班實在是差太多了,可能體驗過就很難回去上班了哈哈,生產力高又自由,缺點就是要自己煩惱錢的來源,恩,挺煩惱的 QQ。

我是 RS,這是我的 不做怎麼知道系列 文章,我們 明天見。


  • 喜歡我的文章嗎? 趕快來看看我都發了什麼文章吧:我的文章目錄
  • 歡迎閱讀我的上一篇: [不做怎麼知道系列之Android開發者的30天後端養成故事 Day16] - 網站上線拉 #Django #LocalHosting #ngrok
  • 歡迎閱讀我的下一篇: [不做怎麼知道系列之Android開發者的30天後端養成故事 Day18] - 替網站取個好名字 #Django #GAE #CustomDomain
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2021-2022 Sam Ho
  • Visitors: | Views:

請我喝杯咖啡吧~

支付宝
微信