【问题标题】:Issue between Django 2.0 / Apache2 and WSGIDjango 2.0 / Apache2 和 WSGI 之间的问题
【发布时间】: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问题尝试了很多东西,但到目前为止没有答案。

我错过了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>

解决方案

Servername 在我的 apache2.conf 文件中应该引用我的 server hostname 而不是我的 Django 项目!

【问题讨论】:

  • @Alasdair 是的 libapache2-mod-wsgi-py3 已经满意了。
  • populate() isn't reentrant 不是腐烂问题。您需要查看 Apache 错误日志中的第一个错误,对应于进程重新启动后发出的第一个请求。那是真正的错误。您只会在第一个请求之后收到您为后续请求提供的错误,因此找到第一个请求的错误很重要。

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


【解决方案1】:

你可以试试下面的配置吗:

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>

使用上面的虚拟主机配置

【讨论】:

  • 这个虚拟配置应该在apache2.conf或者000-default.conf?
  • 你可以使用单独的配置文件
  • 新建 .conf 文件并使符号链接链接到启用的站点?无论如何,这个配置似乎不起作用。到现在为止我也有同样的事情
  • 创建 sites-avliable 文件夹并启用站点其创建符号链接
  • 根据您的回答没有任何变化(与我的问题类似):/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-27
  • 1970-01-01
  • 2018-03-05
  • 1970-01-01
相关资源
最近更新 更多