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

将Django项目部署至Apache服务器时出现内部服务器错误

Django部署至Apache2出现Internal Server Error排查方案

问题背景

将Django项目部署到Apache2服务器,先解决了403访问错误,随后出现Internal Server Error。

Apache主错误日志(/var/log/apache2/error.log)

[Sat Sep 07 22:05:50.292291 2024] [core:warn] [pid 74852:tid 129319697659776] AH00045: child process 74853 still did not exit, sending a SIGTERM
[Sat Sep 07 22:05:50.292335 2024] [core:warn] [pid 74852:tid 129319697659776] AH00045: child process 74854 still did not exit, sending a SIGTERM
[Sat Sep 07 22:05:52.294434 2024] [core:error] [pid 74852:tid 129319697659776] AH00046: child process 74853 still did not exit, sending a SIGKILL
[Sat Sep 07 22:05:52.294476 2024] [core:error] [pid 74852:tid 129319697659776] AH00046: child process 74854 still did not exit, sending a SIGKILL
[Sat Sep 07 22:05:53.295592 2024] [mpm_event:notice] [pid 74852:tid 129319697659776] AH00491: caught SIGTERM, shutting down
[Sat Sep 07 22:08:55.562107 2024] [mpm_event:notice] [pid 76122:tid 129486358599552] AH00489: Apache/2.4.52 (Ubuntu) mod_wsgi/4.9.0 Python/3.10 configured -- resuming normal operations
[Sat Sep 07 22:08:55.562464 2024] [core:notice] [pid 76122:tid 129486358599552] AH00094: Command line: '/usr/sbin/apache2'

Apache站点配置(/etc/apache2/sites-available/mysite.conf)

<VirtualHost *:80>
    ServerName myip

    WSGIScriptAlias / /my/directory/my_project/wsgi.py

    <Directory /my/directory/my_project>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    Alias /static /my/directory/my_project
    <Directory /my/directory/my_project>
        Require all granted
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/mysite_error.log
    CustomLog ${APACHE_LOG_DIR}/mysite_access.log combined
</VirtualHost>

已尝试操作

  • 执行sudo chmod -R 755 /my/directory/my_project调整目录权限
  • 执行sudo chown -R www-data:www-data /my/directory/my_project修改目录所属用户组

排查与解决步骤

1. 查看项目专属错误日志

主Apache日志仅记录服务器启停信息,必须查看${APACHE_LOG_DIR}/mysite_error.log,这里会包含Django运行时的具体报错(如模块缺失、配置错误、数据库连接失败等),这是定位问题最直接的方式。

2. 配置mod_wsgi使用虚拟环境

如果项目使用了虚拟环境,当前配置未指定虚拟环境路径,mod_wsgi会使用系统全局Python,导致依赖缺失。需在WSGIScriptAlias前添加以下配置:

# 替换为你的虚拟环境路径和项目根目录
WSGIDaemonProcess mysite python-home=/path/to/your/venv python-path=/my/directory/my_project
WSGIProcessGroup mysite

3. 修正静态文件Alias配置

当前静态文件Alias指向项目根目录,会导致静态文件访问错误,进而触发500。需修改为:

Alias /static /my/directory/my_project/static
<Directory /my/directory/my_project/static>
    Require all granted
</Directory>

同时确保已执行python manage.py collectstatic收集静态文件到指定目录。

4. 验证wsgi.py配置

检查/my/directory/my_project/wsgi.py中的以下内容是否正确:

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

确保my_project.settings是正确的配置模块路径,且www-data用户能读取该文件及上级目录。

5. 测试Django项目本身

在服务器上执行:

cd /my/directory/my_project
# 激活虚拟环境(如果使用)
source /path/to/venv/bin/activate
python manage.py runserver 0.0.0.0:8000

尝试访问服务器IP:8000,确认项目本身能否正常启动,排除代码、依赖或数据库配置问题。


内容的提问来源于stack exchange,提问作者Михаил

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 15:32:11