4
11

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 5 years have passed since last update.

Python Djangoをnginx+gunicornで起動まで

Posted at

メモ書き

1. 各種インストール

# nginx
sudo yum install nginx

# gunicorn
cd /path/to/djangoproject/
pip install gunicorn
pip freeze > requirements.txt

2. 設定ファイル

# cat /vagrant_data/python/project02/myapp/myapp/wsgi.py
"""
WSGI config for myapp project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")

application = get_wsgi_application()
# cat /etc/systemd/system/app.service
[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=nginx
Group=nginx
# WorkingDirectory=<プロジェクトのパス>
WorkingDirectory=/path/to/djangoproject/myapp/

# ExecStart=<gunicornのパス> -w 3 -b <待ち受けアドレス:ポート> <wsgi.pyの場所>
ExecStart=/home/vagrant/.local/share/virtualenvs/project02-qwwEh6I9/bin/gunicorn -w 3 -b 0.0.0.0:8000 myapp.wsgi

[Install]
WantedBy=multi-user.target
# cat /etc/nginx/conf.d/myapp.conf
upstream app_server {
    server 127.0.0.1:8000;      # gunicornで指定したポート
    }

server {
    # the port your site will be served on
    listen      80;
    # the domain name it will serve for
    # server_name <サーバのドメイン名>;   # substitute your machine's IP address or FQDN
    server_name 192.168.33.10;   # substitute your machine's IP address or FQDN
    charset     utf-8;

    # djangoで指定するstatic root
    root /path/to/djangoproject/staticroot/;

    #Max upload size
    # client_max_body_size 75M;   # adjust to taste


access_log  /var/log/nginx/myapp.access.log  main;
error_log  /var/log/nginx/myapp.error.log warn;


location / {
             try_files $uri @proxy_to_app;
        }

        location @proxy_to_app {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass   http://app_server;
        }
    }

nginx側は初期値のままで読み込むようになってると思うけど下記のとおりになってる事を確認。

# cat /etc/nginx/nginx.conf
..snip..
include /etc/nginx/conf.d/*.conf;
..snip..

3. 起動

# gnicornを有効化
sudo systemctl enable app.service
# gunicornを起動
sudo systemctl start app.service
# プロセス確認
contrib-stretch% ps aux | grep guni
nginx    10902  1.7  2.1  82748 22016 ?        Ss   13:10   0:00 /home/vagrant/.local/share/virtualenvs/project02-qwwEh6I9/bin/python3.6m /home/vagrant/.local/share/virtualenvs/project02-qwwEh6I9/bin/gunicorn -w 3 -b 0.0.0.0:8000 myapp.wsgi
nginx    10905  8.3  4.7 166168 48912 ?        S    13:10   0:01 /home/vagrant/.local/share/virtualenvs/project02-qwwEh6I9/bin/python3.6m /home/vagrant/.local/share/virtualenvs/project02-qwwEh6I9/bin/gunicorn -w 3 -b 0.0.0.0:8000 myapp.wsgi
# ログ確認
sudo journalctl -u app

# nginx起動
systemctl start nginx
# プロセス確認
contrib-stretch% ps aux | grep nginx
root       383  0.0  0.0  32608   832 ?        Ss   Aug09   0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx      384  0.0  0.3  33052  3356 ?        S    Aug09   0:00 nginx: worker process
# ログ確認
contrib-stretch% sudo journalctl -u nginx
-- Logs begin at Mon 2018-08-06 15:53:54 GMT, end at Fri 2018-08-10 13:14:59 GMT. --
Aug 06 15:53:55 contrib-stretch systemd[1]: Starting nginx - high performance web server...
Aug 06 15:53:55 contrib-stretch systemd[1]: nginx.service: PID file /var/run/nginx.pid not readable (yet?) after start: No such file or directory
Aug 06 15:53:55 contrib-stretch systemd[1]: Started nginx - high performance web server.
4
11
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
4
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?