【发布时间】:2018-10-04 13:21:33
【问题描述】:
我正在尝试在我的服务器中部署我的 Django 项目,但遇到了一些问题。
我的环境:
- Ubuntu 16.04 服务器
- Django 2.0
- Python 3.5
- Apache2 2.4
- WSGI
Django 配置:
我的 Django 项目位于:/var/www/html/DatasystemsCORE
我有 wsgi.py 文件 (/var/www/html/DatasystemsCORE/DatasystemsCORE),它看起来像:
import os, sys
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "DatasystemsCORE.settings")
application = get_wsgi_application()
我有这样的ALLOWED_HOST:
ALLOWED_HOSTS = ['localhost', '172.30.10.86', '[::1]']
Apache2 配置:
在我的 apache2.conf 文件中,我有:
WSGIScriptAlias / /var/www/html/DatasystemsCORE/DatasystemsCORE/wsgi.py
WSGIPythonPath /var/www/html/DatasystemsCORE
Alias /static/ /var/www/html/DatasystemsCORE/static/Theme/
<Directory /var/www/html/DatasystemsCORE/static/Theme/>
Require all granted
</Directory>
<Directory /var/www/html/DatasystemsCORE/DatasystemsCORE>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
我在 sites-available/000-default.conf 中有:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html/DatasystemsCORE
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
在我看来,所有参数似乎都是正确的,但是当我在浏览器中写:172.30.10.86:80 时,我得到:500 Internal Server Error
追溯
这是 error.log 在 apache2 中给出的 Traceback :
[Tue Apr 24 12:07:32.526764 2018] [wsgi:error] [pid 2611:tid 139723751765760] [client 172.30.10.73:50128] mod_wsgi (pid=2611): Target WSGI script '/var/www/html/DatasystemsCORE/DatasystemsCORE/wsgi.py' cannot be loaded as Python module.
[Tue Apr 24 12:07:32.526839 2018] [wsgi:error] [pid 2611:tid 139723751765760] [client 172.30.10.73:50128] mod_wsgi (pid=2611): Exception occurred processing WSGI script '/var/www/html/DatasystemsCORE/DatasystemsCORE/wsgi.py'.
[Tue Apr 24 12:07:32.526970 2018] [wsgi:error] [pid 2611:tid 139723751765760] [client 172.30.10.73:50128] Traceback (most recent call last):
[Tue Apr 24 12:07:32.527038 2018] [wsgi:error] [pid 2611:tid 139723751765760] [client 172.30.10.73:50128] File "/var/www/html/DatasystemsCORE/DatasystemsCORE/wsgi.py", line 14, in <module>
[Tue Apr 24 12:07:32.527042 2018] [wsgi:error] [pid 2611:tid 139723751765760] [client 172.30.10.73:50128] application = get_wsgi_application()
[Tue Apr 24 12:07:32.527048 2018] [wsgi:error] [pid 2611:tid 139723751765760] [client 172.30.10.73:50128] File "/usr/local/lib/python3.5/dist-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Tue Apr 24 12:07:32.527050 2018] [wsgi:error] [pid 2611:tid 139723751765760] [client 172.30.10.73:50128] django.setup(set_prefix=False)
[Tue Apr 24 12:07:32.527055 2018] [wsgi:error] [pid 2611:tid 139723751765760] [client 172.30.10.73:50128] File "/usr/local/lib/python3.5/dist-packages/django/__init__.py", line 24, in setup
[Tue Apr 24 12:07:32.527064 2018] [wsgi:error] [pid 2611:tid 139723751765760] [client 172.30.10.73:50128] apps.populate(settings.INSTALLED_APPS)
[Tue Apr 24 12:07:32.527069 2018] [wsgi:error] [pid 2611:tid 139723751765760] [client 172.30.10.73:50128] File "/usr/local/lib/python3.5/dist-packages/django/apps/registry.py", line 81, in populate
[Tue Apr 24 12:07:32.527072 2018] [wsgi:error] [pid 2611:tid 139723751765760] [client 172.30.10.73:50128] raise RuntimeError("populate() isn't reentrant")
[Tue Apr 24 12:07:32.527086 2018] [wsgi:error] [pid 2611:tid 139723751765760] [client 172.30.10.73:50128] RuntimeError: populate() isn't reentrant
我根据多个stackoverflow问题尝试了很多东西,但到目前为止没有答案。
我错过了wsgi 或mod_wsgi 的内容吗?
编辑
我最新的 apache conf 文件看起来像:
<VirtualHost *:80>
ServerName DatasystemsCORE
DocumentRoot /var/www/html/DatasystemsCORE/DatasystemsCORE
WSGIPassAuthorization On
WSGIScriptAlias / /var/www/html/DatasystemsCORE/DatasystemsCORE/DatasystemsCORE.wsgi
WSGIDaemonProcess DatasystemsCORE python-home=/home/valentin python-path=/var/www/html/DatasystemsCORE
Alias /static/ /var/www/html/DatasystemsCORE/static/Theme/
<Directory /var/www/html/DatasystemsCORE/static/Theme/>
Require all granted
</Directory>
<Directory /var/www/html/DatasystemsCORE/DatasystemsCORE>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
</VirtualHost>
解决方案
Servername 在我的 apache2.conf 文件中应该引用我的 server hostname 而不是我的 Django 项目!
【问题讨论】:
-
@Alasdair 是的
libapache2-mod-wsgi-py3已经满意了。 -
populate() isn't reentrant不是腐烂问题。您需要查看 Apache 错误日志中的第一个错误,对应于进程重新启动后发出的第一个请求。那是真正的错误。您只会在第一个请求之后收到您为后续请求提供的错误,因此找到第一个请求的错误很重要。
标签: python django mod-wsgi wsgi django-wsgi