【问题标题】:index.wsgi not finding virtualenv in rootindex.wsgi 在根目录中找不到 virtualenv
【发布时间】:2015-02-17 12:22:50
【问题描述】:

我正在尝试在 Apache VPS 上安装 django 站点,遵循此 tutorial
我的 index.wsgi 应该在根目录中激活一个 virtualenv,它看起来像这样:

import os
import sys
import site

# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('~/.virtualenvs/DBENV/local/lib/python2.7/site-packages')

# Add the app's directory to the PYTHONPATH
sys.path.append('/home/DB2015/')
sys.path.append('/home/DB2015/davidcms/')

os.environ['DJANGO_SETTINGS_MODULE'] = 'davidcms.settings'

# Activate your virtual env
activate_env=os.path.expanduser("~/.virtualenvs/DBENV/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

我得到了这个错误

[Tue Feb 17 07:13:30.701511 2015] [:error] [pid 16103:tid 140396130674432]           [client 217.44.75.146:58169]     execfile(activate_env, dict(__file__=activate_env))
[Tue Feb 17 07:13:30.701653 2015] [:error] [pid 16103:tid 140396130674432] [client 217.44.75.146:58169] IOError: [Errno 2] No such file or directory: '/var/www/.virtualenvs/DBENV/bin/activate_this.py'

所以它在寻找var/www/ 而不是根。当我尝试将 os.path.expanduser 更改为根目录中 .virtualenvs 的路径时:

# Activate your virtual env
activate_env= "~/.virtualenvs/DBENV/bin/activate_this.py"
execfile(activate_env, dict(__file__=activate_env))

它仍然在说

[Tue Feb 17 07:17:12.019641 2015] [:error] [pid 16104:tid 140396206208768] [client 217.44.75.146:58200]     execfile(activate_env, dict(__file__=activate_env))
[Tue Feb 17 07:17:12.019852 2015] [:error] [pid 16104:tid 140396206208768] [client 217.44.75.146:58200] IOError: [Errno 2] No such file or directory: '~/.virtualenvs/DBENV/bin/activate_this.py'

我错过了什么?!

【问题讨论】:

    标签: django apache virtualenv wsgi


    【解决方案1】:

    可能 Apache 不是以您的身份运行,而是以它自己的用户身份运行,因此它不会使用您的主目录。

    无论如何,您不应该将您的代码放在主目录中。选择一个标准的地方 - 例如一个新的顶级目录,如/srv,或/var/sites,或其他东西。

    【讨论】:

    • 并把 virtualenv 放在那里?
    • 是的,使用绝对路径。
    • 谢谢!我认为这解决了它。现在遇到其他错误,但我们不要进入那些:)
    【解决方案2】:

    这似乎是一个相对较新的问题或可能是一个错误?除了与 David 和我都遇到的问题相关的一些最近的帖子(可能还有一两个错误报告)外,我什么也找不到。

    我使用 virtualenvwrapper 来管理我的 VE, 熟悉它的人都知道,您可以通过导航到“~/.virtualenvs/whaterver”之类的目录来访问 VE。

    根据this:~/(波浪线斜线):波浪线是 Linux 的“快捷方式”,表示用户的主目录。

    这意味着您还可以通过导航到“/home/username/.virtualenvs/”(这是 wsgi 正在寻找的绝对路径)来访问 VE。

    [WSGI 配置]

    我可以通过像这样修改我的 index.wsgi 文件来运行我的 Django 欢迎页面:

        ------------
        # Changed From:
    
        # Add the site-packages of the chosen virtualenv to work with
        site.addsitedir('~/.virtualenvs/myprojectenv/local/lib/python2.7/site-packages')
    
        # Activate your virtual env 
        activate_env=os.path.expanduser("~/.virtualenvs/myprojectenv/bin/activate_this.py")
    
        ------------
        # Changed To:
    
        # Add the site-packages of the chosen virtualenv to work with
        site.addsitedir('/home/username/.virtualenvs/myprojectenv/local/lib/python2.7/site-packages')
    
        # Activate your virtual env 
        activate_env=os.path.expanduser('/home/username/.virtualenvs/myprojectenv/bin/activate_this.py') 
    

    重要提示:以我在此处列出的相同方式进行设置时,您必须不要在 django 项目的 settings.py 中指定 wsgi 文件(似乎是从 django-admin 创建新项目时自动生成的情况)。在我的系统上,当两个文件都被加载时,我无法从 Django 获得响应,并且我的浏览器会无限期挂起“正在加载”,并且没有消息被发布到 apache error.log 文件中,尽管来自 django 的“一些”错误会使如果 debug=true,它会发送到浏览器。所以如果你在settings.py中有这样一行:

    • WSGI_APPLICATION = 'myproject.wsgi.application'

    确保将其注释掉或将其删除(我花了一两个小时扯头发才注意到冲突)

    [更新旧示例]

    但是,我确实遇到了从我的 wsgi 文件生成的问题。如果您收到类似于以下内容的错误: “AppRegistryNotReady:在应用程序注册表准备好之前无法初始化翻译基础结构。请检查您在导入时没有进行非惰性 gettext 调用。”

    这是我在尝试使用此过程为 apache 配置 django 时收到的错误:

    • Deploy Django on Apache with Virtualenv and mod_wsgi.

      #Make sure you are not doing this
      import django.core.handlers.wsgi
      application = django.core.handlers.wsgi.WSGIHandler()
      
      #And rather you should be doing this
      from django.core.wsgi import get_wsgi_application
      application = get_wsgi_application()
      

    根据django document,这是(现在)构建 wsgi 文件的正确方法。

    [文件权限]

    在 django 项目目录中设置所有文件的正确组和所有权也是一个好主意。 我更喜欢将所有权和组作为我正在配置的 VE 的用户。

    这可以通过运行命令来完成:

    • sudo chown -R 用户名 /home/django_projects/myproject
    • sudo chgrp -R 用户名/home/django_projects/myproject

    [我的规格]

    • Ubuntu 服务器 14.10
    • Apache 2.4.10
    • Django 1.7.4

    [我的 apache 配置]

    我已经配置了一个虚拟主机,所以我可以从本地网络上的 8080 端口访问我的 django 应用程序。这也允许我通过配置 WSGIPythonHome 指令来设置我的 VE python 的路径。此外,我在守护程序模式下运行 wsgi 以使用 WSGIDaemonProcess 指令来指定守护程序线程应该作为哪个用户运行。

    #My awesome django app's apache config
    WSGIPythonHome /home/username/.virtualenvs/myprojectenv #This is important and sets the path that apache/wsgi uses to the virtualenv's python.
    <VirtualHost *:8080>
            #ServerAdmin [email protected]
            ServerName mydomain.com
            #ServerAlias www.mydomain.com
    
            #Must run as daemon so the delegated process can inherit the virtualenvs user 
            WSGIDaemonProcess SomeProcessGroupName user=username group=username processes=2 threads=5 home=/var/www/my-site.com/
            WSGIProcessGroup SomeProcessGroupName 
            WSGIScriptAlias / /var/www/my-site.com/index.wsgi
    
            Alias /static /var/www/my-site.com/static/
            <Location "/static/">
                #Options -Indexes
            </Location>
    </VirtualHost>
    

    [我的目录结构]

    |
    |_var
    |   |_www
    |       |_my-site.com
    |           |_index.wsgi
    |           |_static
    |               |_css
    |               |_js
    |_home
    |   |_django_projects
    |   |   |_projectbase
    |   |       |_myproject
    |   |       |   |_myproject
    |   |       |       |_settings.py
    |   |       |
    |   |       |_templates
    |   |
    |   |_username
    |       |_.virtualenvs
    |           |_myprojectenv
    |               |_bin
    |                   |_activate_this.py
    |_etc
        |_apache2
            |_apache2.conf
    

    [我的 Python 路径]

    # Printed from python console in my virtual environment
    
    /home/username/.virtualenvs/myprojectenv/lib/python2.7
    /home/username/.virtualenvs/myprojectenv/lib/python2.7/plat-x86_64-linux-gnu
    /home/username/.virtualenvs/myprojectenv/lib/python2.7/lib-tk
    /home/username/.virtualenvs/myprojectenv/lib/python2.7/lib-old
    /home/username/.virtualenvs/myprojectenv/lib/python2.7/lib-dynload
    /usr/lib/python2.7
    /usr/lib/python2.7/plat-x86_64-linux-gnu
    /usr/lib/python2.7/lib-tk
    /home/username/.virtualenvs/myprojectenv/local/lib/python2.7/site-packages
    /home/username/.virtualenvs/myprojectenv/lib/python2.7/site-packages
    
    
    # printed to the apache log from within settings.py
    
    /home/username/.virtualenvs/myprojectenv/lib/python2.7',
    /home/username/.virtualenvs/myprojectenv/lib/python2.7/plat-x86_64-linux-gnu',
    /home/username/.virtualenvs/myprojectenv/lib/python2.7/lib-tk',
    /home/username/.virtualenvs/myprojectenv/lib/python2.7/lib-old',
    /home/username/.virtualenvs/myprojectenv/lib/python2.7/lib-dynload',
    /usr/lib/python2.7',
    /usr/lib/python2.7/plat-x86_64-linux-gnu',
    /usr/lib/python2.7/lib-tk',
    /home/username/.virtualenvs/Horizon_Server_Env/local/lib/python2.7/site-packages',
    /home/django-projects/projectbase/myproject',
    /home/django-projects/projectbase/myproject/myproject'
    

    [夏天]

    我已经记录了我在配置 Django 以按照 David 和我正在使用的教程运行的过程中所采取的一些步骤。正如我之前所说,我的应用程序在本地网络(端口 8080)上运行并且无法访问 Internet。所以这个配置是为了我的需要,可能不符合你的安全要求(尽管我很想得到其他人关于这个主题的意见!!)。正如 Daniel R. 之前所说,您可能需要考虑将虚拟环境放在顶级目录中。但是,我觉得上面提到的方法是运行该教程的好方法,而无需对他们的示例进行很多修改。 希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2016-02-12
      • 1970-01-01
      • 2014-03-21
      • 2019-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-21
      • 2020-04-22
      相关资源
      最近更新 更多