使用mod_wsgi

时间:2017-10-18 21:09:47

标签: django apache python-3.x mod-wsgi django-settings

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

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

文件结构

NOCduncan

   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)所以我认为虚拟环境没有正确执行。

2 个答案:

答案 0 :(得分:1)

最有可能你应该在你的Apache配置中添加python路径,如:

<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>

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

编辑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>

答案 1 :(得分:0)

离开格雷厄姆的评论:

改变了这个:

# 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',
]