2
1

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 1 year has passed since last update.

Django を gunicorn で Nginx にデプロイする

Last updated at Posted at 2021-11-04

こちらのページを参考にしました。
nginxとgunicornでDjangoをデプロイする

Django のプロジェクトが /var/www/html/proj01 にあるとします。

$ tree -L 1 /var/www/html/proj01/
/var/www/html/proj01/
├── api
├── db.sqlite3
├── home
├── manage.py
└── proj01

#gunicorn の設定ファイルの作成#

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

[Socket]
ListenStream=/run/gunicorn.sock

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

[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/html/proj01
ExecStart=/usr/bin/gunicorn \
          --access-logfile - \
          --workers 3 \
          --bind unix:/run/gunicorn.sock \
          proj01.wsgi:application
[Install]
WantedBy=multi-user.target

サービスの起動

sudo systemctl start gunicorn

確認

sudo systemctl status gunicorn

socket の確認

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

#Nginx の設定ファイルの修正#

/etc/nginx/sites-available/default
server {
(省略)
location /api/ {
     include proxy_params;
     proxy_pass http://unix:/run/gunicorn.sock;
     }
}

確認

```bash
sudo nginx -t

Nginx の再起動

sudo systemctl restart nginx

アクセスして確認

http http://localhost/api/
2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?