LoginSignup
0
2

More than 3 years have passed since last update.

socket経由でDjango+gunicon+nginx

Posted at

django の調整

settings.py をデプロイ用に変更。

settings.py
DEBUG=False
ALLOWED_HOSTS = ['*']
STATIC_URL = '/static/'
STATIC_ROOT = '/usr/share/nginx/html/static'

MEDIA_URL = '/media/'
MEDIA_ROOT = '/usr/share/nginx/html/media'

DEBUG=Falseで起動すると ALLOWED_HOSTS で指定したIPからしかアクセスを受け付けなくなる(400を返す)。
本来はきちんと設定すべきだが、ここではとりあえず *ですべて通す。

static ファイルを nginx 用置き場にコピーする

STATIC_URL で指定された場所においてあるファイルを STATIC_ROOT で指定した場所にコピーする。

$ python manage.py collectstatic

nginx用 static 置き場を作成

後々、nginxで読みこむ静的ファイルを置く場所を作る。

$ mkdir /usr/share/nginx/html/static
$ mkdir /usr/share/nginx/html/media

gunicorn のインストール

$ pipenv install gunicorn

起動する場合は$ pipenv run gunicorn ...のようにする。

gunicorn をsocketに紐付ける

socketファイルの作成

$ sudo vi /etc/systemd/system/gunicorn.socket
/etc/systemd/system/gunicorn.socket
[Unit]
Description=gunicorn socket

[Socket]
ListenStream=/run/gunicorn.sock

[Install]
WantedBy=sockets.target

systemファイルの作成

$ sudo vi /etc/systemd/system/gunicorn.service

pipenv 環境のもと gunicorn を実行する。
pipenvの場所は which pipenv で調べる。

/etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target

[Service]
PIDFile=/run/gunicorn/pid
User=root
WorkingDirectory=Djangoを配置したパス
ExecStart=/usr/local/bin/pipenv run gunicorn --access-logfile - --workers 3 --bind unix:/run/gunicorn.sock application_name.wsgi:application

[Install]
WantedBy=multi-user.target

デーモン起動

$ systemctl start gunicorn.socket

/run/gunicorn.sock が存在していれば問題ない。
以下のようにして確かめる。

$ file /run/gunicorn.sock
Output/run/gunicorn.sock: socket

gunicorn.service は 明示的に systemctl start する必要はない、ソケットが受信すれば起動する。
試しに、curlなどでソケットにアクセスすると gunicorn を介して、djangoから html が送られてくる。

$ curl --unix-socket /run/gunicorn.sock localhost

特に問題なければ自動化。

$ sudo systemctl enable gunicorn.socket

nginx の調整

sites-available ディレクトリの作成。

mkdir /etc/nginx/sites-available

作ったディレクトリを sites-enabled としてシンボリックリンクを張る。

sudo ln -s /etc/nginx/sites-available/ /etc/nginx/sites-enabled

http で動かすように設定。

$ sudo vi /etc/nginx/sites-available/project.cfg
/etc/nginx/sites-available/project.cfg
server {
   listen 80;
   server_name サーバのドメイン名かIP;

   location = /favicon.ico { access_log off; log_not_found off; }

   location /static {
       alias /usr/share/nginx/html/static;
   }
   location /media {
       alias /usr/share/nginx/html/media;
   }


   location / {
       proxy_pass http://unix:/run/gunicorn.sock;
   }
}

作った設定を参照するように etc/nginx/nginx.conf を変更

$ sudo vi /etc/nginx/nginx.conf
/etc/nginx/nginx.conf
...

http {

...

   include /etc/nginx/conf.d/*.conf;
   include /etc/nginx/sites-enabled/*; #追記

設定ファイルのチェック。

$ sudo nginx -t

起動と自動化。

$ sudo systemctl start nginx
$ sudo systemctl enable nginx

Djangoを更新した場合

コードを適用したあとは、gunicorn を起動し直すだけでいい。

$ sudo systemctl restart gunicorn
0
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
0
2