Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 试图将django应用程序配置为使用mod_wsgi_Python_Django_Apache_Wsgi - Fatal编程技术网

Python 试图将django应用程序配置为使用mod_wsgi

Python 试图将django应用程序配置为使用mod_wsgi,python,django,apache,wsgi,Python,Django,Apache,Wsgi,当我访问此项目的URL时,我在apache error.log中不断收到此错误: [Fri Jan 20 21:04:16.143990 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326] mod_wsgi (pid=18618): Target WSGI script '/srv/botbot/src/botbot/botbot/wsgi.py' cannot be loaded as Python

当我访问此项目的URL时,我在apache error.log中不断收到此错误:

[Fri Jan 20 21:04:16.143990 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326] mod_wsgi (pid=18618): Target WSGI script '/srv/botbot/src/botbot/botbot/wsgi.py' cannot be loaded as Python module. 
[Fri Jan 20 21:04:16.144124 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326] mod_wsgi (pid=18618): Exception occurred processing WSGI script '/srv/botbot/src/botbot/botbot/wsgi.py'. 
[Fri Jan 20 21:04:16.144195 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326] Traceback (most recent call last): 
[Fri Jan 20 21:04:16.144255 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326]   File "/srv/botbot/src/botbot/botbot/wsgi.py", line 9, in <module> 
[Fri Jan 20 21:04:16.144335 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326]     application = get_wsgi_application() 
[Fri Jan 20 21:04:16.144381 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326]   File "/srv/botbot/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application 
[Fri Jan 20 21:04:16.144437 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326]     django.setup() 
[Fri Jan 20 21:04:16.144487 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326]   File "/srv/botbot/lib/python2.7/site-packages/django/__init__.py", line 17, in setup 
[Fri Jan 20 21:04:16.144540 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING) 
[Fri Jan 20 21:04:16.144586 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326]   File "/srv/botbot/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in __getattr__ 
[Fri Jan 20 21:04:16.144638 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326]     self._setup(name) 
[Fri Jan 20 21:04:16.144675 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326]   File "/srv/botbot/lib/python2.7/site-packages/django/conf/__init__.py", line 44, in _setup 
[Fri Jan 20 21:04:16.144711 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326]     self._wrapped = Settings(settings_module) 
[Fri Jan 20 21:04:16.144747 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326]   File "/srv/botbot/lib/python2.7/site-packages/django/conf/__init__.py", line 92, in __init__ 
[Fri Jan 20 21:04:16.144793 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326]     mod = importlib.import_module(self.SETTINGS_MODULE) 
[Fri Jan 20 21:04:16.144822 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326]   File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module 
[Fri Jan 20 21:04:16.144856 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326]     __import__(name) 
[Fri Jan 20 21:04:16.144884 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326]   File "/srv/botbot/src/botbot/botbot/settings/__init__.py", line 1, in <module> 
[Fri Jan 20 21:04:16.144930 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326]     from base import * 
[Fri Jan 20 21:04:16.145008 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326]   File "/srv/botbot/src/botbot/botbot/settings/base.py", line 27, in <module> 
[Fri Jan 20 21:04:16.145053 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326]     SECRET_KEY = os.environ['WEB_SECRET_KEY'] 
[Fri Jan 20 21:04:16.145085 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326]   File "/usr/lib/python2.7/UserDict.py", line 23, in __getitem__ 
[Fri Jan 20 21:04:16.145118 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326]     raise KeyError(key) 
[Fri Jan 20 21:04:16.145157 2017] [:error] [pid 18618:tid 140192977487616] [remote 66.90.146.30:326] KeyError: 'WEB_SECRET_KEY' 
这是我的.env文件:

# Required 
SECRET_KEY='password1' 
WEB_PORT=8000 
EMAIL_BACKEND='django.core.mail.backends.console.EmailBackend' 
GOPATH=$VIRTUAL_ENV 
WEB_SECRET_KEY='password1' 
STORAGE_URL=postgres://ubuntu:password@localhost:5432/botbot 
REDIS_PLUGIN_STORAGE_URL=redis://localhost:6379/0 
REDIS_PLUGIN_QUEUE_URL=redis://localhost:6379/1 
PUSH_STREAM_URL=http://localhost:8080/pub/?id={id} 

