LoginSignup
24
18

More than 5 years have passed since last update.

nginxでdjangoとuwsgi

Last updated at Posted at 2019-03-25
おことわり

社内向けに書いたものを公開したものです。
怪しい単語を置換しているだけなので動くと思いますが、動かないかもしれません。
そしてdjangoとかあまりよくわからない人間が書いているので、内容の正確性は保証しませんし、センスが悪いのは承知です。。。

やりたいこと

nginxでuwsgi使ってdjangoをくるんでいろいろしたい。
一般ユーザだけでweb周りの開発・運用をできるような構成にしたい。

環境

Amazon Linux 2
python 3.5.5
nginx 1.12.2
Django 2.1.5
uwsgi 2.0.18
PyMySQL 0.9.3

その他もろもろ

ドメイン名 www.test-website.name
プロジェクト名 hogehoge
アプリケーション myapp

ディレクトリ構成

■ 一般ユーザだけで完結させるために、djangoログとnginxログを全部/hogehogeROOT/logs配下にまとめた

■ hogehogeROOT配下のディレクトリやファイルのOWNERはfugaaplで統一
=> 「makemigrationsができるけどcollectstaticできない」「collectstaticできるけどmakemigrationsできない」問題がこれで解決。

■ ついでにconfigもhogehogeROOT/confに集約して、nginxとかsystemdにシンボリックリンクで渡した
=> hogehoge絡みのconfigはほぼすべてここでコントロールできるので、管理性が向上。

