django - 运行 Gunicorn 的自定义 systemd 服务不起作用

标签 django server gunicorn ubuntu-18.04

我正在尝试将我的 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

这意味着:

  1. /home目录属于root:root
  2. /home/django/bin 属于 root:root

systemd 代表 django 用户执行 bash 脚本:

  1. 该脚本应该是可执行的
  2. 所有父目录都应该有执行权限
  3. django 用户应该可以使用所有这些目录和脚本

最快的解决方案:

chown -R /home/django django:django

您也可以玩组和组权限。

关于django - 运行 Gunicorn 的自定义 systemd 服务不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63996283/

相关文章:

python - 如何在 gunicorn 进程之间共享字典?

Django Gunicorn Nginx "connection reset by peer while reading response header"

python - 尝试使用 mod_wsgi 运行 2 个具有不同 Python 版本的 Python 应用程序

python - Django 2.2 翻译不适用,LANGUAGE_CODE 和 i18n URL 模式正确

java - 我怎样才能安全地停止这个Java服务器程序?

php - Apache-CentOS- "Scan this dir for additional .ini files"

python - 我的 Django 模型的权限

django 使用 heroku 设置变量 SECURE_PROXY_SSL_HEADER

mysql : Unable to set password for the MySQL "root" user

django - Django 项目的最低服务器要求