How to install MERN stack on Ubuntu in the AWS cloud

Andrei Maksimov

Andrei Maksimov

0
(0)

A MERN stack is a widely-used JavaScript software stack that can be used to develop efficient and interactive web pages and applications. In this article, we’ll cover how to install the MERN stack on Ubuntu Linux at EC2 instance.

MERN stack architecture

MERN stack consists of four technologies:

MongoDB

MongoDB is a widely used open-source nonSQL database to develop modern and robust web applications by storing data in flexible documents like JSON. It does not need a pre-defined schema or ordering patterns. Because of its scalability and outstanding performance is used to develop modern applications that require efficient, mission-critical, and high-availability databases.

Express.js

Express.js is a Node.js web application framework that offers a vast collection of features for both web and mobile apps as well as used to develop single, multiple pages, and hybrid web-based applications.

React.js

React.js is also a free and open-source JavaScript framework used to create interactive frontend interfaces for web and mobile-based applications. Users can code in JavasScript and build UI components with React. Due to its incredible performance, flexibility, and integrity, most well-known cooperations such as Facebook, Instagram, Twitter, and others used React to create their interfaces. 

Node.js

Node.js: JavaScript environment provided by Node.js allows users to run their code on the server. Node.js is a free, cross-platform technique that lets programmers run their code on a server. It also enhances the productivity and functionality of applications.

MERN application

The MERN architecture makes it simple to build a three-tier architecture (frontend, backend, and database) using only JavaScript and JSON.

Here’s how every technology fits the architecture:

How to install MERN stack on Ubuntu in the AWS cloud

Lets install components of the MERN stack step by step on Ubuntu in the AWS cloud.

Launching Ubuntu EC2 instance

Launching an EC2 instance with Ubuntu operating system is simple and straightforward.

Go to the EC2 instances management console and click Launch instance button.

1. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - Launch EC2 instance

Choosing instance AMI

AMI is a template of an EC2 instance with pre-installed operating system and software.

Choose the latest Ubuntu AMI here.

2. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - Choose instance AMI

Choosing instance type

Next, you need to choose your EC2 instance type. Instance type defined compute resources for your virtual server – CPU and RAM.

Instance type depends on your application requirements. For the purpose of this article, we’ll move forward with t2.micro.

3. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - Choose instance type

Instance configuration

Here, you can configure our instance configuration, for example, networking, placement groups, IAM role, shutdown behavior, and etc.

If you’re new to AWS, proceed with default options.

4. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - Configure instance

Storage configuration

At this step, we need to define instance disks and sizes.

I’ll move forward with one system disk of 20 GiB.

5. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - Add storage

Define Tags

AWS Tags allows you to easily identify and track resources, which you create in AWS cloud.

I’ll set up one tag with the following parameters:

  • KeyName
  • ValueMERN-dev-server
6. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - Add Tags

Security Group configuration

Security Group is an EC2 instance firewall, which you can use to allows incoming traffic to your server.

I’ll open SSH, default port for the React dev server, and default HTTP port for testing production configuration (integration with Nginx or Apache).

7. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - Configure Security Group

Review launch parameters

There’s not so much to do here.

Just review your configuration and press Launch button.

8. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - Review

Choose SSH key pair

Now, we can launch our instance after selecting or generating its SSH key pair (more information about SSH is available in our Top 10 SSH Features You MUST Know To Be More Productive article).

9. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - SSH Key

SSH to the instance

As soon as your instance started, SSH into it to start software installation (default login for Ubuntu instances is ubuntu).

10. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - SSH into the instance

Installing MongoDB

MongoDB’s most recent version is not included in the Ubuntu repository. As a result, we will manually add the MongoDB repository first.

Importing MongoDB Repository

First of all, it is a good idea to update repositories packages list:

sudo apt-get update
11. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - MongoDB - apt-get update

Next, we need to install the gnupg packages:

sudo apt-get install gnupg -y
12. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - MongoDB - apt-get install gnupg

Copy and paste the command to download and add the MongoDB GPG key:

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

13. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - MongoDB - Add GPG key

Adding MongoDB to a packages list

After you’ve downloaded and installed the key, add the MongoDB repository:

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
14. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - MongoDB - Add repository

Installing MongoDB

Update your packages list and install MongoDB on your system:

sudo apt-get update
15. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - MongoDB - Update MongoDB repositories

