Table of contents
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 the 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 web and mobile app features and is used to develop single, multiple pages, and hybrid web-based applications.
React.js
React.js is a free, open-source JavaScript framework for creating 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:
- React.js – Presentation Layer
- Node.js + Express.js – Application Layer
- MongoDB – Data Layer
Let’s 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 straightforward.
Go to the EC2 instances management console and click the Launch instance button.
Choosing instance AMI
AMI is a template of an EC2 instance with a pre-installed operating system and software.
Choose the latest Ubuntu AMI here.
Choosing instance type
Next, you need to choose your EC2 instance type. Instance type defined compute resources for your virtual server – CPU and RAM.
The instance type depends on your application requirements. For this article, we’ll move forward with t2.micro
.
Instance configuration
Here, you can configure our instance configuration, for example, networking, placement groups, IAM role, shutdown behavior, etc.
If you’re new to AWS, proceed with default options.
Storage configuration
At this step, we need to define instance disks and sizes.
I’ll move forward with one system disk of 20 GiB.
Define Tags
AWS Tags allows you to easily identify and track resources that you create in the AWS cloud.
I’ll set up one tag with the following parameters:
- Key –
Name
- Value –
MERN-dev-server
Security Group configuration
Security Group is an EC2 instance firewall that you can use to allow incoming traffic to your server.
I’ll open SSH, the default port for the React dev server, and the default HTTP port for testing production configuration (integration with Nginx or Apache).
Review launch parameters
There’s not so much to do here.
Just review your configuration and press Launch button.
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).
SSH to the instance
As soon as your instance starts, SSH into it to start software installation (default login for Ubuntu instances is ubuntu
).
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 the repositories packages list:
sudo apt-get update
Next, we need to install the gnupg
packages:
sudo apt-get install gnupg -y
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 –
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
Installing MongoDB
Update your packages list and install MongoDB on your system:
sudo apt-get update
To install MongoDB, use the following command:
sudo apt install mongodb-org -y
Running MondgoDB service
Once the MongoDB is installed, manage the MongoDB services state by using the systemctl
command.
Let’s check MongoDB service status:
sudo 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
Verifying installation
Use the command to check the MongoDB version and server address:
mongo --eval 'db.runCommand({ connectionStatus: 1 })'
Creating MongoDB admin user
To work with the database, Launch the MongoDB shell by using the command:
sudo mongo
Let’s switch to MongoDB admin mode:
> use admin
Now, you can create a database in the given order below:
> db.createUser({user: "admin" , pwd: passwordPrompt() , roles: [{ role: "userAdminAnyDatabase" , db: "admin"}]})
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 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
The previous command also installed the Node.js libraries:
node -v
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.
Let’s use create-react-app
that provides the most straightforward way to start the development of any React application.
To install the create-react-app
execute:
sudo npm install -g 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
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
Open your browser and enter your EC2 instance public IP address on the address bar:
The React Demo application page will appear.
Installing Node.js
Installing Node.js on Ubuntu is straightforward.
Of course, we’ve already installed it in the previous step, so here we’ll proceed with the 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
Node.js has been successfully installed on your server.
Installing Express.js
Now, it’s time to cover the installation of Express.js:
sudo npm install -g express-generator
Bootstrapping an Express.js project
Let’s create a new project using Express.js:
express MyFirstProject
Move to your project directory and install required NPM dependencies:
cd MyFirstProject
npm install
Now, let’s start the Express web server:
npm start
Open the browser and enter the IP address of at port 3000 in the address bar:
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 open-source JavaScript stack for developing web applications.
This post has covered how to install and configure MongoDB, Express.js, React.js, and Node.js on the Ubuntu EC2 instance in the AWS cloud.
Related articles
- CloudFormation – How to access CodeCommit repo from EC2 instance
- Building S3 Static Website with AWS CDK and Projen
- Top 10 SSH Features You MUST Know To Be More Productive
- How to Setup a Minecraft Server on Ubuntu, Windows, and CentOS in AWS cloud
- CloudFormation Tutorial – How To Automate EC2 Instance In 5 Mins [Example]
- The Best Linux Distribution for your next cloud server
I’m a passionate Cloud Infrastructure Architect with more than 15 years of experience in IT.
Any of my posts represent my personal experience and opinion about the topic.