# Set encoding if the system hasn't done it properly 
LANG=en_US.UTF-8 
PYTHONIOENCODING=utf8 

# Optional 
# MEMCACHE_URL=127.0.0.1:11211 
# STATIC_ROOT=/var/www/botbot/static 
# MEDIA_ROOT=/var/www/botbot/uploads 
# DEBUG=True 
# SMTP_URL=smtp://user:pass@host:port 
# SMTP_TLS=True 
# ALLOWED_HOSTS=host1,host2 
# INCLUDE_DJANGO_ADMIN=False 
# EXCLUDE_NICKS=nick1,nick2 
这是我的apache conf文件:

<VirtualHost *:80> 
        ServerName server.test.com 
        ErrorLog ${APACHE_LOG_DIR}/error.log 
        SetEnv WEB_SECRET_KEY password1 
        CustomLog ${APACHE_LOG_DIR}/access.log combined 
        DocumentRoot /srv/botbot/src/botbot 
        <Directory /srv/botbot/src/botbot/botbot> 
                <Files wsgi.py> 
                        Require all granted 
                </Files> 
        </Directory> 

        WSGIDaemonProcess botbot python-path=/srv/botbot/src/botbot/botbot:/srv/botbot/lib/python2.7/site-packages 
        WSGIProcessGroup botbot 
        WSGIScriptAlias / /srv/botbot/src/botbot/botbot/wsgi.py 

</VirtualHost> 

ServerName server.test.com
ErrorLog${APACHE_LOG_DIR}/error.LOG
SetEnv WEB_SECRET_密钥密码1
CustomLog${APACHE\u LOG\u DIR}/access.LOG组合
DocumentRoot/srv/botbot/src/botbot
要求所有授权
WSGIDaemonProcess botbot python path=/srv/botbot/src/botbot/botbot:/srv/botbot/lib/python2.7/site-packages
WSGIProcessGroup僵尸程序
WSGIScriptAlias//srv/botbot/src/botbot/botbot/wsgi.py
这是我正在尝试构建的应用程序:


正如您所看到的,我尝试在apacheconf中设置SetEnv变量,但没有成功。我甚至不确定这样做是否正确,无论我做什么,我仍然会收到下面的错误。

我认为您应该尝试使用
setenif
指令,如apache文档中所述:

The internal environment variables set by this directive are set after   
most early request processing directives are run, such as access 
control and URI-to-filename mapping. If the environment variable you're 
setting is meant as input into this early phase of processing such as 
the RewriteRule directive, you should instead set the environment  
variable with SetEnvIf.
如果您想通过SetEnv进行设置,则可能是因为django应用程序在启动时还不能使用SetEnv

但可能您能做的最好的事情是在一些预运行脚本中设置这个env变量


快乐编码

我最后在我的wsgi.py文件中添加了以下行:

import os 

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "botbot.settings") 

# This application object is used by any WSGI server configured to use this 
# file. This includes Django's development server, if the WSGI_APPLICATION 
# setting points here. 
from django.core.wsgi import get_wsgi_application 
application = get_wsgi_application() 
os.environ['WEB_SECRET_KEY'] = 'password'
其中“password”等于.env文件中为WEB_SECRET_KEY变量指定的密码字符串

我还必须递归地将/srv目录(其中包含应用程序,没有其他内容)发送给www数据用户


本文档很有帮助:

无论是
SetEnv
还是
setenif
都不能为mod_WSGI托管的WSGI应用程序设置进程范围的环境变量。它们只在WSGI
environ
dictionary中设置每个请求的变量,这在代码查找进程范围的环境变量时没有帮助。在预运行脚本中设置变量对于由操作系统启动的apachehttpd来说是一件痛苦的事情,不推荐这样做。