在 mod_wsgi 中将 Django 项目添加到 运行 时出现问题

Problems getting Django project to run in mod_wsgi

我在通过 mod_wsgi 正确服务我的 django 项目时遇到了一些问题。

这是我第一次尝试这样的事情,所以请原谅任何菜鸟的错误。

我目前得到的错误如下:

[Wed Oct 19 16:24:23 2016] [error] [client 140.225.0.153] mod_wsgi (pid=20418): Exception occurred processing WSGI script '/opt/badgr/code/apps/mainsite/wsgi.py'.
[Wed Oct 19 16:24:23 2016] [error] [client 140.225.0.153] Traceback (most recent call last):
[Wed Oct 19 16:24:23 2016] [error] [client 140.225.0.153]   File "/opt/badgr/code/apps/mainsite/wsgi.py", line 14, in <module>
[Wed Oct 19 16:24:23 2016] [error] [client 140.225.0.153]     from django.core.wsgi import get_wsgi_application
[Wed Oct 19 16:24:23 2016] [error] [client 140.225.0.153] ImportError: No module named wsgi

主要项目目录在这里:

/opt/badgr/

该项目有自己的 virtualenv 位于此处:

/opt/badgr/env/lib/python2.7/site-packages

我的 wsgi.py 文件:

"""
WSGI config for badgr project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/howto/deployment/wsgi/
"""

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mainsite.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

我还有一个 django.conf 文件位于 /etc/httpd/conf.d/:

Alias /static /opt/badgr/code/staticfiles
<Directory /opt/badgr/code/staticfiles>
</Directory>


<Directory /opt/badgr/code/apps/mainsite/>
    <Files wsgi.py>
    </Files>
</Directory>

WSGISocketPrefix /var/run/wsgi/wsgi
WSGIDaemonProcess badgr python-path=/opt/badgr:/opt/badgr/env/lib/python2.7/site-packages/
WSGIProcessGroup badgr
WSGIScriptAlias / /opt/badgr/code/apps/mainsite/wsgi.py

据我所知 mod_wsgi 已正确安装在 virtualenv 中,因此我不确定为什么在尝试通过 http 连接到该站点时会看到此错误。

