長野エンジニアライフ

東京から長野に移住したエンジニアのブログです。🦒🗻⛰

chicken voiceの作成備忘録(4)〜Herokuにデプロイ〜

TwitterのAccount Activity APIを利用してリプライのトリガを構築するので、先にdjangoをHerokuにデプロイしておく。

「gunicorn」および「django-heroku」のインストール

$ pip3 install gunicorn django-heroku

以下のエラーがでてきた。

Error: pg_config executable not found.
    
    pg_config is required to build psycopg2 from source.  Please add the directory
    containing pg_config to the $PATH or specify the full executable path with the
    option:
    
        python setup.py build_ext --pg-config /path/to/pg_config build ...

pg_configはpostgresqlに含まれてるようなので、以下を実行

$  brew install postgresql

再度インストールする。

$ pip3 install gunicorn django-heroku
...
...
Successfully built psycopg2
Installing collected packages: gunicorn, dj-database-url, whitenoise, psycopg2, django-heroku
Successfully installed dj-database-url-0.5.0 django-heroku-0.3.1 gunicorn-20.0.4 psycopg2-2.8.4 whitenoise-5.0.1

正常に終了した◎

実行環境ファイルの作成

  • runtime.txt Pythonのバージョン指定
python-3.7.6
  • Procfile Herokuプロセスの起動コマンド
web: gunicorn chickenvoice.wsgi --log-file -
  • requirements.txt 依存パッケージのリスト freezeで自動生成する
$ pip3 freeze > requirements.txt

設定ファイルの作成・変更

  • local_settings.pyの作成
import os

DEBUG = True
  • setting.pyの末尾に設定を追記
...
...
# herokuの設定
DEBUG = False # herokuの設定 Falseに設定
try:
    from .local_settings import *
except ImportError:
    pass

if not DEBUG:
    import django_heroku
    django_heroku.settings(locals())
  • ALLOWED_HOSTS設定 herokuで使えるようにsettings.pyにて修正
ALLOWED_HOSTS = ['*']
  • .gitignoreの追加 local_settings.pyの追加
chicken_twitterAPI
chickenEnv
db.sqlite3
__pycache__
.DS_Store
local_settings.py

Herokuの準備

  • Heroku CLIインストール
$ brew tap heroku/brew && brew install heroku

インストール完了後ログインする。

$ heroku login
...
Logging in... done
Logged in as
...
  • プロジェクトの作成
$ heroku create chickenvoice
  • Herokuにプッシュする
$ git push heroku master

以下のコマンドを実行しろとエラーが吐かれたので実行

$ heroku config:set DISABLE_COLLECTSTATIC=1
  • Dynoを起動させる
heroku ps:scale web=1
  • データベース設定
$ heroku run python3 manage.py migrate
$ heroku run python3 manage.py createsuperuser
  • 画面確認
$ heroku open