Python Django mod_wsgi:处理wsgi脚本时发生异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24122175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django mod_wsgi: Exception occurred processing wsgi script
提问by arnold
I am deploying a django project and facing this error.
我正在部署一个 django 项目并面临这个错误。
My project structure like below:
我的项目结构如下:
my_project
my_project
urls.py
settings.py
index.wsgi
home
views.py
models.py
.........
requirements.txt
manage.py
And my index.wsgi looks like below:
我的 index.wsgi 如下所示:
import os
import sys
import site
# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('~/.virtualenvs/my_project/lib/python2.6/site-packages/')
# Add the app's directory to the PYTHONPATH
sys.path.append('/var/www/uni/my_project')
sys.path.append('/var/www/uni/my_project/home')
os.environ['DJANGO_SETTINGS_MODULE'] = 'my_project.settings'
# Activate your virtual env
activate_env=os.path.expanduser("/home/user/.virtualenvs/my_project/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
And in my virtualhost, the configuration is like below :
在我的虚拟主机中,配置如下:
<Directory /var/www/uni/my_project/templates/static>
Allow from all
</Directory>
WSGIScriptAlias / /var/uni/news/my_project/my_project/index.wsgi
The apache error.log is shown as:
apache error.log 显示为:
mod_wsgi (pid=27330): Exception occurred processing WSGI script '/var/www/uni/my_project/my_project/index.wsgi'.
[Mon Jun 09 14:23:53 2014] [error] [client ip] Traceback (most recent call last):
[Mon Jun 09 14:23:53 2014] [error] [client ip] File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/wsgi.py", line 219, in __call__
[Mon Jun 09 14:23:53 2014] [error] [client ip] self.load_middleware()
[Mon Jun 09 14:23:53 2014] [error] [client ip] File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 39, in load_middleware
[Mon Jun 09 14:23:53 2014] [error] [client ip] for middleware_path in settings.MIDDLEWARE_CLASSES:
[Mon Jun 09 14:23:53 2014] [error] [client ip] File "/usr/local/lib/python2.6/dist-packages/django/utils/functional.py", line 184, in inner
[Mon Jun 09 14:23:53 2014] [error] [client ip] self._setup()
[Mon Jun 09 14:23:53 2014] [error] [client ip] File "/usr/local/lib/python2.6/dist-packages/django/conf/__init__.py", line 42, in _setup
[Mon Jun 09 14:23:53 2014] [error] [client ip] self._wrapped = Settings(settings_module)
[Mon Jun 09 14:23:53 2014] [error] [client ip] File "/usr/local/lib/python2.6/dist-packages/django/conf/__init__.py", line 95, in __init__
[Mon Jun 09 14:23:53 2014] [error] [client ip] raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
[Mon Jun 09 14:23:53 2014] [error] [client ip] ImportError: Could not import settings 'my_project.settings' (Is it on sys.path?): No module named my_project.settings
I went through the mod_wsgi and djnago docs. I know the project structure is not maintaining all the best practices. I will change it later but before that I need to go it live.
我浏览了 mod_wsgi 和 djnago 文档。我知道项目结构并没有保持所有的最佳实践。我稍后会更改它,但在此之前我需要让它上线。
I tried by changing file permissions and all the changes that mentioned in same questions.
我尝试更改文件权限以及相同问题中提到的所有更改。
So, I am assuming I am doing something wrong.
所以,我假设我做错了什么。
Where is the mis configuration in the above files?
以上文件中的mis配置在哪里?
Thanks.
谢谢。
回答by xjtian
You should be specifying the virtualenv for the project in your Apache config, not your application WSGI file. Since you're using Apache, see https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/#using-a-virtualenvfor what you need to put into your Apache configuration. I would also highly recommend running in daemon mode - unless you have a very good reason not to, you should almost always be using daemon mode on Apache.
您应该在 Apache 配置中为项目指定 virtualenv,而不是应用程序 WSGI 文件。由于您使用的是 Apache,请参阅https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/modwsgi/#using-a-virtualenv了解需要放入 Apache 配置的内容。我还强烈建议在守护进程模式下运行 - 除非你有很好的理由不这样做,否则你应该几乎总是在 Apache 上使用守护进程模式。
Then reset your project WSGI file to
然后将您的项目 WSGI 文件重置为
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
回答by nahata5
I had this same thing happen to me a few days ago. I was reading all about it but this is how I finally fixed it all.
几天前我也遇到了同样的事情。我正在阅读有关它的所有内容,但这就是我最终解决所有问题的方法。
Your django wsgi file should be a python file (wsgi.py), not an index file (index.wsgi). This is what mine looked like, obviously replace usernames and folders.
import os, sys, site #Add the site-packages site.addsitedir('/home/SERVER_USERNAME/.virtualenvs/histology_env/lib/python2.6/site-packages') #activate_env=os.path.expanduser("~/.virtualenvs/histology_env/bin/activate_this.py") #execfile(activate_env, dict(__file__=activate_env)) sys.path.append('/opt/') sys.path.append('/opt/MY_PROJECT') sys.path.append('/opt/MY_PROJECT/MY_PROJECT') os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MY_PROJECT.settings") from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
DON"T ADD THE PROJECT TO THE /var/www/html/ FOLDER! I didn't realize that was a problem until where I got to you are. I chose to put it into the /opt/ directory.
You don't need to activate the env to get this to work. You add the packages in the first line. This is why I have it commented out in mine (site works fine).
In the virtual configuration
NameVirtualHost *:80 WSGISocketPrefix /var/run/wsgi WSGIPythonPath /home/SERVER_USERNAME/.virtualenvs/histology_env/lib/python2.6/site-packages <VirtualHost *:80> WSGIDaemonProcess safe python-path=/opt/virtual_microscope:/home/SERVER_USERNAME/.virtualenvs/histology_env/lib/python2.6/site-packages WSGIProcessGroup safe WSGIScriptAlias / /opt/MY_PROJECT/MY_PROJECT/wsgi.py Alias /static/ /var/www/html/home/static/ Alias /slides/ /var/www/html/pictures/slides/ <Directory /opt/MY_PROJECT/MY_PROJECT/> Order deny,allow Allow from all </Directory> </VirtualHost>
After you change these settings in the host file, make sure you
sudo service httpd reload
你的 django wsgi 文件应该是一个 python 文件(wsgi.py),而不是一个索引文件(index.wsgi)。这就是我的样子,显然替换了用户名和文件夹。
import os, sys, site #Add the site-packages site.addsitedir('/home/SERVER_USERNAME/.virtualenvs/histology_env/lib/python2.6/site-packages') #activate_env=os.path.expanduser("~/.virtualenvs/histology_env/bin/activate_this.py") #execfile(activate_env, dict(__file__=activate_env)) sys.path.append('/opt/') sys.path.append('/opt/MY_PROJECT') sys.path.append('/opt/MY_PROJECT/MY_PROJECT') os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MY_PROJECT.settings") from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
不要将项目添加到 /var/www/html/ 文件夹中!直到我找到你才意识到这是一个问题。我选择将它放入 /opt/ 目录中。
您无需激活 env 即可使其工作。您在第一行添加包。这就是为什么我在我的(网站工作正常)中将其注释掉的原因。
在虚拟配置中
NameVirtualHost *:80 WSGISocketPrefix /var/run/wsgi WSGIPythonPath /home/SERVER_USERNAME/.virtualenvs/histology_env/lib/python2.6/site-packages <VirtualHost *:80> WSGIDaemonProcess safe python-path=/opt/virtual_microscope:/home/SERVER_USERNAME/.virtualenvs/histology_env/lib/python2.6/site-packages WSGIProcessGroup safe WSGIScriptAlias / /opt/MY_PROJECT/MY_PROJECT/wsgi.py Alias /static/ /var/www/html/home/static/ Alias /slides/ /var/www/html/pictures/slides/ <Directory /opt/MY_PROJECT/MY_PROJECT/> Order deny,allow Allow from all </Directory> </VirtualHost>
在主机文件中更改这些设置后,请确保您
sudo service httpd reload
Good luck, I know what you're going through and it is frustrating to not have the answers. Hope this works for you, it did for me.
祝你好运,我知道你正在经历什么,没有答案令人沮丧。希望这对你有用,它对我有用。
回答by soloidx
I have a checklist in these kind of scenarios:
我在这些场景中有一个清单:
Execute a script testing the
index.wsgi
interface directly with python, correct errors until the output shows that all is ok. (see update)Check for permissions.
Check for apache configuration.
index.wsgi
直接用python执行脚本测试界面,纠正错误直到输出显示一切正常。(见更新)检查权限。
检查 apache 配置。
Update:
更新:
Executing python my_project/index.wsgi
directly shows nothing in console, this approach works on fcgi configuration but no in wsgi, you will need a script that create a test server and show posible errors on console, the example script is this:
python my_project/index.wsgi
直接执行在控制台中不显示任何内容,这种方法适用于 fcgi 配置但不适用于 wsgi,您将需要一个脚本来创建测试服务器并在控制台上显示可能的错误,示例脚本是这样的:
test_server.py
测试服务器.py
#!/usr/bin/env python
from wsgiref.util import setup_testing_defaults
from wsgiref.simple_server import make_server
from my_project.index import application #importing the project's index.wsgi file
httpd = make_server('', 8000, application)
httpd.serve_forever()
Steps:
脚步:
- Run the test in console (even local)
- Go to the browser and navigate to this server:port
- see the logs in console
- 在控制台中运行测试(甚至是本地)
- 转到浏览器并导航到此服务器:端口
- 查看控制台中的日志
Recommendations:
建议:
Check for you
__init__.py
either in the project folder and the settings folderTry to use relative pathsin the settings and in the .wsgi file(see example)
Verify the uppercase/lowecase in your names (probably changing
"MY_PROJECT.settings"
for"my_project.settings"
__init__.py
在项目文件夹和设置文件夹中检查您尝试在设置和 .wsgi 文件中使用相对路径(参见示例)
验证您姓名中的大写/小写(可能更改
"MY_PROJECT.settings"
为"my_project.settings"
example:
例子:
import os, sys, site
CURRENT_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
WORK_DIRECTORY = os.path.join(CURRENT_DIRECTORY, '..')
#Add the site-packages
site.addsitedir('/home/SERVER_USERNAME/.virtualenvs/histology_env/lib/python2.6/site-packages')
#activate_env=os.path.expanduser("~/.virtualenvs/histology_env/bin/activate_this.py")
#execfile(activate_env, dict(__file__=activate_env))
#adding the project to the python path
sys.path.append(WORK_DIRECTORY)
#adding the parent directory to the python path
sys.path.append(os.path.join(WORK_DIRECTORY, '..'))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()