fix: 修复 Gunicorn systemd 配置与 MySQL 连接复用以缓解卡顿

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
XingQue
2026-07-09 04:21:47 +08:00
parent de3ff211a6
commit 303216f918
3 changed files with 65 additions and 0 deletions

View File

@@ -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'",

47
deploy/systemd/README.md Normal file
View File

@@ -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
```

View File

@@ -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