Django 服务器部署指南
来源:
sources/在服务器启动django.18913620.md
安装 Gunicorn
pip install gunicorn
创建 systemd 服务
创建 /etc/systemd/system/gunicorn.service:
[Unit]
Description=gunicorn daemon for Django
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/home/youruser/your_project
ExecStart=/home/youruser/your_env/bin/gunicorn \
--workers 3 \
--bind 0.0.0.0:8000 \
your_project.wsgi:application
Restart=always
[Install]
WantedBy=multi-user.target
启动服务
sudo systemctl daemon-reload
sudo systemctl enable gunicorn
sudo systemctl start gunicorn
常见问题
Nginx 静态资源 403 错误
原因: Nginx 用户没有权限访问 Django 静态文件
解决: 将 Nginx 用户添加到 Django 守护进程用户的组内
sudo usermod -aG www-data nginx_user
相关页面
- Django - Django Web 框架
- Django开发实践笔记 - Django 开发完整指南
- Gunicorn - WSGI HTTP 服务器
- Systemd_Service_Create - 创建 systemd 守护进程
最后更新: 2026-04-30