Setting Up a Basic Server for the MEAN Stack

A Comprehensive Guide to Configuring Your MEAN Stack Environment

By Rajan Maharjan

MEAN Tech Stack

Running a Node.js Server and AngularJS Application on a Single Ubuntu Server

In this guide, we will cover two main sections:

  • Installation
    Get your server ready by installing the necessary software.

  • Setup
    Configure your server to run both the Node.js backend and the AngularJS frontend.

Will be installing following;

  1. Nginx
    Nginx acts as a front-end server, which, in this case, proxies requests to a Node.js server.

  2. NodeJS
    Cross-platform JavaScript run-time environment that executes JavaScript code server-side

  3. Yarn
    Fast, reliable, and secure dependency management

  4. PM2
    Advanced process manager for production Node.js applications

  5. MongoDB
    MongoDB is a document database with the scalability and flexibility that you want with the querying and indexing that you need

  6. Git
    Git (/ɡɪt/) is a version control system

 

INSTALLATION PART for Linux (Ubuntu System)

 

  1. Install Nginx

     sudo apt-get install nginx
    

    Reference

  2. Install Node.js

     curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
    
     export NVM_DIR="$HOME/.nvm"
     [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
    

    Install Node.js:

     nvm install --lts
    

    Reference

  3. Install Yarn

     curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
     echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
    

    Reference

  4. Install PM2

     sudo npm install pm2 -g
    

    Reference

  5. Install MongoDB

     sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
    
     echo "deb [arch=amd64,arm64] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
    
     sudo apt-get update
    
     sudo apt-get install -y mongodb-org
    
     sudo systemctl start mongod
    
     sudo systemctl enable mongod
    

    Adding an Administrative User:

     mongo
    
     use admin
    
     db.createUser({user: "peacenepal", pwd: "peacezone@123", roles:[{role:"dbAdminAnyDatabase", db: "admin"}]})
    
     # Type 'exit' and press ENTER or use CTRL+C to leave the client.
    
     sudo nano /etc/mongod.conf
    

    Reference

 

SETUP PART

 

Node.js Server

  1. Run the server using PM2

    Go to the project folder and run the following command:

    pm2 start app.js -i 2 --name api-server
    

    Note: The command depends on your project.

    After that, the API would be accessible at:

    http://<SERVER_ADDRESS>:<PORT>

    • SERVER_ADDRESS: IP address or domain name used for server access.

    • PORT: Port used to start the Node.js server.

  2. Angular App Setup

  • Add the project files to the server in your desired location.
  • Configure Nginx to serve our app files

    • Create a new Nginx configuration file in the following folder:

        /etc/nginx/sites-enabled/ 
      

    • Add the following details to the configuration file:

            server {
            listen 80;
            listen [::]:80;
      
            root <PROJECT_FOLDER_PATH>;
            index index.html index.htm index.nginx-debian.html;
      
            server_name <SERVER_ADDRESS>;
      
            location / {
                try_files $uri $uri/ =404;
            }
        }
      

    • Reload the Nginx server with the following commands to apply the new server block:

        sudo systemctl reload nginx
              
        sudo systemctl restart nginx
      

What am I missing here? Let me know in the comments and I’ll add it in!

Summary:

  • Headings are used to organize the content.
  • Code Blocks contain commands and configurations.
  • Notes clarify details for commands and configuration.
  • Instructions are presented in a clear, step-by-step format.

Medium Article Link

Share: X (Twitter) Facebook LinkedIn