How to Deploy Django to AWS: Step-by-Step Guide
To deploy a
Django app to AWS, use AWS Elastic Beanstalk which automates server setup. Prepare your app with a requirements.txt and Procfile, then use the eb cli commands to create and deploy your environment.Syntax
Deploying Django to AWS Elastic Beanstalk involves these key files and commands:
requirements.txt: Lists Python packages your app needs.Procfile: Tells Elastic Beanstalk how to run your app.eb init: Initializes your Elastic Beanstalk project.eb create: Creates an environment on AWS.eb deploy: Deploys your app to the environment.
bash
requirements.txt Django==4.2 psycopg2-binary Procfile web: gunicorn myproject.wsgi # Commands to run in terminal eb init -p python-3.9 my-django-app eb create my-django-env eb deploy
Example
This example shows a simple Django app deployment using Elastic Beanstalk CLI.
First, create requirements.txt with your dependencies. Then add a Procfile to specify the web server command. Use eb init to set up your AWS project, eb create to make the environment, and eb deploy to upload your app.
bash
# requirements.txt Django==4.2 psycopg2-binary # Procfile web: gunicorn myproject.wsgi # Terminal commands $ eb init -p python-3.9 my-django-app $ eb create my-django-env $ eb deploy # After deployment, open your app $ eb open
Output
Creating application version archive "app-20240610-123456"...
Uploading my-django-app/app-20240610-123456.zip to S3 bucket...
Application version "app-20240610-123456" created successfully.
Environment details for: my-django-env
Application name: my-django-app
Region: us-east-1
Deployed Version: app-20240610-123456
Environment ID: e-abc123xyz
Platform: Python 3.9 running on 64bit Amazon Linux 2
URL: http://my-django-env.us-east-1.elasticbeanstalk.com
Deployment completed successfully.
Common Pitfalls
Common mistakes when deploying Django to AWS include:
- Not setting
ALLOWED_HOSTSinsettings.pyto include your Elastic Beanstalk URL. - Forgetting to collect static files with
python manage.py collectstatic. - Missing the
Procfileor having incorrect gunicorn command. - Not configuring the database or environment variables properly.
Always test locally before deploying and check AWS logs if deployment fails.
text
Wrong Procfile example: web: python manage.py runserver Right Procfile example: web: gunicorn myproject.wsgi
Quick Reference
Summary tips for deploying Django on AWS Elastic Beanstalk:
- Use
gunicornas the production server. - Include
requirements.txtwith all dependencies. - Set
ALLOWED_HOSTSto your AWS domain. - Run
collectstaticto serve static files. - Use
eb clicommands:init,create,deploy.
Key Takeaways
Use AWS Elastic Beanstalk for easy Django deployment with automated server management.
Prepare your app with a proper requirements.txt and Procfile using gunicorn.
Set ALLOWED_HOSTS and run collectstatic to avoid common deployment errors.
Use eb CLI commands to initialize, create environment, and deploy your app.
Check AWS logs and test locally to troubleshoot deployment issues.