ArcHive requires MongoDB for persistent data storage and Redis for caching and job queues.
MongoDB Setup
MongoDB stores all application data including users, collections, and content items.
Install MongoDB 7.0
Import MongoDB GPG Key
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
Add MongoDB Repository
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | \
sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
Update and Install
sudo apt update
sudo apt install -y mongodb-org
Start MongoDB
sudo systemctl start mongod
sudo systemctl enable mongod
Verify Status
sudo systemctl status mongod
Create Database and Users
Always use strong, unique passwords in production. Never use the example passwords shown here.
Create Admin User
use admin
db . createUser ({
user: "admin" ,
pwd: "YOUR_STRONG_PASSWORD" , // Change this!
roles: [ { role: "userAdminAnyDatabase" , db: "admin" } ]
})
Create ArcHive Database User
use archive
db . createUser ({
user: "archiveuser" ,
pwd: "YOUR_ARCHIVE_DB_PASSWORD" , // Change this!
roles: [ { role: "readWrite" , db: "archive" } ]
})
Enable MongoDB Authentication
Edit MongoDB Configuration
sudo nano /etc/mongod.conf
Add Security Settings
Add or modify these lines: security :
authorization : enabled
net :
bindIp : 127.0.0.1
port : 27017
Save and Exit
Press Ctrl+X, then Y, then Enter
Restart MongoDB
sudo systemctl restart mongod
Test MongoDB Connection
# Test with credentials
mongosh -u archiveuser -p YOUR_ARCHIVE_DB_PASSWORD --authenticationDatabase archive
You should successfully connect to the archive database.
Connect to MongoDB and create text search indexes:
mongosh -u archiveuser -p YOUR_ARCHIVE_DB_PASSWORD --authenticationDatabase archive
use archive
// Create text index for full-text search
db . contentitems . createIndex ({
title: "text" ,
description: "text" ,
content: "text" ,
url: "text"
})
// Verify indexes
db . contentitems . getIndexes ()
exit
Indexes are typically created automatically by Mongoose models, but you can create them manually if needed.
Redis Setup
Redis is used for caching and as a job queue for BullMQ background workers.
Install Redis
Install Redis Server
sudo apt install -y redis-server
Start Redis
sudo systemctl start redis-server
sudo systemctl enable redis-server
Verify Status
sudo systemctl status redis-server
Edit Redis Configuration
sudo nano /etc/redis/redis.conf
Modify Settings
Find and modify these lines: # Bind to localhost only
bind 127.0.0.1 ::1
# Use systemd for supervision
supervised systemd
# Set maximum memory (adjust based on your VPS RAM)
maxmemory 512mb
# Eviction policy when max memory is reached
maxmemory-policy allkeys-lru
# Set password (optional but recommended)
requirepass YOUR_REDIS_PASSWORD
Save and Restart
# Save file (Ctrl+X, Y, Enter)
sudo systemctl restart redis-server
Setting a Redis password is highly recommended for production deployments, even when binding to localhost.
Test Redis Connection
# Test without password
redis-cli ping
# Should return: PONG
# If you set a password:
redis-cli
auth YOUR_REDIS_PASSWORD
ping
# Should return: PONG
Redis Memory Configuration
For a 4GB VPS, recommended Redis memory settings:
VPS RAM Redis maxmemory 4 GB 512 MB 8 GB 1 GB 16 GB 2 GB
Database Connection Strings
Use these connection strings in your .env file:
MongoDB Connection String
MONGODB_URI = mongodb://archiveuser:YOUR_ARCHIVE_DB_PASSWORD@localhost:27017/archive? authSource = archive
Redis Connection
Without Password
With Password
REDIS_HOST = localhost
REDIS_PORT = 6379
Database Maintenance
MongoDB Backup Script
Create an automated backup script:
#!/bin/bash
BACKUP_DIR = "/home/archive-api/backups/mongodb"
TIMESTAMP = $( date +"%Y%m%d_%H%M%S" )
MONGODB_USER = "archiveuser"
MONGODB_PASS = "YOUR_ARCHIVE_DB_PASSWORD"
MONGODB_DB = "archive"
# Create backup directory
mkdir -p $BACKUP_DIR
# Create backup
mongodump --username= $MONGODB_USER --password= $MONGODB_PASS \
--authenticationDatabase= $MONGODB_DB --db= $MONGODB_DB \
--out= $BACKUP_DIR / $TIMESTAMP
# Compress backup
tar -czf $BACKUP_DIR /archive_ $TIMESTAMP .tar.gz -C $BACKUP_DIR $TIMESTAMP
rm -rf $BACKUP_DIR / $TIMESTAMP
# Keep only last 7 days of backups
find $BACKUP_DIR -name "archive_*.tar.gz" -mtime +7 -delete
echo "Backup completed: $BACKUP_DIR /archive_ $TIMESTAMP .tar.gz"
Make executable and schedule:
chmod +x /root/backup-mongodb.sh
# Add to crontab (daily at 2 AM)
crontab -e
# Add this line:
0 2 * * * /root/backup-mongodb.sh >> /var/log/mongodb-backup.log 2>&1
Restore MongoDB Backup
# Extract backup
tar -xzf /path/to/backup/archive_20240315_020000.tar.gz
# Restore
mongorestore --username=archiveuser --password=YOUR_ARCHIVE_DB_PASSWORD \
--authenticationDatabase=archive --db=archive \
/path/to/extracted/backup/archive
Monitoring Databases
Check MongoDB Status
# Service status
sudo systemctl status mongod
# View logs
sudo tail -f /var/log/mongodb/mongod.log
# Check database size
mongosh -u archiveuser -p YOUR_ARCHIVE_DB_PASSWORD --authenticationDatabase archive --eval "db.stats()"
Check Redis Status
# Service status
sudo systemctl status redis-server
# View logs
sudo tail -f /var/log/redis/redis-server.log
# Check memory usage
redis-cli info memory
# Monitor in real-time
redis-cli --stat
Troubleshooting
MongoDB Connection Refused
# Check if MongoDB is running
sudo systemctl status mongod
# Restart MongoDB
sudo systemctl restart mongod
# Check logs for errors
sudo tail -f /var/log/mongodb/mongod.log
# Verify port is listening
sudo netstat -tulpn | grep :27017
Redis Connection Issues
# Check if Redis is running
sudo systemctl status redis-server
# Restart Redis
sudo systemctl restart redis-server
# Test connection
redis-cli ping
# Check logs
sudo tail -f /var/log/redis/redis-server.log
Authentication Failed
# Verify user exists
mongosh
use admin
db.system.users.find ()
# Reset user password if needed
use archive
db.updateUser( "archiveuser" , { pwd: "NEW_PASSWORD" } )
Next Steps
Backend Deployment Deploy the Node.js backend with PM2
Configuration Configure environment variables