MEAN Stack Installation on Ubuntu Linux: A Complete Guide for Web Developers 2

MEAN Stack Installation on Ubuntu Linux: A Complete Guide for Web Developers

The MEAN stack is a powerful combination of technologies that provides a comprehensive solution for web development. The acronym stands for MongoDB, Express.js, AngularJS, and Node.js, which are the four technologies that make up the stack. MongoDB is a NoSQL database, Express.js is a web framework for Node.js, AngularJS is a JavaScript framework for building dynamic web apps, and Node.js is a JavaScript runtime that allows you to run JavaScript on the server-side.

In this article, we will be discussing how to set up the MEAN stack on Ubuntu Linux, a popular open-source operating system. We will cover all the necessary steps to install and configure each component of the stack, so you can have a fully functional development environment up and running in no time.

The MEAN stack is a popular choice for web application development due to its ability to streamline the development process and provide a solid foundation for building fast, dynamic, and robust web applications. The stack, which comprises MongoDB, Express.js, AngularJS, and Node.js, offers developers the convenience of using a single set of technologies throughout the entire development process, from the back-end to the front-end.

This allows developers to spend less time on integrating different components and more time on creating innovative and engaging applications. By utilizing the MEAN stack, developers can standardize on proven technologies and focus on the creative aspects of development, such as designing user interfaces and implementing new features.

If you are looking to build a cutting-edge web application and want to take advantage of the many benefits the MEAN stack offers, the following steps will guide you through the process of installing it on Ubuntu Linux. With the MEAN stack at your disposal, you’ll be well-equipped to create powerful and engaging web applications that will meet the demands of today’s users.


How to install and use the MEAN stack on Ubuntu Linux

Installing the MEAN stack on Ubuntu Linux is a straightforward process. The following steps will guide you through the process of setting up the stack on your Ubuntu system, so you can start building powerful and engaging web applications right away.

Please note that before you proceed with the installation, you should make sure your system is up-to-date and has the necessary dependencies installed. This may include updates to the operating system, as well as the installation of specific tools and libraries. Once your system is ready, you can proceed with the installation of the MEAN stack.

The installation process typically involves downloading and configuring the individual components of the stack, such as MongoDB, Express.js, AngularJS, and Node.js. In some cases, you may need to configure additional settings or perform additional tasks to get the stack up and running. But overall, it’s a simple and straightforward process that will have you up and running with the MEAN stack in no time.

Step 1: Installing MongoDB

The first step in setting up the MEAN stack is to install MongoDB. MongoDB is a NoSQL document-oriented database that is designed to scale horizontally and handle large amounts of data. It is a popular choice for web applications because it is easy to use and can handle data in a more flexible way than traditional relational databases.

Before proceeding with the installation of the MongoDB, it’s important to ensure that your system has the necessary dependencies in place. This can be done by running a series of commands that will add the required APT repositories to your Ubuntu system.

sudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common

To install MongoDB on Ubuntu Linux, type the following commands to add its repository’s GPG key to sign its packages:

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

Next, to set up MongoDB on Ubuntu Linux, you will need to create a repository file for it. This can be done by running the following command:

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

Make sure to replace the placeholder in the command with the version code name of your Ubuntu system. For example, if you are running Ubuntu 20.04, the version code name is “focal“.

echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

Once the repository file has been created and the GPG key added, you can then run the following commands to update the Ubuntu packages index and install MongoDB:

sudo apt update
sudo apt install mongodb-org

In case you face any issues with the installation of MongoDB, you can try running the following commands to install the necessary dependencies:

wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2_amd64.deb
sudo dpkg -i libssl1.1_1.1.1f-1ubuntu2_amd64.deb

By following these steps, you will have successfully installed MongoDB on your Ubuntu Linux system and will be ready to proceed with the next steps in the MEAN stack installation process.

If you require additional assistance with the installation and management of your MongoDB database, follow the link below:

How to Install MongoDB on Ubuntu Linux.


Step 2: Installing Node.js

To install the MEAN stack, it is necessary to first install Node.js. Express.js, Angular.js, and Node.js are all web frameworks based on JavaScript. Express.js is a fast, minimalistic web framework for Node.js, Angular.js is a fully extensible toolset for building web and app frameworks based on Node.js and JavaScript, and Node.js is the premier JavaScript web server.

Even though Node.js packages are included in the default Ubuntu repositories, the version included may not be the latest. To ensure you have the latest version, it is recommended to add the Node.js repository.

To do this, you can run the following commands to download the Node.js version 18 repository script:

sudo apt install curl
curl -sL https://deb.nodesource.com/setup_18.x | sudo bash -

Once the repository script has been downloaded and executed, the next step is to install Node.js version 18 by running the following command:

sudo apt install nodejs

Additionally, it is recommended to install some additional packages along with the MEAN stack which can be installed by running the following commands:

sudo npm install -g yarn
sudo npm install -g gulp
sudo npm install pm2 -g

These packages such as yarn, gulp and pm2, are optional but can be useful for various tasks such as dependency management and process management.

If you require additional assistance with the installation and usage of Node.js on Ubuntu Linux, follow the link below:

How to install Node.js on Ubuntu Linux.


Step 3: Installing MEAN stack

Now that we have installed Node.js and other additional packages, we can proceed to download the MEAN stack from GitHub. The git package includes all the necessary dependencies for MEAN development. To do this, you can run the following commands:

