Table of contents
No headings in the article.
The MEAN stack is a popular web development environment that consists of four components: MongoDB, Express.js, AngularJS, and Node.js. In this tutorial, we will walk you through the steps of creating a MEAN stack server on an Amazon Web Services (AWS) Elastic Compute Cloud (EC2) Ubuntu instance.
Step 1: Launch an EC2 Instance
First, log in to your AWS Management Console and navigate to the EC2 dashboard. Click the "Launch Instance" button and select the Ubuntu Server image. Choose the appropriate instance type and configure the instance details. Make sure to select a security group that allows incoming traffic on ports 22 (SSH), 80 (HTTP), and 443 (HTTPS). Launch the instance and wait for it to initialize.
Step 2: Connect to the Instance
Once the instance is up and running, connect to it using SSH. You can use the following command to connect:
ssh -i /path/to/your/private/key.pem ubuntu@your-public-ip-address
Replace /path/to/your/private/key.pem
with the path to your private key and your-public-ip-address
with the public IP address of your instance.
Step 3: Install MongoDB
Once you are connected to the instance, run the following command to import the MongoDB public GPG key:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
Then, create a MongoDB list file using the following command:
echo "deb http://repo.mongodb.org/apt/ubuntu $(lsb_release -sc)/mongodb-org/4.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list
Update the package index and install MongoDB using the following commands:
sudo apt-get update
sudo apt-get install -y mongodb-org
Start the MongoDB service using the following command:
sudo systemctl start mongod
You can test the installation by running the following command:
mongo --eval 'db.runCommand({ connectionStatus: 1 })'
Step 4: Install Node.js and NPM
Next, install Node.js and NPM using the following commands:
sudo apt-get update
sudo apt-get install -y nodejs npm
Verify that Node.js and NPM are installed by running the following commands:
node -v
npm -v
Step 5: Install Express.js and AngularJS
Install Express.js and AngularJS using the following commands:
sudo npm install -g express
sudo npm install -g angular
Step 6: Create a MEAN Stack Application
To create a MEAN stack application, create a new directory for your application using the following command:
mkdir myapp
cd myapp
Create a new file named server.js
using the following command:
nano server.js
Add the following code to the file:
var express = require('express');
var app = express();
app.get('/', function(req, res) {
res.send('Hello World!');
});
app.listen(3000, function() {
console.log('App listening on port 3000');
});
Save the file and exit the text editor.
Step 7: Test the MEAN Stack Server
To test the MEAN stack server, start the server using the following command:
node server.js
You can test the installation by opening your web browser and navigating to your instance's public IP address, followed by :3000
. You should see a page displaying "Hello World!".
Note: If you face any issues, you need to check your node version and update it Step 2 worked for me. If you face any issues with MongoDB, click here and make sure you have allowed port 3000 in inbound rules in the security group.
Step 8: Secure the MEAN Stack
By default, the MEAN stack is not secure. To secure the stack, you should follow security best practices such as disabling root login, setting up a firewall, and configuring HTTPS.
Congratulations! You have successfully created a MEAN stack server on an AWS EC2 Ubuntu instance. You can now use this stack to host your web applications.
For more information on creating a LAMP stack server, check out our article on How to Create a LAMP Stack Server on Ubuntu EC2. Click here