1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

nginx・gunicorn・djangoの環境におけるTips

Last updated at Posted at 2021-02-25

webアプリケーションを開発したものの、インフラ周りが弱いため、Tipsとしてまとめていくつもりです。
適宜、更新はしていくつもりです。間違いだった場合は指摘いただければ幸いです。

クラウドには、digital oceanを使用しています。
基本的には、現場で使える Django の教科書《実践編》を模倣しています。
アーキテクチャはこちらから

技術スタック

  • python(3.8.5)
  • Django(3.0.8)
  • nginx(1.18.0)
  • gunicorn(20.0.4)
  • ubuntu(20.04)

 ファイル構成

/home/webapp/mysite/[django-project]

変更を反映させたい時

nginx、gunicorn、djangoなど色々いじると思います。
開発環境とは異なり、いじったらrestartしないといけないです。

gunicorn周りを変更した時

反映させるファイルは、/etc/systemd/system/mysite.service/etc/systemd/system/mysite.socketです。特に、daemon-reloadも必要らしい。

ということで、

sudo systemctl daemon-reload
(↑sudoをつけないと怒られた)

sudo systemctl restart mysite.service
sudo systemcctl restart mysite.socket

毎回、↑を書くのが面倒だったので、.bashrcにaliasを設定しました。

fuction all-reset(){
    sudo systemctl restart mysite.service
    sudo systemcctl restart mysite.socket
}
alias allreset = all-reset

djangoの中身(models.pyとかviews.pyとか)を変更したとき

なぜかわかりませんが、ここら辺の変更を適用するためには
sudo systemctl restart mysite.service
が必要でした。

もちろん、models.py周りを変更した際はpython manage.py makemigrationspython manage.py migrateも忘れずに。

また、開発環境では、sqlite3、本番環境では、postgresqlmysqlと使い分けがある場合があるかと思います。
(というより、開発時にちゃんとアーキテクチャを設計せず、ノリでデフォルトのsqlite3を利用した)
そんなときは、manage.pyを二つ定義しました。こうして中のbase.pyを別々にしてあげれば、OK.

nginx周りを変更させたとき

restartをしましよう。

sudo systemctl restart nginx

状態を知りたいとき

基本的には、sudo systemctl statusでOKでした。

ex.
sudo systemctl status nginx
sudo systemctl status mysite.service
sudo systemctl status mysite.socket

後は、error_logですね。
/var/log/nginx/error_logです。

最終更新日

  • 2020.02.25
  • 2021.05.27
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?