尝试在ubuntu上部署django应用程序。
start-server.sh具有
cd /home/django
source env/bin/activate
cd tutorial
gunicorn tutorial.wsgi
如果我抨击start-server.sh,一切都会运行得很好。
所以,我写了以下内容。
保存在/etc/systemd/system/的gunicorn.service看起来像
[Unit]
Description=Gunicorn
After=network.target
[Service]
Type=simple
User=django
ExecStart=/bin/bash /home/django/bin/start-server.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
然后我运行
sudo systemctl enable gunicorn
sudo systemctl start gunicorn
但现在我看到502错误。当我抨击start-server.sh时,一切都很完美。但是,不知怎么的,对于gunicorn来说,它不起作用。
guincorn版本18.0(我尝试了20.0,但没有运气(
sudo systemctl状态gunicorn显示
● gunicorn.service - Gunicorn
Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Wed 2021-06-09 16:56:59 UTC; 2min 5s ago
Main PID: 26285 (code=exited, status=126)
Jun 09 16:56:58 ip-184-168-120-14.ip.secureserver.net systemd[1]: gunicorn.service: Main process exited, code=exited, status=126/n/a
Jun 09 16:56:58 ip-184-168-120-14.ip.secureserver.net systemd[1]: gunicorn.service: Failed with result 'exit-code'.
Jun 09 16:56:59 ip-184-168-120-14.ip.secureserver.net systemd[1]: gunicorn.service: Scheduled restart job, restart counter is at 5.
Jun 09 16:56:59 ip-184-168-120-14.ip.secureserver.net systemd[1]: Stopped Gunicorn.
Jun 09 16:56:59 ip-184-168-120-14.ip.secureserver.net systemd[1]: gunicorn.service: Start request repeated too quickly.
Jun 09 16:56:59 ip-184-168-120-14.ip.secureserver.net systemd[1]: gunicorn.service: Failed with result 'exit-code'.
Jun 09 16:56:59 ip-184-168-120-14.ip.secureserver.net systemd[1]: Failed to start Gunicorn.
在start-server.sh
顶部添加以下行:
#!/bin/bash
这被称为";shebang";线Unixlike系统通常不注意文件扩展名,因此忽略了文件名以.sh
结尾的事实。系统会查看这一行,以确定该文件将由bash执行。
此外,您需要确保文件是可执行的:
chown +x /home/django/bin/start-server.sh
但是,您根本不需要start-server.sh
,因为它所做的一切都可以由systemd完成。将此行添加到[Service]
部分:
WorkingDirectory=/home/django/tutorial
并将ExecStart
替换为:
ExecStart=/home/django/env/bin/gunicorn tutorial.wsgi
仅此而已。如果你觉得它令人困惑,请阅读virtualenv demystified。
不要在bash脚本中执行cd
-命令,而是使用全路径
source /home/django/env/bin/activate
gunicorn /home/django/env/bin/activate/tutorial.wsgi