Server Deployment
- Deploy Django applications on VPS and cloud servers securely.
What is Server Deployment?
Server Deployment means hosting your Django project on a remote server so that anyone on the internet can access it.
Provides real-world access to your application
Handles traffic, databases, and static files
Can be done on cloud platforms like Heroku, AWS, DigitalOcean
Heroku Deployment
What is Heroku?
Cloud Platform as a Service (PaaS)
Supports Python / Django apps
Handles server, database, scaling automatically
Steps to Deploy Django on Heroku
Step 1: Install Heroku CLI
# For Ubuntu
curl https://cli-assets.heroku.com/install.sh | sh
# Login
heroku login
Step 2: Prepare Django Project
1.Install Gunicorn (WSGI server):
pip install gunicorn
2.Add gunicorn to requirements.txt:
pip freeze > requirements.txt
3.Create Procfile (no file extension):
web: gunicorn projectname.wsgi
4. Install django-heroku (optional):
pip install django-heroku
Step 3: Configure Settings for Production
settings.py
import django_heroku
import os
DEBUG = False
ALLOWED_HOSTS = ['your-app-name.herokuapp.com']
# Activate Heroku settings
django_heroku.settings(locals())
- Automatically sets DATABASE_URL, static files, logging
Step 4: Git Commit & Push
git init
git add .
git commit -m "Initial commit for Heroku"
heroku create your-app-name
git push heroku main # or master
Step 5: Migrate Database
heroku run python manage.py migrate
Creates tables in Heroku Postgres
Step 6: Open App
heroku open
Tips for Heroku
Free dynos sleep after inactivity → use paid for production
Use Heroku Postgres for DB
Manage environment variables in Heroku dashboard
AWS Deployment (EC2)
What is AWS EC2?
Elastic Compute Cloud → virtual server in the cloud
Full control over OS, Django app, database, web server
Good for scalable production applications
Steps to Deploy Django on AWS EC2
Step 1: Launch EC2 Instance
Choose Ubuntu Server
Configure security group (allow HTTP 80, HTTPS 443, SSH 22)
Connect via SSH:
ssh -i yourkey.pem ubuntu@your-ec2-ip
Step 2: Install Server Components
sudo apt update
sudo apt install python3-pip python3-venv git nginx postgresql postgresql-contrib
Step 3: Clone Project & Setup Virtualenv
git clone https://github.com/username/project.git
cd project
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
Step 4: Configure Database (PostgreSQL)
sudo -i -u postgres
psql
CREATE DATABASE myprojectdb;
CREATE USER myprojectuser WITH PASSWORD 'password';
ALTER ROLE myprojectuser SET client_encoding TO 'utf8';
ALTER ROLE myprojectuser SET default_transaction_isolation TO 'read committed';
ALTER ROLE myprojectuser SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE myprojectdb TO myprojectuser;
\q
exit
Update settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'myprojectdb',
'USER': 'myprojectuser',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '5432',
}
}
Step 5: Run Gunicorn
gunicorn projectname.wsgi:application --bind 0.0.0.0:8000
Test app on http://ec2-ip:8000
Step 6: Configure Nginx
sudo nano /etc/nginx/sites-available/project
Add:
server {
listen 80;
server_name your-domain.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/project;
}
location / {
include proxy_params;
proxy_pass http://127.0.0.1:8000;
}
}
sudo ln -s /etc/nginx/sites-available/project /etc/nginx/sites-enabled
sudo nginx -t
sudo systemctl restart nginx
Enable HTTPS (Optional)
Use Certbot / Let’s Encrypt
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d your-domain.com
DigitalOcean Deployment
Similar to AWS EC2 → Ubuntu droplet
Offers easy 1-click Django setup
Nginx + Gunicorn recommended
Supports managed database