Installing the MERN stack (MongoDB, Express.js, React, Node.js) on AlmaLinux is an excellent choice for VPS hosting. It offers several advantages over the LEMP stack (Linux, NGINX, MariaDB, PHP), especially for modern web applications. With JavaScript across the entire stack, MERN simplifies development and enhances scalability for dynamic, real-time applications.
React, a leading front-end library, excels in building dynamic, single-page applications (SPAs) with efficient, component-based architectures. Node.js, with its non-blocking, event-driven design, delivers high performance for handling numerous concurrent connections, making it ideal for real-time apps. MongoDB, a flexible NoSQL database, integrates smoothly with JavaScript, unlike traditional SQL databases found in the LEMP stack. There are a few alternatives to React including, Vue.js and Next.js
AlmaLinux, an open-source Linux distribution, is a stable and enterprise-grade alternative to CentOS, built to be binary-compatible with RHEL. By combining the reliability of AlmaLinux with the modern, scalable MERN stack, you can build a powerful hosting environment that supports applications from small-scale projects to large, high-traffic platforms.
Scaling your MERN-based VPS to handle increased traffic is simple, allowing you to support a wide range of use cases, from blogs to complex web applications. Start building your MERN stack VPS by following our guide, and enjoy the benefits of this cutting-edge, full-stack JavaScript solution.
1. To begin login to your server, with the credentials provided in your welcome email using PuTTY or a terminal window connect with the servers IP or the hostname.
2. This guide is based on a new VPS with AlmaLinux 8 installed. Make sure that all of the installed packages are updated using dnf using the command below.
dnf update -y
3. To allow traffic to reach NGINX through the firewall you need to open connections over ports 80 (non-SSL) and 443 (SSL) you can check which connections are currently open with the following command.
firewall-cmd --zone=public --list-services
3.1 The following screenshot confirms that by default your firewall is not configured to allow connections of port 80 or 443. Run the following commands to enable traffic over those ports and reload the firewall.
firewall-cmd –permanent -–zone=public -–add-service=http
firewall-cmd –permanent -–zone=public -–add-service=https
firewall-cmd -–reload
3.2 Now, to check the current status of those ports in the firewall run the command from earlier again which should return the following output.
firewall-cmd --zone=public --list-services
1. To start install FNM (Fast Node Manager) using the following command. For more information check out node.js.org.
curl -fsSL https://fnm.vercel.app/install | bash
2. With FNM installed you need to reload the shell to enable the FNM command, to do this use the command below.
source ~/.bashrc
3. Moving on install the latest LTS (long-term support) version of Node.js, set it as the default and check the version using the commands below.
fnm install --lts
fnm default --20.17.0
node -v
1. First off because MongoDB does not exist by default in AlmaLinux’s default package repositories we will need to manually create the .repo file using nano and add the code shown below.
nano /etc/yum.repos.d/mongodb-org-5.0.repo
[mongodb-org-5.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/8/mongodb-org/5.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-5.0.asc
2. After that’s added we can run dnf to install MongoDB on your VPS.
dnf install -y mongodb-org
3. Finally, start and enable MongoDB, then verify its working with the status command.
systemctl start mongod
systemctl enable mongod
systemctl status mongod
1. To start we need to create a directory and then use the change directory command to enter that directory.
mkdir ~/my-mern-app
cd ~/my-mern-app
2. Moving on you need to initialise the Node.js application with the following command.
npm init -y
3. After that we can install Express.js with node package manager or npm with the command below.
npm install express
4. Now that Express is installed we need to add a server.js file in the my-mern-app directory you made earlier and add the code below.
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', {
useNewUrlParser: true,
useUnifiedTopology: true,
}).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}`);
});
5. Finally start Express.js with the command below.
node server.js
1. To start off install create react app app and use CD to enter your app directory.
npx create-react-app client
cd client
2. Once you have finished your project you can build it with the following command.
npm run build