VPS Deployment
Deploy on a VPS for full control over your environment.
Providers
| Provider | Starting Price | Locations | Notes |
|---|---|---|---|
| DigitalOcean | $6/mo | Global | Simple, great docs |
| Linode | $5/mo | Global | Reliable |
| Vultr | $5/mo | Global | Many locations |
| Hetzner | €4/mo | EU | Great value |
| AWS EC2 | ~$8/mo | Global | Enterprise |
Recommended Specs
| Use Case | RAM | CPU | Storage |
|---|---|---|---|
| Small | 1 GB | 1 vCPU | 25 GB |
| Medium | 2 GB | 1 vCPU | 50 GB |
| Large | 4 GB | 2 vCPU | 80 GB |
Initial Setup
1. Create Server
- Choose Ubuntu 22.04 LTS (recommended)
- Select your region (close to your users)
- Add your SSH key for secure access
- Create the server
2. Secure the Server
# Connect via SSHssh root@your-server-ip
# Update systemapt update && apt upgrade -y
# Create non-root useradduser deployusermod -aG sudo deploy
# Set up SSH for new usermkdir -p /home/deploy/.sshcp ~/.ssh/authorized_keys /home/deploy/.ssh/chown -R deploy:deploy /home/deploy/.sshchmod 700 /home/deploy/.sshchmod 600 /home/deploy/.ssh/authorized_keys
# Disable root loginsed -i 's/PermitRootLogin yes/PermitRootLogin no/' /etc/ssh/sshd_configsystemctl restart sshd3. Configure Firewall
# Enable UFWufw default deny incomingufw default allow outgoingufw allow sshufw allow httpufw allow httpsufw enableInstalling Dependencies
Node.js
# Install Node.js 20curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -sudo apt install -y nodejs
# Verify installationnode --version # v20.x.xnpm --versionpnpm
# Install pnpmnpm install -g pnpm
# Or via Corepackcorepack enable pnpmGit
sudo apt install -y gitDeploying the Application
Clone and Build
# Switch to deploy usersu - deploy
# Clone repositorygit clone https://github.com/KevinTrinh1227/discord-forum-api.gitcd discord-forum-api
# Install dependenciespnpm install
# Buildpnpm buildConfigure Environment
# Create environment filecp .env.example .envnano .envAdd your configuration:
# DiscordDISCORD_TOKEN=your_bot_tokenDISCORD_CLIENT_ID=your_client_idDISCORD_CLIENT_SECRET=your_client_secret
# DatabaseDATABASE_TYPE=sqliteDATABASE_PATH=./data/discord-forum.db
# APIAPI_PORT=3000CORS_ORIGIN=https://yourdomain.comNODE_ENV=productionInitialize Database
pnpm db:pushProcess Management with PM2
Install PM2
sudo npm install -g pm2Create Ecosystem File
Create ecosystem.config.js:
module.exports = { apps: [ { name: 'forum-bot', script: 'packages/bot/dist/index.js', cwd: '/home/deploy/discord-forum-api', instances: 1, autorestart: true, watch: false, max_memory_restart: '500M', env: { NODE_ENV: 'production', }, }, { name: 'forum-api', script: 'packages/api/dist/index.js', cwd: '/home/deploy/discord-forum-api', instances: 'max', exec_mode: 'cluster', autorestart: true, watch: false, max_memory_restart: '500M', env: { NODE_ENV: 'production', }, }, ],};Start Services
# Start all servicespm2 start ecosystem.config.js
# Save process listpm2 save
# Enable startup on bootpm2 startup# Follow the instructions it printsPM2 Commands
pm2 status # View statuspm2 logs # View all logspm2 logs forum-bot # View bot logspm2 restart all # Restart allpm2 reload forum-api # Zero-downtime reloadpm2 stop all # Stop allpm2 monit # Real-time monitoringNginx Reverse Proxy
Install Nginx
sudo apt install -y nginxConfigure Site
Create /etc/nginx/sites-available/forum-api:
server { listen 80; server_name api.yourdomain.com;
location / { proxy_pass http://localhost:3000; 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;
# Timeout settings proxy_connect_timeout 60s; proxy_send_timeout 60s; proxy_read_timeout 60s; }
# Health check endpoint location /health { proxy_pass http://localhost:3000/health; access_log off; }}Enable Site
# Create symlinksudo ln -s /etc/nginx/sites-available/forum-api /etc/nginx/sites-enabled/
# Test configurationsudo nginx -t
# Reload Nginxsudo systemctl reload nginxSSL with Let’s Encrypt
Install Certbot
sudo apt install -y certbot python3-certbot-nginxObtain Certificate
sudo certbot --nginx -d api.yourdomain.comAuto-Renewal
Certbot automatically sets up renewal. Test it:
sudo certbot renew --dry-runSystemd Services (Alternative to PM2)
Bot Service
Create /etc/systemd/system/forum-bot.service:
[Unit]Description=Discord Forum BotAfter=network.target
[Service]Type=simpleUser=deployWorkingDirectory=/home/deploy/discord-forum-apiExecStart=/usr/bin/node packages/bot/dist/index.jsRestart=on-failureRestartSec=10StandardOutput=syslogStandardError=syslogSyslogIdentifier=forum-botEnvironment=NODE_ENV=production
[Install]WantedBy=multi-user.targetAPI Service
Create /etc/systemd/system/forum-api.service:
[Unit]Description=Discord Forum APIAfter=network.target
[Service]Type=simpleUser=deployWorkingDirectory=/home/deploy/discord-forum-apiExecStart=/usr/bin/node packages/api/dist/index.jsRestart=on-failureRestartSec=10StandardOutput=syslogStandardError=syslogSyslogIdentifier=forum-apiEnvironment=NODE_ENV=production
[Install]WantedBy=multi-user.targetEnable Services
sudo systemctl daemon-reloadsudo systemctl enable forum-bot forum-apisudo systemctl start forum-bot forum-api
# Check statussudo systemctl status forum-bot forum-apiUpdates and Deployments
Manual Update Script
Create deploy.sh:
#!/bin/bashset -e
cd /home/deploy/discord-forum-api
# Pull latest changesgit pull origin main
# Install dependenciespnpm install
# Buildpnpm build
# Run migrations (if any)pnpm db:migrate
# Restart servicespm2 restart all
echo "Deployment complete!"Automated Deployment
For GitHub Actions, set up a webhook or use SSH:
name: Deploy
on: push: branches: [main]
jobs: deploy: runs-on: ubuntu-latest steps: - name: Deploy via SSH uses: appleboy/ssh-action@v1.0.0 with: host: ${{ secrets.SERVER_HOST }} username: deploy key: ${{ secrets.SSH_PRIVATE_KEY }} script: | cd /home/deploy/discord-forum-api ./deploy.shMonitoring
Log Management
# View PM2 logspm2 logs --lines 100
# Or with journalctl (for systemd)journalctl -u forum-bot -fjournalctl -u forum-api -fLogrotate
Create /etc/logrotate.d/forum-api:
/home/deploy/.pm2/logs/*.log { daily rotate 7 compress delaycompress missingok notifempty copytruncate}Uptime Monitoring
Use external services:
- UptimeRobot (free)
- Better Uptime (free tier)
- Pingdom
Backup
Database Backup Script
Create backup.sh:
#!/bin/bashBACKUP_DIR="/home/deploy/backups"DB_PATH="/home/deploy/discord-forum-api/data/discord-forum.db"DATE=$(date +%Y%m%d_%H%M%S)
mkdir -p $BACKUP_DIR
# Backup databasecp $DB_PATH "$BACKUP_DIR/discord-forum_$DATE.db"
# Keep only last 7 daysfind $BACKUP_DIR -name "*.db" -mtime +7 -delete
echo "Backup complete: discord-forum_$DATE.db"Cron Job
# Edit crontabcrontab -e
# Add daily backup at 2 AM0 2 * * * /home/deploy/discord-forum-api/backup.shTroubleshooting
Service Won’t Start
# Check logspm2 logs forum-bot --lines 50# orjournalctl -u forum-bot -n 50
# Check if port is in usesudo lsof -i :3000High Memory Usage
# Check memoryfree -hpm2 monit
# Restart if neededpm2 restart forum-apiSSL Certificate Issues
# Check certificatesudo certbot certificates
# Force renewalsudo certbot renew --force-renewal