繁体   English   中英

django mod_wsgi 和 apache:找不到模块错误

[英]django mod_wsgi and apache: module not found error

我正在尝试使用 apache2 和 mod_wsgi 将我的 django 项目投入生产。 我在 apache 日志中不断收到 wsgi_error “ModuleNotFoundError”。

我在 ubuntu 22.04 上运行。

在浏览了 web 上的各种帖子后,我仍然一无所获,所以我现在通过创建一个新的示例 django 应用程序从头开始。 发生同样的错误。

PS,当我使用“runserver”时,django 应用程序在浏览器中显示正常。 还有一个简单的 wsgi“hello_world”应用程序在 apache 中运行良好。 所以我认为基本设置很好。

我用 django-admin startproject mytest 创建了一个 django 测试应用程序“mytest”

项目在/home/user/myprojects/mytest中创建,所有目录的组是www-data

在 mytest 我有通常的 django 结构

home/user/myprojects/mytest
    manage.py
    mytest
      asgi.py
      __init__.py
      settings.py
      urls.py
      wsgi.py

wsgi.py是:(django/python运行在虚拟环境中发现需要在wsgi中添加代码才能真正激活虚拟环境)

"""
WSGI config for mytest project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/4.1/howto/deployment/wsgi/
"""

python_home='/home/user/.virtualenvs/myproject'
activate_this= python_home + '/bin/activate_this.py'
with open(activate_this)as f:
    code=compile(f.read(),activate_this,'exec')
    exec(code, dict(__file__=activate_this))

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mytest.settings')

application = get_wsgi_application()

apache 000-default.conf 是:

        WSGIDaemonProcess mytest
        WSGIProcessGroup mytest
        WSGIScriptAlias /mytest /home/user/myprojects/mytest/mytest/wsgi.py
        <Directory /home/user/myprojects/mytest/mytest>
                Require all granted
        </Directory>

使用此设置,如果我运行“python manage.py runserver my_ip_addr”(my_ip_addr 添加到设置文件中允许的主机),我可以浏览到 my_ip_addr 并显示“安装成功”

运行标准 web (apache) 显示:ModuleNotFoundError: No module named 'mytest'

如果我取出 django 和 get_wsgi_aplication 部分并添加代码以显示 Hello_World,例如:

import os
def application(environ, start_response):
    status = '200 OK'
    output = b'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

然后确实显示了 hello_world

但是,我找不到 wsgi mytest 应用程序不运行的原因

它应该是:

WSGIScriptAlias / /home/user/myprojects/mytest/mytest/wsgi.py

代替:

WSGIScriptAlias /mytest /home/user/myprojects/mytest/mytest/wsgi.py

由于这篇文章,我想我终于解决了:

Django+Apache ModuleNotFoundError:没有名为“myproject”的模块

我必须将项目文件夹路径添加到 wsgi.py,如下所示:

sys.path.append('home/user/project/mytest')
sys.path.append('home/user/project/mytest/mytest')

django 服务页面的链接是:http://ip_addr/mytest/mytest

@NixonSparrow 关于 WSGIScriptAlias 的评论也很有意义:将其更改为WSGIScriptAlias / /home/user/project/mytest/mytest/wsgi.py然后将浏览器链接更改为 http://ip_addr/mytest

有效的设置是:

Apache配置:

WSGIScriptAlias / /home/user/project/mytest/mytest/wsgi.py
<Directory /home/user/project/mytest/mytest>
        Require all granted
</Directory>

wsgi.py:

python_home='/home/user/.virtualenvs/project'
activate_this= python_home + '/bin/activate_this.py'
with open(activate_this)as f:
    code=compile(f.read(),activate_this,'exec')
    exec(code, dict(__file__=activate_this))

import os
import sys
sys.path.append('home/user/project/mytest')
sys.path.append('home/user/project/mytest/mytest')

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mytest.settings')

application = get_wsgi_application()

settings.py,installed_aps:

INSTALLED_APPS = [
    'mytest',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

网址.py:

urlpatterns = [
    path('mytest/',views.index,name='index'),
    path('admin/', admin.site.urls),
]

视图.py:

from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
    def index(request):
        return HttpResponse("<h1>MyTest Hello World ....</h1>")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM