运行 Gunicorn 的自定义系统服务无法正常工作
Custom systemd service to run Gunicorn not working
我正在尝试将我的 Django 网站部署到 Ubuntu 服务器。我正在学习本教程:linuxhint.com/create_django_app_ubuntu/。但是,Gunicorn 服务不起作用。
我的网站位于 /home/django/blog
。
我的 Python 3.6 virtualenv 在 /home/django/.venv/bin/activate
(-rwxr-xr-x 1 django root 2207 Sep 21 14:07 activate
) 激活。
启动服务器的脚本在/home/django/bin/start-server.sh
(-rwxr-xr-x 1 django root 69 Sep 21 15:50 start-server.sh
),内容如下:
cd /home/django
source .venv/bin/activate
cd blog
gunicorn blog.wsgi
运行 这个脚本手动工作得很好。
Gunicorn 服务位于 /etc/systemd/system/gunicorn.service
,内容为:
[Unit]
Description=Gunicorn
After=network.target
[Service]
Type=simple
User=django
ExecStart=/home/django/bin/start-server.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
运行 systemctl status gunicorn.service
给出:
● gunicorn.service - Gunicorn
Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2020-09-21 16:15:17 UTC; 6s ago
Process: 1114 ExecStart=/home/django/bin/start-server.sh (code=exited, status=203/EXEC)
Main PID: 1114 (code=exited, status=203/EXEC)
Sep 21 16:15:17 example.com systemd[1]: gunicorn.service: Failed with result 'exit-code'.
Sep 21 16:15:17 example.com systemd[1]: gunicorn.service: Service hold-off time over, scheduling restart.
Sep 21 16:15:17 example.com systemd[1]: gunicorn.service: Scheduled restart job, restart counter is at 5.
Sep 21 16:15:17 example.com systemd[1]: Stopped Gunicorn.
Sep 21 16:15:17 example.com systemd[1]: gunicorn.service: Start request repeated too quickly.
Sep 21 16:15:17 example.com systemd[1]: gunicorn.service: Failed with result 'exit-code'.
Sep 21 16:15:17 example.com systemd[1]: Failed to start Gunicorn.
Sep 21 16:15:18 example.com systemd[1]: gunicorn.service: Start request repeated too quickly.
Sep 21 16:15:18 example.com systemd[1]: gunicorn.service: Failed with result 'exit-code'.
Sep 21 16:15:18 example.com systemd[1]: Failed to start Gunicorn.
Sep 21 14:22:36 example.com systemd[7906]: gunicorn.service: Failed to execute command: Permission denied
Sep 21 14:22:36 example.com systemd[7906]: gunicorn.service: Failed at step EXEC spawning /home/django/bin/start-server.sh: Permission denied
Sep 21 14:23:40 example.com systemd[7940]: gunicorn.service: Failed to execute command: Permission denied
Sep 21 14:23:40 example.com systemd[7940]: gunicorn.service: Failed at step EXEC spawning /home/django/bin/start-server.sh: Permission denied
Sep 21 14:24:47 example.com systemd[7958]: gunicorn.service: Failed to execute command: Permission denied
Sep 21 14:24:47 example.com systemd[7958]: gunicorn.service: Failed at step EXEC spawning /home/django/bin/start-server.sh: Permission denied
Permission denied
.
.
.
我运行chown -R django:django /home/django
。现在,ls -lah /home/django
的输出是:
total 32K
drwxr-xr-x 5 django django 4.0K Sep 21 14:19 .
drwxr-xr-x 3 root root 4.0K Sep 21 14:04 ..
-rw-r--r-- 1 django django 220 Apr 4 2018 .bash_logout
-rw-r--r-- 1 django django 3.7K Apr 4 2018 .bashrc
-rw-r--r-- 1 django django 807 Apr 4 2018 .profile
drwxr-xr-x 4 django django 4.0K Sep 21 14:07 .venv
drwxr-xr-x 2 django django 4.0K Sep 21 15:58 bin
drwxr-xr-x 3 django django 4.0K Sep 21 14:08 blog
解决方案
感谢Dmitry Belaventsev,解决这个问题的方法是改变
ExecStart=/home/django/bin/start-server.sh
至
ExecStart=/bin/bash /home/django/bin/start-server.sh
在文件中 /etc/systemd/system/gunicorn.service
.
您的 systemd 服务已设置为代表 django
用户执行脚本。与此同时:
ls -lah /home/django
total 32K
drwxr-xr-x 5 django django 4.0K Sep 21 14:19 .
drwxr-xr-x 3 root root 4.0K Sep 21 14:04 ..
-rw-r--r-- 1 django django 220 Apr 4 2018 .bash_logout
-rw-r--r-- 1 django django 3.7K Apr 4 2018 .bashrc
-rw-r--r-- 1 django django 807 Apr 4 2018 .profile
drwxr-xr-x 4 django root 4.0K Sep 21 14:07 .venv
drwxr-xr-x 2 root root 4.0K Sep 21 15:58 bin
drwxr-xr-x 3 root root 4.0K Sep 21 14:08 blog
如您所见:
drwxr-xr-x 3 root root 4.0K Sep 21 14:04 ..
和
drwxr-xr-x 2 root root 4.0K Sep 21 15:58 bin
这意味着:
/home
目录属于 root:root
/home/django/bin
属于 root:root
让 systemd
代表 django
用户执行 bash 脚本:
- 该脚本应该是可执行的
- 所有父目录都应该有执行权限
django
用户 应该可以使用所有这些目录和脚本
最快的解决方案:
chown -R /home/django django:django
你也可以玩组和组权限。
我正在尝试将我的 Django 网站部署到 Ubuntu 服务器。我正在学习本教程:linuxhint.com/create_django_app_ubuntu/。但是,Gunicorn 服务不起作用。
我的网站位于 /home/django/blog
。
我的 Python 3.6 virtualenv 在 /home/django/.venv/bin/activate
(-rwxr-xr-x 1 django root 2207 Sep 21 14:07 activate
) 激活。
启动服务器的脚本在/home/django/bin/start-server.sh
(-rwxr-xr-x 1 django root 69 Sep 21 15:50 start-server.sh
),内容如下:
cd /home/django
source .venv/bin/activate
cd blog
gunicorn blog.wsgi
运行 这个脚本手动工作得很好。
Gunicorn 服务位于 /etc/systemd/system/gunicorn.service
,内容为:
[Unit]
Description=Gunicorn
After=network.target
[Service]
Type=simple
User=django
ExecStart=/home/django/bin/start-server.sh
Restart=on-failure
[Install]
WantedBy=multi-user.target
运行 systemctl status gunicorn.service
给出:
● gunicorn.service - Gunicorn
Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2020-09-21 16:15:17 UTC; 6s ago
Process: 1114 ExecStart=/home/django/bin/start-server.sh (code=exited, status=203/EXEC)
Main PID: 1114 (code=exited, status=203/EXEC)
Sep 21 16:15:17 example.com systemd[1]: gunicorn.service: Failed with result 'exit-code'.
Sep 21 16:15:17 example.com systemd[1]: gunicorn.service: Service hold-off time over, scheduling restart.
Sep 21 16:15:17 example.com systemd[1]: gunicorn.service: Scheduled restart job, restart counter is at 5.
Sep 21 16:15:17 example.com systemd[1]: Stopped Gunicorn.
Sep 21 16:15:17 example.com systemd[1]: gunicorn.service: Start request repeated too quickly.
Sep 21 16:15:17 example.com systemd[1]: gunicorn.service: Failed with result 'exit-code'.
Sep 21 16:15:17 example.com systemd[1]: Failed to start Gunicorn.
Sep 21 16:15:18 example.com systemd[1]: gunicorn.service: Start request repeated too quickly.
Sep 21 16:15:18 example.com systemd[1]: gunicorn.service: Failed with result 'exit-code'.
Sep 21 16:15:18 example.com systemd[1]: Failed to start Gunicorn.
Sep 21 14:22:36 example.com systemd[7906]: gunicorn.service: Failed to execute command: Permission denied
Sep 21 14:22:36 example.com systemd[7906]: gunicorn.service: Failed at step EXEC spawning /home/django/bin/start-server.sh: Permission denied
Sep 21 14:23:40 example.com systemd[7940]: gunicorn.service: Failed to execute command: Permission denied
Sep 21 14:23:40 example.com systemd[7940]: gunicorn.service: Failed at step EXEC spawning /home/django/bin/start-server.sh: Permission denied
Sep 21 14:24:47 example.com systemd[7958]: gunicorn.service: Failed to execute command: Permission denied
Sep 21 14:24:47 example.com systemd[7958]: gunicorn.service: Failed at step EXEC spawning /home/django/bin/start-server.sh: Permission denied
Permission denied
.
.
.
我运行chown -R django:django /home/django
。现在,ls -lah /home/django
的输出是:
total 32K
drwxr-xr-x 5 django django 4.0K Sep 21 14:19 .
drwxr-xr-x 3 root root 4.0K Sep 21 14:04 ..
-rw-r--r-- 1 django django 220 Apr 4 2018 .bash_logout
-rw-r--r-- 1 django django 3.7K Apr 4 2018 .bashrc
-rw-r--r-- 1 django django 807 Apr 4 2018 .profile
drwxr-xr-x 4 django django 4.0K Sep 21 14:07 .venv
drwxr-xr-x 2 django django 4.0K Sep 21 15:58 bin
drwxr-xr-x 3 django django 4.0K Sep 21 14:08 blog
解决方案
感谢Dmitry Belaventsev,解决这个问题的方法是改变
ExecStart=/home/django/bin/start-server.sh
至
ExecStart=/bin/bash /home/django/bin/start-server.sh
在文件中 /etc/systemd/system/gunicorn.service
.
您的 systemd 服务已设置为代表 django
用户执行脚本。与此同时:
ls -lah /home/django
total 32K
drwxr-xr-x 5 django django 4.0K Sep 21 14:19 .
drwxr-xr-x 3 root root 4.0K Sep 21 14:04 ..
-rw-r--r-- 1 django django 220 Apr 4 2018 .bash_logout
-rw-r--r-- 1 django django 3.7K Apr 4 2018 .bashrc
-rw-r--r-- 1 django django 807 Apr 4 2018 .profile
drwxr-xr-x 4 django root 4.0K Sep 21 14:07 .venv
drwxr-xr-x 2 root root 4.0K Sep 21 15:58 bin
drwxr-xr-x 3 root root 4.0K Sep 21 14:08 blog
如您所见:
drwxr-xr-x 3 root root 4.0K Sep 21 14:04 ..
和
drwxr-xr-x 2 root root 4.0K Sep 21 15:58 bin
这意味着:
/home
目录属于root:root
/home/django/bin
属于root:root
让 systemd
代表 django
用户执行 bash 脚本:
- 该脚本应该是可执行的
- 所有父目录都应该有执行权限
django
用户 应该可以使用所有这些目录和脚本
最快的解决方案:
chown -R /home/django django:django
你也可以玩组和组权限。