iT邦幫忙

2022 iThome 鐵人賽

DAY 22
0
Modern Web

傳承D的意志~ 邁向Django的偉大航道系列 第 22

[Day 22] 實戰篇: 飛上雲端! GCP之部署(下集)

  • 分享至 

  • xImage
  •  

嗨大家好,我是Sean!
昨天把我們GCP Cloud SQL的部分說完了! 我們的雲端資料庫也就完成了!
今天我們來講解如何使用GCP App Engine做部屬。

GCP App Engine


App Engine是一個無伺服器託管的服務,可以省去很多設定基本伺服器環境的力氣,來部署我們的應用程式。

那麼,我們就繼續!
在連結完資料庫後,下一步就是將我們的應用程式來連結App Engine!
首先,我們先進入Cloud SDK來登入我們要使用Google帳號!

gcloud auth application-default login

輸入完畢後,我們登錄完我們帳號,回到我們的專案。
我們必須在我們專案中,加入三個檔案並且更改我們設定,才能完成在App Engine的部署。
1.app.yaml
2.main.py
3.requirements.txt

app.yaml 壓摸

第一次聽到壓摸這個字的時候,覺得莫名的好笑!回到正題,我們app.yaml檔的內容如下:

#
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# [START django_app]
# [START gaestd_py_django_app_yaml]
runtime: python38 #python版本

env_variables:
  # This setting is used in settings.py to configure your ALLOWED_HOSTS
  # APPENGINE_URL: PROJECT_ID.uc.r.appspot.com
  INSTANCE_UNIX_SOCKET: /cloudsql/你的project_id:執行個體名稱
  DB_USER: #你的db帳號
  DB_PASS: #你的db密碼
  DB_NAME: #db個體名稱

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 gaestd_py_django_app_yaml]
# [END django_app]

main.py

第二個則是新增一個main.py

from django_practice.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

唯一需要修正的地方只有上方引用,我們專案的project name是 django_practice,所以如果專案名不一樣,要在引用的地方做修改。

requirements.txt

最後一個是常用到的requirements.txt也就是常用到的套件版本包。
裡面會記載著你在此虛擬環境的套件,並以此為基準,給雲端辨識使用。
最方便生成的方法就是輸入指令囉,首先先到你想生成檔案的資料夾後:

pip freeze > requirements.txt

這麼一來就可以輕鬆生成我們的requirements.txt囉!

那麼,他們的樣子大概會長這樣:
https://ithelp.ithome.com.tw/upload/images/20221007/20151096zHgSo5Oaq0.png

settings

最後我們來更改settings中的設定:

ALLOWED_HOSTS = ['*']

if os.getenv('GAE_APPLICATION', None):

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST':'/cloudsql/你的project_id:你的instance',
            'USER': #你的db帳號,
            'PASSWORD': #你的db密碼,
            'NAME': 你的db名稱,
        }
    }
else:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': #你的db ip,
            'PORT': '3306',
            'NAME': #instance名稱,
            'USER': #你的db帳號,
            'PASSWORD': #你的db密碼,      
        }
    }

完成後就可以來啟動部署

gcloud app deploy

恭喜!如此一來你的django專案應該就已經上雲成功了!
那麼,我們今天的文章就到此結束!
我是Sean,你各位海上的人,我們明天見!
https://ithelp.ithome.com.tw/upload/images/20221007/20151096FZQwZyPX8R.jpg


上一篇
[Day 21] 實戰篇: 飛上雲端! GCP之部署(中集)
下一篇
[Day 23] 激戰篇: 正面對決啊Migration error (上)
系列文
傳承D的意志~ 邁向Django的偉大航道30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言