代码之家  ›  专栏  ›  技术社区  ›  Anuj TBE

在apache上使用pipenv部署django应用程序

  •  2
  • Anuj TBE  · 技术社区  · 6 年前

    我在中创建了一个python web应用程序 Django 2.0 使用 pipenv 虚拟人

    现在,我必须在apache服务器上托管它。我已经安装好了 libapache2-mod-wsgi-py3 python-setuptools 在服务器上。

    我的申请结构如下

    myapp_dir
     |- myapp
        |- settings
           |- __init__.py
           |- production.py
        |- __init__.py
        |- urls.py
        |- wsgi.py
     |- otherapp
     |- templates
     |- static_my_project
     |- manage.py
     |- Pipfile
     |- Pipfile.lock
    

    要放置的应用程序的路径

    /home/user/app.application.com/
    

    我已将所有文件移动到目录并安装了 Pipfile 在目录中运行

    pipenv install
    

    这已经创建了一个virtualenv并安装了所有必需的模块和 pipenv --venv 给予

    # pipenv --venv
    /home/user/.local/share/virtualenvs/app.application.com-IuTkL8w_
    

    我的 VirtualHost 配置看起来像

    ServerName app.application.com
    ServerAlias app.application.com
    
    ErrorLog /home/user/error.log
    CustomLog /home/user/custom.log combined
    
    Alias /static /home/user/app.application.com/static_my_project
    <Directory /home/user/app.application.com/static_my_project>
        Require all granted
    </Directory>
    
    <Directory /home/user/app.application.com/pricearbitrase>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>
    
    WSGIScriptAlias / /home/user/app.application.com/myapp/wsgi.py
    

    虚拟包含

    <IfModule mod_wsgi>
    
        WSGIPythonHome /home/user/.local/share/virtualenvs/app.application.com-IuTkL8w_
    
    </IfModule>
    

    但在访问时 http://app.application.com 它给予 Internal server error 生成的日志文件包含

    [wsgi:error] [pid 60730] mod_wsgi (pid=60730): Target WSGI script '/home/amzitrage/app.amzitrage.com/pricearbitrase/wsgi.py' cannot be loaded as Python module.
    [wsgi:error] [pid 60730] mod_wsgi (pid=60730): Exception occurred processing WSGI script '/home/amzitrage/app.amzitrage.com/pricearbitrase/wsgi.py'.
    [wsgi:error] [pid 60730] Traceback (most recent call last):
    [wsgi:error] [pid 60730]   File "/home/amzitrage/app.amzitrage.com/pricearbitrase/wsgi.py", line 12, in <module>
    [wsgi:error] [pid 60730]     from django.core.wsgi import get_wsgi_application
    [wsgi:error] [pid 60730] ImportError: No module named django.core.wsgi
    

    编辑2

    应用程序/wsgi.py 被改进的 wsgi.py 要激活的文件 virtual environment

    activate_this = '/home/user/.local/share/virtualenvs/app.application.com-IuTkL8w_/bin/activate_this.py'
    exec(compile(open(activate_this,"rb").read(),activate_this, 'exec'), dict(__file__=activate_this))
    
    import os
    
    from django.core.wsgi import get_wsgi_application
    
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "application.settings")
    
    application = get_wsgi_application()
    

    也, ls -l /path_to_pipenv_venv/bin 给予

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  2
  •   qocu    6 年前

    首先,我做了一个指向pipenv virtualenv to的符号链接 myapp_dir/venv 只是为了方便在apache文件中使用。 我遇到的主要问题是权限。

    如果你的虚拟路径是 /home/user/.local/share/virtualenvs/app.application.com-IuTkL8w_ 然后试试这个:

    sudo -u www-data cat /home/user/.local/share/virtualenvs/app.application.com-IuTkL8w_/bin/activate_this.py`
    

    在哪里? www-data 是您的apache用户。

    如果你被拒绝了许可,可能也是同样的问题。 尝试更改的权限 ~/.local ~/.local/share apache进程可以访问它们。

    请注意,这是一个开发设置,在生产中我不会使用pipenv。我只会创造 requirements.txt 设置一个普通的虚拟机或者使用gunicorn或者类似的东西。