Django wsgi Apache2:'AH01630: client denied by server configuration'

Django wsgi Apache2: 'AH01630: client denied by server configuration'

我已经按照本教程部署了我的 django 项目:https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-apache-and-mod_wsgi-on-debian-8

为什么无法从 apache 加载静态文件?

我正在使用 Debian9、Python3.5 和 Django 1.11

这是我的虚拟主机配置:

<VirtualHost *:80>

    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /static /home/alex/djangoproject/static
    <Directory /home/alex/djangoproject/static>
        Require all granted
    </Directory>

    <Directory /home/alex/djangoproject/cpanel>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    WSGIDaemonProcess djangoproject python-home=/home/alex/djangoproject/djangoprojectenv python-path=/home/alex/djangoproject
    WSGIProcessGroup djangoproject
    WSGIScriptAlias / /home/alex/djangoproject/cpanel/wsgi.py

</VirtualHost>

`

以及来自 error.log 的 apache 错误:

AH01630: client denied by server configuration /home/alex/djangoproject/static

我的 settings.py 配置与静态文件有关

STATIC_URL = '/static/' PROJECT_DIR = os.path.dirname(os.path.abspath(__file__)) STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')

通过在static之前添加django项目名称目录解决了这个问题 所以这个:

Alias /static /home/alex/djangoproject/static
<Directory /home/alex/djangoproject/static>
    Require all granted
</Directory>

会变成这样:

Alias /static /home/alex/djangoproject/"django-project-name(in mycase: cpanel)"/static
<Directory /home/alex/djangoproject/"django-project-name(in mycase: cpanel)"/static>
    Require all granted
</Directory>

执行 ./migrate.py collectstatic ,重新启动 apache 和所有 运行 顺利。

对于 Ubuntu 20 :

/etc/apache2/apache2.conf 在第 175 行之后添加:

<Directory /<project_folder_parent_directory>/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

/etc/apache2/sites-available/000-default.conf 中,使用此快速设置:

<VirtualHost *:80>
    ServerAlias *
    ErrorLog /<wsgi.py_folder_path>/error.log
    
    CustomLog /<wsgi.py_folder_path>/access.log combine
    
    Alias /static /<static_parent_directory_path>/static
    <Directory /<project_folder_path>/static>
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>
    <Directory /<wsgi.py_folder_path>/>
        <Files wsgi.py>
        Order allow,deny
        Allow from all
        Require all granted
        </Files>
        Order allow,deny
        Allow from all
        Require all granted
    </Directory>

    #Demon process for multiple virtual hosts
    WSGIDaemonProcess projectname  python-path=/<project_folder_path>/
    # if you're using virtual environment
    #WSGIDaemonProcess projectname python-home=/<project_virtual_env_path> python-path=/<project_folder_path>/
    WSGIProcessGroup projectname
    #Pointing wsgi script to config file
    WSGIScriptAlias / /<wsgi.py_folder_path>/wsgi.py

</VirtualHost>

重启阿帕奇:

$ sudo systemctl restart apache2