使用 mod_wsgi 在 Ubuntu 16.04 apache2 上部署 Django 应用程序
Deploying Django app on Ubuntu 16.04 apache2 with mod_wsgi
我正在尝试使用 mod_wdgi 将我的 Django 应用程序部署到 Ubuntu 16.04 运行ning Apache。我已经正确设置了 apache 服务器(我认为),但是当 apache 尝试 运行 我的 "prod_wgsi.py" 脚本时,我 运行 遇到了一个问题,它试图导入错误的设置模块。
我可以通过 "python -i prod_wsgi.py" 加载我的设置文件和 运行 django.setup() 来启动 python 解释器。
文件结构
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)所以我认为是虚拟环境没有被正确执行。
很可能您应该将 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>
离开格雷厄姆的评论:
更改为:
# 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_wdgi 将我的 Django 应用程序部署到 Ubuntu 16.04 运行ning Apache。我已经正确设置了 apache 服务器(我认为),但是当 apache 尝试 运行 我的 "prod_wgsi.py" 脚本时,我 运行 遇到了一个问题,它试图导入错误的设置模块。
我可以通过 "python -i prod_wsgi.py" 加载我的设置文件和 运行 django.setup() 来启动 python 解释器。
文件结构
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)所以我认为是虚拟环境没有被正确执行。
很可能您应该将 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>
离开格雷厄姆的评论:
更改为:
# 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',
]