To install MongoDB, use the following command:

sudo apt install mongodb-org -y
16. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - MongoDB - apt-get install mongodb-org

Running MondgoDB service

Once the MongoDB is installed, manage the MongoDB services state by using the systemctl command.

Lets check MongoDB service status:

sudo systemctl status mongod
17. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - MongoDB - systemctl status mongod

If MongoDB is not running, we need to enable and start its service:

sudo systemctl start mongod
sudo systemctl enable mongod
sudo systemctl status mongod
17. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - MongoDB - systemctl status mongod

Verifying installation

Use the command to check the MongoDB version and server address:

mongo --eval 'db.runCommand({ connectionStatus: 1 })'
19. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - MongoDB - Verify connection status

Creating MongoDB admin user

To work with the database, Launch the MongoDB shell by using the command:

sudo mongo
20. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - MongoDB - sudo mongo

Lets switch to MongoDB admin mode:

> use admin

Now, you can create a database by the given order below:

> db.createUser({user: "admin" , pwd: passwordPrompt() , roles: [{ role: "userAdminAnyDatabase" , db: "admin"}]})
21. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - MongoDB - Create admin user

To exit the MongoDB shell, use:

> quit()

Installing React.js

To install React, we need to install NPM packages first.

NPM

The NPM is a node package manager which is used to install all the libraries and other tools to manage the development of JavaScript applications.

To install NPM, use:

sudo apt-get install npm -y

Check the npm version with the command to see if the installation is completed:

npm -v
22. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - React - npm version

The previous command also installed the Node.js libraries:

node -v
23. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - React - node version

If you’d like to upgrade to the latest version of NPM, use the following command:

sudo npm install -g npm@next

create-react-app

Now, you need to install tools like babel, webpack, and others to move forward with React development. But this can be challenging for beginners.

Lets use create-react-app that provides the most simple and straightforward way to start the development of any React application.

To install the create-react-app execute:

sudo npm install -g create-react-app
24. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - React - npm install create-react-app

Bootstrapping the React application

Let’s create the first app by using create-react-app.

You can provide the name of your application by adding it as an argument: 

create-react-app myapp
25. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - React - create-react-app myapp

The boilerplate React application code is available for you in myapp folder.

Lets move there and start the React development server.

cd myapp
npm start
26. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - React - npm start

Open your browser and enter your EC2 instance public IP address on the address bar:

27. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - React - Demo app

The React Demo application page will appear.

Installing Node.js

Installing Node.js on Ubuntu is very simple and straightforward. 

Of course, we’ve already installed it in the previous step, so here we’ll proceed with installation process for a brand new server.

The first step is updating your existing packages list:

sudo apt-get update

Now, install the Node.js by executing the command:

sudo apt install nodejs -y

To check the installed version of Node.js, use:

nodejs -v
23. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - React - node version

Node.js has been successfully installed on your server. 

Installing Express.js

Now, it’s time to cover installation of Express.js:

sudo npm install -g express-generator
28. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - Express.js - npm install express-generator

Bootstrapping an Express.js project

Let’s create a new project using Express.js:

express MyFirstProject
29. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - Express.js - express MyFirstProject

Move to your project directory and install required NPM dependencies: 

cd MyFirstProject
npm install
30. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - Express.js - Install project dependencies

Now, let’s start the Express web-server:

npm start
31. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - Express.js - npm start

Open the browser and enter the IP address of at port 3000 in the address bar:

32. How to install MERN stack on Ubuntu in the AWS cloud - Ubuntu - Express.js - Demo app

The bootstrap project has been successfully created.

Now, you’re ready to start MERN applications development.

Summary

The MERN stack consists of MongoDB, Express.js, React.js, and Node.js, a standard and open-source JavaScript stack for developing web applications.

In this post, we have covered how to install and configure MongoDB, Express.js, React.js, and Node.js on the Ubuntu EC2 instance in the AWS cloud.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Subscribe to our updates

Like this article?

Share on facebook
Share on Facebook
Share on twitter
Share on Twitter
Share on linkedin
Share on Linkdin
Share on pinterest
Share on Pinterest

Want to be an author of another post?

We’re looking for skilled technical authors for our blog!

Leave a comment

If you’d like to ask a question about the code or piece of configuration, feel free to use https://codeshare.io/ or a similar tool as Facebook comments are breaking code formatting.