Install and Configure Mean Stack on Ubuntu 20.04

    Posted in Linux Servers on May 30, 2022

    What is Mean Stack: It is a free, Open-source, popular Javascript software stack used for developing dynamic websites and web applications.

    A Mean stack is based on Javascript language so it can handle all aspects of an application.

    Step 1: First of all update the base system with the latest available packages.

    • • Command: apt-get update -y

    Step 2: Install MongoDB

    • • Command: apt-get install mongodb -y
    • • Command: systemctl start mongodb
    • • Command: systemctl enable mongodb

    Step 3: Install Node.js

    Install all required dependencies with this command:

    • • Command: Apt-get install curl gnupg2 unzip git gcc g++ make -y

    Once all dependencies are installed, add the Node.js repository with the command:

    Now install Node.js

    • • Command: apt-get install nodejs -y

    Please verify the Node.js version:

    • • Command: node -v

    Install yarn, gulp, pm2 packages with npm command:

    • • Command: npm install -g yarn
    • • Command: npm install -g gulp
    • • Command: npm install -g pm2

    Step 4: Install and Configure Mean Stack

    Now change the directory and install yarn

    • • Command: cd mean
    • • Command: yarn install

    Edit the server.js file with command:

    • • Command: nano server.js

    Replace all the lines with the following:

    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;

    Save and close the file and start the server:

    • • Command: pm2 start server.js

    Enable server.js to start at system reboot:

    • • Command: pm2 startup

    Now Meanstack has been installed and listening on port 3000 you can check with this command:

    • • Command: ss -antpl | grep 3000

    Step 5 – Configure Nginx as a reverse Proxy for MEAN

    • • Command: apt-get install nginx -y

    Create Nginx virtual host configuration file:

    • • Command: nano /etc/nginx/sites-available/mean

    Add the following lines:

    server {
    
    listen 80;
    
    server_name PARTICULAR DOMAIN;
    
    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/;
    
    }
    
    }

    Create a soft link in sites-enabled;

    • • Command: ln -s /etc/nginx/sites-available/mean /etc/nginx/sites-enabled/

    Edit the nginx main configuration file:

    • • Command: nano /etc/nginx/nginx.conf

    Add the following line below http{

    Server_names_hash_bucket_size 64;

    Check nginx configuration is correct :

    • • Command: nginx -t

    Now restart the nginx and you can hit the Domain Name to check the Mean stack;

    • • Command: systemctl restart nginx

    http:// Particular Domain

    Thank you!!!

    Services Cloudtechtiq offers: