0

尝试启动一项服务以将 gunicorn 作为 Flask 的后端服务器运行,但无法正常工作。运行 nginx 作为 React 的前端服务器,工作正常。

服务器:

  Virtualization: vmware
  Operating System: Red Hat Enterprise Linux 8.4 (Ootpa)
  CPE OS Name: cpe:/o:redhat:enterprise_linux:8.4:GA
  Kernel: Linux 4.18.0-305.3.1.el8_4.x86_64
  Architecture: x86-64

/etc/systemd/system/myservice.service中的服务文件:

[Unit]
Description="Description"
After=network.target

[Service]
User=root
Group=root
WorkingDirectory=/home/project/app/api
ExecStart=/home/project/app/api/venv/bin/gunicorn -b 127.0.0.1:5000 api:app
Restart=always

[Install]
WantedBy=multi-user.target

/应用程序/api

-rwxr-xr-x. 1 root root 2018 Jun  9 20:06 api.py
drwxrwxr-x+ 5 root root  100 Jun  7 10:11 venv

错误信息:

● myservice.service - "Description"
 Loaded: loaded (/etc/systemd/system/myservice.service; enabled; vendor preset: disabled)
  Active: failed (Result: exit-code) since Thu 2021-06-10 19:01:01 CEST; 5s ago
  Process: 18307 ExecStart=/home/project/app/api/venv/bin/gunicorn -b 127.0.0.1:5000 api:app (code=exited, status=203/EXEC)
 Main PID: 18307 (code=exited, status=203/EXEC)

Jun 10 19:01:01 xxxx systemd[1]: myservice.service: Service RestartSec=100ms expired, scheduling restart.
Jun 10 19:01:01 xxxx systemd[1]: myservice.service: Scheduled restart job, restart counter is at 5.
Jun 10 19:01:01 xxxx systemd[1]: Stopped "Description".
Jun 10 19:01:01 xxxx systemd[1]: myservice.service: Start request repeated too quickly.
Jun 10 19:01:01 xxxx systemd[1]: myservice.service: Failed with result 'exit-code'.
Jun 10 19:01:01 xxxx systemd[1]: Failed to start "Description".

试过了,不行:

  • [Service]下添加Environment="PATH=/home/project/app/api/venv/bin"
  • $ systemctl reset-failed myservice.service
  • $ systemctl 守护进程重载
  • 重启,ofc。

试过,工作:

  • /app/api目录中运行(以 root 身份)/home/project/app/api/venv/bin/gunicorn -b 127.0.0.1:5000 api:app

有谁知道如何解决这个问题?

4

1 回答 1

0

通常情况下,我在发布此问题后不久就想通了。

SELinux 搞乱了文件和目录的权限,因此对于遇到相同问题的任何人,请确保使用以下更改(以 root 身份)进行测试:

$ setsebool -P httpd_can_network_connect on

$ chcon -Rt httpd_sys_content_t /path/to/your/Flask/dir

就我而言:$ chcon -Rt httpd_sys_content_t /home/project/app/api

虽然这不是永久修复,但值得一试。查看SELinux 文档以获得更永久的解决方案。

于 2021-06-10T18:07:57.473 回答