Django使用apache,而wsgi使用ImportError

时间:2022-12-06 20:25:13

I'm trying to deploy my Django app to an Apache server with no luck. I succeeded with the WSGI sample application, and tried to host an empty Django project. While it works properly with the manage.py runserver, it throws the following error when using apache:

我正在尝试将Django应用程序部署到Apache服务器上,但运气不佳。我成功地使用了WSGI示例应用程序,并尝试托管一个空的Django项目。当它与管理部门正常工作时。py runserver,使用apache时抛出如下错误:

[notice] Apache/2.2.22 (Debian) PHP/5.4.4-14+deb7u9 mod_python/3.3.1 Python/2.7.3 mod_wsgi/2.7 configured -- resuming normal operations
[error] [client x.x.x.x] mod_wsgi (pid=8300): Exception occurred processing WSGI script '/usr/local/www/django/myapp/wsgi.py'.
[error] [client x.x.x.x] Traceback (most recent call last):
[error] [client x.x.x.x]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 187, in __call__
[error] [client x.x.x.x]     self.load_middleware()
[error] [client x.x.x.x]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 44, in load_middleware
[error] [client x.x.x.x]     for middleware_path in settings.MIDDLEWARE_CLASSES:
[error] [client x.x.x.x]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 54, in __getattr__
[error] [client x.x.x.x]     self._setup(name)
[error] [client x.x.x.x]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 49, in _setup
[error] [client x.x.x.x]     self._wrapped = Settings(settings_module)
[error] [client x.x.x.x]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 132, in __init__
[error] [client x.x.x.x]     % (self.SETTINGS_MODULE, e)
[error] [client x.x.x.x] ImportError: Could not import settings 'myapp.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named myapp.settings

My wsgi.py is the following:

我的wsgi。py如下:

"""
WSGI config for myapp 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.6/howto/deployment/wsgi/
"""

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

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

I have a wsgi.conf in the conf.d library for apache:

我有一个wsgi。conf在conf.d库中的apache:

<VirtualHost *:80>
ServerName myapp.example.com
ServerAlias myapp
ServerAdmin admin@example.com
DocumentRoot /var/www

<Directory /usr/local/www/django>
    Order allow,deny
    Allow from all
</Directory>

WSGIDaemonProcess myapp processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup myapp

WSGIScriptAlias /myapp /usr/local/www/django/myapp/wsgi.py
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so

</VirtualHost>

WSGIPythonPath /usr/local/www/django/myapp

[SOLVED] Thanks, I started all over again, made the suggested modifications to my configuration files, and now it's working. I couldn't flag both suggestions correct, but I think both of them were necessary and I had a third (fourth, fifth...) bug to, which went away after reinstallation.

谢谢,我重新开始,对我的配置文件做了建议的修改,现在可以工作了。我无法标记两个建议是否正确,但我认为这两个建议都是必要的,我有第三个(第四个,第五个…)错误,在重新安装后就消失了。

2 个解决方案

#1


2  

It looks like you've been using an old guide for setting up apache2 / wsgi. I'd recommend using the official guide at https://code.google.com/p/modwsgi/wiki/InstallationInstructions

看起来您一直在使用一个旧的指南来设置apache2 / wsgi。我建议使用https://code.google.com/p/modwsgi/wiki/InstallationInstructions的官方指南

Anyway, your specific problem is that the wsgi application isn't picking up the python path correctly. Change you VirtualHost conf to something like this

不管怎样,您的具体问题是wsgi应用程序没有正确地获取python路径。将您的虚拟主机conf更改为以下内容

<VirtualHost *:80>
    ServerName myapp.example.com
    ServerAlias myapp
    ServerAdmin admin@example.com

    DocumentRoot /usr/local/www/django/myapp
    WSGIDaemonProcess myapp processes=2 threads=15 display-name=%{GROUP} python-path=/usr/local/www/django/myapp:/path/to/system/python/site-packages
    WSGIProcessGroup myapp
    WSGIScriptAlias / /usr/local/www/django/myapp/wsgi.py

    <Directory /usr/local/www/django/myapp>
        <Files wsgi.py>
            Order allow,deny
            Allow from all
        </Files>
    </Directory>
</VirtualHost>

#2


1  

Your settings file is at /usr/local/www/django/myapp/settings.py, but you've set the PythonPath to /usr/local/www/django/myapp and then set DJANGO_SETTINGS_MODULE to "myapp.settings" - that would only be suitable if settings was in myapp/myapp/settings. Drop one of those references to "myapp".

您的设置文件位于/usr/local/www/django/myapp/settings但是您已经将python路径设置为/usr/local/www/django/myapp,然后将DJANGO_SETTINGS_MODULE设置为“myapp”。“设置”——只有在myapp/myapp/settings中设置才适合。删除其中一个对“myapp”的引用。

#1


2  

It looks like you've been using an old guide for setting up apache2 / wsgi. I'd recommend using the official guide at https://code.google.com/p/modwsgi/wiki/InstallationInstructions

看起来您一直在使用一个旧的指南来设置apache2 / wsgi。我建议使用https://code.google.com/p/modwsgi/wiki/InstallationInstructions的官方指南

Anyway, your specific problem is that the wsgi application isn't picking up the python path correctly. Change you VirtualHost conf to something like this

不管怎样,您的具体问题是wsgi应用程序没有正确地获取python路径。将您的虚拟主机conf更改为以下内容

<VirtualHost *:80>
    ServerName myapp.example.com
    ServerAlias myapp
    ServerAdmin admin@example.com

    DocumentRoot /usr/local/www/django/myapp
    WSGIDaemonProcess myapp processes=2 threads=15 display-name=%{GROUP} python-path=/usr/local/www/django/myapp:/path/to/system/python/site-packages
    WSGIProcessGroup myapp
    WSGIScriptAlias / /usr/local/www/django/myapp/wsgi.py

    <Directory /usr/local/www/django/myapp>
        <Files wsgi.py>
            Order allow,deny
            Allow from all
        </Files>
    </Directory>
</VirtualHost>

#2


1  

Your settings file is at /usr/local/www/django/myapp/settings.py, but you've set the PythonPath to /usr/local/www/django/myapp and then set DJANGO_SETTINGS_MODULE to "myapp.settings" - that would only be suitable if settings was in myapp/myapp/settings. Drop one of those references to "myapp".

您的设置文件位于/usr/local/www/django/myapp/settings但是您已经将python路径设置为/usr/local/www/django/myapp,然后将DJANGO_SETTINGS_MODULE设置为“myapp”。“设置”——只有在myapp/myapp/settings中设置才适合。删除其中一个对“myapp”的引用。