GAE standard環境にて Python3 + Djangoでサーバを構築する

GAE standard環境にて Python3 + Djangoでサーバを構築する:

どうも。初記事です。緊張します。


GAE python3 standard環境使えるようになったってよ

長らくGAEのstandard環境ではpython2しか使えませんでした。

python3にするにはflexible環境を用いなければならなかったのですが、2018年7月にpython3対応しました! ヤッターーーー!
https://cloudplatform.googleblog.com/2018/07/bringing-the-best-of-serverless-to-you.html

それの何がすごいん? という方はこちら
https://qiita.com/mokrai/items/77f44551b8d219cfb370

今回はそのpython3を使ってDjangoを動かすところまでやります。

構築完了後にうろ覚えで書いているので抜けているところが多分あります。ご容赦を。


事前準備


GAEセットアップ

python2 standard環境でのDjango構築方法は公式ページに記載されているので見に行きます。

事前準備はpython3でも変わらないので、公式に従いながらチェックボックスを埋めていきましょう。
https://cloud.google.com/python/django/appengine?hl=ja

Google Cloud SQL API を有効にするところまでやってください。


python開発環境構築

もちろんpythonもインストールしておきましょう。これも公式が書いてくれているよ!
https://cloud.google.com/python/setup?hl=ja


サンプルダウンロード

公式の 「アプリのダウンロードと実行」にて、サンプルが取得できるとあります。

取得して見ましょう。リポジトリをcloneじゃ!

git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git 
...おっ!

flexibleとstandardと並んで、python3のサンプルアプリもある! (他はpython2のサンプルアプリダヨ)

どれどれ...

スクリーンショット 2018-10-11 11.18.12.png

すくな!!

hello_wordの中身を見たらflaskの最小構造だった。でもリリースしたばかりだ、仕方ない。

今回はこのhelloワールドと、python2のDjangoのサンプルアプリを参考にして構築してみる。

/python-docs-samples/appengine/standard_python37/hello_world
/python-docs-samples/appengine/standard/django


Djangoの新規アプリを作成する

余計な不純物を混ざらないようにするため、DjangoをインストールしてDjangoのコマンドからアプリ作成

pip install Django==2.1.1 
django-admin.py startproject my_app ./ 


app.yamlの修正

GAEにデプロイするための情報を変更するため、python2からapp.yamlをコピーしてきていじる。

これがサンプルアプリのyml

# [START django_app] 
runtime: python27 
api_version: 1 
threadsafe: yes 
 
handlers: 
- url: /static 
  static_dir: static/ 
- url: .* 
  script: mysite.wsgi.application 
 
# Only pure Python libraries can be vendored 
# Python libraries that use C extensions can 
# only be included if they are part of the App Engine SDK  
# Using Third Party Libraries: https://cloud.google.com/appengine/docs/python/tools/using-libraries-python-27 
libraries: 
- name: MySQLdb 
  version: 1.2.5 
# [END django_app] 
 
# Google App Engine limits application deployments to 10,000 uploaded files per 
# version. The skip_files section allows us to skip virtual environment files 
# to meet this requirement. The first 5 are the default regular expressions to 
# skip, while the last one is for all env/ files. 
skip_files: 
- ^(.*/)?#.*#$ 
- ^(.*/)?.*~$ 
- ^(.*/)?.*\.py[co]$ 
- ^(.*/)?.*/RCS/.*$ 
- ^(.*/)?\..*$ 
- ^env/.*$ 
 
先に出すと、最終的に動いたのはこの設定。

runtime: python37 
entrypoint: gunicorn -b :$PORT [app_name].wsgi:application 
 
handlers: 
- url: /static 
  static_dir: static/ 
- url: .* 
  script: [app_name].wsgi.application 


変更点

handlersは従来のまま。それ以外は変更。
runtimepython27 => python37 に変更。

api_version, threadsafe, skip_filesはpython37では対応してないため軒並み削除。

これがあるとdeployエラーが出る。

公式のリファレンスをみるとpython37のruntimeではサポートしてないよ! って書いてあるね。
https://cloud.google.com/appengine/docs/standard/python/config/appref

entrypointはアプリの起動方法。なにそれ? って方はこちら。
https://cloud.google.com/appengine/docs/flexible/python/runtime?hl=ja#application_startup

何もいじらないとgunicornが無いのでデプロイ失敗しちゃいます。

requirements.txtをいじりましょう。

今回の最小構成は以下な感じ

PyMySQL 
Django>=2.1.1 
gunicorn 


cloud sqlの接続

djangoのコマンドでアプリを作成するとデータベース接続がsqliteになってます。

もちろんcloud sqlにしましょう。

sqliteを削除して、 [my_app]/settings.pyはpython2のサンプルアプリの物をコピーして、自分のたてたcloud sqlインスタンスに変更しましょう。ここはpython2の公式と同じ手順で大丈夫です。
https://cloud.google.com/python/django/appengine?hl=ja#understanding_the_code

ここからはほとんどpython2の公式ページを参考にすればOKですが、一応簡単に書いておきます。


ローカルで実行を確認

virtualenv env 
source env/bin/activate 
manage.py migrate 
python manage.py runserver 
終了時

deactivate 
公式:
https://cloud.google.com/python/django/appengine?hl=ja#run_the_app_on_your_local_computer


GAEにdeploy

gcloud auth login 
python manage.py collectstatic 
gcloud app deploy 
公式:
https://cloud.google.com/python/django/appengine?hl=ja#deploy_the_app_to_the_app_engine_standard_environment

コメント

このブログの人気の投稿

投稿時間:2021-06-17 05:05:34 RSSフィード2021-06-17 05:00 分まとめ(1274件)

投稿時間:2021-06-20 02:06:12 RSSフィード2021-06-20 02:00 分まとめ(3871件)

投稿時間:2024-02-12 22:08:06 RSSフィード2024-02-12 22:00分まとめ(7件)