这是我的 httpd.conf:

   ServerTokens OS
 /mod/mpm_common.html#lockfile>);
    ServerRoot "/etc/httpd"
    PidFile run/httpd.pid
    Timeout 60
    KeepAlive Off
    MaxKeepAliveRequests 100
    KeepAliveTimeout 15
    <IfModule prefork.c>
    StartServers       8
    MinSpareServers    5
    MaxSpareServers   20
    ServerLimit      256
    MaxClients       256
    MaxRequestsPerChild  4000
    </IfModule>
    <IfModule worker.c>
    StartServers         4
    MaxClients         300
    MinSpareThreads     25
    MaxSpareThreads     75 
    ThreadsPerChild     25
    MaxRequestsPerChild  0
    </IfModule>
    Listen 80
    LoadModule auth_basic_module modules/mod_auth_basic.so
    LoadModule auth_digest_module modules/mod_auth_digest.so
    LoadModule authn_file_module modules/mod_authn_file.so
    LoadModule authn_alias_module modules/mod_authn_alias.so
    LoadModule authn_anon_module modules/mod_authn_anon.so
    LoadModule authn_dbm_module modules/mod_authn_dbm.so
    LoadModule authn_default_module modules/mod_authn_default.so
    LoadModule authz_host_module modules/mod_authz_host.so
    LoadModule authz_user_module modules/mod_authz_user.so
    LoadModule authz_owner_module modules/mod_authz_owner.so
    LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
    LoadModule authz_dbm_module modules/mod_authz_dbm.so
    LoadModule authz_default_module modules/mod_authz_default.so
    LoadModule ldap_module modules/mod_ldap.so
    LoadModule authnz_ldap_module modules/mod_authnz_ldap.so
    LoadModule include_module modules/mod_include.so
    LoadModule log_config_module modules/mod_log_config.so
    LoadModule logio_module modules/mod_logio.so
    LoadModule env_module modules/mod_env.so
    LoadModule ext_filter_module modules/mod_ext_filter.so
    LoadModule mime_magic_module modules/mod_mime_magic.so
    LoadModule expires_module modules/mod_expires.so
    LoadModule deflate_module modules/mod_deflate.so
    LoadModule headers_module modules/mod_headers.so
    LoadModule usertrack_module modules/mod_usertrack.so
    LoadModule setenvif_module modules/mod_setenvif.so
    LoadModule mime_module modules/mod_mime.so
    LoadModule dav_module modules/mod_dav.so
    LoadModule status_module modules/mod_status.so
    LoadModule autoindex_module modules/mod_autoindex.so
    LoadModule info_module modules/mod_info.so
    LoadModule dav_fs_module modules/mod_dav_fs.so
    LoadModule vhost_alias_module modules/mod_vhost_alias.so
    LoadModule negotiation_module modules/mod_negotiation.so
    LoadModule dir_module modules/mod_dir.so
    LoadModule actions_module modules/mod_actions.so
    LoadModule speling_module modules/mod_speling.so
    LoadModule userdir_module modules/mod_userdir.so
    LoadModule alias_module modules/mod_alias.so
    LoadModule substitute_module modules/mod_substitute.so
    LoadModule rewrite_module modules/mod_rewrite.so
    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
    LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
    LoadModule proxy_http_module modules/mod_proxy_http.so
    LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
    LoadModule proxy_connect_module modules/mod_proxy_connect.so
    LoadModule cache_module modules/mod_cache.so
    LoadModule suexec_module modules/mod_suexec.so
    LoadModule disk_cache_module modules/mod_disk_cache.so
    LoadModule cgi_module modules/mod_cgi.so
    LoadModule version_module modules/mod_version.so
    Include conf.d/*.conf
    User apache
    Group apache
    ServerAdmin root@localhost
    UseCanonicalName Off
    DocumentRoot "/var/www/html"

    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>

    <Directory "/var/www/html">

        Options Indexes FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all

    </Directory>


    <IfModule mod_userdir.c>

        UserDir disabled

    </IfModule>




    DirectoryIndex index.html index.html.var


    AccessFileName .htaccess


    <Files ~ "^\.ht">
        Order allow,deny
        Deny from all
        Satisfy All
    </Files>

    TypesConfig /etc/mime.types

    DefaultType text/plain

    <IfModule mod_mime_magic.c>
        MIMEMagicFile conf/magic
    </IfModule>

    HostnameLookups Off

    ErrorLog logs/error_log

    LogLevel warn

    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    LogFormat "%{Referer}i -> %U" referer
    LogFormat "%{User-agent}i" agent


    CustomLog logs/access_log combined

    ServerSignature On


    Alias /icons/ "/var/www/icons/"

    <Directory "/var/www/icons">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>


    <IfModule mod_dav_fs.c>
        # Location of the WebDAV lock database.
        DAVLockDB /var/lib/dav/lockdb
    </IfModule>

    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"

    <Directory "/var/www/cgi-bin">
        AllowOverride None
        Options None
        Order allow,deny
        Allow from all
    </Directory>


    IndexOptions FancyIndexing VersionSort NameWidth=* HTMLTable Charset=UTF-8

    AddIconByEncoding (CMP,/icons/compressed.gif) x-compress x-gzip

    AddIconByType (TXT,/icons/text.gif) text/*
    AddIconByType (IMG,/icons/image2.gif) image/*
    AddIconByType (SND,/icons/sound2.gif) audio/*
    AddIconByType (VID,/icons/movie.gif) video/*

    AddIcon /icons/binary.gif .bin .exe
    AddIcon /icons/binhex.gif .hqx
    AddIcon /icons/tar.gif .tar
    AddIcon /icons/world2.gif .wrl .wrl.gz .vrml .vrm .iv
    AddIcon /icons/compressed.gif .Z .z .tgz .gz .zip
    AddIcon /icons/a.gif .ps .ai .eps
    AddIcon /icons/layout.gif .html .shtml .htm .pdf
    AddIcon /icons/text.gif .txt
    AddIcon /icons/c.gif .c
    AddIcon /icons/p.gif .pl .py
    AddIcon /icons/f.gif .for
    AddIcon /icons/dvi.gif .dvi
    AddIcon /icons/uuencoded.gif .uu
    AddIcon /icons/script.gif .conf .sh .shar .csh .ksh .tcl
    AddIcon /icons/tex.gif .tex
    AddIcon /icons/bomb.gif /core

    AddIcon /icons/back.gif ..
    AddIcon /icons/hand.right.gif README
    AddIcon /icons/folder.gif ^^DIRECTORY^^
    AddIcon /icons/blank.gif ^^BLANKICON^^


    DefaultIcon /icons/unknown.gif

    ReadmeName README.html
    HeaderName HEADER.html

    IndexIgnore .??* *~ *# HEADER* README* RCS CVS *,v *,t

    AddLanguage ca .ca
    AddLanguage cs .cz .cs
    AddLanguage da .dk
    AddLanguage de .de
    AddLanguage el .el
    AddLanguage en .en
    AddLanguage eo .eo
    AddLanguage es .es
    AddLanguage et .et
    AddLanguage fr .fr
    AddLanguage he .he
    AddLanguage hr .hr
    AddLanguage it .it
    AddLanguage ja .ja
    AddLanguage ko .ko
    AddLanguage ltz .ltz
    AddLanguage nl .nl
    AddLanguage nn .nn
    AddLanguage no .no
    AddLanguage pl .po
    AddLanguage pt .pt
    AddLanguage pt-BR .pt-br
    AddLanguage ru .ru
    AddLanguage sv .sv
    AddLanguage zh-CN .zh-cn
    AddLanguage zh-TW .zh-tw

    LanguagePriority en ca cs da de el eo es et fr he hr it ja ko ltz nl nn no pl pt pt-BR ru sv zh-CN zh-TW


    ForceLanguagePriority Prefer Fallback

    AddDefaultCharset UTF-8

    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz

    AddType application/x-x509-ca-cert .crt
    AddType application/x-pkcs7-crl    .crl

    AddHandler type-map var
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml


    Alias /error/ "/var/www/error/"

    <IfModule mod_negotiation.c>
    <IfModule mod_include.c>
        <Directory "/var/www/error">
            AllowOverride None
            Options IncludesNoExec
            AddOutputFilter Includes html
            AddHandler type-map var
            Order allow,deny
            Allow from all
            LanguagePriority en es de fr
            ForceLanguagePriority Prefer Fallback
        </Directory>


    </IfModule>
    </IfModule>


    BrowserMatch "Mozilla/2" nokeepalive
    BrowserMatch "MSIE 4\.0b2;" nokeepalive downgrade-1.0 force-response-1.0
    BrowserMatch "RealPlayer 4\.0" force-response-1.0
    BrowserMatch "Java/1\.0" force-response-1.0
    BrowserMatch "JDK/1\.0" force-response-1.0


    BrowserMatch "Microsoft Data Access Internet Publishing Provider" redirect-carefully
    BrowserMatch "MS FrontPage" redirect-carefully
    BrowserMatch "^WebDrive" redirect-carefully
    BrowserMatch "^WebDAVFS/1.[0123]" redirect-carefully
    BrowserMatch "^gnome-vfs/1.0" redirect-carefully
    BrowserMatch "^XML Spy" redirect-carefully
    BrowserMatch "^Dreamweaver-WebDAV-SCM1" redirect-carefully

WSGI 通过 conf.d:

中的文件加载
LoadModule wsgi_module modules/mod_wsgi.so

我在 HTTPD.conf 文件中并没有真正编辑太多,只是删除了评论以便我可以将其放在此处。

我也不确定为什么 python 对于 httpd 似乎是 运行 2.6.# 而不是 2.7.

任何想法或其他信息表示赞赏。如果我还应该提供其他任何东西,请告诉我。

关于 django.core.wsgi 的错误是因为您安装了非常旧的 Django 版本,比您的项目设置的版本旧。

这可能是因为您的 mod_wsgi 是为 Python 2.6 编译的,并且您在该 Python 安装中安装了较旧的 Django 版本。

如果您想使用 Python 2.7,您必须使用为 Python 2.7 编译的版本重新安装 mod_wsgi。您不能强制为 Python 2.6 编译的 mod_wsgi 为 Python 2.7 使用虚拟环境。

注意:我在 pip 中提供了使用 mod_wsgi 的答案,因为我最近必须自己做这个才能在 FreeBSD 中使用 Python 获得它 运行ning 3. 它不是' 通常这样做,但它应该工作。

如果你想从 pip 安装 mod_wsgi,那么你应该给 Apache 正确的 mod_wsgi 路径,或者将它创建的模块 pip 安装到 Apache 安装中。如果您 运行 使用 apt-get 或 pkg 或 BSD 端口在您的系统上安装它,请听取 Graham 的明智建议。

还有其他可能同时出错的地方,但我认为最接近的错误是您的应用程序无法导入 wsgi。您应该通过以下方式进入您的环境:

cd /opt/badgr
. env/bin/activate

如果失败尝试source env/bin/activate

然后安装mod_wsgi:

pip install mod_wsgi

这将安装一个名为 mod_wsgi-express 的脚本,它允许您从命令行 运行 Apache/mod_wsgi 作为开发服务器。该脚本还可以帮助您将它编译的模块安装到您的 Apache 中。

首先我们必须处理 Apache 已经从系统包中安装了 mod_wsgi 模块的事实。最好的办法是卸载 mod_wsgi 的系统包。这可能会删除 conf.d 中带有 wsgi_module.

的 LoadModule 行的文件

下一个运行:

sudo mod_wsgi-express install-module

这应该将模块复制到 Apache 模块目录并输出两行 LoadModule 和 WSGIPythonHome 指令。在 conf.d 目录下添加一个新的 wsgi.conf 文件并将这两行添加到其中。重新启动 Apache。

从你的 virtualenv 中的 pip 执行它确保它创建正确的 mod_wsgi 版本相对于你 运行ning 的 python 版本。

至少对于这个问题,我认为是我没有授予 site-packages 文件夹及其内容足够的权限。这样做之后我取得了一些进展,但现在 运行 进入我在另一个线程中打开的其他问题。

我递归地为组 apache 提供了 site-packages 文件夹 rwx。

这是我的问题的下一部分: