【问题标题】:What do I need to do to make django work using apache+mod_wsgi? I keep getting an import error for the settings module我需要做什么才能使 django 使用 apache+mod_wsgi 工作?我不断收到设置模块的导入错误
【发布时间】:2013-07-04 04:11:02
【问题描述】:

我很难弄清楚我的安装出了什么问题。这是我第一次在生产服务器上设置 django 应用程序,所以我想我只需要一点帮助来弄清楚如何配置设置模块的路径。这就是我的 httpd.conf 文件中的内容。

<VirtualHost *:80>
ServerAdmin admin@example.com
ServerName  nodeline.com
WSGIScriptAlias / /home/mysite/mysite/wsgi.py

</VirtualHost>

我使用此命令在 /home 目录中启动了一个名为 mysite 的基本项目。

django-admin.py startproject mysite

这是 apache 错误日志中出现的内容:

[Sat Jul 06 02:49:42 2013] [error] [client 119.63.193.132] mod_wsgi (pid=26120): Exception occurred processing WSGI script '/home/mysite/mysite/wsgi.py'.
[Sat Jul 06 02:49:42 2013] [error] [client 119.63.193.132] Traceback (most recent call last):
[Sat Jul 06 02:49:42 2013] [error] [client 119.63.193.132]   File "/usr/lib/python2.6/site-packages/django/core/handlers/wsgi.py", line 236, in __call__
[Sat Jul 06 02:49:42 2013] [error] [client 119.63.193.132]     self.load_middleware()
[Sat Jul 06 02:49:42 2013] [error] [client 119.63.193.132]   File "/usr/lib/python2.6/site-packages/django/core/handlers/base.py", line 45, in load_middleware
[Sat Jul 06 02:49:42 2013] [error] [client 119.63.193.132]     for middleware_path in settings.MIDDLEWARE_CLASSES:
[Sat Jul 06 02:49:42 2013] [error] [client 119.63.193.132]   File "/usr/lib/python2.6/site-packages/django/conf/__init__.py", line 53, in __getattr__
[Sat Jul 06 02:49:42 2013] [error] [client 119.63.193.132]     self._setup(name)
[Sat Jul 06 02:49:42 2013] [error] [client 119.63.193.132]   File "/usr/lib/python2.6/site-packages/django/conf/__init__.py", line 48, in _setup
[Sat Jul 06 02:49:42 2013] [error] [client 119.63.193.132]     self._wrapped = Settings(settings_module)
[Sat Jul 06 02:49:42 2013] [error] [client 119.63.193.132]   File "/usr/lib/python2.6/site-packages/django/conf/__init__.py", line 134, in __init__
[Sat Jul 06 02:49:42 2013] [error] [client 119.63.193.132]     raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
[Sat Jul 06 02:49:42 2013] [error] [client 119.63.193.132] ImportError: Could not import settings 'mysite.settings' (Is it on sys.path?): No module named mysite.settings

这是 /home/mysite/mysite/wsgi.py 文件中的内容(这是启动项目时文件附带的默认配置。):

"""
WSGI config for mysite project.

This module contains the WSGI application used by Django's development server
and any production WSGI deployments. It should expose a module-level variable
named ``application``. Django's ``runserver`` and ``runfcgi`` commands discover
this application via the ``WSGI_APPLICATION`` setting.

Usually you will have the standard Django WSGI application here, but it also
might make sense to replace the whole Django WSGI application with a custom one
that later delegates to the Django one. For example, you could introduce WSGI
middleware here, or combine a Django application with an application of another
framework.

"""
import os

# We defer to a DJANGO_SETTINGS_MODULE already in the environment. This breaks
# if running multiple sites in the same mod_wsgi process. To fix this, use
# mod_wsgi daemon mode with each site in its own daemon process, or use
# os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.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()

# Apply WSGI middleware here.
# from helloworld.wsgi import HelloWorldApplication
# application = HelloWorldApplication(application)

我真的只需要显示标准屏幕,我知道我可以从那里继续前进。我还应该注意,当我仅使用 django 内置的开发服务器启动应用程序时,它可以工作。我使用以下命令在端口 8000 上启动它:

python manage.py runserver 0:8000

访问 nodeline.com:8000 时,它可以工作。

【问题讨论】:

标签: python django apache wsgi production


【解决方案1】:

您是否阅读过有关 mod_wsgi 部署的 Django 文档?在:

它谈到需要设置 Python 模块搜索路径。

我建议你去阅读文档。

【讨论】:

    【解决方案2】:
    Let me try to help you.
    i hope you are using ubuntu like OS.
    first after installed apache and get it works.
    install mod-wsgi which integrate mod-python by:
    sudo apt-get install libpache2-mod-wsgi
    and create a directory named apache in your Project directory
    open that directory [project]/apache and create a file django.wsgi.py
    and write this code inside
    wsgi_dir =os.path.abspath(os.path.dirname(__file__))
    project_dir = os.path.dirname(wsgi_dir)
    sys.path.append(project_dir)
    project_settings =os.join(project_dir,'settings')
    os.environ['DJANGO_SETTINGS_MODULE'] ='[project-name].settings'
    import django.core.handlers.wsgi
    application =django.core.handlers.wsgi.WSGIHandler()
    
    In your apache server directory and in the sites-available create a file which going
    to reflect your project name and inside that file write your code like this:
    NameVirtualHost 127.0.0.1:8080
    <VirtualHost 127.0.0.1:8080>
    ServerAdmin webmaster@you-domain.com
        ServerName your-domain.com
    ServerAlias  your-domain.com
    
    Alias /static /path/to/your/project/static
    
    DocumentRoot /path/to/your/project
    WSGIScriptAlias / /path/to/your/project/apache/django.wsgi
    
    Errorlog /var/log/apache2/error.log
    #ErrorLog ${APACHE_LOG_DIR}/error.log
    #ErrorLog /var/log/apache2/error.log
    
    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn
    
    # CustomLog /home/user/Customerlog/
    #CustomLog ${APACHE_LOG_DIR}/access.log combined
    CustomLog /var/log/apache2/access.log combined
    
    RPAFenable On
    RPAFsethostname On
    RPAFproxy_ips 127.0.0.1
    </VirtualHost>
    
    under your terminal type:
    sudo a2ensite [name-of-file create under site-available]
    
    open ports.conf and type listen:80
    and in the urls.py remove the DEBUG_MODE and restart apache
    sudo service apache2 restart
    IF YOU ARE USING STATIC FILES too :
    you have to install nginx (engine X) and configured it before got the statics files work
    with apache.
    

    【讨论】:

      【解决方案3】:

      确保将 settings.py 的设置路径添加到 wsgi.py 中:

      import sys, os
      sys.path.append(os.path.dirname(os.path.abspath(__file__)))
      

      【讨论】:

        猜你喜欢
        • 2012-10-22
        • 2012-07-13
        • 1970-01-01
        • 1970-01-01
        • 2015-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多