Self-Hosting
This guide covers self-hosting Discord Forum API on your own servers.
Architecture Overview
Discord Forum API consists of two services:
| Service | Purpose | Requirements |
|---|---|---|
| Bot | Connects to Discord, syncs content | Persistent connection (WebSocket) |
| API | Serves REST endpoints | HTTP server |
Both services share the same database.
┌─────────────┐ │ Discord │ └──────┬──────┘ │ WebSocket ┌──────▼──────┐ │ Bot │ └──────┬──────┘ │ Write ┌──────▼──────┐ │ Database │◄──── Read ────┐ │ (SQLite/ │ │ │ Turso) │ ┌──────┴──────┐ └─────────────┘ │ API │ └──────┬──────┘ │ HTTP ┌──────▼──────┐ │ Clients │ └─────────────┘Requirements
| Component | Minimum | Recommended |
|---|---|---|
| RAM | 512MB | 1GB |
| Storage | 1GB | 5GB+ |
| Node.js | 20.x | 20.x LTS |
| OS | Linux (any) | Ubuntu 22.04 |
Quick Deploy
-
Clone and build
Terminal window git clone https://github.com/KevinTrinh1227/discord-forum-api.gitcd discord-forum-apipnpm installpnpm build -
Configure environment
Terminal window cp .env.example .envnano .env # Edit with your values -
Initialize database
Terminal window pnpm db:push -
Start services
Terminal window # Start both bot and APInode packages/bot/dist/index.js &node packages/api/dist/index.js &
Production Setup with PM2
PM2 is recommended for production deployments.
-
Install PM2
Terminal window npm install -g pm2 -
Create ecosystem file
Create
ecosystem.config.js:module.exports = {apps: [{name: 'forum-bot',script: 'packages/bot/dist/index.js',env: {NODE_ENV: 'production',},instances: 1, // Bot must be single instanceautorestart: true,max_restarts: 10,restart_delay: 5000,},{name: 'forum-api',script: 'packages/api/dist/index.js',env: {NODE_ENV: 'production',},instances: 'max', // API can scaleexec_mode: 'cluster',autorestart: true,},],}; -
Start with PM2
Terminal window pm2 start ecosystem.config.js -
Enable startup on boot
Terminal window pm2 savepm2 startup -
Monitor processes
Terminal window pm2 statuspm2 logspm2 monit
PM2 Commands
| Command | Description |
|---|---|
pm2 status | View process status |
pm2 logs | View logs |
pm2 restart all | Restart all processes |
pm2 reload forum-api | Zero-downtime reload |
pm2 stop all | Stop all processes |
Reverse Proxy with Nginx
Set up Nginx as a reverse proxy for the API.
-
Install Nginx
Terminal window sudo apt install nginx -
Create configuration
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;}} -
Enable the site
Terminal window sudo ln -s /etc/nginx/sites-available/forum-api /etc/nginx/sites-enabled/sudo nginx -tsudo systemctl reload nginx -
Add SSL with Certbot
Terminal window sudo apt install certbot python3-certbot-nginxsudo certbot --nginx -d api.yourdomain.com
systemd Service (Alternative to PM2)
If you prefer systemd over PM2:
Bot Service
Create /etc/systemd/system/forum-bot.service:
[Unit]Description=Discord Forum BotAfter=network.target
[Service]Type=simpleUser=www-dataWorkingDirectory=/var/www/discord-forum-apiExecStart=/usr/bin/node packages/bot/dist/index.jsRestart=on-failureRestartSec=5Environment=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=www-dataWorkingDirectory=/var/www/discord-forum-apiExecStart=/usr/bin/node packages/api/dist/index.jsRestart=on-failureRestartSec=5Environment=NODE_ENV=production
[Install]WantedBy=multi-user.targetEnable Services
sudo systemctl daemon-reloadsudo systemctl enable forum-bot forum-apisudo systemctl start forum-bot forum-apiEnvironment Variables
Production .env example:
# DiscordDISCORD_TOKEN=your_bot_tokenDISCORD_CLIENT_ID=your_client_idDISCORD_CLIENT_SECRET=your_client_secret
# Database (Turso recommended for production)DATABASE_TYPE=tursoTURSO_DATABASE_URL=libsql://your-db.turso.ioTURSO_AUTH_TOKEN=your-token
# APINODE_ENV=productionAPI_PORT=3000CORS_ORIGIN=https://yourdomain.com
# OptionalLOG_LEVEL=infoSYNC_BOT_MESSAGES=trueProduction Checklist
Security
- Use HTTPS for all API traffic
- Set restrictive CORS origins
- Use a secrets manager for credentials
- Regular security updates
- Firewall configured (only expose port 443)
- Bot token rotated regularly
Performance
- Enable response caching
- Use CDN for static assets
- Monitor memory usage
- Set up database backups
- Configure log rotation
Monitoring
- Application logging configured
- Error tracking (Sentry, etc.)
- Uptime monitoring
- Resource alerts (CPU, memory, disk)
Maintenance
- Automated backups scheduled
- Update strategy documented
- Rollback plan ready
- On-call rotation (if applicable)
Scaling
Horizontal Scaling
For high availability:
| Component | Scaling Strategy |
|---|---|
| API | Multiple instances behind load balancer |
| Bot | Single instance only (Discord limitation) |
| Database | Use Turso for multi-region reads |
Database Considerations
For multi-instance API:
- SQLite: Not recommended (file locking issues)
- Turso: Recommended (handles concurrent access)
- PostgreSQL: Coming soon
Caching
The API includes built-in caching. For additional caching:
- Use Redis for shared cache across instances
- Configure CDN caching for public endpoints
- Adjust cache TTLs based on your needs
Troubleshooting
High memory usage
# Check current memorypm2 monit
# Increase Node.js heap sizeNODE_OPTIONS="--max-old-space-size=1024" pm2 restart forum-apiBot disconnecting
- Check network stability
- Verify token is valid
- Monitor Discord Status
- Review rate limiting in logs
API slow responses
- Enable caching if disabled
- Check database query performance
- Profile with
NODE_DEBUG=http - Add indexes if needed