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

     2023-02-24     223

关键词:

【中文标题】使用 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',
]

【讨论】:

试图让 django 应用程序在 CentOS 5 上使用 mod_wsgi

】试图让django应用程序在CentOS5上使用mod_wsgi【英文标题】:Tryingtogetdjangoapptoworkwithmod_wsgionCentOS5【发布时间】:2011-01-2623:12:25【问题描述】:我正在运行CentOS5,并试图让django应用程序与mod_wsgi一起工作。我正在使用在Ubuntu上工作... 查看详情

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

】如何在Ubuntu+Apache2.4+mod_wsgi上为Ansible配置ARA?【英文标题】:HowtoconfigureARAforAnsibleonUbuntu+Apache2.4+mod_wsgi?【发布时间】:2018-08-2002:17:57【问题描述】:我正在尝试在Ubuntu16.04上安装ARA(https://github.com/openstack/ara)以监控我的Ansibleplayb... 查看详情

使用 apache2 在 mod_wsgi 上运行 django python 3.4

】使用apache2在mod_wsgi上运行djangopython3.4【英文标题】:runningdjangopython3.4onmod_wsgiwithapache2【发布时间】:2015-03-1703:28:09【问题描述】:您好,我在ubuntu服务器14.10上使用mod_wsgi和python在django上运行apache2时收到以下错误。我的django应... 查看详情

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

】如何在Ubuntu上将mod_wsgi安装到xampp服务器?遇到libtool错误【英文标题】:HowdoIinstallmod_wsgitoxamppserveronUbuntu?Runningintolibtoolerrors【发布时间】:2014-01-2211:23:53【问题描述】:我正在尝试将Python和mod_wsgi添加到XAMPP/LAMPP服务器。我正... 查看详情

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

我正在尝试在我的Ubuntu服务器中的apache中安装mod_wsgi模块,但我需要它专门用于Python中的2.7.13版本。无论出于何种原因我每次运行sudoapt-getinstalllibapache2-mod-wsgi它都会安装用于Python2.7.12的mod_wsgi模块。我正在做所有这些因为我遇到... 查看详情

使用make安装时如何卸载mod_wsgi?

】使用make安装时如何卸载mod_wsgi?【英文标题】:Howtouninstallmod_wsgiwheninstalledwithmake?【发布时间】:2016-11-1903:02:23【问题描述】:我已经在Ubuntu14.04上安装了mod_wsgi,如他们的documentation中所述。虽然我没有从Apache收到任何错误或... 查看详情

使用 mod_wsgi 在 apache 上设置 Django

】使用mod_wsgi在apache上设置Django【英文标题】:SettingupDjangoonapachewithmod_wsgi【发布时间】:2014-05-1114:56:12【问题描述】:尝试使用mod_wsgi在CentOS6上的apache上设置Django,但我不确定我的设置有什么问题。我尝试了很多不同的设置指... 查看详情

如何在 Apache 和 mod_wsgi 中使用 Flask 路由?

】如何在Apache和mod_wsgi中使用Flask路由?【英文标题】:HowdoIuseFlaskrouteswithApacheandmod_wsgi?【发布时间】:2012-03-2914:53:00【问题描述】:我已经设置好我的Apache服务器,它正在通过mod_wsgi处理Flask响应。我已经通过别名注册了WSGI脚本... 查看详情

使用 mod_wsgi 在 Django 中配置 Celery

】使用mod_wsgi在Django中配置Celery【英文标题】:ConfiguringCelerywithDjangousingmod_wsgi【发布时间】:2015-04-3014:10:51【问题描述】:我正在使用Django1.6和Celery3.1(所以不使用django-celery)。我的WSGI文件如下所示:importosimportsyspath=\'/code_base... 查看详情

mod_wsgi、mod_python,还是只是 cgi?

】mod_wsgi、mod_python,还是只是cgi?【英文标题】:mod_wsgi,mod_python,orjustcgi?【发布时间】:2011-03-2004:08:07【问题描述】:我一直在玩我自己的网络服务器(Apache+Ubuntu)和python。据我所见,有3(?)种主要方法:Apache配置为将.py作为cgi... 查看详情

Apache 2 + mod_wsgi + WSGIScriptAlias

】Apache2+mod_wsgi+WSGIScriptAlias【英文标题】:【发布时间】:2012-02-2722:15:43【问题描述】:我目前正在研究Python和Django是否适合我将要从事的项目(目前看起来不错)。作为一种测试手段,我想让python在实际服务器(ubuntu上的apache2... 查看详情

使用 Apache 和 mod_wsgi 在 Centos 上设置 Django

】使用Apache和mod_wsgi在Centos上设置Django【英文标题】:SetupDjangoonCentoswithApacheandmod_wsgi【发布时间】:2014-03-0214:23:51【问题描述】:我很难在centos上设置mod_wsgi,我不断收到以下错误:403Forbidden您无权访问此服务器上的/。-----服务... 查看详情

mod_wsgi 错误:ModuleNotFoundError:没有名为“django”的模块

】mod_wsgi错误:ModuleNotFoundError:没有名为“django”的模块【英文标题】:mod_wsgierror:ModuleNotFoundError:Nomodulenamed\'django\'【发布时间】:2019-11-1303:40:42【问题描述】:我正在尝试在Ubuntu18.04上使用Apache2.4部署一个非常基本的Django应用... 查看详情

在 linux 中使用 apache 和 mod_wsgi 配置 Python flask 应用程序

】在linux中使用apache和mod_wsgi配置Pythonflask应用程序【英文标题】:ConfigurePythonflaskapplicationwithapacheandmod_wsgiinlinux【发布时间】:2016-08-1211:52:49【问题描述】:我在应用程序帐户下有一个linuxapache2.4.12和mod_wsgi4.5.2(mod_wsgi.so安装到ap... 查看详情

烧瓶 mod_wsgi 地址已在使用中 [重复]

】烧瓶mod_wsgi地址已在使用中[重复]【英文标题】:flaskmod_wsgiaddressalreadyinuse[duplicate]【发布时间】:2017-06-2721:16:15【问题描述】:我从过去2天开始尝试了所有方法。但没有什么能真正帮到我。背景:我按照this在生产环境中安装... 查看详情

在 Windows 64 位上使用 python 2.7 安装 mod_wsgi

】在Windows64位上使用python2.7安装mod_wsgi【英文标题】:Installingmod_wsgiwithpython2.7onWindows64bit【发布时间】:2016-08-0705:37:52【问题描述】:我正在尝试使用apache和mod_wsgi来部署我在windows764位机器上编写的Django应用程序。我使用的是pyth... 查看详情

使用 mod_wsgi 在 Apache 上部署多个 django 应用程序

】使用mod_wsgi在Apache上部署多个django应用程序【英文标题】:DeployingmultipledjangoappsonApachewithmod_wsgi【发布时间】:2012-07-1509:00:45【问题描述】:我想在同一主机上部署两个不同的django应用程序:第一个对应url/site1,第二个对应url/s... 查看详情

如何仅在一个进程中使用 mod_wsgi 和 django 运行 Apache?

】如何仅在一个进程中使用mod_wsgi和django运行Apache?【英文标题】:howtorunApachewithmod_wsgianddjangoinoneprocessonly?【发布时间】:2015-04-0318:45:43【问题描述】:我正在运行apache,并在2个不同的进程中启用了django和mod_wsgi。我读到第二个... 查看详情