Nginx+Unit+Djangoで本番環境の設定をする

仮想環境構築とpip

コードを管理するアプリの場合

python3 -m venv venv-django
source venv-django/bin/activate
pip3 install django
pip3 install django-bootstrap5
pip3 install markdown
pip3 install pygments

Nginxのconfファイル

sudo vim /etc/nginx/sites-available/demo.dattesar.com.conf

demo.dattesar.com.conf

  • locationの設定で最後に/(スラッシュ)があるかどうかで動作が変わるので、調べる必要あり
  • /home/xxxxx/に改変している部分あり。このままでは使えない。
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
server {

	server_name demo.dattesar.com;

	proxy_set_header	Host		$host;
	proxy_set_header	X-Real-IP	$remote_addr;
	proxy_set_header	X-Forwarded-Host	$host;
	proxy_set_header	X-Forwarded-Server	$host;
	proxy_set_header	X-Forwarded-For		$proxy_add_x_forwarded_for;

	access_log /var/log/nginx/demo.dattesar.com-access.log;
	error_log /var/log/nginx/demo.dattesar.com-error.log;

	root /var/www/demo.dattesar.com/html;
	index index.html;

	location / {
		try_files $uri $uri/ =404;
	}

	location /demo/ {
		alias /home/xxxxx/demo.dattesar.com/demo/;
		index index.html;
	}

	location /djangotest/ {
		proxy_pass http://localhost:8400;
		proxy_redirect http:// https://;

#		try_files $uri $uri/ =404;
	}

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/demo.dattesar.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/demo.dattesar.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
    if ($host = demo.dattesar.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


	listen 80;
	listen [::]:80;

	server_name demo.dattesar.com;
    return 404; # managed by Certbot


}

Nginxの構文チェックと再起動

sudo nginx -t
sudo systemctl restart nginx

Unitの設定

config.json

  • /home/xxxxx/に改変している部分あり。このままでは使えない。
{
        "listeners": {
                "*:8300": {
                        "application": "receipt"
                },

                "*:8400": {
                        "application": "djangotest"
                }
        },

        "applications": {
                "receipt": {
                        "type": "python 3.8",
                        "path": "/home/xxxxx/receipt.dattesar.com/html/",
                        "home": "/home/xxxxx/receipt.dattesar.com/receipt_venv/",
                        "module": "wsgi",
                        "callable": "app"
                },

                "djangotest": {
                        "type": "python 3.8",
                        "path": "/home/xxxxx/demo.dattesar.com/djangotest/conponentproject/",
                        "home": "/home/xxxxx/demo.dattesar.com/djangotest/venv-django/",
                        "module": "conponentproject.wsgi"
                }
        }
}

設定を反映

sudo curl -X PUT --data-binary @config.json --unix-socket /run/control.unit.sock http://localhost/config/

再起動

sudo systemctl restart unit

Djangoのファイル

settings.py

  • DEBUGはFalseにする
  • ALLOWED_HOSTS = ['demo.dattesar.com/djangotest']ではエラーが出た
  • csrf_tokenのエラーが出るので、CSRF_TRUSTED_ORIGINSを追加
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False 

ALLOWED_HOSTS = ['demo.dattesar.com']

CSRF_TRUSTED_ORIGINS = ['https://demo.dattesar.com']

project/project/urls.py

  • projectの方のurls.pyで、djangotestを追加
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('djangotest/admin/', admin.site.urls),
    path('djangotest/', include('conponentapp.urls')),
]

所有者をunitに変更

  • 所有者をunitに変更しないとsqlite3に書き込めなかった
sudo chown -R unit:unit demo.dattesar.com/djangotest/
sudo systemctl restart unit

管理画面

  • まだ、管理画面にCSSが適用されていない
  • staticディレクトリの設定が、DjangoとNginxで必要らしい