Deployment & Maintenance
This document outlines the environment setup, deployment workflow, backup, recovery, and rollback procedures for the MERN application.
Environment Setup
To prepare the server for the MERN application, you need to install Node.js, MongoDB, and PM2.
1. Install Node.js
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt-get install -y nodejs
# Verify installation
node --version
npm --version2. Install MongoDB
If you are hosting your own database rather than using MongoDB Atlas, install MongoDB:
sudo apt-get update
sudo apt-get install -y mongodb
sudo systemctl start mongodb
sudo systemctl enable mongodb3. Install PM2
PM2 is a production process manager for Node.js.
sudo npm install -g pm2Deployment Workflow
1. Initial Server Configuration (Ubuntu)
Create a new user, add them to the sudo group, and configure SSH key authentication for enhanced security. Disable password authentication in /etc/ssh/sshd_config and restart the SSH service.
2. Get Files on the Server
Clone the repository to your server:
mkdir sites
cd sites
git clone <your-repository-url>
cd <repository-directory>3. App Setup (.env and dependencies)
Create and configure your .env file for production (e.g., set NODE_ENV=production and your MONGO_URI).
Install dependencies for the backend and frontend:
npm install
cd frontend
npm install
npm run build
cd ..4. Run the App with PM2
Start your backend server with PM2 to keep it running in the background:
pm2 start backend/server.js --name "mern-app"
pm2 save
pm2 startup5. Reverse Proxy (NGINX) and Firewall
Enable the ufw firewall, allowing OpenSSH, HTTP, and HTTPS traffic.
Install NGINX:
sudo apt install nginxConfigure NGINX to proxy requests to your Node.js application running on localhost:5000 (or your chosen port) and restart NGINX.
6. SSL Configuration (Let's Encrypt)
To secure your site with HTTPS, install Certbot and generate an SSL certificate:
sudo apt-get install python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.comBackup Procedures
It is crucial to maintain weekly manual backups of your database to prevent data loss.
Run the mongodump command to create a binary export of the database:
# Create a backup archive
mongodump --uri="<your_mongo_uri>" --archive="backup-$(date +%F).archive" --gzipStore these backup files securely off-site (e.g., an S3 bucket or external server) on a weekly basis.
Recovery Steps
If data loss or corruption occurs, use the mongorestore process to restore your database from a backup archive.
# Restore the database from the compressed archive
mongorestore --uri="<your_mongo_uri>" --archive="backup-YYYY-MM-DD.archive" --gzip --drop(Note: The --drop flag drops existing collections before restoring them. Use with caution.)
Rollback Procedures
If a new deployment introduces critical bugs, follow these steps to roll back to the previous stable version:
-
Revert the code repository:
cd <repository-directory> git checkout <previous-stable-commit-hash> -
Re-install dependencies (if package.json changed):
npm install cd frontend npm install -
Rebuild the frontend:
npm run build cd .. -
Restart the PM2 process:
pm2 restart "mern-app" -
Verify the application: Test the endpoints and frontend to ensure the rollback was successful.