centos Django项目的Gunicorn文件gunicorn.service keep getting Permission denied

r8xiu3jd  于 12个月前  发布在  Go
关注(0)|答案(1)|浏览(159)

你好,我目前正在使用Gunicorn和Nginx在CentOS上练习Linux和Django项目。
我正在AWS Lightsail上测试我的服务器,这里有一些关于我在此实践中使用的信息。

***CS9-20230110
*Python 3.9

  • Django 4.0.2**
    *Nginx 1.22.1
  • Gunicorn 21.2.0,克罗地亚

通过Gunicorn打开一个gunicorn socket和端口8000来打开服务器的测试是成功的。我在这里没有遇到任何麻烦。
第一个月
gunicorn --bind unix:/home/ec2-user/Django/venv/gunicorn.sock config.wsgi:application
(我把sock文件放到venv目录中,以为这可能是问题所在(可惜不是))
但是当我尝试在systemd上启动Gunicorn服务时,系统在gunicorn文件上发送权限拒绝消息。
所以我把每个文件的UID和GID都设置为我的用户帐户和组,并对大多数文件设置了775授权。
我不知道我现在错过了什么。只是想我犯了一个错误,现在到处都是一团糟。请有人告诉我这里发生了什么。

目录树

# /home/ec2-user/Django
Django
├── Django_Project
│   ├── db.sqlite3
│   ├── manage.py
│   ├── mysite
│   │   ├── asgi.py
│   │   ├── db.sqlite3
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   ├── settings
│   │   │   ├── base.py
│   │   │   ├── local.py
│   │   │   └── prod.py
│   │   ├── urls.py
│   │   └── wsgi.py
│   ├── nohup.out
│   ├── README.md
│   ├── static
│   │   ├── bootstrap.min.css
│   │   ├── bootstrap.min.js
│   │   └── style.css
│   └── templates
│       └── base.html
└── venv
    ├── bin
    │   ├── activate
    │   ├── activate.csh
    │   ├── activate.fish
    │   ├── Activate.ps1
    │   ├── django-admin
    │   ├── gunicorn
    │   ├── markdown_py
    │   ├── pip
    │   ├── pip3
    │   ├── pip3.11
    │   ├── pip3.9
    │   ├── python -> python3
    │   ├── python3 -> /usr/bin/python3
    │   ├── python3.9 -> python3
    │   ├── sqlformat
    │   └── wheel
    ├── gunicorn.sock
    ├── include
    ├── lib
    │   └── python3.9
    ├── lib64 -> lib
    ├── mysite.env
    └── pyvenv.cfg

字符串

这是系统状态

[root@ip-172-26-14-187 ec2-user]# systemctl status gunicorn.service
× gunicorn.service - gunicorn daemon
     Loaded: loaded (/etc/systemd/system/gunicorn.service; disabled; preset: disabled)
     Active: failed (Result: exit-code) since Sat 2023-12-09 17:55:51 UTC; 1s ago
   Duration: 6ms
