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

Elastic Beanstalk部署Django Channels报错:虚拟环境路径不存在

Elastic Beanstalk Amazon Linux 2: Virtual Environment Path Not Found When Deploying Django Channels

Problem Description

I'm following a tutorial to deploy a Django Channels app on AWS Elastic Beanstalk, using the following configuration files:

01_env.config

option_settings:
  aws:elasticbeanstalk:container:python:
    WSGIPath: "dashboard/wsgi.py"
  aws:elasticbeanstalk:application:environment:
    DJANGO_SETTINGS_MODULE: "dashboard.settings"
    PYTHONPATH: /opt/python/current/app/dashboard:$PYTHONPATH
  aws:elbv2:listener:80:
    DefaultProcess: http
    ListenerEnabled: 'true'
    Protocol: HTTP
    Rules: ws
  aws:elbv2:listenerrule:ws:
    PathPatterns: /websockets/*
    Process: websocket
    Priority: 1
  aws:elasticbeanstalk:environment:process:http:
    Port: '80'
    Protocol: HTTP
  aws:elasticbeanstalk:environment:process:websocket:
    Port: '5000'
    Protocol: HTTP

02_setup.config

container_commands:
  00_pip_upgrade:
    command: "source /opt/python/run/venv/bin/activate && pip install --upgrade pip"
    ignoreErrors: false
  01_migrate:
    command: "django-admin.py migrate"
    leader_only: true
  02_collectstatic:
    command: "django-admin.py collectstatic --noinput"
  03_wsgipass:
    command: 'echo "WSGIPassAuthorization On" >> ../wsgi.conf'

When running eb create django-env, the deployment fails with the error:

Command failed on instance. An unexpected error has occurred [ErrorCode: 0000000001]

Checking the logs, I found the root cause:

2020-06-17 16:36:41,880 P4189 [INFO] Command 00_pip_upgrade
2020-06-17 16:36:41,883 P4189 [INFO] -----------------------Command Output-----------------------
2020-06-17 16:36:41,883 P4189 [INFO] /bin/sh: /opt/python/run/venv/bin/activate: No such file or directory

I can't SSH into the EC2 instance to investigate further. Has the Python virtual environment directory changed in Amazon Linux 2?


Solution

Yes, the virtual environment path has changed significantly between the legacy Amazon Linux AMI and Amazon Linux 2 on Elastic Beanstalk. The tutorial you followed was written for the old AMI, which uses the hardcoded path /opt/python/run/venv/bin/activate — this path does not exist on Amazon Linux 2.

Key Changes to Fix the Issue

  1. Remove manual virtual environment activation
    Elastic Beanstalk's container_commands for Python on Amazon Linux 2 automatically run within the application's virtual environment. You don't need to explicitly source an activate script.

  2. Update your 02_setup.config file
    Replace your existing container commands with this revised version, which is compatible with Amazon Linux 2:

    container_commands:
      00_pip_upgrade:
        command: "pip install --upgrade pip"
        ignoreErrors: false
      01_migrate:
        command: "python manage.py migrate"
        leader_only: true
        cwd: "/opt/python/current/app"
      02_collectstatic:
        command: "python manage.py collectstatic --noinput"
        cwd: "/opt/python/current/app"
      03_wsgipass:
        command: 'echo "WSGIPassAuthorization On" >> ../wsgi.conf'
    

Additional Notes

  • Why this works:

    • The pip and python commands in container_commands point directly to the virtual environment's binaries (no activation needed).
    • Adding cwd: "/opt/python/current/app" ensures commands run from your application's root directory, where manage.py resides.
    • If you ever need to manually activate the venv for custom scripts (rare), use a dynamic path instead of hardcoding:
      source $(find /var/app/venv -name activate)
      
      Amazon Linux 2 stores virtual environments under /var/app/venv/ with a randomly generated suffix (e.g., /var/app/venv/staging-LQM1lest/bin/activate), so hardcoding the path will always fail.
  • Verify other configuration:
    Your 01_env.config looks correct for routing WebSocket traffic, but ensure your Django Channels setup (e.g., ASGI_APPLICATION, channel layer configuration) is compatible with Elastic Beanstalk's process mapping for port 5000.


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 08:57:39