安装
pip install gunicorn
配置配置 Gunicorn 的 systemd 服务文件
创建 Gunicorn 服务文件:
sudo vim /etc/systemd/system/gunicorn.service
添加以下内容到服务文件:
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=youruser
Group=www-data
WorkingDirectory=/home/youruser/myproject
ExecStart=/home/youruser/myprojectenv/bin/gunicorn --access-logfile /home/youruser/myproject/logs/gunicorn-access.log --error-logfile /home/youruser/myproject/logs/gunicorn-error.log --workers 3 --bind unix:/home/youruser/myproject/myproject.sock myproject.wsgi:applicationunix:/home/youruser/myproject/myproject.sock myproject.wsgi:application
ExecReload=/bin/kill -HUP $MAINPID
KillMode=mixed
TimeoutStopSec=5
PrivateTmp=true
[Install]
WantedBy=multi-user.target
确保日志目录存在,并且具有正确的权限:
sudo mkdir -p /home/youruser/myproject/logs
sudo chown -R ubuntu:www-data /home/youruser/myproject/logs
请确保替换以下内容:
youruser:你的 Ubuntu 用户名
/home/youruser/myproject:你的 Django 项目目录
/home/youruser/myprojectenv:你的虚拟环境目录
myproject.wsgi:application
crm.wsgi:application 中的 crm 是你的 Django 项目的正确名称
crm为wsgi.py的所在文件夹
重新加载 systemd 配置:
sudo systemctl daemon-reload
启动 Gunicorn 服务:
sudo systemctl start gunicorn
使 Gunicorn 服务在系统启动时自动启动:
sudo systemctl enable gunicorn
配置nginx
server {
listen 80;
server_name your_domain_or_IP;
location / {
proxy_pass http://unix:/home/youruser/myproject/myproject.sock;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /static/ {
alias /home/youruser/myproject/static/;
}
}
请确保替换以下内容:
your_domain_or_IP:你的域名或服务器 IP 地址或者127.0.0.1
/home/youruser/myproject:你的 Django 项目目录
平滑重启 Gunicorn:
实现零停机重启
sudo systemctl reload gunicorn
Gunicorn其他命令
查看日志
sudo journalctl -u gunicorn
查看状态
sudo systemctl status gunicorn
启动
sudo systemctl start gunicorn
标签:youruser,Gunicorn,部署,sudo,django,myproject,home,gunicorn
From: https://www.cnblogs.com/chunyouqudongwuyuan/p/18256385