WEBサイトへのDjangoアプリ実装

◆ 現在の想定構成(例)
[ Client ] ─ HTTPS ─> [ Nginx on Pi 4B ] ─> [ 静的Webサイト ]
これに Django アプリを /reserve/ や /booking/ などのパスで統合します。

◆ 追加構成案
[ クライアント ]


[ Nginx(既存Web)]
├── location / → 静的ファイル or HTML
└── location /reserve/ → Gunicorn経由で Django (予約アプリ)

[ Gunicorn (WSGI) ]

[ Django App ]

[ DB (SQLiteやPostgreSQL) ]

1. Django予約アプリを作成(または既存プロジェクトに追加)
django-admin startproject reserve_site
cd reserve_site
python manage.py startapp booking

設定:
settings.py に booking を追加
URLは /reserve/ などにする

2. Gunicornでアプリを起動確認
pip install gunicorn
gunicorn –bind 127.0.0.1:8001 reserve_site.wsgi:application

→ ブラウザで http://127.0.0.1:8001/reserve/ で表示確認

3. Nginxの設定に /reserve/ を追記

server {
listen 80;
server_name あなたのdomain名 www.あなたのdomai名;

# 静的HTMLサイト(例)
root /var/www/html;

location / {
index index.html;
}

# Django予約アプリのルーティング
location /reserve/ {
proxy_pass http://127.0.0.1:8001;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}

location /static/ {
alias /home/pi/reserve_site/static/;
}
}

・reserve_site/static/ には collectstatic で静的ファイルを集めておきます

4. Gunicornのsystemd化(起動スクリプト)
/etc/systemd/system/gunicorn-reserve.service:

[Unit]
Description=Gunicorn for reserve_site
After=network.target

[Service]
User=pi
WorkingDirectory=/home/pi/reserve_site
ExecStart=/home/pi/venv/bin/gunicorn –workers 3 –bind 127.0.0.1:8001 reserve_site.wsgi:application
Restart=always

[Install]
WantedBy=multi-user.target

起動:
sudo systemctl start gunicorn-reserve
sudo systemctl enable gunicorn-reserve

5. HTTPS化(既にLet’s Encrypt利用中ならOK)

✅ ユーザーに見える構成(例)
URL 内容
https://www.あなたのdomain名/ 現在の静的Webサイト
https://www.あなたのdomain名/reserve/ Django予約アプリのページ

✅ おすすめ追加オプション
SQLiteではなく PostgreSQL(軽量かつ信頼性高)
簡易認証:django.contrib.auth を使って予約画面の管理も追加可能
メール通知機能:django.core.mail+Gmailなど
モバイル対応:テンプレートでBootstrapなど導入

\ 最新情報をチェック /