This guide covers all procedures for deploying the HostingCo system to production environments.
The HostingCo system supports multiple deployment strategies:
# Clone repository
git clone https://github.com/your-org/hostingco-system.git
cd hostingco-system
# Install all dependencies
npm run install:all
# Copy environment configuration
cp .env.example .env
# Build all packages for production
npm run build
# Or build individual packages
npm run build:backend
npm run build:frontend
npm run build:shared # from shared directory
# Build and start with Docker Compose
docker-compose up -d --build
# Check logs
docker-compose logs -f
# Stop services
docker-compose down
Production .env:
NODE_ENV=production
LOG_LEVEL=info
# Database
DATABASE_URL=postgresql://hostingco:secure_password@postgres:5432/hostingco
POSTGRES_DB=hostingco
POSTGRES_USER=hostingco
POSTGRES_PASSWORD=secure_password
# Redis
REDIS_URL=redis://redis:6379
# JWT
JWT_SECRET=your-super-secure-jwt-secret-change-this
JWT_EXPIRES_IN=24h
# Frontend
FRONTEND_URL=https://yourdomain.com
# Email
SMTP_HOST=smtp.yourprovider.com
SMTP_PORT=587
SMTP_USER=noreply@yourdomain.com
SMTP_PASS=your-email-password
# File Upload
MAX_FILE_SIZE=10485760
UPLOAD_PATH=/app/uploads
# SSL
SSL_CERT_PATH=/etc/nginx/ssl/certificate.crt
SSL_KEY_PATH=/etc/nginx/ssl/private.key
docker-compose.prod.yml:
version: '3.8'
services:
frontend:
build:
context: ./frontend
dockerfile: Dockerfile.prod
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./ssl:/etc/nginx/ssl
depends_on:
- backend
networks:
- hostingco-network
backend:
build:
context: ./backend
dockerfile: Dockerfile.prod
environment:
- NODE_ENV=production
- DATABASE_URL=postgresql://hostingco:secure_password@postgres:5432/hostingco
- REDIS_URL=redis://redis:6379
- FRONTEND_URL=https://yourdomain.com
depends_on:
- postgres
- redis
volumes:
- ./backend/logs:/app/logs
- ./backend/uploads:/app/uploads
networks:
- hostingco-network
postgres:
image: postgres:15
environment:
- POSTGRES_DB=hostingco
- POSTGRES_USER=hostingco
- POSTGRES_PASSWORD=secure_password
volumes:
- postgres_data:/var/lib/postgresql/data
- ./backups:/backups
networks:
- hostingco-network
redis:
image: redis:7-alpine
volumes:
- redis_data:/data
networks:
- hostingco-network
volumes:
postgres_data:
redis_data:
networks:
hostingco-network:
driver: bridge
# Use automated deployment scripts (recommended)
./scripts/build.sh production
./scripts/deploy.sh production
# Or use Docker scripts:
./scripts/docker-build.sh production
./scripts/docker-up.sh production
# Or use docker-compose directly:
# Build production images
docker-compose -f docker-compose.prod.yml build
# Start production services
docker-compose -f docker-compose.prod.yml up -d
# Initialize database
docker-compose -f docker-compose.prod.yml exec backend npm run migrate
docker-compose -f docker-compose.prod.yml exec backend npm run seed
# Check services status
docker-compose -f docker-compose.prod.yml ps
# Create SSL directory
mkdir -p ssl
# Generate self-signed certificate (for testing)
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout ssl/private.key -out ssl/certificate.crt
# Or use Let's Encrypt (for production)
sudo certbot certonly --standalone -d yourdomain.com
sudo cp /etc/letsencrypt/live/yourdomain.com/fullchain.pem ssl/certificate.crt
sudo cp /etc/letsencrypt/live/yourdomain.com/privkey.pem ssl/private.key
namespace.yaml:
apiVersion: v1
kind: Namespace
metadata:
name: hostingco
configmap.yaml:
apiVersion: v1
kind: ConfigMap
metadata:
name: hostingco-config
namespace: hostingco
data:
NODE_ENV: "production"
LOG_LEVEL: "info"
FRONTEND_URL: "https://yourdomain.com"
secret.yaml:
apiVersion: v1
kind: Secret
metadata:
name: hostingco-secrets
namespace: hostingco
type: Opaque
data:
DATABASE_URL: <base64-encoded-database-url>
REDIS_URL: <base64-encoded-redis-url>
JWT_SECRET: <base64-encoded-jwt-secret>
backend-deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: backend
namespace: hostingco
spec:
replicas: 3
selector:
matchLabels:
app: backend
template:
metadata:
labels:
app: backend
spec:
containers:
- name: backend
image: hostingco/backend:latest
ports:
- containerPort: 3003
envFrom:
- configMapRef:
name: hostingco-config
- secretRef:
name: hostingco-secrets
resources:
requests:
memory: "256Mi"
cpu: "250m"
limits:
memory: "512Mi"
cpu: "500m"
# Apply all manifests
kubectl apply -f k8s/
# Check deployment status
kubectl get pods -n hostingco
kubectl get services -n hostingco
# Check logs
kubectl logs -f deployment/backend -n hostingco
# Update system
sudo apt update && sudo apt upgrade -y
# Install Node.js
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
# Install PostgreSQL
sudo apt install postgresql postgresql-contrib
sudo systemctl start postgresql
sudo systemctl enable postgresql
# Install Redis
sudo apt install redis-server
sudo systemctl start redis-server
sudo systemctl enable redis-server
# Install Nginx
sudo apt install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
# Create application user
sudo useradd -m -s /bin/bash hostingco
sudo usermod -aG sudo hostingco
# Clone repository
sudo -u hostingco git clone https://github.com/your-org/hostingco-system.git /home/hostingco/app
cd /home/hostingco/app
# Install dependencies
sudo -u hostingco npm run install:all
# Build applications
sudo -u hostingco npm run build
# Setup database
sudo -u postgres createdb hostingco
sudo -u postgres createuser hostingco
sudo -u postgres psql -c "ALTER USER hostingco PASSWORD 'secure_password';"
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE hostingco TO hostingco;"
# Run migrations
sudo -u hostingco cd backend && npm run migrate && npm run seed
backend.service:
[Unit]
Description=HostingCo Backend
After=network.target postgresql.service redis.service
[Service]
Type=simple
User=hostingco
WorkingDirectory=/home/hostingco/app/backend
Environment=NODE_ENV=production
Environment=PORT=3003
ExecStart=/usr/bin/npm start
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
:::bash
# Install service files
sudo cp backend.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable backend
sudo systemctl start backend
/etc/nginx/sites-available/hostingco:
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name yourdomain.com www.yourdomain.com;
ssl_certificate /etc/ssl/certs/yourdomain.crt;
ssl_certificate_key /etc/ssl/private/yourdomain.key;
root /home/hostingco/app/frontend/dist;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
location /api {
proxy_pass http://localhost:3003;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
}
}
:::bash
# Enable site
sudo ln -s /etc/nginx/sites-available/hostingco /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Using AWS ECS with Fargate:
Create ECR Repository:
:::bash
aws ecr create-repository --repository-name hostingco-backend
aws ecr create-repository --repository-name hostingco-frontend
Build and Push Images:
:::bash
docker build -t hostingco-backend ./backend
aws ecr get-login-password --region us-west-2 | docker login --username AWS --password-stdin <account-id>.dkr.ecr.us-west-2.amazonaws.com
docker tag hostingco-backend:latest <account-id>.dkr.ecr.us-west-2.amazonaws.com/hostingco-backend:latest
docker push <account-id>.dkr.ecr.us-west-2.amazonaws.com/hostingco-backend:latest</account-id></account-id></account-id>
Create ECS Task Definition:
:::json
{
"family": "hostingco",
"networkMode": "awsvpc",
"requiresCompatibilities": ["FARGATE"],
"cpu": "512",
"memory": "1024",
"executionRoleArn": "arn:aws:iam::<account-id>:role/ecsTaskExecutionRole",
"containerDefinitions": [
{
"name": "backend",
"image": "<account-id>.dkr.ecr.us-west-2.amazonaws.com/hostingco-backend:latest",
"portMappings": [
{
"containerPort": 3003,
"protocol": "tcp"
}
],
"environment": [
{
"name": "NODE_ENV",
"value": "production"
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/ecs/hostingco",
"awslogs-region": "us-west-2",
"awslogs-stream-prefix": "ecs"
}
}
}
]
}</account-id></account-id>
Using Cloud Run:
# Build and deploy backend
gcloud builds submit --tag gcr.io/PROJECT-ID/hostingco-backend ./backend
gcloud run deploy hostingco-backend --image gcr.io/PROJECT-ID/hostingco-backend --platform managed
# Build and deploy frontend
gcloud builds submit --tag gcr.io/PROJECT-ID/hostingco-frontend ./frontend
gcloud run deploy hostingco-frontend --image gcr.io/PROJECT-ID/hostingco-frontend --platform managed
Using Azure Container Instances:
# Create resource group
az group create --name hostingco-rg --location eastus
# Deploy containers
az container create \
--resource-group hostingco-rg \
--name hostingco-backend \
--image hostingco/backend:latest \
--dns-name-label hostingco-backend \
--ports 3003
.github/workflows/deploy.yml:
name: Deploy to Production
on:
push:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- run: npm run install:all
- run: npm run test
build-and-deploy:
needs: test
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v3
- name: Build and push Docker images
run: |
docker build -t hostingco-backend ./backend
docker build -t hostingco-frontend ./frontend
echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
docker push hostingco-backend:latest
docker push hostingco-frontend:latest
- name: Deploy to production
run: |
ssh ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }} "
cd /opt/hostingco &&
docker-compose pull &&
docker-compose up -d
"
# Health checks
curl https://yourdomain.com/api/health
# Monitor logs
docker-compose logs -f backend
docker-compose logs -f frontend
# System metrics
docker stats
# Connection status
psql -h localhost -U hostingco -d hostingco -c "SELECT * FROM pg_stat_activity;"
# Database size
psql -h localhost -U hostingco -d hostingco -c "SELECT pg_size_pretty(pg_database_size('hostingco'));"
# Slow queries
psql -h localhost -U hostingco -d hostingco -c "SELECT query, mean_time, calls FROM pg_stat_statements ORDER BY mean_time DESC LIMIT 10;"
# Install monitoring tools
npm install -g @pm2/io
pm2 start ecosystem.config.js
# Monitor with PM2
pm2 monit
pm2 logs
# Install SSL certificates
sudo certbot --nginx -d yourdomain.com
# Auto-renewal
sudo crontab -e
# Add: 0 12 * * * /usr/bin/certbot renew --quiet
# Configure UFW
sudo ufw allow ssh
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
Nginx security headers:
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer-when-downgrade" always;
add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
# Docker Compose rolling update
docker-compose -f docker-compose.prod.yml pull
docker-compose -f docker-compose.prod.yml up -d --no-deps backend
# Kubernetes rolling update
kubectl set image deployment/backend backend=hostingco/backend:v2 -n hostingco
# Database backup
pg_dump -h localhost -U hostingco hostingco > backup_$(date +%Y%m%d_%H%M%S).sql
# Automated backup script
#!/bin/bash
BACKUP_DIR="/backups"
DATE=$(date +%Y%m%d_%H%M%S)
pg_dump -h localhost -U hostingco hostingco > $BACKUP_DIR/backup_$DATE.sql
find $BACKUP_DIR -name "backup_*.sql" -mtime +7 -delete
# Configure logrotate
sudo nano /etc/logrotate.d/hostingco
# Content:
/var/log/hostingco/*.log {
daily
missingok
rotate 30
compress
delaycompress
notifempty
create 644 hostingco hostingco
postrotate
systemctl reload nginx
endscript
}
# Check all services
systemctl status nginx postgresql redis backend
# Check ports
netstat -tlnp | grep -E ":80|:443|:3003"
# Check logs
journalctl -u backend -f
tail -f /var/log/nginx/error.log
# Check system resources
top
htop
df -h
free -m
# Check database performance
psql -h localhost -U hostingco -d hostingco -c "SELECT * FROM pg_stat_activity;"
# Optimize database
psql -h localhost -U hostingco -d hostingco -c "VACUUM ANALYZE;"
Last updated: $(date)