Deploying A Django Project Integrated With MongoDB
Deploying A Django Project Integrated With MongoDB
Prerequisites
1. Server or Cloud Provider: Choose a cloud provider like AWS, Google Cloud Platform
(GCP), or DigitalOcean. Alternatively, you can use Heroku or Render for simpler
deployments.
2. Domain: Register a domain if you want to access the application with a custom domain.
3. SSL Certificate: For HTTPS, consider using Let’s Encrypt or a service like Cloudflare.
bash
Copy code
ssh your_username@your_server_ip
bash
Copy code
sudo apt update && sudo apt upgrade -y
sudo apt install python3-pip python3-venv nginx
1. Install MongoDB:
bash
Copy code
sudo apt install -y mongodb
2. Configure MongoDB:
o Start MongoDB and ensure it’s running.
o Configure your Django settings to connect to MongoDB.
1. Install PostgreSQL:
bash
Copy code
sudo apt install postgresql postgresql-contrib
2. Configure PostgreSQL:
bash
Copy code
sudo -u postgres psql
CREATE DATABASE your_db_name;
CREATE USER your_user WITH PASSWORD 'your_password';
ALTER ROLE your_user SET client_encoding TO 'utf8';
ALTER ROLE your_user SET default_transaction_isolation TO 'read
committed';
ALTER ROLE your_user SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE your_db_name TO your_user;
bash
Copy code
python3 -m venv /path/to/your/venv
source /path/to/your/venv/bin/activate
bash
Copy code
pip install -r requirements.txt
Install Gunicorn for serving HTTP requests and Daphne for handling WebSocket connections.
bash
Copy code
pip install gunicorn daphne
Step 5: Configure Environment Variables and Secrets
bash
Copy code
export DJANGO_SECRET_KEY="your-secret-key"
export OPENAI_API_KEY="your-openai-api-key"
python
Copy code
# settings.py
ASGI_APPLICATION = 'your_project_name.asgi.application'
CHANNEL_LAYERS = {
'default': {
'BACKEND': 'channels_redis.core.RedisChannelLayer',
'CONFIG': {
"hosts": [("127.0.0.1", 6379)],
},
},
}
Install Redis, which Django Channels uses for managing WebSocket connections.
bash
Copy code
sudo apt install redis-server
bash
Copy code
redis-cli ping
python
Copy code
# asgi.py
import os
from django.core.asgi import get_asgi_application
from channels.routing import ProtocolTypeRouter, URLRouter
from channels.auth import AuthMiddlewareStack
import medical.routing
os.environ.setdefault('DJANGO_SETTINGS_MODULE',
'your_project_name.settings')
application = ProtocolTypeRouter({
"http": get_asgi_application(),
"websocket": AuthMiddlewareStack(
URLRouter(
medical.routing.websocket_urlpatterns
)
),
})
1. Create Gunicorn Service: Create a service file for Gunicorn to handle HTTP.
ini
Copy code
# /etc/systemd/system/gunicorn.service
[Unit]
Description=gunicorn daemon
After=network.target
[Service]
User=your_username
Group=www-data
WorkingDirectory=/path/to/your/project
ExecStart=/path/to/your/venv/bin/gunicorn --workers 3 --bind
unix:/path/to/your/project/gunicorn.sock
your_project_name.wsgi:application
[Install]
WantedBy=multi-user.target
2. Create Daphne Service: Create a service file for Daphne to handle WebSockets.
ini
Copy code
# /etc/systemd/system/daphne.service
[Unit]
Description=Daphne ASGI server
After=network.target
[Service]
User=your_username
Group=www-data
WorkingDirectory=/path/to/your/project
ExecStart=/path/to/your/venv/bin/daphne -b 0.0.0.0 -p 8001
your_project_name.asgi:application
[Install]
WantedBy=multi-user.target
bash
Copy code
sudo systemctl start gunicorn
sudo systemctl enable gunicorn
sudo systemctl start daphne
sudo systemctl enable daphne
Set up Nginx to route HTTP requests to Gunicorn and WebSocket connections to Daphne.
conf
Copy code
# /etc/nginx/sites-available/your_project_name
server {
listen 80;
server_name your_domain_or_IP;
location / {
include proxy_params;
proxy_pass http://unix:/path/to/your/project/gunicorn.sock;
}
location /ws/ {
proxy_pass http://127.0.0.1:8001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location /static/ {
alias /path/to/your/staticfiles/;
}
location /media/ {
alias /path/to/your/mediafiles/;
}
}
2. Enable Nginx Configuration:
bash
Copy code
sudo ln -s /etc/nginx/sites-available/your_project_name
/etc/nginx/sites-enabled
sudo nginx -t # Test configuration
sudo systemctl restart nginx
bash
Copy code
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d your_domain
Use Git to push code changes to the server and deploy automatically.
Final Steps
This deployment setup leverages Gunicorn for HTTP, Daphne for WebSockets, Redis for Django
Channels, and Nginx as a reverse proxy, allowing your Django project to handle HTTP and
WebSocket traffic efficiently in production. Let me know if you need further assistance!
4o
How ca