How to install the MERN stack on AlmaLinux

By Angus Published 3 October 2024 Updated 4 March 2026 8 min read

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 you begin

  • You need root access to an AlmaLinux VPS.
  • Your server should have at least 2GB RAM for running MongoDB and Node.js applications.
  • We recommend updating all system packages before installing new software.

Prepare your VPS

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.

  1. Connect to your server.
    Use SSH to connect with the credentials from your welcome email. On Windows, use PuTTY. On macOS or Linux, open Terminal and run ssh root@your-server-ip replacing your-server-ip with your actual server address.
  2. Update system packages.
    Run the following command to update all installed packages. The -y flag skips confirmation prompts.
dnf update -y
  1. Check current firewall services.
    View which services are currently allowed through the firewall. This shows whether HTTP and HTTPS traffic can reach your server.
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.

  1. Open HTTP and HTTPS ports.
    Run these commands to allow traffic on ports 80 and 443. The --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
  1. Verify the firewall configuration.
    Run the status command again to confirm HTTP and HTTPS now appear in the output.
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.

Install 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.

  1. Add the NodeSource repository.
    This command downloads and runs the NodeSource setup script, which configures the repository for the latest LTS version of Node.js.
curl -fsSL https://rpm.nodesource.com/setup_lts.x | bash -
  1. Install Node.js and npm.
    Install both Node.js and npm in a single command. npm comes bundled with Node.js and manages JavaScript packages.
dnf install -y nodejs
  1. Verify the installation.
    Check which versions of Node.js and npm are installed. This confirms the installation completed successfully.
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.

Install MongoDB

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.

  1. Create the MongoDB repository file.
    Open a new repository configuration file in the nano text editor. This tells dnf where to download MongoDB packages.
nano /etc/yum.repos.d/mongodb-org-7.0.repo
  1. Add the repository configuration.
    Paste the following configuration into the file. This points to MongoDB’s official repository and enables GPG signature verification for security.
[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.

  1. Install MongoDB.
    Use dnf to install MongoDB from the repository you just added.
dnf install -y mongodb-org
  1. Start and enable MongoDB.
    Start the MongoDB service immediately and configure it to start automatically when your server boots.
systemctl start mongod
systemctl enable mongod
  1. Verify MongoDB is running.
    Check the service status to confirm MongoDB started correctly. Look for active (running) in the output.
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.

Create an Express server

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.

  1. Create a project directory.
    Make a new directory for your application and navigate into it. This keeps your project files organised.
mkdir ~/my-mern-app
cd ~/my-mern-app
  1. Initialise a Node.js project.
    Create a package.json file with default settings. This file tracks your project dependencies and configuration.
npm init -y
  1. Install Express and Mongoose.
    Install Express.js for the web server and Mongoose for MongoDB integration. Mongoose provides a schema-based solution for modelling application data.
npm install express mongoose
  1. Create the server file.
    Open a new file called server.js in nano. This file will contain your Express application code.
nano server.js
  1. Add the server code.
    Paste the following code into the file. This creates an Express server that connects to MongoDB and responds to requests on the root path.
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.

  1. Start the Express server.
    Run your server with Node.js. You should see confirmation messages that MongoDB connected and the server is listening on port 5000.
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.

Install and build React

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.

  1. Create a React application.
    Use npx to run Create React App without installing it globally. This creates a new React project in a directory called client.
npx create-react-app client
  1. Navigate to the client directory.
    Change into the React application directory to run development and build commands.
cd client
  1. Start the development server.
    Run the React development server to test your application. This starts a local server on port 3000 with hot reloading enabled.
npm start
  1. Build for production.
    When you finish developing your application, create an optimised production build. This generates static files in the build directory that you can deploy to a web server.
npm run build

Your React application is now set up and ready for development. The production build creates optimised files suitable for deployment.

Further reading on the MERN stack

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.

Wrapping up

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.

Need more power?

Get scalable resources with our VPS hosting with root access and optional software.

Get VPS Hosting

Starting something new?

Perfect for websites and small businesses unlimited bandwidth with cPanel hosting.

Get cPanel Hosting