TriggeredBy: ○ gunicorn.socket
    Process: 55311 ExecStart=/home/ec2-user/Django/venv/bin/gunicorn --workers 2 --bind unix:/home/ec2-user/Django/venv/gunicorn.sock mysite.wsgi:application (code=exited>
   Main PID: 55311 (code=exited, status=203/EXEC)
        CPU: 3ms

Dec 09 17:55:51 ip-172-26-14-187.ap-northeast-2.compute.internal systemd[1]: Started gunicorn daemon.
Dec 09 17:55:51 ip-172-26-14-187.ap-northeast-2.compute.internal systemd[55311]: gunicorn.service: Failed to locate executable /home/ec2-user/Django/venv/bin/gunicorn: Permission denied
Dec 09 17:55:51 ip-172-26-14-187.ap-northeast-2.compute.internal systemd[55311]: gunicorn.service: Failed at step EXEC spawning /home/ec2-user/Django/venv/bin/gunicorn: Permission denied
Dec 09 17:55:51 ip-172-26-14-187.ap-northeast-2.compute.internal systemd[1]: gunicorn.service: Main process exited, code=exited, status=203/EXEC
Dec 09 17:55:51 ip-172-26-14-187.ap-northeast-2.compute.internal systemd[1]: gunicorn.service: Failed with result 'exit-code'.

gunicorn.service文件

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=ec2-user
Group=ec2-user

WorkingDirectory=/home/ec2-user/Django/Django_Project
ExecStart=/home/ec2-user/Django/venv/bin/gunicorn --workers 2 --bind unix:/home/ec2-user/Django/venv/gunicorn.sock mysite.wsgi:a
pplication

[Install]
WantedBy=multi-user.target


当我再写一行类似EnvironmentFile=/home/ec2-user/Django/venv/mysite.env的代码时,也会出现Permission denied消息。

gunicorn.service: Failed to load environment files: Permission denied
gunicorn.service: Failed to run 'start' task: Permission denied
gunicorn.service: Failed with result 'resources'.

*

项目和venv目录

drwxrwxr-x. 8 ec2-user ec2-user 176 Dec  9 11:07 Django_Project
drwxr-xr-x. 8 ec2-user ec2-user 176 Dec  7 17:43 Django_Project_Backup
drwxrwxr-x. 5 ec2-user ec2-user 113 Dec  9 17:28 venv


Django/Venv**

drwxr-xr-x. 2 ec2-user ec2-user 4096 Dec  9 16:54 bin
srwxrwxrwx. 1 ec2-user ec2-user    0 Dec  9 17:23 gunicorn.sock
drwxr-xr-x. 2 ec2-user ec2-user    6 Dec  7 03:11 include
drwxr-xr-x. 3 ec2-user ec2-user   23 Dec  7 03:11 lib
lrwxrwxrwx. 1 ec2-user ec2-user    3 Dec  7 03:11 lib64 -> lib
-rwxrwxr-x. 1 ec2-user ec2-user   44 Dec  9 15:49 mysite.env
-rw-r--r--. 1 ec2-user ec2-user   70 Dec  7 03:11 pyvenv.cfg

  • Django/venv/bin**
-rw-r--r--. 1 ec2-user ec2-user 1901 Dec  7 03:12 activate
-rw-r--r--. 1 ec2-user ec2-user  850 Dec  7 03:12 activate.csh
-rw-r--r--. 1 ec2-user ec2-user 1990 Dec  7 03:12 activate.fish
-rw-r--r--. 1 ec2-user ec2-user 8834 Dec  7 03:12 Activate.ps1
-rwxr-xr-x. 1 ec2-user ec2-user  285 Dec  7 03:21 django-admin
-rwxr-xr-x. 1 ec2-user ec2-user  239 Dec  9 16:54 gunicorn
-rwxr-xr-x. 1 ec2-user ec2-user  236 Dec  7 03:21 markdown_py
-rwxr-xr-x. 1 ec2-user ec2-user  243 Dec  7 03:22 pip
-rwxr-xr-x. 1 ec2-user ec2-user  243 Dec  7 03:22 pip3
-rwxr-xr-x. 1 ec2-user ec2-user  243 Dec  7 03:22 pip3.11
-rwxr-xr-x. 1 ec2-user ec2-user  243 Dec  7 03:22 pip3.9
lrwxrwxrwx. 1 ec2-user ec2-user    7 Dec  7 03:11 python -> python3
lrwxrwxrwx. 1 ec2-user ec2-user   16 Dec  7 03:11 python3 -> /usr/bin/python3
lrwxrwxrwx. 1 ec2-user ec2-user    7 Dec  7 03:11 python3.9 -> python3
-rwxr-xr-x. 1 ec2-user ec2-user  238 Dec  7 03:17 sqlformat
-rwxr-xr-x. 1 ec2-user ec2-user  230 Dec  7 03:20 wheel

/etc/systemd/system

-rw-r--r--. 1 root root  327 Dec  9 17:42  gunicorn.service
drwxr-xr-x. 2 root root 4096 Dec  9 15:46  multi-user.target.wants


我尝试更改UID和GID,在Django项目中的所有文件中将授权设置为775。我希望系统有权限使用venv/bin目录中的gunicorn文件。

jogvjijk

jogvjijk1#

我发现它是SELinux。
我使用setenforce 0命令禁用SELinux。现在它在systemd上。

相关问题