Elastic Beanstalk部署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
Remove manual virtual environment activation
Elastic Beanstalk'scontainer_commandsfor Python on Amazon Linux 2 automatically run within the application's virtual environment. You don't need to explicitlysourcean activate script.Update your
02_setup.configfile
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
pipandpythoncommands incontainer_commandspoint 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, wheremanage.pyresides. - If you ever need to manually activate the venv for custom scripts (rare), use a dynamic path instead of hardcoding:
Amazon Linux 2 stores virtual environments undersource $(find /var/app/venv -name activate)/var/app/venv/with a randomly generated suffix (e.g.,/var/app/venv/staging-LQM1lest/bin/activate), so hardcoding the path will always fail.
- The
Verify other configuration:
Your01_env.configlooks 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

