【问题标题】:Error when serve Django 1.8 Applications with Apache2 and mod_wsgi on Ubuntu 14.04在 Ubuntu 14.04 上使用 Apache2 和 mod_wsgi 服务 Django 1.8 应用程序时出错
【发布时间】:2015-08-03 09:34:41
【问题描述】:

我正在尝试使用 Apache2 和 mod_wsgi 为我的 django 项目提供服务,但我被卡住了。在我尝试集成 mod_wsgi 之前一切正常。我用的是python 3,相关编码如下:


Apache2.conf

<VirtualHost *:80>
        ServerAdmin myemail@example.com
        ServerName example.com
        ServerAlias www.example.com

        DocumentRoot /var/www/mysite

        Alias /static/ /var/www/mysite/static/

        <Directory /var/www/mysite/static>
        Require all granted
        </Directory>


        WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py


        <Directory /var/www/mysite/mysite/wsgi.py>
        <Files wsgi.py>
        Require all granted
        </Files>
        </Directory>

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

wsgi.py

​​>
import os

from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()

错误日志

[Mon Aug 03 18:45:42.192589 2015] [:error] [pid 2638:tid 140604829079296] [client 130.102.158.21:49084]   File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
[Mon Aug 03 18:45:42.192606 2015] [:error] [pid 2638:tid 140604829079296] [client 130.102.158.21:49084]   File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
[Mon Aug 03 18:45:42.192623 2015] [:error] [pid 2638:tid 140604829079296] [client 130.102.158.21:49084]   File "<frozen importlib._bootstrap>", line 2224, in _find_and_load_unlocked
[Mon Aug 03 18:45:42.192649 2015] [:error] [pid 2638:tid 140604829079296] [client 130.102.158.21:49084] ImportError: No module named 'mysite'

我知道这与 wsgi 在 apache2.conf 文件中的配置方式有关,但不知道如何修复它。任何帮助,将不胜感激。 谢谢

【问题讨论】:

  • 尝试将此添加到您的 wsgi.py:import sys sys.path.append('path to you site')。

标签: python linux django apache ubuntu


【解决方案1】:

你需要添加这个:

WSGIPythonPath /var/www/mysite

来自doc“WSGIPythonPath 行确保您的项目包可用于在 Python 路径上导入;换句话说,导入 mysite 有效。”

【讨论】:

  • 非常感谢您的帮助。我还使用了 WSGIDaemonProcess。它现在正在发挥魅力。
猜你喜欢
  • 2018-03-30
  • 2016-08-06
  • 2016-03-30
  • 2019-11-25
  • 2017-08-07
  • 2016-08-10
  • 2013-07-01
  • 2020-08-02
  • 2019-05-16
相关资源
最近更新 更多