How To Install MEAN Stack on Rocky Linux 9
The MEAN Stack—an acronym for MongoDB, Express.js, Angular, and Node.js—remains a favorite choice among web developers for building dynamic applications. It is completely JavaScript-based, meaning developers can write client-side, server-side, and database queries in a single language. This guide demonstrates how to install MEAN Stack on Rocky Linux 9, ensuring you have all the tools needed to build and deploy modern web applications. Whether you plan on using it for personal projects or production environments, this tutorial will walk you through each step of the process.
The MEAN Stack is an end-to-end JavaScript solution for developing robust and scalable web applications. Each component plays an integral role:
- MongoDB for the database layer
- Express.js as the backend web framework
- Angular for the frontend
- Node.js as the runtime environment
By leveraging a single programming language—JavaScript—across all layers of the application, teams can reduce context switching and streamline their development workflow. Rocky Linux 9, a community-driven distribution derived from the source code of Red Hat Enterprise Linux, offers stability, long-term support, and a robust package ecosystem suited for both production and testing environments.
In the following sections, you will learn best practices, prerequisites, and step-by-step instructions required to set up your own MEAN Stack. From preparing Rocky Linux 9 to verifying that each component is installed correctly, this comprehensive guide will ensure you can build, run, and secure MEAN-based applications smoothly.
Prerequisites
Before you install the MEAN Stack on Rocky Linux 9, it is important to confirm that your system meets the necessary requirements and that you possess the appropriate level of access. Here are the key prerequisites:
System Requirements
1. Hardware Specifications: Aim for at least 2 GB of RAM and a few gigabytes of storage space. Consider using more memory and disk space if you plan on running multiple applications or large-scale databases.
2. Operating System: This guide is specifically for Rocky Linux 9. Make sure your system is updated to the latest packages by running:
sudo dnf update -y
3. Sudo Privileges: Ensure you have root or sudo
access for installations and configurations.
Command Line Knowledge
A fundamental understanding of terminal commands on Linux is essential for this tutorial. You will need to edit files, update repositories, manage services, and install packages using the command line.
These prerequisites serve as the foundation of a successful MEAN Stack installation. Without meeting them, you may encounter unexpected errors or performance bottlenecks that can disrupt the setup process.
Installing Node.js
Node.js powers the server-side logic for your MEAN Stack-based applications, enabling developers to write JavaScript on the backend. Follow this step-by-step process to install Node.js on Rocky Linux 9:
Setting Up NodeSource Repository
Rocky Linux 9 may not have the latest Node.js packages in its default repositories. To ensure you get a recent version, add the NodeSource repository:
curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -
Replace 16.x
with the Node.js version you prefer if newer versions are available.
Updating System Packages
To make the newly added repository effective, update your system packages:
sudo dnf update -y
Installing Node.js and npm
Once the repositories are up to date, install Node.js and npm using:
sudo dnf install -y nodejs
Verifying Installation
Confirm that Node.js and npm were installed successfully:
node -v
npm -v
You should see version numbers that match or exceed the version from the NodeSource repository. This ensures that your Node.js environment is ready to handle backend logic, package installation, and local development servers for Express and Angular applications.
If you encounter any errors at this stage, check your repository setup, ensure your system is connected to the internet, and confirm that you used the right version of the NodeSource script for Rocky Linux 9.
MongoDB Installation
MongoDB is a popular NoSQL database that stores data in JSON-like documents. It offers high performance, scalability, and flexibility—making it ideal for modern web applications. Here’s how to install MongoDB on your Rocky Linux 9 system:
Repository Configuration
Rocky Linux 9 does not include MongoDB in its default repositories, so you need to create a MongoDB repository file:
sudo nano /etc/yum.repos.d/mongodb-org.repo
Paste the following lines into the file (adjust the version number if a newer release is available):
[mongodb-org-6.0]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/6.0/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-6.0.asc
Then save and exit the file.
Installing MongoDB Packages
Run the following commands to install MongoDB:
sudo dnf update -y
sudo dnf install -y mongodb-org
This command installs the MongoDB server, shell, and related tools.
Starting and Enabling MongoDB
Enable the MongoDB service to automatically start at boot:
sudo systemctl start mongod
sudo systemctl enable mongod
Configuring Firewall
If you plan to access MongoDB remotely or host multiple services, make sure to configure the Rocky Linux 9 firewall to allow incoming connections on MongoDB’s default port (27017). For a secure environment, limit external access unless necessary:
sudo firewall-cmd --permanent --add-port=27017/tcp
sudo firewall-cmd --reload
Verifying MongoDB Status
Confirm that MongoDB is running without issues:
sudo systemctl status mongod
If the status indicates it is active (running), MongoDB is ready for use. In case you run into errors, check the journal logs using journalctl -u mongod
for information on any missing dependencies or configuration issues.
By completing these steps, you have successfully installed MongoDB on Rocky Linux 9. Next, we will cover installing Angular CLI, which allows you to efficiently create and scale client-side projects in the MEAN architecture.
Installing Angular CLI
Angular CLI simplifies the process of creating, building, servicing, and testing Angular applications. Its consistent structure helps streamline development, especially for large-scale projects. To install Angular CLI globally, follow these steps:
Global Installation via npm
sudo npm install -g @angular/cli
This command ensures Angular CLI is available globally. If you prefer a local project installation, you can omit the -g
flag. However, installing Angular CLI globally provides a convenient environment for quickly scaffolding new projects.
Version Verification
Confirm that the installation was successful:
ng version
The output should display the installed Angular CLI version along with details about your Node.js and npm versions.
Common Installation Troubleshooting
- If you encounter errors about permissions, try using
sudo
or adjusting file ownership on the npm global directory. - Ensure that you have sufficient disk space and memory, especially if you plan to work on multiple Angular projects concurrently.
Testing Angular Installation
Generate a new Angular app to validate the installation:
ng new test-app
cd test-app
ng serve --open
If your default browser launches the Angular application on http://localhost:4200/
, it confirms a successful setup. With Angular CLI in place, you’re ready to build and manage the MEAN Stack frontend.
Express.js Setup
Express.js is the heart of your application’s server-side logic, providing routing, middleware, and a range of features to handle HTTP requests seamlessly. Below is how to integrate Express.js into your environment:
Project Initialization
Create a new project directory to keep your files organized:
mkdir mean-project
cd mean-project
This folder will house your server code and potentially your Angular build artifacts later.
Initializing npm Project
Initialize a package.json
file:
npm init -y
This file contains metadata such as dependencies and scripts necessary for your Express.js project.
Installing Express.js
npm install express --save
Adding Express.js to your project ensures you can start coding your routes and middleware right away.
Basic Express.js Configuration
Create a file named app.js
(or server.js
) to define a simple server:
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello World from Express!');
});
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});
Run the application using node app.js
and navigate to http://localhost:3000
to see the “Hello World from Express!” message.
This confirms that Express.js is installed and functioning correctly within your local environment.
Configuration and Integration
To unify the MEAN Stack components, you must configure Node.js, Express.js, Angular, and MongoDB to communicate effectively. This integration allows you to build dynamic, data-driven apps.
Environment Setup
Use environment variables to store configuration data like database connection strings and secret keys. Node.js offers various packages (e.g., dotenv
) to set up environment variables securely:
npm install dotenv
require('dotenv').config();
Configuring MongoDB Connection
Within app.js
or a dedicated database configuration file, add the following:
const mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true
}).then(() => {
console.log('Connected to MongoDB');
}).catch(err => {
console.error('MongoDB connection error:', err);
});
Set MONGO_URI
in your .env
file (e.g., MONGO_URI=mongodb://localhost:27017/mean-stack
).
Integrating Express with Angular
By default, Angular’s project will run separately. Once you build the Angular application for production, copy or move its distribution files into your Express server’s public
folder or a dedicated folder you designate for static files. Express can then serve the compiled frontend, providing a seamless user experience.
Perform a quick test to confirm all four components (Node.js, Express, Angular, MongoDB) interact as expected before proceeding to advanced configurations or production deployments.
Testing the Installation (Approx. 150 words)
Verifying that each MEAN Stack component is communicating correctly is crucial for spotting configuration errors early. Here is a basic approach:
Verification Steps
- Start your Express server using
node app.js
. Make sure the console logs indicate the server is running. - Open a browser and navigate to
http://localhost:3000
. You should see the “Hello World” message (or a similar output). - Open your Angular app by running
ng serve
in the Angular project folder. Access the app athttp://localhost:4200
. - Confirm the Angular application can send requests to the Express server by creating a simple route and checking if the response is displayed on the frontend.
- Use MongoDB either through a shell or an API endpoint in your Express application to insert and retrieve sample data.
After successful testing, you know the fundamental pieces of the MEAN Stack are operational on Rocky Linux 9.
Security Considerations
Securing your MEAN Stack installation on Rocky Linux 9 is essential. Here are a few foundational measures:
Firewall Configuration
Only open necessary ports for traffic, such as 3000 (Express), 4200 (Angular dev server), and 27017 (MongoDB).
SELinux Settings
By default, Rocky Linux provides SELinux in enforcing mode. Check for any SELinux denials and consider creating custom policies if your application requires them.
Secure Authentication
Use strong passwords for the MongoDB database. In production, enable username-password authentication in MongoDB by configuring /etc/mongod.conf
and creating an admin user.
Access Control Setup
For both the Angular frontend and Express backend, ensure that you properly handle tokens, sessions, or other authentication mechanisms to protect sensitive routes and data transfers.
Security is an ongoing process. Continuously update packages and apply patches to minimize vulnerabilities.
Troubleshooting Guide (Approx. 150 words)
Even carefully executed setups can face issues. Below are some common problems and solutions:
Permission Errors
If you encounter “EACCES” or “Permission denied” during installations, prefix commands with sudo
, adjust file ownership, or update access permissions on the npm directories as needed.
Connectivity Problems
When Angular cannot communicate with Express, verify that the correct URLs are being used in your environment files. For issues connecting to MongoDB, check that the MongoDB service is active and that your firewall settings allow traffic on port 27017.
Version Compatibility
Sometimes older versions of MongoDB or Node.js may cause conflicts. Ensure your packages are up to date and that your package.json
dependencies are compatible.
Service Startup Problems
If mongod
or Node.js processes fail to start, review logs with journalctl -u mongod
or systemctl status mongod
for hints on missing dependencies or misconfigurations.
Congratulations! You have successfully installed MEAN Stack. Thanks for using this tutorial for installing MEAN Stack on Rocky Linux 9 system. For additional help or useful information, we recommend you check the Ubuntu website.