-
Notifications
You must be signed in to change notification settings - Fork 0
Session 9 WebApp Deployment
Welcome to Session 9! In this session, we'll focus on deploying our web application to production environments. We'll learn how to deploy the frontend to Vercel and the backend to Render, configure our application for production, and set up continuous deployment for seamless updates.
- Understand the deployment process for web applications
- Prepare your Next.js frontend for production deployment
- Prepare your Django backend for production deployment
- Deploy your frontend to Vercel
- Deploy your backend to Render
- Configure environment variables for different environments
- Set up continuous deployment
- Monitor your deployed application
Deployment is the process of making your application available to users on the internet. It involves several important considerations:
- Production vs Development Environments: Different configurations for different stages
- Environment Variables: Secure storage of sensitive configuration data
- Static vs Dynamic Content: Different hosting requirements
- Domain Names and DNS: How users access your application
- HTTPS: Secure communication between users and your application
- CI/CD (Continuous Integration/Continuous Deployment): Automated testing and deployment
- Monitoring and Logging: Tracking application performance and errors
Before deploying, you need to prepare your Next.js application:
Create an .env.local file for development and configure environment variables in your deployment platform for production.
# .env.local (development)
NEXT_PUBLIC_API_URL=http://localhost:8000/api
Add environment variables to your Next.js components:
// Example component using environment variables
function TaskList() {
const apiUrl = process.env.NEXT_PUBLIC_API_URL;
useEffect(() => {
fetch(`${apiUrl}/tasks`)
.then(response => response.json())
.then(data => setTasks(data));
}, []);
// ...
}# Install dependencies for production
npm install --production
# Build your application
npm run build# Start the production server locally
npm run startDjango requires some configuration changes for production:
# todo_backend/settings.py
import os
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get('SECRET_KEY', 'your-default-development-key')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ.get('DEBUG', 'False') == 'True'
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', 'localhost,127.0.0.1').split(',')
# Application definition
INSTALLED_APPS = [
# ...existing apps
]
# Configure CORS for production
CORS_ALLOWED_ORIGINS = os.environ.get('CORS_ALLOWED_ORIGINS', 'http://localhost:3000').split(',')
# Database configuration
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': os.environ.get('DB_NAME', 'todo_app'),
'USER': os.environ.get('DB_USER', 'todo_user'),
'PASSWORD': os.environ.get('DB_PASSWORD', 'password'),
'HOST': os.environ.get('DB_HOST', 'localhost'),
'PORT': os.environ.get('DB_PORT', '5432'),
}
}
# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# Security settings for production
if not DEBUG:
SECURE_HSTS_SECONDS = 3600
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')pip freeze > requirements.txtweb: gunicorn todo_backend.wsgi --log-file -
python-3.9.6
Vercel is a cloud platform for static sites and serverless functions, optimized for Next.js.
-
Create a Vercel Account:
- Sign up at vercel.com
-
Install Vercel CLI:
npm install -g vercel
-
Login to Vercel:
vercel login
-
Configure Your Project: Create a
vercel.jsonfile in your project root:{ "version": 2, "builds": [ { "src": "package.json", "use": "@vercel/next" } ] } -
Deploy Your Project:
vercel
-
For Production Deployment:
vercel --prod
-
Configure Environment Variables:
- Go to your project on the Vercel dashboard
- Navigate to Settings > Environment Variables
- Add your environment variables (e.g.,
NEXT_PUBLIC_API_URL)
Render is a unified cloud for web services with a free tier for small projects.
-
Create a Render Account:
- Sign up at render.com
-
Create a New Web Service:
- Click "New" and select "Web Service"
- Connect your GitHub repository
- Give your service a name
-
Configure Your Web Service:
- Environment: Python
- Build Command:
pip install -r requirements.txt - Start Command:
gunicorn todo_backend.wsgi
-
Add Environment Variables:
-
SECRET_KEY: A strong, random secret key -
DEBUG: 'False' -
ALLOWED_HOSTS: 'your-app.onrender.com' -
CORS_ALLOWED_ORIGINS: 'https://your-frontend-url.vercel.app' -
DB_NAME: Your database name -
DB_USER: Your database username -
DB_PASSWORD: Your database password -
DB_HOST: Your database host -
DB_PORT: Your database port
-
-
Create a PostgreSQL Database:
- Click "New" and select "PostgreSQL"
- Choose a name and region
- Connect your web service to this database
-
Deploy Your Service:
- Click "Create Web Service"
To use your own domain name instead of the default ones provided by Vercel and Render:
- Go to your project settings
- Click on "Domains"
- Add your custom domain
- Configure your domain's DNS settings as instructed by Vercel
- Go to your web service
- Click on "Settings"
- Scroll down to "Custom Domains"
- Add your custom domain
- Configure your domain's DNS settings as instructed by Render
Continuous Deployment (CD) automatically deploys your code when you push changes to your repository.
Vercel automatically sets up CD when you connect your GitHub repository. Every time you push to your main branch, Vercel will deploy the changes.
You can configure branch deployments in your project settings:
- Production: main branch
- Preview: other branches
Render also provides automatic deployments when you push to your repository.
Configure automatic deployments in your web service settings:
- Go to your web service
- Click on "Settings"
- Scroll to "Auto-Deploy"
- Select your branch and deploy method
Monitoring your deployed application is crucial for identifying and fixing issues quickly.
- Provides real-time performance metrics
- Tracks page views and user engagement
- Shows Core Web Vitals scores
- View logs in real-time from the dashboard
- Monitor CPU and memory usage
- Set up log drains to external services
- Sentry: For error tracking and performance monitoring
- LogRocket: For session replay and error tracking
- Google Analytics: For user behavior tracking
- Uptime Robot: For monitoring application availability
- Configure your Next.js application for production
- Configure your Django application for production
- Deploy your frontend to Vercel
- Deploy your backend to Render
- Set up environment variables in both platforms
- Test your deployed application end-to-end
- Set up a custom domain for your application (optional)
- Configure continuous deployment for your repositories
- Next.js Deployment Documentation
- Django Deployment Checklist
- Vercel Documentation
- Render Documentation
- Free SSL Certificates with Let's Encrypt
- DNS Configuration Guide
Now that you've learned how to deploy your web application, you're ready to move on to Session 10: WebApp - Final Project where you'll apply everything you've learned to build a complete Todo application from scratch.