Web Programming TutorialsHow to Install MERN Stack on Ubuntu 20.04

How to Install MERN Stack on Ubuntu 20.04

MERN stands for MongoDB, Express, React, and Node is a collection of four major technologies used for the deployment of full-stack web applications. React is used for the front-end, Node and Express are for the backend, and MongoDB is used as a database backend. In the MERN stack, all technologies support the same language for both the front-end and back-end. Each of these technologies provides an end-to-end framework for developing modern and scalable web applications.

M stands for MongoDB, which is a NoSQL database system used to store application data.

E stands for Express Js, used to build backend using the Node.js framework.

R stands for React Js, used to build a client-side JavaScript framework.

N stands for Node Js, which is a JavaScript runtime environment, used to run JavaScript on a server.

In this post, we will explain how to install the MERN stack on Ubuntu 20.04 server.

Prerequisites

  • An Ubuntu 20.04 server is installed in your system.
  • A root user or a user with sudo privileges configured in your server.

Install MongoDB

By default, the MongoDB package is not included in the Ubuntu 20.04 main repository. So you will need to add the MongoDB official repository to the APT.

First, install the required dependencies using the following command:

apt-get install wget curl gnupg2 -y

Next, download and add the MongoDB GPG key, and add the MongoDB repository using the following command:

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

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

Next, update the repository cache and install the MongoDB by running the following command:

apt-get update -y

apt-get install mongodb-org -y

Once the MongoDB is installed, start the MongoDB service using the following command:

systemctl start mongod

To check the MongoDB service, run the following command:

systemctl status mongod

You will get the following output:

mongod.service - MongoDB Database Server

Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)

Active: active (running) since Tue 2022-03-29 16:42:56 UTC; 5s ago

Docs: https://docs.mongodb.org/manual

Main PID: 2170 (mongod)

Memory: 63.7M

CGroup: /system.slice/mongod.service

└─2170 /usr/bin/mongod --config /etc/mongod.conf

Mar 29 16:42:56 ubuntu systemd[1]: Started MongoDB Database Server.

Secure MongoDB Instance

By default, you can connect the MongoDB instance without any authentication. So it is recommended to enable password-based authentication in MongoDB. You can enable it by editing the MongoDB main configuration file:

nano /etc/mongod.conf

Add the following lines:

security:

authorization: enabled

Save and close the file when you are finished.

Next, connect to the MongoDB instance with the following command:

mongo

Next, create an administrative user to manage MongoDB datbases:

use admin

db.createUser({user: "admin" , pwd: passwordPrompt() , roles: [{ role: "userAdminAnyDatabase" , db: "admin"}]})

You will get the following output:

Enter password:

Successfully added user: {

"user" : "admin",

"roles" : [

{

"role" : "userAdminAnyDatabase",

"db" : "admin"

}

]

}

Next, exit from the MongoDB instance with the following command:

exit

Finally, restart the MongoDB service to apply the configuration changes:

systemctl restart mongod

Install Node.js

Node.js is an open-source and scalable Javascript runtime environment used to build server-side applications. By default, the latest version of Node.js is not available in the Ubuntu 20.04 main repository. So you will need to add the Node.js repository to your server.

You can add it by running the following command:

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

Once the Node.js repository is added, install the Node.js using the following command:

apt-get install nodejs -y

After installing Node.js, verify the Node.js version with the following command:

node -v

You will get the following output:

v16.14.2

Install React.js

React.js is a JavaScript library that allows users to create a modern user interface. First, you will need to install the create-react-app utility to build and run a React application.

Run the following command to install and build frontend React.js:

npx create-react-app frontend

This will create a frontend directory including all basic React scripts.

Change the directory to the frontend and build it using the following command:

cd frontend

npm run build

This will creates HTML, CSS, and JS files for your frontend.

Install Express.js

You can use the npx command to install the Express.js as shown below:

npx express-generator

Next, go back to your home directory and run the following command to create your backend application

cd ~/

./.npm/_npx/85bfc4373bfefd98/node_modules/.bin/express backend

Next, change the directory to the backend directory and install all required Node modules using the following command:

cd backend

npm install

Install PM2 to Manage Node.js Application

Next, you will need to install PM2 to run a Node.js application in background.

Run the following command to install the PM2.

npm install pm2@latest -g

Next, go to the backend directory and start your backend application with the following command:

cd ~/backend

pm2 start npm --name "backend" -- start

You will get the following output:

[PM2] Spawning PM2 daemon with pm2_home=/root/.pm2

[PM2] PM2 Successfully daemonized

[PM2] Starting /usr/bin/npm in fork_mode (1 instance)

[PM2] Done.

┌─────┬────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐

│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │

├─────┼────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤

│ 0 │ backend │ default │ N/A │ fork │ 3394 │ 0s │ 0 │ online │ 0% │ 27.8mb │ root │ disabled │

└─────┴────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘


Next, add your application to start after the system reboot:

pm2 startup

pm2 save

Access Your Backend Application

At this point, your backend application is started and listening on port 3000. You can now access it using the URL http://your-server-ip:3000 in your web browser. If everything is fine, you should see your backend application on the following screen:

Conclusion

In this guide, you learned how to install the MERN stack on Ubuntu 20.04 server. You also learned how to create a frontend and backend application using the MERN stack. You can now start building your own application using the MERN stack.

Also Read: How to build a React CRUD application using MERN stack?

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -