我正在尝试在新安装的 centos7 服务器上部署 Django Web 应用程序。为此,我使用带有 mod_wsgi 的 apache2.4。
我被困在提供静态文件服务上,因为我是新手,所以这让我很困惑。
这是我的配置文件,如果您能帮助我的话。
/etc/httpd/conf/httpd.conf:
(我删除了这里没用到的所有内容,并删除了注释行)
Listen 80
Include conf.modules.d/*.conf
User apache
Group apache
ServerAdmin [email protected]
<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
在我的 sites-enabled 文件夹中,我的项目只有一个配置文件
/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 [email protected]
<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/静态文件/:
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
正如我所怀疑的(在我对问题的编辑中)它与我的 mod_wsgi 设置有关。
在查看了本安装流程后关联,我缺少的是 mod_wsgi python 包。所以一个简单的方法pip install mod_wsgi
解决了我的问题