Deploying a MERN Stack Application on Linux (Step-by-Step Guide)

Deploying a MERN Stack Application on Linux (Step-by-Step Guide)

Most of my MERN stack deployments have been on Platform as a Service (PaaS) solutions such as Heroku, Netlify, and more recently Render. These services provide seamless GitHub integration, built-in CI/CD, and take care of infrastructure so we can focus purely on writing code, which is fantastic!

But what happens when, due to project requirements or learning goals, you need to deploy everything from scratch? Running your application in a home lab, on internal company servers, or on a virtual machine in the cloud gives you full control over updates, security configurations, and scalability. More importantly, it strengthens your DevOps skills and helps you understand what’s happening behind the PaaS “magic.”

Assuming you have an Ubuntu server.

Prepare your server:

1 - First, update the packages and install what you need:

sudo apt update && sudo apt upgrade -y
sudo apt install git curl build-essential -y        

2 - Install Node.js and npm (or yarn):

curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
node -v
npm -v        

You will see a screen similar to this, the last two lines correspond to the versions of node and npm that were installed.

Article content
Terminal linux ubuntu screen

3 - Install MongoDB Community Edition (if running locally) otherwise if you using the MongoDB Atlas, you can skip this step and just configure the connection in the backend:

3.1 - Import the public key.

From a terminal, install gnupg and curl if they are not already available:sudo apt install -y mongodb

sudo apt-get install gnupg curl        

3.2 - To import the MongoDB public GPG key, run the following command:

curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | \
   sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg \
   --dearmor        

3.3 - Create the list file:

Create the list file /etc/apt/sources.list.d/mongodb-org-8.0.list for your version of Ubuntu 22.04 (Jammy):

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list        

3.4 - Reload the package database.

Issue the following command to reload the local package database:

sudo apt-get update        

You will see a screen similar to this, we now have mongo repositories on our Linux, which means we can install mongo packages.

Article content

3.5 - Install MongoDB Community Server

To install the latest stable version, issue the following

sudo apt-get install -y mongodb-org        
sudo systemctl start mongod        
sudo systemctl status mongod        

After executing the status command you should receive the return like this, active and running.

Article content

every time our server is restarted, I would like mongo to start the service and get it working again automatically, this is this command

sudo systemctl enable mongod        

4 - Clone your project:

git clone https://github.com/richardnixondev/deploy-linux.git        

Frontend Setup (React)

5 - Enter the frontend folder, install the dependencies and run build:

cd frontend        
npm install        
npm run build        

the build command, will generate a dist/ or build/ folder with the static files.


Backend Setup

6- Clone the project repository and install the dependencies:

cd backend        
npm install        

6.1 - Create the environment variables (e.g. .env):

cat <<EOF > .env
PORT=3000
MONGO_URI=mongodb://localhost:27017/db_name
JWT_SECRET=supersecretpassword
EOF        

6.2 - Test locally:

node server.js        


Setup Process Manager (PM2)

Using a process manager like PM2 is essential when deploying a MERN stack application on Linux. It ensures that your Node.js server runs continuously in the background, automatically restarts on crashes or reboots, and provides monitoring and log management. This improves reliability, scalability, and makes the deployment production-ready.

sudo npm install -g pm2        
pm2 start server.js --name "mern-app"        
Article content

The startup command ensures that the app restarts automatically after reboot.

pm2 startup        

You now have a fully functional MERN stack deployed on Linux, running with PM2 for stability and scalability. From here, you can enhance security with Nginx as a reverse proxy, add HTTPS with Certbot, and scale horizontally with Docker or Kubernetes.

Article content

#MERNStack #Linux #WebDevelopment #FullStackDeveloper #NodeJS #MongoDB #ReactJS #DevOps #PM2 #BackendDevelopment #FrontendDevelopment #CloudComputing

To view or add a comment, sign in

More articles by Richard Nixon

Others also viewed

Explore content categories