Issue between Django 2.0 / Apache2 and WSGI

只谈情不闲聊 提交于 2019-12-25 02:28:58

问题


I'm trying to deploy my Django project in my server but I'm encountering some issues.

My environment :

  • Ubuntu 16.04 Server
  • Django 2.0
  • Python 3.5
  • Apache2 2.4
  • WSGI

Django configuration :

My Django project is located to : /var/www/html/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()

I have ALLOWED_HOST like this :

ALLOWED_HOSTS = ['localhost', '172.30.10.86', '[::1]']

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>

And I have in 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>

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

Traceback

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

I tried lots of things according to multiple stackoverflow questions, but none answer up to now.

Did I miss something with wsgi or mod_wsgi ?

Edit

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>

SOLUTION

Servername in my apache2.conf file should refer to my server hostname and not my Django project !


回答1:


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 module

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>

Use above virtual host configuration



来源:https://stackoverflow.com/questions/49998997/issue-between-django-2-0-apache2-and-wsgi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!