使用 mod_wsgi 在 Ubuntu 16.04 apache2 上部署 Django 应用程序

Posted

技术标签:

【中文标题】使用 mod_wsgi 在 Ubuntu 16.04 apache2 上部署 Django 应用程序【英文标题】:Deploying Django app on Ubuntu 16.04 apache2 with mod_wsgi 【发布时间】:2018-03-30 20:46:47 【问题描述】:

我正在尝试将我的 django 应用程序部署到使用 mod_wdgi 运行 Apache 的 Ubuntu 16.04。我正确设置了 apache 服务器(我认为),但是当 apache 尝试运行我的“prod_wgsi.py”脚本时遇到问题,它试图导入错误的设置模块。

我可以使用“python -i prod_wsgi.py”启动 python 解释器,加载我的设置文件并运行 django.setup()。

文件结构

NOC邓肯

   website
     ___init__.py
     manage.py
     prod_wsgi.py
     website
        settings
           ___init__.py
           dev.py
           prod.py
           base.py
        templates
        ___init__.py

prod_wsgi.py

import os
from django.core.wsgi import get_wsgi_application
import sys
import site

# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('/home/biffduncan/.virtualenvs/nocduncanenv/lib/python3.5/site-packages')

# Add the app's directory to the PYTHONPATH
paths = [
    '/home/biffduncan/opt/NOCduncan',
    '/home/biffduncan/opt/NOCduncan/website/',
    '/var/www/nocduncan/website',
    '/var/www/nocduncan',
]

for path in paths:
    if path not in sys.path:
        sys.path.append(path)

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'website.settings.prod')

# Activate your virtual env
activate_env=os.path.expanduser('/home/biffduncan/.virtualenvs/nocduncanenv/bin/activate_this.py')
exec(open(activate_env).read())

application = get_wsgi_application()

prod.py

from website.settings.base import *


WSGI_APPLICATION = 'prod_wsgi.application'

Apache 配置

<VirtualHost *:80>
    #My site Name
    ServerName noc.biffduncan.com

    #Demon process for multiple virtual hosts
    WSGIDaemonProcess noc.biffduncan.com threads=5

    #Pointing wsgi script to config file
    WSGIScriptAlias / /var/www/nocduncan/prod_wsgi.py
    WSGIProcessGroup noc.biffduncan.com

    #Your static files location
    Alias /static/ "/var/www/nocduncan/website/static"
    <Location "/static/">
        Options -Indexes
    </Location>
</VirtualHost>

Apache 错误

