Deploy NodeJS App to AWS EC2 : A Complete Guide
Launching your NodeJS app to the online world demands a robust hosting environment and Amazon Web Services Elastic Compute Cloud (AWS EC2) stands out as a secure and flexible solution. In this comprehensive guide we’ll take you through the entire process of hosting your NodeJS app to AWS EC2. starting from the ground up. By the end of this step-by-step manual you’ll not only have your API securely hosted but also gain valuable skills to manage it effortlessly. Embark on this exciting journey of “How to deploy node js application on aws ec2 ?”
Table of Contents
Essentials things to Get Started
Before starting journey of hosting your NodeJS app on AWS EC2 with PM2 . Be make sure that you have the following prerequisites in place:
- An operational Amazon Web Services (AWS) account.
- A fully developed and production-ready Node API project stored on GitHub.
Setting Up Your AWS EC2 Instance
Follow these steps to create your AWS EC2 instance :
- Log in to your AWS Management Console.
- Navigate to the EC2 service.
- Initiate the instance creation process by clicking on Launch Instance.
- Choose an Amazon Machine Image (AMI) that aligns with your preferences and project requirements.
- Select the appropriate instance type to meet the specifications of your application.
- Configure instance details that including network settings and storage allocations.
- Enhance management and identification by adding tags to your instance.
- Safeguard your instance by configuring security group settings. Enable inbound traffic on port 80 for HTTP access.
- Expand security measures by adding a rule. Opt for “HTTP” from the dropdown menu.
- Maintain flexibility by leaving the source as “Anywhere,” allowing access from any IP address.
- Review the configured instance details meticulously.
- When satisfied take the final step by clicking Launch.
Connect EC2 instance
After successfully launching your EC2 instance. proceed with the following steps to connect to it:
- Navigate to the EC2 Dashboard and select the recently launched instance.
- Locate and click on the Connect button initiating the connection setup.
- Follow the provided instructions to connect to your EC2 instance using SSH. This typically involves using a secure terminal or SSH client.
Open a terminal window on your local machine or use an SSH client to execute the connection command provided in the AWS console.
In the connection instructions you’ll find the SSH command for connecting to your instance. It looks like this:
ssh "your-key-pair.pem" ec2-user@your-ec2-public-ip
Replace “your-key-pair.pem” with the path to your private key file and “your-ec2-public-ip” with your EC2 instance’s public IP address.
If prompted to confirm the connection. type “yes” and press Enter.
Now you have successfully connected to your AWS EC2 instance using SSH that allowing you to manage and configure your NodeJS app on the hosted environment.
Configure Instance for Hosting the NodeJS App to AWS EC2
1. Update and Upgrade the Instance:
Ensure your instance is running the latest software updates by executing the following commands:
sudo apt update
sudo apt upgrade
2. Install NodeJS, npm, and Git to AWS EC2 instance
Install the necessary tools for running and managing your Node.js application. This includes NodeJS, npm (Node Package Manager) and Git for version control.
sudo apt install nodejs npm git
3. Clone Your NodeJS app Repository:
Use the git clone command to fetch your NodeJS app repository onto your EC2 instance. Replace with the actual URL of your Git repository.
git clone <your_repo_url>
4. Navigate to Your Project Directory:
Move into the directory where your NodeJS app project resides. Replace with the name of your project directory.
cd <project_directory>
5. Install Necessary Package For NodeJS app
Ensure all the required packages for your NodeJS app are installed. Navigate to your project directory and run:
npm install
By following these comprehensive steps you’re not only updating and upgrading your EC2 instance but also installing Node.js, npm and Git cloning your app repository. navigating to your project directory and installing project-specific packages. This meticulous preparation ensures that your EC2 instance is fully equipped to host and execute your NodeJS app effectively.
Run Your NodeJS app with PM2 and Enable HTTP Access
Once your EC2 instance is prepared the next steps involve installing PM2 on ubuntu globally starting your NodeJS app with PM2 and configuring the EC2 security group for HTTP access.
1. Install PM2 Globally
Firstly you need to install PM2 globally on your EC2 instance. PM2 is a process manager for NodeJS applications.
sudo npm install -g pm2
2. Start Your NodeJS app with PM2
With PM2 installed start your NodeJS app using the following command. Note that in this scenario app.js is assumed to be the main server file. Adjust the filename accordingly based on your project.
pm2 start app.js
3. Configure HTTP Access on the EC2 Security Group for Your NodeJS app
To enable HTTP access to your NodeJS app on your EC2 instance follow these steps for modifying the security group rules
- Access AWS Management Console :
- Log in to the AWS Management Console and navigate to the EC2 service.
- Select Your EC2 Instance:
- From the EC2 Dashboard identify and select your specific EC2 instance from the list.
- Locate Security Groups:
- In the “Description” tab of your selected instance locate the “Security Groups” section.
- Access Security Group Settings:
- Click on the security group associated with your instance. This action directs you to the detailed settings of that security group.
- Edit Inbound Rules:
- Within the “Inbound rules” tab. locate and click on the “Edit inbound rules” option.
- Add a New Rule for HTTP Access:
- To allow inbound traffic on port 80 for HTTP access click “Add rule”.
- From the “Type” dropdown menu select “HTTP”.
- Ensure that the source is set to “Anywhere” allowing access from any IP address.
- Save Rule Changes:
- Confirm the changes by clicking “Save rules”.
By completing these steps you’ve successfully configured the EC2 security group to permit incoming HTTP requests on port 80. This adjustment ensures that users can interact with your NodeJS app through the designated public IP address or domain name. Now your app is accessible over the web and users can engage with your application seamlessly.
Custom Domain for Your NodeJS app
If you wish to provide users with a more user-friendly experience by using a custom domain name follow these steps:
- Register a Domain Name:
- Choose a domain registrar of your preference and register a domain name for your NodeJS app. Popular registrars include GoDaddy, Namecheap or AWS Route 53.
- Configure DNS Settings:
- After registering your domain access the registrar’s dashboard.
- Navigate to the DNS management or domain settings section.
- Point to the Public IP Address of Your EC2 Instance:
- Locate the DNS settings that allow you to configure the domain’s DNS records.
- Update the DNS records to point to the public IP address of your EC2 instance.
By completing these steps you’ve registered a custom domain for your NodeJS app and configured the DNS settings to direct traffic to your EC2 instance. This optional step enhances the accessibility of your app allowing users to interact with it using a memorable and branded domain name.
Setup NodeJS app with a Reverse Proxy Using Nginx
In this step we’ll setting up Nginx as a reverse proxy for your NodeJS app. This adds an extra layer of optimization, security and flexibility to your deployment.
Install Nginx
sudo apt install nginx
Configure Nginx for Reverse Proxy
sudo nano /etc/nginx/sites-available/api
This will Create a New Configuration File
server {
listen 80;
server_name <your_api_domain>;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Insert the following content into the file. replacing with your app’s domain or IP address and adjust 3000 with the port where your Node app is running
sudo ln -s /etc/nginx/sites-available/api /etc/nginx/sites-enabled/
This will Create a symbolic link to enable the new configuration
sudo rm /etc/nginx/sites-enabled/default
This will Remove the default Nginx configuration to avoid conflicts:
And now Finally Restart the Nginex
sudo service nginx restart
Is a Reverse Proxy Compulsory ?
While not strictly compulsory employing a reverse proxy is highly recommended for production deployments. It enhances performance, security and scalability by providing a robust infrastructure for your NodeJS app. The benefits it brings in terms of load balancing, security features and optimized resource utilization make it a valuable addition to your deployment strategy.
Conclusion
You’ve successfully taken your NodeJS app and planted it securely on the internet using AWS EC2. This guide has been all about simplifying the process – from setting up your EC2 instance to giving your app its own cool domain name.
Remember why we brought in Nginx? It’s like the trusty sidekick for your app that making sure everything runs smoothly and safe from online mischief and with a bit of added speed.
Now, having a custom domain isn’t a must but it sure makes your “NodeJS app to AWS EC2” more memorable for users. And that’s a win
So your app is out there ready for action. Keep coding, keep deploying and keep building awesome stuff
FAQs
How can I host my Nodejs API on an AWS EC2 instance directly from GitHub ?
To host your Node.js API on AWS EC2 using GitHub follow these steps:
1. Set up your EC2 instance on AWS.
2. During instance creation make sure to configure security groups to allow inbound traffic on port 80 for HTTP access.
3. Connect to your EC2 instance using SSH.
4. Clone your Nodejs API project repository from GitHub onto the EC2 instance.
5. Navigate to the project directory install necessary dependencies and run your Nodejs app.
6. Deploy Node.js App to AWS EC2 with Nginx
How can I securely connect to my AWS EC2 instance when deploying a NodeJS app ?
For secure connections use SSH. Generate a key pair during EC2 instance creation and connect using the private key. Avoid default usernames such as ‘ec2-user’ on Amazon Linux.
Why use PM2 for deploying NodeJS apps on AWS EC2?
PM2 serves as a powerful process manager offering automatic restarts, easy management of multiple processes, centralized logging and enhanced application performance and stability.
Is it necessary to set up a custom domain for my NodeJS app on AWS EC2?
While not mandatory a custom domain enhances user experience, adds professionalism, improves accessibility, enables SSL integration for security and facilitates marketing efforts.