Django 部署:错误 403 禁止访问您无权访问此服务器上的/

Django Deployment: Error 403 Forbidden You don't have permission to access / on this server

我目前正在根据 Django 文档 here 将我的 Django 应用程序部署到 CentOS 7 服务器(CentOS Linux 7.8.2003 版)。但是我遇到了下面错误日志中所述的这个问题(错误 403)。

注意事项:

  1. 是的,我能够通过虚拟环境端口 8000 运行 服务器。

  2. 我使用的数据库是mysql (guide).

  3. 这跟apache设置的访问权限有关系吗?至于现在,我已经将apache的权限设置如下:

sudo chown :apache colus_cafe/
sudo chown -R :apache colus_cafe/colus_cafe/media
  1. Python 版本 3.6.8 & WSGI python36-mod_wsgi.x86_64 ().
  2. 我尝试了什么:将根据给出的答案进行更新
    • 删除并重新安装虚拟环境。

/etc/httpd/conf.d/django.conf:

Alias /static /home/colus/colus_cafe/colus_cafe/static
<Directory /home/colus/colus_cafe/colus_cafe/static>
        Require all granted
</Directory>

Alias /media /home/colus/colus_cafe/colus_cafe/media
<Directory /home/colus/colus_cafe/colus_cafe/media>
        Require all granted
</Directory>

<Directory /home/colus/colus_cafe/colus_cafe>                                                                       
        <Files wsgi.py>
            Require all granted
    </Files>
</Directory>

WSGIScriptAlias / /home/colus/colus_cafe/colus_cafe/wsgi.py
WSGIDaemonProcess colus_cafe_app python-home=/home/colus/colus_cafe/env python-path=/home/colus/colus_cafe
WSGIProcessGroup colus_cafe_app

/etc/httpd/logs/error_log

Current thread 0x00007fee066d6880 (most recent call first):                                                          

[Wed Jul 08 07:11:09.691137 2020] [mpm_prefork:notice] [pid 10044] AH00170: caught SIGWINCH, shutting down gracefully
[Wed Jul 08 07:11:10.768060 2020] [core:notice] [pid 10231] SELinux policy enabled; httpd running as context system_$
[Wed Jul 08 07:11:10.769024 2020] [suexec:notice] [pid 10231] AH01232: suEXEC mechanism enabled (wrapper: /usr/sbin/$[Wed Jul 08 07:11:10.789925 2020] [so:warn] [pid 10231] AH01574: module wsgi_module is already loaded, skipping      
[Wed Jul 08 07:11:10.793580 2020] [lbmethod_heartbeat:notice] [pid 10231] AH02282: No slotmem from mod_heartmonitor  
[Wed Jul 08 07:11:10.796988 2020] [mpm_prefork:notice] [pid 10231] AH00163: Apache/2.4.6 (CentOS) mod_wsgi/4.6.2 Pyt$[Wed Jul 08 07:11:10.797021 2020] [core:notice] [pid 10231] AH00094: Command line: '/usr/sbin/httpd -D FOREGROUND'   
[Wed Jul 08 07:11:10.798024 2020] [wsgi:warn] [pid 10232] (13)Permission denied: mod_wsgi (pid=10232): 
Unable to stat Python home /home/colus/colus_cafe/env. 
Python interpreter may not be able to be initialized correctly. 
Verify the supplied path and access permissions for whole of the path. 
Fatal Python error: Py_Initialize: Unable to get the locale encoding                                           
ModuleNotFoundError: No module named 'encodings'

根据 Maarten 的评论,我找到了这个问题的答案。

  1. 我需要使用 chmod 更改 apache 的访问权限以读取和执行 django 项目文件夹。但是,这稍后显示了下面的另一个问题。

/etc/httpd/logs/error_log

failed to map segment from shared object permission denied mysql.
  1. 然后我发现错误显示Python(在虚拟环境中)无法执行包(mysqlclient)。因此,可以找到解决方案 here,即更改允许 Apache 执行的“httpd_sys_script_exec_t”的安全上下文。

我希望这对遇到此问题的任何人有所帮助。如果我有任何不好的做法或错误,请发表评论。

谢谢你,祝你有愉快的一天。