Apache错误(AH01276),无法提供目录/ var / www / project /:

时间:2016-03-05 03:05:04

标签: django python-3.x centos mod-wsgi apache2.4

我正在尝试在新安装的centos7服务器上部署Django Web应用程序。 为此,我使用apache2.4与mod_wsgi。

我坚持服务静态文件,因为我是新手,这对我来说太混乱了。

这是我的配置文件,如果你可以帮我这个。

/etc/httpd/conf/httpd.conf中:

(我删除了我在这里没有使用的所有内容并删除了注释行)

Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin admin@serv.com
<Files ".ht*">
    Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel info

<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access_log" combined
</IfModule>
<IfModule mime_module>
    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>
AddDefaultCharset UTF-8

<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>
EnableSendfile on
IncludeOptional sites-enabled/*.conf
LoadModule wsgi_module modules/mod_wsgi.so

在我的启用网站的文件夹中,我的项目只有一个配置文件

/etc/httpd/sites-enabled/project.conf:

Alias /favicon.ico /var/www/cvctools/staticfiles/templates/images/favicon.ico
Alias /static/ /var/www/cvctools/staticfiles/
<VirtualHost *:80>
    DocumentRoot /var/www/cvctools
    ServerName proj.ma
    ServerAlias www.proj.ma
    ServerAdmin admin@proj.ma

    <Directory /var/www/cvctools/staticfiles>
        AllowOverride None
        Require all granted
    </Directory>

    <IfModule wsgi_module>
        WSGIScriptAlias / /var/www/cvctools/CapValue/wsgi.py
        WSGIScriptReloading On
        WSGIDaemonProcess CapValue processes=1 threads=1 maximum-requests=5000 display-name=my-wsgi
        WSGIProcessGroup CapValue
        WSGIPythonPath /var/www/cvctools
        <Directory /var/www/cvctools/CapValue>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>
    </IfModule>
</VirtualHost>

位于/ var / www / cvctools的项目文件夹中我有这个..

./ CapValue / wsgi.py:

import django.core.handlers.wsgi
import os,sys

sys.path.append('/vat/www/cvctools')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CapValue.settings")
application = django.core.handlers.wsgi.WSGIHandler()

./CapValue/settings.py中的静态文件设置:

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(os.getcwd(), 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(PROJECT_ROOT, 'static'),)
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    # 'compressor.finders.CompressorFinder',
)
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
TEMPLATES = [
    {
        'BACKEND' : 'django.template.backends.django.DjangoTemplates',
        'DIRS'    : [os.path.join(STATIC_ROOT, 'templates'), ],
        'APP_DIRS': True,
        'OPTIONS' : {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

./ CapValue / views.py:

from django.views.decorators.csrf import ensure_csrf_cookie
from django.views.generic.base import TemplateView
from django.utils.decorators import method_decorator


class IndexView(TemplateView):
    template_name = 'index.html'

    @method_decorator(ensure_csrf_cookie)
    def dispatch(self, *args, **kwargs):
        return super(IndexView, self).dispatch(*args, **kwargs)

./ CapValue / urls.py:

#imports...
urlpatterns = [
    #...
    url('^.*$', IndexView.as_view(), name='index')
]

运行manage.py collectstatic之后,在我的项目目录中,我有staticfiles文件夹,其中包含以下内容:

/ var / www / cvctools / staticfiles /:

drwxr-xr-x.  6 admin admin    47 Mar  4 22:36 admin
drwxr-xr-x.  2 admin admin  4096 Mar  4 22:36 css
drwxr-xr-x.  5 admin admin    35 Mar  4 22:36 django_extensions
-rwxr-xr-x.  1 admin admin     0 Mar  4 22:36 human.d41d8cd98f00.txt
-rwxr-xr-x.  1 admin admin     0 Mar  4 22:36 human.txt
drwxr-xr-x.  2 admin admin  4096 Mar  4 22:36 images
drwxr-xr-x. 12 admin admin  4096 Mar  4 22:36 javascript
drwxr-xr-x.  2 admin admin  4096 Mar  4 22:36 notifications
drwxr-xr-x.  6 admin admin    47 Mar  4 22:36 rest_framework
-rwxr-xr-x.  1 admin admin 12925 Mar  4 22:36 staticfiles.json
drwxr-xr-x.  6 admin admin  4096 Mar  4 22:36 templates

但是当我尝试访问该应用程序时,在我的apache日志中出现此错误:

 [autoindex:error] [pid 3150] [client 192.168.0.126:57623] AH01276: Cannot serve directory /var/www/cvctools/: No matching DirectoryIndex (index.html) found, and server-generated directory index forbidden by Options directive

我究竟做错了什么?

修改

显然它与mod_wsgi有关,因为我将我的DcumentRoot改为/ var / www / cvctools / staticfiles / templates 然后我能够看到我的索引页面,但它是这样的:

{% load staticfiles %} {% include 'stylesheets.html' %}
{% include 'navbar.html' %}
{% include 'menu.html' %}
{#
#} {# {% include 'breadcrumb.html' %}#} {#
#}
{% include 'footer.html' %}
{% include 'javascripts.html' %} 

它没有选择我的模板标签!

1 个答案:

答案 0 :(得分:0)

我怀疑(在我的问题编辑中)它与我的mod_wsgi设置有关。

在查看此link中的设置过程后,我缺少的是mod_wsgi python包。 所以一个简单的pip install mod_wsgi解决了我的问题