Deploy MEAN Application On AWS EC2

Search for a command to run...

No comments yet. Be the first to comment.
The Scenario

Introduction In this guide, we'll walk through setting up HTTPS for your website using AWS Certificate Manager (ACM) and CloudFront with an EC2 instance. This approach provides free SSL certificates, CDN benefits, and robust security. Prerequisites ...

Introduction:Setting up and hosting a Node server on AWS EC2 can be a powerful and flexible solution for deploying your web applications. In this step-by-step guide, we will cover the entire process, from creating an EC2 instance to configuring Nginx...

Introduction: In today's digital era, having a website is essential for businesses and individuals alike. While there are many ways to host a website, Amazon Web Services (AWS) provides a reliable and scalable infrastructure for hosting websites of a...

Why Following Rules Isn't Enough: The Importance of Standards in Development

Deploying an application on an EC2 instance can be a complex task, but with a few straightforward steps, it can be achieved relatively easily. In this blog, we will deploy an application with the following tech stack:
Node.js for the backend
Angular for the frontend
PostgreSQL as the database
Nginx as the web server on the EC2 instance.
Here are the steps to deploy the application:
Launch an EC2 instance:
Login to AWS Console
Click on EC2 Service
Click on "Launch instance"
Choose "Amazon Linux 2 AMI"
Choose the instance type and other details
In the "Step 3: Configure Instance Details" section, you can configure the network settings, security groups, and other details.
In the "Step 4: Add Storage" section, you can specify the storage settings for your instance.
In the "Step 5: Add Tags" section, you can add tags to your instance for easy identification.
In the "Step 6: Configure Security Group" section, you can specify the security group settings for your instance.
In the "Review and Launch" section, review your instance details and click on "Launch".
In the "Select an existing key pair or create a new key pair" dialog, select "Create a new key pair" and enter a name for your key pair.
Click "Download Key Pair" and save the .pem file in a secure location on your local machine.
Click "Launch Instances" to launch the instance.
Install and configure PostgreSQL:
Find the public IP address of your EC2 instance by going to the EC2 dashboard in the AWS Console, selecting your instance, and looking for the "IPv4 Public IP" in the "Description" tab.
Open a terminal window on your local machine and navigate to the directory where you saved the .pem file.
Change the permissions of the .pem file to read-only by running the following command: chmod 400 mykey.pem (replace mykey.pem with the name of your .pem file).
Connect to the EC2 instance via SSH using the following command: ssh -i /path/to/mykey.pem ec2-user@<Public-IP-Address> (replace /path/to/mykey.pem with the path to your .pem file and <Public-IP-Address> with the public IP address of your EC2 instance).
Once you are logged in, update the system package using the command sudo yum update.
Next, add the PostgreSQL repository to your system by running the following command:
sudo amazon-linux-extras install postgresql12
To make sure that the PostgreSQL server starts automatically whenever the system boots up, you need to enable it using the command:
sudo systemctl enable PostgreSQL
Next, you need to create a new user and database for PostgreSQL. You can do this by running the following commands:
sudo su - postgres
This command will switch the user to the PostgreSQL user.
psql
This command will open the PostgreSQL shell.
CREATE USER myuser WITH PASSWORD 'mypassword';
This command will create a new user with the username 'myuser' and the password 'mypassword'.
CREATE DATABASE mydb myuser;
This command will create a new database named 'mydb' with the owner set to the user 'myuser'.
Finally, exit the PostgreSQL shell and switch back to the root user using the command:
\q
This command will exit the PostgreSQL shell.
exit
This command will switch back to the root user.
You can verify if PostgreSQL is already installed on your system by checking if the PostgreSQL service is running or by using the command-line client psql.
To check if the PostgreSQL service is running, you can use the following command:
Install Node.js and Angular:
Install Node.js: sudo yum install nodejs npm
Install Angular CLI: sudo npm install -g @angular/cli
Build and package the application:
Navigate to the home directory by running the command: cd ~
Install Git by running the command: sudo yum install git -y
Clone the Node.js backend project from Git by running the command: git clone <backend-git-repository-url>
Navigate to the cloned directory by running the command: cd <backend-directory-name> (replace <backend-directory-name> with the name of the directory where the backend project was cloned)
Install the Node.js dependencies by running the command: npm install
Clone the Angular frontend project from Git by running the command: git clone <frontend-git-repository-url>
Navigate to the cloned directory by running the command: cd <frontend-directory-name> (replace <frontend-directory-name> with the name of the directory where the frontend project was cloned)
Install the Angular dependencies by running the command: npm install
Start the Node.js and Angular applications using PM2:
Install PM2 by running the command: sudo npm install pm2 -g
Navigate to the Node.js backend directory by running the command: cd <backend-directory-name>
Start the Node.js application using PM2 by running the command: pm2 start <backend-start-command> --name=<backend-app-name> (replace <backend-start-command> with the command to start your Node.js application and <backend-app-name> with the name you want to give to your PM2 process).
Navigate to the Angular frontend directory by running the command: cd <frontend-directory-name>
Build the Angular application by running the command:
ng build --prod
or
ng build --configuration=production
Start the application using the ng serve command with the --port flag to specify the port to listen on:
ng serve --port 4200 --host 0.0.0.0
This will start the application on port 4200 and allow access from any IP address.
Press Ctrl-C to stop the ng serve command.
Start the application using the PM2 process manager:
pm2 start "ng serve --port 4200 --host 0.0.0.0"
Confirm that applications are running
pm2 ls
This will display the status of the PM2 process
Install and configure Nginx:
Install Nginx: sudo yum install nginx
Configure Nginx to serve the Angular app and proxy requests to the Node.js server:
sudo vi /etc/nginx/nginx.conf
`location / {
proxy_pass http://localhost:4200/;
`proxy_set_header Host $host;
`proxy_set_header X-Read-IP $remote_addr;
`proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}`
`location /api/ {
proxy_pass http://localhost:<port at which your node app is running>}/;
`proxy_set_header Host $host;
`proxy_set_header X-Read-IP $remote_addr;
`proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}`
Start Nginx: sudo service nginx start
If you face any issues during the deployment process, please feel free to contact me at hunbalsiddiqui.com. I'm always happy to help!
In summary, deploying an application with Node.js, Angular, PostgreSQL, and Nginx on an EC2 instance involves creating and configuring the instance, installing and configuring the necessary software, building and packaging the application, installing and configuring Nginx, and deploying the application. With these steps, you can successfully deploy your application on an EC2 instance.