The MERN stack combines MongoDB, Express.js, React and Node.js into a unified JavaScript environment for building modern web applications. Running JavaScript across your entire stack simplifies development, reduces context switching and makes it easier for teams to maintain codebases.
You will install each component of the MERN stack on AlmaLinux, configure firewall rules to allow web traffic and create a basic Express server connected to MongoDB. Once complete, you will have a working development environment ready for building full-stack applications.
Before installing the MERN stack components, you need to update your system packages and configure firewall rules. This ensures your server has the latest security patches and can accept incoming web traffic.
ssh root@your-server-ip replacing your-server-ip with your actual server address.-y flag skips confirmation prompts.dnf update -y
firewall-cmd --zone=public --list-services
By default, AlmaLinux does not allow HTTP or HTTPS traffic. You need to add these services to accept web connections.
--permanent flag makes the changes persist after reboot.firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload
firewall-cmd --zone=public --list-services
Your server now accepts web traffic and has up-to-date packages. You can proceed with installing Node.js.
Node.js powers the backend of your MERN stack and provides the npm package manager for installing dependencies. Installing from the NodeSource repository gives you the latest Long Term Support version recommended for production environments.
curl -fsSL https://rpm.nodesource.com/setup_lts.x | bash -
dnf install -y nodejs
node -v ; npm -v
Node.js and npm are now installed and ready to run JavaScript applications. Next, you will install MongoDB to handle data storage.
MongoDB provides the database layer for your MERN stack. AlmaLinux does not include MongoDB in its default repositories, so you need to add the official MongoDB repository before installation.
nano /etc/yum.repos.d/mongodb-org-7.0.repo
[mongodb-org-7.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/8/mongodb-org/7.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-7.0.asc
Save the file by pressing Ctrl+O, then press Enter. Exit nano by pressing Ctrl+X.
dnf install -y mongodb-org
systemctl start mongod
systemctl enable mongod
systemctl status mongod
MongoDB is now running and will start automatically after system reboots. You can connect to it using the mongosh shell to manage databases. Next, you will set up Express.js to handle HTTP requests.
Express.js provides a minimal framework for building web servers with Node.js. You will create a basic Express application that connects to MongoDB and responds to HTTP requests.
mkdir ~/my-mern-app
cd ~/my-mern-app
package.json file with default settings. This file tracks your project dependencies and configuration.npm init -y
npm install express mongoose
server.js in nano. This file will contain your Express application code.nano server.js
const express = require('express');
const mongoose = require('mongoose');
const app = express();
const PORT = process.env.PORT || 5000;
// Middleware
app.use(express.json());
// Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/mydatabase')
.then(() => {
console.log('MongoDB connected');
})
.catch(err => {
console.error('MongoDB connection error:', err);
});
// Sample Route
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
Save and exit nano using Ctrl+O, Enter, then Ctrl+X.
node server.js
Your Express server is now running and connected to MongoDB. Press Ctrl+C to stop the server when you finish testing. Next, you will set up React for the frontend.
React provides the frontend layer of your MERN stack. Create React App sets up a complete React development environment with build tools and configuration already in place.
npx create-react-app client
cd client
npm start
npm run build
Your React application is now set up and ready for development. The production build creates optimised files suitable for deployment.
The MERN stack offers flexibility beyond basic installations. Understanding how each component interacts helps you build more robust applications and troubleshoot issues effectively.
MongoDB’s document-based structure differs significantly from traditional relational databases. The MongoDB data modelling guide explains how to design schemas that take advantage of MongoDB’s flexible document format. For production deployments, consider implementing replica sets to ensure data availability and automatic failover.
Express.js middleware forms the backbone of request processing in your application. The Express middleware documentation covers how to chain middleware functions for authentication, logging and error handling. For larger applications, structuring your Express routes and controllers properly prevents code from becoming difficult to maintain.
React’s component architecture requires careful planning as applications grow. The Thinking in React guide from the official documentation explains how to break down user interfaces into reusable components. State management becomes more complex in larger applications. Consider installing Yarn as an alternative package manager if you need faster dependency resolution or better monorepo support.
Production MERN deployments benefit from process managers and reverse proxies. Setting up NGINX as a reverse proxy in front of your Node.js application improves performance and enables SSL termination. PM2 keeps your Node.js processes running and restarts them automatically if they crash.
You have installed all four components of the MERN stack on AlmaLinux. MongoDB stores your application data, Express.js handles HTTP requests, React provides the user interface and Node.js powers the entire stack. Your development environment is ready for building full-stack JavaScript applications.
Test your setup by running the Express server and React development server simultaneously. Make sure MongoDB connects successfully and your React application can communicate with the Express API. For production deployments, configure a process manager to keep your Node.js application running and set up NGINX as a reverse proxy. Our VPS hosting provides the resources and control you need for running MERN stack applications.
Get scalable resources with our VPS hosting with root access and optional software.
Get VPS HostingPerfect for websites and small businesses unlimited bandwidth with cPanel hosting.
Get cPanel Hosting