How to Install & Setup MEAN Stack on Ubuntu (MongoDB, Express.JS, Angular.JS, Node.JS)


The MEAN stack is a popular web development framework consisting of MongoDB, Express.js, Angular.js, and Node.js. It is an open-source platform that allows developers to create robust web applications quickly and efficiently. In this article, we will guide you through the installation and setup process of the MEAN stack on Ubuntu.

Step 1: Install Node.js and NPM

Node.js is the runtime environment that allows developers to run JavaScript code outside of the browser. It is the backbone of the MEAN stack. To install Node.js on Ubuntu, follow these steps −

Open the terminal on Ubuntu by pressing Ctrl+Alt+T.

Type the following command to add the Node.js PPA (Personal Package Archive) to your system −

$ curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -

Once the PPA is added, run the following command to install Node.js and npm −

$ sudo apt-get install -y nodejs

To verify the installation, type the following command to check the version of Node.js and npm −

$ node -v
$ npm -v

If the installation is successful, the version numbers of Node.js and npm will be displayed on the terminal.

Step 2: Install MongoDB

MongoDB is a NoSQL database that stores data in JSON-like documents. It is a popular choice among developers because of its flexibility and scalability. To install MongoDB on Ubuntu, follow these steps −

Open the terminal and type the following command to import the MongoDB public GPG key −

$ wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -

Next, add the MongoDB repository to the list of sources on your system by running the following command −

$ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -sc)/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list

Once the repository is added, update the package list and install MongoDB by running the following commands −

$ sudo apt-get update
$ sudo apt-get install -y mongodb-org

To start the MongoDB service, run the following command −

$ sudo systemctl start mongod

To enable MongoDB to start at boot, run the following command −

$ sudo systemctl enable mongod

To verify the installation, type the following command to check the status of the MongoDB service −

$ sudo systemctl status mongod

If the installation is successful, the MongoDB service status should be displayed as "active (running)".

Step 3: Install Express.js and Angular.js

Express.js is a web application framework for Node.js that provides a set of tools and features for building web applications. Angular.js is a JavaScript framework for building dynamic web applications. To install Express.js and Angular.js on Ubuntu, follow these steps −

Open the terminal and navigate to your project directory

Install Express.js by running the following command −

$ npm install express --save

Install Angular.js by running the following command −

$ npm install angular --save

Step 4: Create a MEAN Stack Application

Now that we have installed all the required components, let's create a simple MEAN stack application to test our installation.

Open the terminal and navigate to your project directory.

Create a new directory for your MEAN stack application by running the following command −

$ mkdir mean-app

Navigate to the new directory by running the following command −

$ cd mean-app

Create a new file called server.js by running the following command −

$ touch server.js

Open the server.js file in your preferred text editor and add the following code −

const express = require('express');
const app = express();
const port = 3000;

app.get('/', (req, res) => {
   res.send('Hello World!');
});

app.listen(port, () => {
   console.log(`Server running on port ${port}`);
});

Save the server.js file.

Open a new terminal window and navigate to your project directory.

Start the MongoDB service by running the following command −

$ sudo systemctl start mongod

Start the server by running the following command −

$ node server.js

Open a web browser and navigate to http://localhost:3000. You should see the message "Hello World!" displayed on the page.

Congratulations! You have successfully installed and set up the MEAN stack on Ubuntu and created a simple MEAN stack application.

While the steps outlined above provide a basic understanding of how to install and set up the MEAN stack on Ubuntu, there are many additional tools and features that developers can use to enhance their applications.

For example, developers can use the Mongoose library to interact with MongoDB, which provides additional features such as validation and schema definition. They can also use Angular CLI (Command Line Interface) to generate components, services, and other parts of their Angular application.

In addition, developers can use Express.js middleware to add features such as authentication and error handling to their application. They can also use Node.js packages to add additional functionality such as sending emails or processing payments.

One important consideration when using the MEAN stack is security. Because the MEAN stack is built on open-source components, it is important for developers to take steps to secure their application against potential vulnerabilities.

One way to improve security is to use HTTPS to encrypt data transmitted between the client and the server. This can be achieved by obtaining an SSL certificate and configuring the server to use HTTPS. Developers can also use libraries such as Helmet.js to add additional security headers to their application and protect against common attacks such as cross-site scripting (XSS) and cross-site request forgery (CSRF).

Another important consideration is data validation and sanitization. Because the MEAN stack uses MongoDB, which is a NoSQL database, it is important to carefully validate and sanitize user input to prevent injection attacks. Developers can use libraries such as Joi or express-validator to validate incoming data and prevent malicious code from being executed.

Finally, developers should be aware of common security vulnerabilities such as SQL injection, buffer overflows, and directory traversal attacks, and take steps to mitigate these risks. They can use tools such as OWASP ZAP or Nikto to scan their application for potential vulnerabilities and take steps to address them.

By taking these steps to secure their application, developers can ensure that their MEAN stack application is secure and protects user data against potential threats.

Conclusion

The MEAN stack is a powerful web development framework that allows developers to create robust web applications quickly and efficiently. By following the steps outlined in this article, you can easily install and set up the MEAN stack on Ubuntu and create your own MEAN stack applications. With its flexibility, scalability, and ease of use, the MEAN stack is an excellent choice for developers of all skill levels.

Updated on: 28-Apr-2023

795 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements