[英]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.