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

Apache2部署Django报错cannot import name 'zoneinfo' 排查求助

问题背景
  • 部署环境:Apache2 + Python 3.8.10 + Django,已安装mod_wsgi模块,项目在ip:8000开发服务器运行状态正常
  • 故障现象:完成部署配置后,不通过8000端口直接访问服务时报错,核心错误信息如下:
[Thu Jul 07 10:18:36.178228 2022] [wsgi:error] [pid 368483:tid 139647379601152] [remote 106.79.194.125:58245]   File "/usr/lib/python3.8/importlib/__init__.py", line 127, in import_module
[Thu Jul 07 10:18:36.178240 2022] [wsgi:error] [pid 368483:tid 139647379601152] [remote 106.79.194.125:58245]     return _bootstrap._gcd_import(name[level:], package, level)
[Thu Jul 07 10:18:36.178247 2022] [wsgi:error] [pid 368483:tid 139647379601152] [remote 106.79.194.125:58245]   File "/root/novo-ai-api-main/backend/dj/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py", line 20, in <module>
[Thu Jul 07 10:18:36.178253 2022] [wsgi:error] [pid 368483:tid 139647379601152] [remote 106.79.194.125:58245]     from django.db.backends.base.base import BaseDatabaseWrapper, timezone_constructor
[Thu Jul 07 10:18:36.178260 2022] [wsgi:error] [pid 368483:tid 139647379601152] [remote 106.79.194.125:58245]   File "/root/novo-ai-api-main/backend/dj/lib/python3.8/site-packages/django/db/backends/base/base.py", line 12, in <module>
[Thu Jul 07 10:18:36.178277 2022] [wsgi:error] [pid 368483:tid 139647379601152] [remote 106.79.194.125:58245]     from backports import zoneinfo
[Thu Jul 07 10:18:36.178308 2022] [wsgi:error] [pid 368483:tid 139647379601152] [remote 106.79.194.125:58245] ImportError: cannot import name 'zoneinfo' from 'backports' (unknown location)
  • 已完成操作:配置了相关文件访问权限,现有配置文件、权限信息如下:

现有配置信息

000-default.conf(Apache站点配置)

<VirtualHost *:80>
        #ServerAdmin webmaster@localhost
        DocumentRoot /root/novo-ai-api-main


        ErrorLog /root/novo-ai-api-main/error.log
        CustomLog /root/novo-ai-api-main/access.log combine


        <Directory /root/novo-ai-api-main/backend/server/server>
                <Files wsgi.py>
                         Require all granted
                </Files>
        </Directory>
        Alias /static /root/novo-ai-api-main/static
        <Directory /root/novo-ai-api-main/static>
                Require all granted
        </Directory>


        WSGIScriptAlias / /root/novo-ai-api-main/backend/server/server/wsgi.py
        WSGIDaemonProcess django_app python-path=/root/novo-ai-api-main/backend/server  python-home=/root/novo-ai-api-main/backend/dj/
        WSGIProcessGroup django_app

</VirtualHost>

apache2.conf(Apache全局配置)

ServerRoot "/etc/apache2"
Timeout 300
MaxKeepAliveRequests 100

#KeepAliveTimeout: Number of seconds to wait for the next request from the
#same client on the same connection.
KeepAliveTimeout 5

#These need to be set in /etc/apache2/envvars
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

LogLevel warn

#Sets the default security model of the Apache2 HTTPD server. It does

<Directory />
        Options FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

<Directory /var/www/>
        AllowOverride All
        Require all granted
</Directory>

<Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>


#AccessFileName .htaccess

#<FilesMatch "^\.ht">
#Require all denied
#</FilesMatch>


IncludeOptional conf-enabled/*.conf


IncludeOptional sites-enabled/*.conf

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

wsgi.py(Django WSGI配置)

import os, sys
import site
site.addsitedir('/root/novo-ai-api-main/backend/dj/lib/python3.8/site-packages')
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'server.settings')
application = get_wsgi_application()

目录权限信息

drwx--x--x  9 root     root       4096 Jun 16 22:05 .
drwxr-xr-x 19 root     root       4096 Jun 14 14:05 ..
drwxr-xr-x  3 root     root       4096 Jun 13 23:38 .local
drwxrwxr-x  8 root     root       4096 Jun 16 01:24 mod_wsgi-4.9.2
drwxr-xr-x  5 www-data www-data   4096 Jun 14 23:53 novo-ai-api-main <-(project)
drwxr-xr-x  5 www-data www-data     4096 Jun 14 23:14 backend
drw-r--r-- 8 www-data www-data     4096 Jun 14 00:33 static
drwxr-xr-x  6 www-data www-data 4096 Jun 14 00:16 dj <- (venv)
排查修复方案

按优先级从高到低依次操作:

  • 补装缺失依赖:Python 3.8标准库未内置zoneinfo模块,Django 4+版本在Python3.8环境运行时必须依赖backports.zoneinfo反向移植包,该报错大概率是虚拟环境中未安装该包导致。执行以下命令安装:
# 激活项目虚拟环境
source /root/novo-ai-api-main/backend/dj/bin/activate
# 安装缺失依赖
pip install backports.zoneinfo
# 重启Apache生效
systemctl restart apache2

注意:必须将包装到项目使用的dj虚拟环境中,不要装到系统全局Python环境,否则mod_wsgi调用虚拟环境时仍然找不到包。

  • 简化wsgi.py配置:现有wsgi.py中手动添加site-packages路径的写法容易和WSGIDaemonProcess指定的虚拟环境路径产生冲突,将wsgi.py改回Django默认生成的版本即可,路径加载逻辑交给mod_wsgi处理:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'server.settings')
application = get_wsgi_application()

修改完成后重启Apache测试。

  • 修复目录权限隐患:

    1. 静态目录static当前权限为drw-r--r--,缺少执行权限,Apache进程无法进入目录读取文件,后续会触发静态资源403错误,执行chmod +x /root/novo-ai-api-main/static补全权限。
    2. 项目存放在/root目录下存在权限穿透问题:/root目录默认权限为700,仅root用户可访问,即使子目录给www-data授权,www-data用户也无法遍历进入子目录,建议将整个项目迁移到/var/www/目录下,再重新给www-data用户授予项目目录的读、执行权限。
  • 修正Apache WSGI配置:现有WSGIDaemonProcess配置的python-path仅包含server目录,补充项目上层目录避免模块导入失败,将配置行修改为:

WSGIDaemonProcess django_app python-path=/root/novo-ai-api-main/backend/server:/root/novo-ai-api-main/backend python-home=/root/novo-ai-api-main/backend/dj/

修改后执行apache2ctl configtest验证配置语法无报错,再重启Apache。

  • 验证mod_wsgi版本匹配:手动编译的mod_wsgi需要和虚拟环境的Python版本完全一致,执行以下命令检查mod_wsgi绑定的Python版本:
ldd /usr/lib/apache2/modules/mod_wsgi.so | grep python

如果输出的Python版本不是虚拟环境使用的3.8.10,需要进入虚拟环境重新编译安装mod_wsgi,避免版本不匹配导致的包加载异常。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.27 18:09:36