You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS Ubuntu服务器部署Django遇ModuleNotFoundError及500错误求助

Django部署问题:ModuleNotFoundError与500内部服务器错误

在AWS Ubuntu实例上搭建Django Web服务器时,遭遇ModuleNotFoundError: No module named 'tutorial'错误,同时触发500内部服务器错误,以下是关键文件及目录信息:

error.log

GNU nano 6.2                                                        /home/ubuntu/django-project/site/logs/error.log                                                                  
[Mon Jan 16 19:35:18.750368 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330] mod_wsgi (pid=10330): Failed to exec Python script file '/home/ubuntu/djang>
[Mon Jan 16 19:35:18.750441 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330] mod_wsgi (pid=10330): Exception occurred processing WSGI script '/home/ubun>
[Mon Jan 16 19:35:18.750979 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330] Traceback (most recent call last):
[Mon Jan 16 19:35:18.751031 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330]   File "/home/ubuntu/django-project/src/tutorial/wsgi.py", line 16, in <mod>
[Mon Jan 16 19:35:18.751037 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330]     application = get_wsgi_application()
[Mon Jan 16 19:35:18.751043 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330]   File "/home/ubuntu/django-project/venv/lib/python3.10/site-packages/djang>
[Mon Jan 16 19:35:18.751048 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330]     django.setup(set_prefix=False)
[Mon Jan 16 19:35:18.751053 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330]   File "/home/ubuntu/django-project/venv/lib/python3.10/site-packages/djang>
[Mon Jan 16 19:35:18.751058 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Mon Jan 16 19:35:18.751068 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330]   File "/home/ubuntu/django-project/venv/lib/python3.10/site-packages/djang>
[Mon Jan 16 19:35:18.751073 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330]     self._setup(name)
[Mon Jan 16 19:35:18.751078 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330]   File "/home/ubuntu/django-project/venv/lib/python3.10/site-packages/djang>
[Mon Jan 16 19:35:18.751083 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330]     self._wrapped = Settings(settings_module)
[Mon Jan 16 19:35:18.751088 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330]   File "/home/ubuntu/django-project/venv/lib/python3.10/site-packages/djang>
[Mon Jan 16 19:35:18.751092 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330]     mod = importlib.import_module(self.SETTINGS_MODULE)
[Mon Jan 16 19:35:18.751097 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330]   File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_mod>
[Mon Jan 16 19:35:18.751102 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330]     return _bootstrap._gcd_import(name[level:], package, level)
[Mon Jan 16 19:35:18.751107 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330]   File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
[Mon Jan 16 19:35:18.751112 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330]   File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
[Mon Jan 16 19:35:18.751117 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330]   File "<frozen importlib._bootstrap>", line 992, in _find_and_load_unlocked
[Mon Jan 16 19:35:18.751122 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330]   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_remo>
[Mon Jan 16 19:35:18.751128 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330]   File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
[Mon Jan 16 19:35:18.751133 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330]   File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
[Mon Jan 16 19:35:18.751146 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330]   File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlock>
[Mon Jan 16 19:35:18.751161 2023] [wsgi:error] [pid 10330:tid 139960159290944] [remote 136.41.64.54:40330] ModuleNotFoundError: No module named 'tutorial'

wsgi.py

"""
WSGI config for tutorial 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/3.2/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()

default000.conf

<VirtualHost *:80>

        ErrorLog /home/ubuntu/django-project/site/logs/error.log
        CustomLog /home/ubuntu/django-project/site/logs/access.log combine

        <Directory /django-project/src/tutorial>

                Require all granted

        </Directory>


        WSGIDaemonProcess tutorial python-home=/home/ubuntu/django-project/venv python-path=/home/ubuntu/django-project/src/tutorial/
        WSGIProcessGroup tutorial
        WSGIScriptAlias / /home/ubuntu/django-project/src/tutorial/wsgi.py

</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

项目目录结构

── site
│   ├── logs
│   │   ├── access.log
│   │   └── error.log
│   └── public
│       ├── media
│       └── static
├── src
│   ├── db.sqlite3
│   ├── manage.py
│   └── tutorial
│       ├── __init__.py
│       ├── __pycache__
│       │   ├── __init__.cpython-310.pyc
│       │   ├── settings.cpython-310.pyc
│       │   ├── urls.cpython-310.pyc
│       │   └── wsgi.cpython-310.pyc
│       ├── asgi.py
│       ├── settings.py
│       ├── urls.py
│       └── wsgi.py
└── venv
    ├── bin
    │   ├── __pycache__

问题根源与修复步骤

核心问题

  1. Apache配置中Directory路径缺少根目录前缀,导致权限配置无效
  2. WSGIDaemonProcess的python-path指向了tutorial子目录,Python无法将tutorial识别为可导入的顶级模块(正确路径应为src目录,tutorial是src下的子模块)

修复操作

  1. 修改default000.conf配置:
    • 修正Directory路径为完整绝对路径:
      <Directory /home/ubuntu/django-project/src/tutorial>
          Require all granted
      </Directory>
      
    • 修正python-path指向src目录:
      WSGIDaemonProcess tutorial python-home=/home/ubuntu/django-project/venv python-path=/home/ubuntu/django-project/src/
      
  2. 重启Apache服务生效配置:
    sudo systemctl restart apache2
    
  3. 验证项目状态:
    激活虚拟环境后运行项目检查命令,确认无配置错误:
    source /home/ubuntu/django-project/venv/bin/activate
    cd /home/ubuntu/django-project/src
    python manage.py check
    

MySQL与Django关联配置

  1. 安装MySQL驱动依赖:
    sudo apt-get install python3-dev default-libmysqlclient-dev build-essential
    source /home/ubuntu/django-project/venv/bin/activate
    pip install mysqlclient
    
  2. 修改settings.py中的数据库配置:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': '你的数据库名称',
            'USER': '数据库用户名',
            'PASSWORD': '数据库密码',
            'HOST': 'localhost',
            'PORT': '3306',
        }
    }
    
  3. 执行数据库迁移:
    python manage.py makemigrations
    python manage.py migrate
    

内容的提问来源于stack exchange,提问作者Braeden

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 07:10:47