diff --git a/a_long_dianjing/settings.py b/a_long_dianjing/settings.py index 8ae3595..fa7ea50 100644 --- a/a_long_dianjing/settings.py +++ b/a_long_dianjing/settings.py @@ -125,6 +125,7 @@ DATABASES = { 'HOST': app_secrets.DATABASE_HOST, 'PASSWORD': app_secrets.DATABASE_PASSWORD, 'PORT': app_secrets.DATABASE_PORT, + 'CONN_MAX_AGE': 600, 'OPTIONS': { 'charset': 'utf8mb4', 'init_command': "SET time_zone='+08:00'", diff --git a/deploy/systemd/README.md b/deploy/systemd/README.md new file mode 100644 index 0000000..3b53b07 --- /dev/null +++ b/deploy/systemd/README.md @@ -0,0 +1,47 @@ +# Gunicorn systemd(生产环境) + +修复 sync×9 + 访问日志灌 journal 导致的卡顿。 + +## 服务器安装(一次性) + +```bash +cd /opt/1panel/apps/openresty/nginx/www/sites/43.142.166.152.443/django +git pull origin main + +# 备份旧 service(若有) +cp /etc/systemd/system/gunicorn-dianjing.service /etc/systemd/system/gunicorn-dianjing.service.bak 2>/dev/null || true + +cp deploy/systemd/gunicorn-dianjing.service /etc/systemd/system/gunicorn-dianjing.service + +pkill -f 'gunicorn.*a_long_dianjing' 2>/dev/null || true +sleep 2 + +systemctl daemon-reload +systemctl enable gunicorn-dianjing.service +systemctl restart gunicorn-dianjing.service + +systemctl status gunicorn-dianjing.service --no-pager | head -12 +pgrep -af gthread | head -2 +curl -s -o /dev/null -w '8001: HTTP:%{http_code} %{time_total}s\n' http://127.0.0.1:8001/ +``` + +## 1Panel + +关闭该站点 **Python 运行环境自动管理**,避免与 systemd 冲突。 + +## 日常 + +```bash +systemctl restart gunicorn-dianjing.service +tail -f /tmp/gunicorn-error.log +``` + +## journal 限制(可选,建议) + +```bash +grep -q '^SystemMaxUse=' /etc/systemd/journald.conf \ + && sed -i 's/^SystemMaxUse=.*/SystemMaxUse=200M/' /etc/systemd/journald.conf \ + || echo 'SystemMaxUse=200M' >> /etc/systemd/journald.conf +journalctl --vacuum-size=200M +systemctl restart systemd-journald +``` diff --git a/deploy/systemd/gunicorn-dianjing.service b/deploy/systemd/gunicorn-dianjing.service new file mode 100644 index 0000000..10cd659 --- /dev/null +++ b/deploy/systemd/gunicorn-dianjing.service @@ -0,0 +1,17 @@ +[Unit] +Description=Gunicorn a_long_dianjing Django Service +After=network.target + +[Service] +Type=simple +User=root +Group=root +WorkingDirectory=/opt/1panel/apps/openresty/nginx/www/sites/43.142.166.152.443/django +ExecStart=/usr/local/bin/gunicorn --bind 127.0.0.1:8001 --workers 4 --threads 4 --worker-class gthread --timeout 120 --log-level warning --access-logfile /dev/null --error-logfile /tmp/gunicorn-error.log a_long_dianjing.wsgi:application +Restart=always +RestartSec=3 +StandardOutput=null +StandardError=file:/tmp/gunicorn-systemd.err.log + +[Install] +WantedBy=multi-user.target