/hogehogeROOT
|-- hogehoge  <------------------projectroot
|   |-- manage.py
|   |-- myapp <---------------startappしたやつ
|   |   |-- __init__.py
|   |   |-- __pycache__
|   |   |-- admin.py
|   |   |-- apps.py
|   |   |-- migrations
|   |   |-- models.py
|   |   |-- tests.py
|   |   |-- urls.py
|   |   `-- views.py
|   |-- hogehoge
|   |   |-- __init__.py
|   |   |-- __pycache__
|   |   |-- settings.py
|   |   |-- urls.py
|   |   `-- wsgi.py
|   `-- static
|       `-- admin
|-- conf
|   |-- test-website.name.conf
|   |-- uwsgi-hogehoge.service
|   |-- uwsgi.ini
|   `-- uwsgi_params
`-- logs
    |-- djangolog
    |   `-- hogehoge.log
    `-- nginx
        |-- hogehoge-error_log
        `-- hogehoge-access_log

Config

uwsgiなconfigたち
config 説明
uwsgi-hogehoge.service serviceコマンドでuwsgiを動かすためのconfig
uwsgi.ini uwsgiを起動するためのconfig。service uwsgi-hogehogeから読まれる。
uwsgi_params 謎のuwsgi_params。お作法的な?
uwsgi-hogehoge.service
[Unit]
Description=uWSGI service for mysite

[Service]
ExecStartPre=/bin/bash -c 'mkdir -p /var/run/uwsgi.hogehoge; chown root:fugaapl /var/run/uwsgi.hogehoge; chmod g+w /var/run/uwsgi.hogehoge'
ExecStart=/bin/bash -c 'DJANGO_SETTINGS_MODULE=hogehoge.settings; uwsgi --ini /hogehogeROOT/conf/uwsgi.ini'
Restart=always
KillSignal=SIGQUIT
Type=notify
StandardError=syslog
NotifyAccess=all

[Install]
WantedBy=multi-user.target
uwsgi.ini
[uwsgi]
uid = fugaapl
gid = fugaapl
chdir = /hogehogeROOT/hogehoge/
module = hogehoge.wsgi
env DJANGO_SETTINGS_MODULE = hogehoge.settings
pidfile = /var/run/uwsgi.hogehoge/master.pid
socket = /var/run/uwsgi.hogehoge/master.sock
#http = 127.0.0.1:8000
master = true
vaccum = true
uwsgi_param  QUERY_STRING       $query_string;
uwsgi_param  REQUEST_METHOD     $request_method;
uwsgi_param  CONTENT_TYPE       $content_type;
uwsgi_param  CONTENT_LENGTH     $content_length;

uwsgi_param  REQUEST_URI        $request_uri;
uwsgi_param  PATH_INFO          $document_uri;
uwsgi_param  DOCUMENT_ROOT      $document_root;
uwsgi_param  SERVER_PROTOCOL    $server_protocol;
uwsgi_param  REQUEST_SCHEME     $scheme;
uwsgi_param  HTTPS              $https if_not_empty;

uwsgi_param  REMOTE_ADDR        $remote_addr;
uwsgi_param  REMOTE_PORT        $remote_port;
uwsgi_param  SERVER_PORT        $server_port;
uwsgi_param  SERVER_NAME        $server_name;
nginxなconfigたち
config 説明
test-website.name.conf nginxのconf
test-website.name.conf
server {
    listen      80;
    server_name www.test-website.name;

    access_log  /hogehogeROOT/logs/nginx/hogehoge-access_log main;
    error_log   /hogehogeROOT/logs/nginx/hogehoge-error_log;

    location / {
        include uwsgi_params;
        uwsgi_pass  unix:/var/run/uwsgi.hogehoge/master.sock;
    }

    location /static/ {
        root  /hogehogeROOT/hogehoge/;
    }
}

設定方法

djangoは入ってる前提で。
urls.pyとかその辺は適宜好きなように変える。
とりあえず何も考えずrootで進める。

① nginxインストール

amazon linux exstrasで入れる。

Q:Amazon Linux Extras とは何ですか?
Extras は、安定したオペレーティングシステムで新しいバージョンのアプリケーションソフトウェアを利用可能にする Amazon Linux 2 のメカニズムで、2023 年 6 月 30 日までサポートされます。Extras は、OS の安定性を確保しつつ、最新のソフトウェアを利用できるようにするうえで役立ちます。例えば、5 年間サポートされる安定したオペレーティングシステムに MariaDB の新しいバージョンをインストールすることができます。Extras の例には、Ansible 2.4.2、memcached 1.5、nginx 1.12、Postgresql 9.6、MariaDB 10.2、Go 1.9、Redis 4.0、R 3.4、Rust 1.22.1 などがあります。

amazon-linux-extras install nginx
② uwsgiインストール

何も考えずpip install

pip install uwsgi
③ディレクトリ作成
mkdir -p /hogehogeROOT/hogehoge
mkdir -p /hogehogeROOT/conf
mkdir -p /hogehogeROOT/logs
mkdir -p /var/run/uwsgi.hogehoge
④とりあえずstartproject & startapp
django-admin manage.py startproject hogehoge
django-admin manage.py startapp myapp
⑤ uwsgiのconfigを作成
cd /hogehogeROOT/conf
vim uwsgi.ini
vim uwsgi-hogehoge.service
⑥ uwsgiをsystemctlとかserviceで動くように、シンボリックリンクを設定&確認
ln -s /hogehogeROOT/conf/uwsgi-hogehoge.service /etc/systemd/system/uwsgi-hogehoge.service
ll /etc/systemd/system/uwsgi-hogehoge.service
⑦とりあえずdaemon-reload
systemctl daemon-reload
⑧ uwsgiがsystemctlとかserviceで動いてくれるか確認
systemctl start uwsgi-hogehoge
systemctl status uwsgi-hogehoge
sytemctl stop uwsgi-hogehoge
systemctl status uwsgi-hogehoge
⑨ nginxのconfigを作成
cd /hogehogeROOT/conf
vim test-website.name.conf
⑩ nginxのconf.d配下にシンボリックリンクを張る
cd /etc/nginx/conf.d/
ln -s /hogehogeROOT/conf/test-website.name.conf test-website.name.conf
ll test-website.name.conf
⑪ nginx.confを修正する

ユーザを/var/run/uwsgi.hogehogeのグループに属するユーザに変更する。
変えとかないとpidファイルとsocketが作れなくて、PermissionDeniedでコケる。

/etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user fugaapl; <-----------ユーザを変更する
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
~以下略~
⑫ nginxが動いたり止まったりするか確認する
systemctl start nginx
systemctl status nginx
systemctl stop nginx
systemctl status nginx
⑬ uwsgiとnginxを起動する
systemctl uwsgi-hogehoge start
systemctl nginx start
⑭ 外部から叩いてみる

https://test-website.name
-> djangoの404画面が返ればOK

⑮ ログ出力を確認する 1
tail /hogehogeROOT/logs/djangolog/hogehoge.log
tail /hogehogeROOT/logs/nginx/hogehoge-access_log
tail /hogehogeROOT/logs/nginx/hogehoge-error_log

-> 何かしら出てればOK

⑯ もろもろ終わったら丸ごとOWNER変更
chown -R fugaapl:fugaapl /hogehogeROOT/

備考

ドメイン名は環境に合わせて変えて。

あとは好きにして。


  1. djangoログ設定はsettings.pyとかに入っている前提 

24
18
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
24
18