部署Django至AWS EC2遇Gunicorn报错:找不到ckeditor模块
解决方案:Gunicorn启动报错ModuleNotFoundError: 'ckeditor'
你遇到的问题核心是Gunicorn没有使用你安装了ckeditor的虚拟环境,导致找不到模块。以下是具体解决步骤:
1. 检查并修正Supervisor配置文件
编辑你的Supervisor应用配置文件(通常位于/etc/supervisor/conf.d/[你的应用名].conf),确保command行使用虚拟环境内的Gunicorn,而不是系统默认的Gunicorn:
[program:myapp] command=/home/ubuntu/env/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/myapp/app.sock myapp.wsgi:application directory=/home/ubuntu/myapp user=ubuntu autostart=true autorestart=true stderr_logfile=/var/log/myapp.err.log stdout_logfile=/var/log/myapp.out.log
重点确认command开头的路径是虚拟环境下的Gunicorn(/home/ubuntu/env/bin/gunicorn),而不是直接写gunicorn。
2. 验证虚拟环境内的依赖安装
手动激活虚拟环境,确认django-ckeditor(注意包名是django-ckeditor而非单纯ckeditor)已正确安装:
source /home/ubuntu/env/bin/activate pip list | grep ckeditor
如果没有找到,重新安装:
pip install django-ckeditor
3. 重启Supervisor服务
更新配置并重启应用:
sudo supervisorctl reread sudo supervisorctl update sudo supervisorctl restart myapp
4. 手动测试Gunicorn启动(可选)
如果上述步骤无效,先手动在虚拟环境内启动Gunicorn,排查是否是配置问题:
source /home/ubuntu/env/bin/activate cd /home/ubuntu/myapp gunicorn --bind unix:/home/ubuntu/myapp/app.sock myapp.wsgi:application
如果手动启动成功,说明问题确实出在Supervisor配置;如果手动也报错,检查项目路径是否正确,或虚拟环境是否损坏。
5. (备选)在WSGI文件中强制激活虚拟环境
如果以上都不行,在项目的wsgi.py开头添加虚拟环境激活代码:
import os import sys # 添加项目路径到系统路径 sys.path.append('/home/ubuntu/myapp') # 激活虚拟环境 activate_this = '/home/ubuntu/env/bin/activate_this.py' with open(activate_this) as file_: exec(file_.read(), dict(__file__=activate_this)) os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myapp.settings') from django.core.wsgi import get_wsgi_application application = get_wsgi_application()
内容的提问来源于stack exchange,提问作者MortalRave
相关产品推荐
相关产品推荐

