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

Amazon Linux EC2上Apache+WSGI部署Django的版本冲突问题

在Amazon Linux EC2上用Apache部署Django时的Python版本冲突问题

我在Amazon Linux EC2上尝试用Apache部署Django网站,目前遇到了棘手的问题:

我已按相关指导配置Apache,并针对Python3.4(使用位于/home/ec2-user/web-dev的virtualenv)编译了mod_wsgi,但加载Django测试应用时出现Internal Server Error。

错误日志详情

查看Apache错误日志,发现处理过程中调用了多个Python版本(路径从/home/ec2-user/web-dev/lib64/python3.4跳转到/usr/lib64/python3.7),且提示无法导入mod_wsgi相关模块,但在EC2终端的Python提示符中执行import mod_wsgi可正常运行。具体日志如下:

[Thu Jan 19 15:19:27.329376 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582] mod_wsgi (pid=9955): Failed to exec Python script file '/var/www/test/test_site/test_site/wsgi.py'.
[Thu Jan 19 15:19:27.329445 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582] mod_wsgi (pid=9955): Exception occurred processing WSGI script '/var/www/test/test_site/test_site/wsgi.py'.
[Thu Jan 19 15:19:27.330049 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582] Traceback (most recent call last):
[Thu Jan 19 15:19:27.330088 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582]   File "/var/www/test/test_site/test_site/wsgi.py", line 25, in <module>
[Thu Jan 19 15:19:27.330093 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582]     application = get_wsgi_application()
[Thu Jan 19 15:19:27.330108 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582]   File "/home/ec2-user/web-dev/lib64/python3.4/site-packages/django/core/wsgi.py", line 12, in get_wsgi_application
[Thu Jan 19 15:19:27.330112 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582]     django.setup(set_prefix=False)
[Thu Jan 19 15:19:27.330117 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582]   File "/home/ec2-user/web-dev/lib64/python3.4/site-packages/django/__init__.py", line 24, in setup
[Thu Jan 19 15:19:27.330121 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582]     apps.populate(settings.INSTALLED_APPS)
[Thu Jan 19 15:19:27.330126 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582]   File "/home/ec2-user/web-dev/lib64/python3.4/site-packages/django/apps/registry.py", line 89, in populate
[Thu Jan 19 15:19:27.330129 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582]     app_config = AppConfig.create(entry)
[Thu Jan 19 15:19:27.330134 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582]   File "/home/ec2-user/web-dev/lib64/python3.4/site-packages/django/apps/config.py", line 123, in create
[Thu Jan 19 15:19:27.330137 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582]     import_module(entry)
[Thu Jan 19 15:19:27.330142 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582]   File "/usr/lib64/python3.7/importlib/__init__.py", line 127, in import_module
[Thu Jan 19 15:19:27.330146 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582]     return _bootstrap._gcd_import(name[level:], package, level)
[Thu Jan 19 15:19:27.330151 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582]   File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
[Thu Jan 19 15:19:27.330156 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582]   File "<frozen importlib._bootstrap>", line 983, in _find_and_load
[Thu Jan 19 15:19:27.330161 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582]   File "<frozen importlib._bootstrap>", line 962, in _find_and_load_unlocked
[Thu Jan 19 15:19:27.330176 2023] [wsgi:error] [pid 9955] [remote 81.109.251.90:61582] ModuleNotFoundError: No module named 'mod_wsgi.server'; 'mod_wsgi' is not a package

调试过程与结果

为了查看实际调用的Python版本,我修改了wsgi.py,注释掉调用Django的get_wsgi_application()的代码,替换为一个自定义的application函数,代码如下:

# In test_site/test_site/wsgi.py:
# ...

python_home = '/home/ec2-user/web-dev'

activator = python_home + '/bin/activate_this.py'
with open(activator) as f:
    exec(f.read(), {'__file__': activator})

import os
import sys

from django.core.wsgi import get_wsgi_application


sys.path.append('/var/www/test/test_site')
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'test_site.settings')

#application = get_wsgi_application()

def application(environ, start_response):
    status = '200 OK'
    msg = f"sys.path: {sys.path}    |   sys.prefix: {sys.prefix}     |   sys.executable: {sys.executable}   |  Python version {sys.version}"
    output = str.encode(msg)
    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)

    return [output]

修改后错误消失,浏览器输出如下:

sys.path: ['/home/ec2-user/web-dev/lib64/python3.4/site-packages', '/home/ec2-user/web-dev/lib/python3.4/site-packages', '/var/www/test/test_site/test_site/wsgi.py', '/usr/lib64/python37.zip', '/usr/lib64/python3.7', '/usr/lib64/python3.7/lib-dynload', '/var/www/test/test_site', '/var/www/test/test_site', '/var/www/test/test_site']    |   sys.prefix: /home/ec2-user/web-dev     |   sys.executable: /home/ec2-user/web-dev/bin/python   |  Python version 3.7.15 (default, Oct 31 2022, 22:44:31) 
[GCC 7.3.1 20180712 (Red Hat 7.3.1-15)]

可以看到sys.path中包含了Python3.7的目录,实际运行的是Python3.7而非预期的3.4,但mod_wsgi本身能正常工作。

当前Apache配置(httpd.conf)

我在httpd.conf中添加的配置内容如下:

WSGIScriptAlias / /var/www/test/test_site/test_site/wsgi.py
WSGIPythonPath /var/www/test:/home/ec2-user/web-dev/lib/python3.4/site-packages:/home/ec2-user/web-dev/lib64/python3.4/site-packages

<Directory /var/www/test/test_site/test_site>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>

我该如何让这个Django应用正常加载?这只是部署的第一步,后续还要上线自己的网站并配置数据库,本来预计几小时完成,结果已经耗了几天时间!


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 19:10:19