Django 2.0 / Apache2和WSGI之间的问题

时间:2018-04-24 10:10:51

标签: python django mod-wsgi wsgi django-wsgi

我正在尝试在我的服务器中部署我的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

回溯

这是apache2中 error.log 提供的回溯:

[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问题尝试了很多东西,但到目前为止都没有回答。

我是否遗漏了wsgimod_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>

我的 apache2.conf 文件中的

Servername应该引用我的server hostname而不是我的Django项目!

1 个答案:

答案 0 :(得分:2)

您可以尝试以下配置:

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

  

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>

使用上面的虚拟主机配置