Django 2.0/Apache2和WSGI之间的问题 [英] Issue between Django 2.0 / Apache2 and WSGI
问题描述
我正在尝试在服务器中部署Django项目,但是遇到一些问题.
I'm trying to deploy my Django project in my server but I'm encountering some issues.
- Ubuntu 16.04服务器
- Django 2.0
- Python 3.5
- Apache2 2.4
- WSGI
我的Django项目位于:/var/www/html/DatasystemsCORE
My Django project is located to : /var/www/html/DatasystemsCORE
我有 wsgi.py 文件(/var/www/html/DatasystemsCORE/DatasystemsCORE
),看起来像是:
I have wsgi.py file (/var/www/html/DatasystemsCORE/DatasystemsCORE
) which looks like :
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 文件中,我有:
Apache2 configuration :
In my apache2.conf file, I have :
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
To my mind, all parameters seem to be right, but when I write : 172.30.10.86:80 in my browser, I get : 500 Internal Server Error
这是 error.log 在apache2中给出的回溯:
This is the Traceback given by error.log in apache2 :
[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问题尝试了很多方法,但到目前为止都没有答案.
I tried lots of things according to multiple stackoverflow questions, but none answer up to now.
我错过了wsgi
或mod_wsgi
吗?
我最新的apache conf文件如下:
My latest apache conf file looks like :
<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>
解决方案
我的 apache2.conf 文件中的 Servername
应该引用我的server hostname
而不是我的Django项目!
SOLUTION
Servername
in my apache2.conf file should refer to my server hostname
and not my Django project !
推荐答案
HI您可以尝试以下配置:
HI can you try the below configuration:
import os
from django.core.wsgi import get_wsgi_application
import sys
sys.path.append('/var/www/html/DatasystemsCORE')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "DatasystemsCORE.settings")
application = get_wsgi_application()
Apache2 python wsgi模块
Apache2 python wsgi module
sudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3
sudo apt-get install python3-pip apache2 libapache2-mod-wsgi-py3
<VirtualHost *:80>
ServerName dev.example.com #your server name
ServerAlias dev.example.com #your server alias
DocumentRoot #your document root
WSGIProcessGroup dev.example.com
WSGIPassAuthorization On
WSGIDaemonProcess dev.example.com python-home=/home/robert/django/robertenv python-path=/var/www/html/DatasystemsCORE <Here should be your virtual env path >
WSGIScriptAlias / /var/www/html/DatasystemsCORE/DatasystemsCORE/wsgi.py
Alias /static/ /var/www/html/DatasystemsCORE/static/Theme/ #static directory
<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>
使用上述虚拟主机配置
这篇关于Django 2.0/Apache2和WSGI之间的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!