Server Deployment Guide
Use this comprehensive guide to configure your Ubuntu 24.04 server for production.
Prerequisites: Ubuntu Server 24.04, Root Access, and a registered domain name.
Update System
Start by updating your server’s package list to the latest version.
sudo apt update && sudo apt upgrade -yInstall Node.js (via NVM)
We use Node Version Manager (NVM) to install and manage Node.js.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install 20 && nvm use 20 && nvm alias default 20Setup Project Directory
Navigate to the web root and clone your project repository.
cd /var/www
sudo git clone <your-repo-url> project
cd project && npm installConfigure PM2 (Process Manager)
Keep your application running in the background.
npm install -g pm2
pm2 start index.js --name "dramavie-server"
pm2 startup && pm2 saveInstall & Configure Nginx
Set up Nginx as a reverse proxy to direct traffic to your Node.js app.
1. Install Nginx:
sudo apt install nginx -y2. Create Config File:
sudo nano /etc/nginx/sites-available/backend3. Paste Configuration:
server {
listen 80;
server_name backend.yourdomain.com;
location / {
proxy_pass http://127.0.0.1:3006;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}4. Activate Site:
sudo ln -s /etc/nginx/sites-available/backend /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginxSecure with SSL (Certbot)
Encrypt your traffic using a free Let’s Encrypt certificate.
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d backend.yourdomain.comInstall MongoDB 7.0
Add the official MongoDB repository and install the database engine.
curl -fsSL https://pgp.mongodb.com/server-7.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
echo "deb [ 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
sudo apt update && sudo apt install -y mongodb-org
sudo systemctl start mongod && sudo systemctl enable mongodSecure MongoDB
Create an admin user and enable authorization.
1. Create User via mongosh:
use admin
db.createUser({ user: "admin", pwd: "YourStrongPassword", roles: ["root"] })
exit2. Enable Authorization:
Edit config: sudo nano /etc/mongod.conf
security:
authorization: enabled3. Restart Service:
sudo systemctl restart mongod
sudo ufw allow 27017Connection String
Use this string in your .env file to connect the API to MongoDB.
MONGO_URI=mongodb://admin:YourStrongPassword@your-server-ip:27017/yourdbname?authSource=admin