[Wed Oct 18 20:57:08.018901 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061] mod_wsgi (pid=21696): Target WSGI script '/var/www/nocduncan/prod_wsgi.py' cannot be loaded as Python module.
[Wed Oct 18 20:57:08.019036 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061] mod_wsgi (pid=21696): Exception occurred processing WSGI script '/var/www/nocduncan/prod_wsgi.py'.
[Wed Oct 18 20:57:08.020480 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061] Traceback (most recent call last):
[Wed Oct 18 20:57:08.020709 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "/var/www/nocduncan/prod_wsgi.py", line 27, in <module>
[Wed Oct 18 20:57:08.020741 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]     application = get_wsgi_application()
[Wed Oct 18 20:57:08.020765 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "/usr/local/lib/python3.5/dist-packages/django/core/wsgi.py", line 13, in get_wsgi_application
[Wed Oct 18 20:57:08.020866 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]     django.setup(set_prefix=False)
[Wed Oct 18 20:57:08.020957 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "/usr/local/lib/python3.5/dist-packages/django/__init__.py", line 22, in setup
[Wed Oct 18 20:57:08.021004 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Wed Oct 18 20:57:08.021032 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 56, in __getattr__
[Wed Oct 18 20:57:08.021046 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]     self._setup(name)
[Wed Oct 18 20:57:08.021064 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 41, in _setup
[Wed Oct 18 20:57:08.021073 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]     self._wrapped = Settings(settings_module)
[Wed Oct 18 20:57:08.021089 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "/usr/local/lib/python3.5/dist-packages/django/conf/__init__.py", line 110, in __init__
[Wed Oct 18 20:57:08.021098 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]     mod = importlib.import_module(self.SETTINGS_MODULE)
[Wed Oct 18 20:57:08.021113 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
[Wed Oct 18 20:57:08.021122 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]     return _bootstrap._gcd_import(name[level:], package, level)
[Wed Oct 18 20:57:08.021137 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Wed Oct 18 20:57:08.021194 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Wed Oct 18 20:57:08.021247 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "<frozen importlib._bootstrap>", line 944, in _find_and_load_unlocked
[Wed Oct 18 20:57:08.021304 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
[Wed Oct 18 20:57:08.021335 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "<frozen importlib._bootstrap>", line 986, in _gcd_import
[Wed Oct 18 20:57:08.021355 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "<frozen importlib._bootstrap>", line 969, in _find_and_load
[Wed Oct 18 20:57:08.021373 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061]   File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
[Wed Oct 18 20:57:08.021413 2017] [wsgi:error] [pid 21696:tid 140276717238016] [remote 10.2.1.127:59061] ImportError: No module named 'website.settings'

从该日志看来,它使用了错误的 python 环境变量(它使用系统 python3.5 而不是虚拟环境 python3.5)所以我认为虚拟环境没有被正确执行。

【问题讨论】:

Apache 错误日志中的实际错误是什么?另请阅读modwsgi.readthedocs.io/en/develop/user-guides/…,了解如何根据最佳实践配置要使用的虚拟环境。 这也可能不起作用:Alias /static/ "/var/www/nocduncan/website/static"。使用Alias /static/ "/var/www/nocduncan/website/static/"。斜线必须平衡。 您要使用的设置文件的完整路径是什么? /var/www/nocduncan/website/settings/prod.py 那么 /var/www/nocduncan 应该在您添加到 Python 模块搜索路径的开头附近,而不是结尾。很可能正在使用其他名为 website 的东西,特别是因为在 Python 3.5 中,您不需要在包目录中使用 __init__.py。更改该文档中关于虚拟环境的方法,用于指定虚拟环境的位置,并使用WSGIDaemonProcesspython-path 选项指定其他模块搜索位置。不要在 WSGI 脚本文件中这样做。 【参考方案1】:

您很可能应该将 python 路径添加到您的 Apache 配置中,例如:

<VirtualHost *:80>
    #Demon process for multiple virtual hosts
    WSGIDaemonProcess noc.biffduncan.com threads=5 python-path=/var/www/nocduncan/website:/home/biffduncan/virtualenvs/nocduncanenv/lib/python3.5/site-packages
</VirtualHost>

其中第一个路径是项目本身的路径,第二个路径是在您的虚拟环境中打包的站点的路径。

编辑 1 为 @GrahamDumpleton 指出你不应该再那样做了。请改用 python-home:

<VirtualHost *:80>
    #Demon process for multiple virtual hosts
    WSGIDaemonProcess noc.biffduncan.com threads=5 python-home=/home/biffduncan/virtualenvs/nocduncanenv
</VirtualHost>

【讨论】:

您不应将site-packages 添加到python-path。您应该将python-home 用于虚拟环境的根目录。见modwsgi.readthedocs.io/en/develop/user-guides/… @GrahamDumpleton 感谢您的链接。我去看看 @GrahamDumpleton 再次感谢您。阅读后,我实际上已经解决了我在服务器上遇到的问题。基本上使用以前的方法,我使用全局安装的包而不是虚拟环境。切换到“python-home”后,我清理了全局包并最终切换到虚拟环境 => 服务器上有更多空间,apache 日志中的错误更少! @AlexanderTyapkov Grahams 的评论起到了作用,但我将此添加到我的 apache2 conf 中,谢谢。【参考方案2】:

离开格雷厄姆的评论:

改变了这个:

# Add the app's directory to the PYTHONPATH
paths = [
    '/home/biffduncan/opt/NOCduncan',
    '/home/biffduncan/opt/NOCduncan/website/',
    '/var/www/nocduncan/website',
    '/var/www/nocduncan',
]

到这里:

# Add the app's directory to the PYTHONPATH
paths = [
    '/var/www/nocduncan/website',
    '/var/www/nocduncan',
]

【讨论】:

以上是关于使用 mod_wsgi 在 Ubuntu 16.04 apache2 上部署 Django 应用程序的主要内容,如果未能解决你的问题,请参考以下文章

如何在Ubuntu中将mod_wsgi安装到特定的python版本?

如何在 Ubuntu + Apache2.4 + mod_wsgi 上为 Ansible 配置 ARA?

如何在 Ubuntu 上将 mod_wsgi 安装到 xampp 服务器?遇到 libtool 错误

Ubuntu安装Apache + mod_wsgi + Trac + Python 2.7

一次安装win10 ubuntu16.0经过记录

Apache+Django++mod_wsgi(ubuntu下虚拟机方式部署过程)