MEAN Stack is a free, open-source JavaScript software stack used to build dynamic web applications and websites. It combines four powerful technologies:
- MongoDB – NoSQL database
- Express.js – A Backend web application framework
- Angular – Frontend JavaScript framework
- Node.js – Runtime for executing JavaScript on the server
Together, they provide a full-stack JavaScript development environment, with Angular handling the frontend and Node.js, Express, and MongoDB powering the backend.
This guide walks you through installing and configuring the MEAN stack on a fresh Ubuntu 20.04 VPS.
Requirements:
- A VPS with a freshly installed Ubuntu 20.04 OS
- Root access
Update System Packages
Step 1: Update the Packages
First, log in to the VPS as root and run the following command to update each outdated package, as well as dependencies in your VPS
# apt-get update -y
Install MongoDB
Step 2: Installing MongoDB
By default, the latest MongoDB package is already added to the Ubuntu repository. So, to install MongoDB, you can simply run the following command:
# apt-get install mongodb -y
Next, to start the MongoDB service and make it start automatically when the system reboots, run the following commands:
# systemctl start mongodb
Install Node.js and Dependencies
Step 3: Installing Node.js
Before we proceed further, we will install the dependencies with the following command:
# apt-get install make unzip gcc git curl gnupg2 g++ -y
Unlike MongoDB, the Ubuntu repository doesn’t come with Node.js. So, you can add the Node.js repository in your system using the following command:
# curl -sL https://deb.nodesource.com/setup_14.x | bash -
We have now added the Node.js repository. So, to install Node.js run the following command:
# apt-get install nodejs -y
To verify, run the below command, and you should be able to see the Node.js version:
# node -v
The output of the above command, in my case:
v14.21.3
Now, we also require other packages such as gulp, pm2, and yarn. So to install that, run the following commands one by one:
# npm install -g gulp # npm install -g pm2 # npm install -g yarn
Install and Configure the MEAN Stack App
Step 4: Installing and Configuring MEAN stack
To download the latest version of MEAN, run the following command:
# git clone https://github.com/meanjs/mean
After the installation is completed, navigate to the mean directory and install other dependencies with the following command:
# cd mean # yarn install
Now, edit the server.js file with the following command:
# vi server.js
Remove all the lines and add the following lines:
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const app = express();
app.use('/', (req, res) => {
MongoClient.connect("mongodb://localhost:27017/test", function(err, db){
db.collection('Example', function(err, collection){
collection.insert({ pageHits: 'pageHits' });
db.collection('Example').count(function(err, count){
if(err) throw err;
res.status(200).send('Page Hits: ' + Math.floor(count/2));
});
});
});
});
app.listen(3000);
console.log('Server running at http://localhost:3000/');
module.exports = app;
Now save and close your file by pressing ‘Esc’ , then type “:wq!” and hit Enter
Now start the server using the below command:
# pm2 start server.js
Now run the command to start server.js at every system reboot:
# pm2 startup
After completing all the above steps, the MEAN stack should be installed and listening on port 3000. To verify it, run the following command:
# ss -antpl | grep 3000
The output should be like this:
LISTEN 0 511 *:3000 *:* users:(("node /root/mean",pid=20120,fd=20))
Step 5: Configuring the Nginx server as a Proxy for MEAN
Firstly, you will be required to install the Nginx web server. So to do that, run the following command:
# apt-get install nginx -y
Now, create a new Nginx virtual host configuration file by:
# nano /etc/nginx/sites-available/mean
Add the following lines to it:
server {
listen 80;
server_name mean.example.com;
access_log /var/log/nginx/mean-access.log;
error_log /var/log/nginx/mean-error.log;
location / {
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:3000/;
}
}
Save the file and exit.
The next step is to enable the virtual host file. To do so, run the following command:
# ln -s /etc/nginx/sites-available/mean /etc/nginx/sites-enabled/
Now, to set the hash_bucket_size, edit the Nginx main configuration file with the following command:
# nano /etc/nginx/nginx.conf
Inside the http block, add the following line:
server_names_hash_bucket_size 64;
Save and close the file.
To verify if there are any syntax errors, run the following command:
# nginx -t
You should be able to get the following output:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
To apply the changes made in the configuration file, restart the Nginx service by using the following command:
# systemctl restart nginx
Test the Application
Open your browser and navigate to: http://<your-server-ip>:3000
You should see: Page Hits: 1
Refresh the page to see the count increase. Your MEAN stack app is up and running!
Conclusion:
You've now set up a full MEAN Stack environment on Ubuntu 20.04 — capable of running full-scale JavaScript applications from backend to frontend.
With Nginx fronting the app and PM2 keeping your server process alive and boot-persistent, your MEAN stack is production-ready.
Whether you're building a dashboard, API backend, or scalable real-time app, this setup gives you a robust, JavaScript-powered foundation to build on.
