你好,我目前正在 CentOS 上使用 Gunicorn 和 Nginx 练习 Linux 和 Django 项目。
我正在 AWS Lightsail 上测试我的服务器,以下是我在此实践中使用的一些信息。
用Gunicorn打开gunicorn套接字和8000端口来打开服务器测试成功。我在这里没有遇到任何麻烦。
gunicorn --bind 0:8000 mysite.wsgi:application
gunicorn --bind unix:/home/ec2-user/Django/venv/gunicorn.sock config.wsgi:application
但是当我尝试在 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'.
[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/系统
-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 文件。
我发现它是 SELinux。
我使用
setenforce 0
命令禁用 SELinux。
现在它在 systemd 上。