sudo apt install git
git clone https://github.com/meanjs/mean

This will download the MEAN stack package from GitHub.

After the package has been downloaded, navigate to the package directory and run the following command to install all dependencies:

cd mean
yarn install

Step 4: Create a Node.js based Web Application

Now that all the necessary packages and dependencies are installed for the MEAN stack, we can proceed to build our first web application. A crucial step in this process is editing the server.js file within the MEAN directory. This can be accomplished by utilizing the command line editor, nano, and navigating to the appropriate file location:

nano ~/mean/server.js

Once the file is open, it is important to add the appropriate code for our web application. This includes utilizing the Express.js and MongoDB libraries, setting up routes and database connections, and configuring the server to listen on a specific port.

Copy and paste the following lines of code into the file and save it:

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;

The next step is to start the server and bring the web application to life. This is done by utilizing the PM2 process manager, and running the command:

pm2 start ~/mean/server.js

When the server starts, it will display information similar to the following:

[PM2] Spawning PM2 daemon with pm2_home=/home/user/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /home/user/mean/server.js in fork_mode (1 instance)
[PM2] Done.
┌─────┬───────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name      │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼───────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0   │ server    │ default     │ 0.6.0   │ fork    │ 7377     │ 0s     │ 0    │ online    │ 0%       │ 38.8mb   │ user    │ disabled │
└─────┴───────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

To ensure that your web application starts automatically whenever your system boots up, you can use the PM2 startup command. This command will create a startup script that PM2 can use to start your application on system boot. To execute this command, run the following command in your terminal:

pm2 startup ~/mean/server.js

Upon running this command, you may see output similar to the following:

[PM2] Init System found: systemd

-----------------------------------------------------------
 PM2 detected systemd but you precised /home/linuxtutorial/mean/server.js
 Please verify that your choice is indeed your init system
 If you arent sure, just run : pm2 startup
[PM2] To setup the Startup Script, copy/paste the following command:
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup /home/linuxtutorial/mean/server.js -u linuxtutorial --hp /home/linuxtutorial

Once you have completed the previous steps, your web application will be configured to automatically start whenever your system boots up.

To access the application, you will need to open a web browser and navigate to the server’s hostname or IP address, followed by the port number 3000. For example, if your server’s IP address is 192.168.1.100, you can access the application by navigating to “http://192.168.1.100:3000” in your web browser. The application is now running and is available for use.

http://localhost:3000

How to Run MEAN app with reverse proxy

A reverse proxy is a server that sits in front of one or more web servers and acts as an intermediary between the client and the web servers. It can be used to provide additional functionality, such as load balancing, SSL termination, or caching.

To run a MEAN app with a reverse proxy, you will need to first set up and configure the reverse proxy server. Popular reverse proxy servers include Nginx, Apache, and Caddy.

Once the reverse proxy is set up, you will need to configure it to forward incoming requests to the appropriate web server(s) running your MEAN app. This can typically be done by creating a virtual host or server block that listens on the desired domain or IP address and forwards requests to the appropriate back-end server(s) running your MEAN app.

How to Setup Nginx Reverse Proxy:

For example, you might configure Nginx to listen on port 80 and forward requests to your MEAN app running on port 3000. The configuration might look something like this:

server {
    listen 80;
    server_name example.com;
    location /backend_app {
        proxy_pass http://localhost:3000;
    }
}

How to Setup Apache Reverse Proxy:

Here’s an example of how you might configure Apache to act as a reverse proxy for a MEAN app running on port 3000:


    ServerName example.com
    ProxyPreserveHost On
    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/

This configuration tells Apache to listen on port 80 and forward all incoming requests to the MEAN app running on localhost:3000. The ProxyPreserveHost directive tells Apache to pass the original host header from the client to the proxied server, so the MEAN app can determine the correct hostname to use. And the ProxyPassReverse directive tells Apache to adjust the Location and Content-Location headers in the response sent back to the client, so they match the public-facing URL of the reverse proxy.

It’s also important to note that if you’re using a reverse proxy, you will have to make sure that the requests coming from the reverse proxy to the MEAN app contains the correct headers like X-Forwarded-For and X-Real-IP.

It’s recommended to use a tool like PM2 to run the MEAN app in production as it makes it easy to run it as a service, manage it and also it will automatically start the app after reboot.


Conclusion

In conclusion, the MEAN stack is a powerful set of technologies that provides a comprehensive solution for web development. This guide has provided a complete guide on how to install and set up the MEAN stack on Ubuntu Linux, a popular open-source operating system. By following the steps outlined in this guide, developers can easily install MongoDB, Express, Angular, and Node.js on their Ubuntu Linux systems, and have a fully functional development environment ready for building and deploying dynamic web applications. The MEAN stack is a powerful tool for web development and with Ubuntu Linux as your operating system, you are well on your way to creating robust and scalable web applications.

We hope that this article has helped you to navigate Linux directories with greater ease and efficiency. If you found it useful, please share with others and leave us your feedback.

If our tutorials helped you, please consider buying us a coffee. We appreciate your support!

Thank you for your support.

The post MEAN Stack Installation on Ubuntu Linux: A Complete Guide for Web Developers appeared first on Linux Tutorial Hub.

Leave a Comment

Your email address will not be published. Required fields are marked *