pip install awsebcli --upgrade
Verify installation:
eb --version
aws configure
Provide:
us-east-1
)json
)Navigate to the Django project directory:
eb init -p python3.9 my-django-app
-p python3.9
: Specifies the Python version.my-django-app
: The application name.Create a .ebextensions
folder and an environment config file:
mkdir .ebextensions
touch .ebextensions/django.config
Add environment variables inside django.config
:
option_settings:
aws:elasticbeanstalk:application:environment:
DJANGO_SECRET_KEY: "your-secret-key"
DJANGO_DEBUG: "False"
DATABASE_URL: "postgres://user:password@db-instance.amazonaws.com:5432/dbname"
eb create my-django-env
After deployment:
eb open
To update the deployment:
eb deploy
SSH into the instance:
eb ssh my-django-env
Run migrations:
python manage.py migrate
eb logs
Restart application:
eb restart
t3.micro
for free-tier).Copy the public IP and SSH into the instance:
ssh -i my-key.pem ec2-user@ec2-xx-xx-xx-xx.compute-1.amazonaws.com
For Ubuntu instances:
ssh -i my-key.pem ubuntu@ec2-xx-xx-xx-xx.compute-1.amazonaws.com
sudo yum update -y # Amazon Linux
sudo apt update -y # Ubuntu
sudo yum install python3-pip -y # Amazon Linux
sudo apt install python3-pip -y # Ubuntu
python3 -m venv venv
source venv/bin/activate
pip install --upgrade pip
git clone https://github.com/user/my-django-project.git
cd my-django-project
Install dependencies:
pip install -r requirements.txt
Using PostgreSQL:
sudo yum install postgresql postgresql-server postgresql-devel -y # Amazon Linux
sudo apt install postgresql postgresql-contrib libpq-dev -y # Ubuntu
Update settings.py
:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "mydb",
"USER": "myuser",
"PASSWORD": "mypassword",
"HOST": "mydb-instance.amazonaws.com",
"PORT": "5432",
}
}
Apply migrations:
python manage.py migrate
Install Gunicorn:
pip install gunicorn
Start Gunicorn:
gunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
Install Nginx:
sudo yum install nginx -y # Amazon Linux
sudo apt install nginx -y # Ubuntu
Edit Nginx config file /etc/nginx/nginx.conf
:
server {
listen 80;
server_name ec2-xx-xx-xx-xx.compute-1.amazonaws.com;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Restart Nginx:
sudo systemctl restart nginx
Enable on boot:
sudo systemctl enable nginx
Create /etc/systemd/system/django.service
:
[Unit]
Description=Django Service
After=network.target
[Service]
User=ec2-user
Group=ec2-user
WorkingDirectory=/home/ec2-user/my-django-project
ExecStart=/home/ec2-user/venv/bin/gunicorn --workers 4 --bind 0.0.0.0:8000 myproject.wsgi:application
Restart=always
[Install]
WantedBy=multi-user.target
Enable and start the service:
sudo systemctl daemon-reload
sudo systemctl enable django
sudo systemctl start django
Check service logs:
sudo journalctl -u django --no-pager
sudo yum install certbot python3-certbot-nginx -y # Amazon Linux
sudo apt install certbot python3-certbot-nginx -y # Ubuntu
sudo certbot --nginx -d example.com -d www.example.com
Auto-renew SSL certificate:
sudo certbot renew --dry-run
Create a deployment script deploy.sh
:
#!/bin/bash
cd /home/ec2-user/my-django-project
git pull origin main
source venv/bin/activate
pip install -r requirements.txt
python manage.py migrate
sudo systemctl restart django
Make it executable:
chmod +x deploy.sh
Run deployment:
./deploy.sh
Check logs:
sudo journalctl -u django --no-pager
sudo tail -f /var/log/nginx/error.log
Monitor server:
htop