Node.js is an open-source, cross-platform JavaScript runtime environment and library for running web applications outside the client's browser. Developers use Node.js to create server-side web applications, and it is perfect for data-intensive applications. You can also learn more about Node.js via this link.
AWS EC2 (Amazon Elastic Compute Cloud) is a web service that provides secure, resizable computing capacity in the cloud. You can host a Node.js server on any of your preferred hosting choices, and one way to host it is using an AWS EC2.
Termius is a terminal emulator that allows users to access and manage remote servers and devices from their local machines. You can also get a better understanding of Termius via this link and you can also sign up here.
Prerequisites
To follow along with this tutorial you need the following
- A GitHub account.
- A working Node.js application. In my case, I am using this repository.
- An AWS Account with permission to interact with an AWS EC2. You can create a free account to get started with AWS EC2 here.
- An account with Termius.
Step 1: Launch an EC2 Instance on AWS
- Sign into your AWS account, and open the EC2 console.
- Then from the EC2 console dashboard, click Launch instance.
- Next, under name and tags, enter a descriptive name for your instance.
- Under Application and OS Images (Amazon Machine Image), select an Amazon Machine Image for your instance; in this case, I selected Ubuntu.
- Under Instance type, from the Instance type list, you can select the hardware configuration for your instance.
- Then under Create key pair, type in your desired key pair name, click create a new key pair, and the key is downloaded to your local machine.
- Under the Network settings, check the box that says “Allow HTTP traffic from the internet”.
Keep the default selections for the other configuration settings for your instance.
Review a summary of your instance configuration in the Summary panel, and when you're ready, click Launch instance.
A confirmation page lets you know that your instance has successfully been launched.
- On the Instances screen, you can view the launch status. When you launch an instance, its initial state is pending. After the instance starts, its state changes to running and it receives a public DNS address/IP.
- Lastly, It can take a few minutes for the instance to be ready for you to connect to it. Check that your instance has passed its status checks; you can view this information in the Status check column.
Step 2: Connect to your Instance via Termius
- Once your instance starts running, check the instance box and click connect and you will be presented with the interface below.
- Open Termius on your desktop if downloaded or you can use the web version and select Create host.
- Add a label, address ( the public IP of the EC2 instance you created in AWS), username (ubuntu) and click Set a Key.
- Set a key by importing the key pair created when you were setting up an EC2 instance in AWS from your local machine. Click NEW KEY and then Import from key file to upload the key from your local machine.
- After importing the key file click Save and then Host.
- Then click Add and continue.
- You will see that your host has successfully connected to your instance.
Step 3: Update the Instance, and install Nodejs and NPM using Node Version Manager
- Update the instance by typing the following command in the terminal.
sudo apt update -y
- Type the following command to gain access to root privileges.
sudo su -
- Install node version manager (nvm) by typing the following command in the terminal.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
- Activate the node version manager by typing the following command in the terminal.
. ~/.nvm/nvm.sh
- Use the node version manager to install the latest version of Node.js by typing the following command in the terminal.
nvm install node
- Test that node and npm are successfully installed by typing the following commands in the terminal.
node -v
npm -v
Step 4: Install Git and clone the repository from GitHub
- Install git by typing the following commands in the terminal.
apt-get update -y
apt-get install git -y
- Verify that git has been installed by typing the following command and it will print the git version to the terminal.
git --version
- Type the following command to clone the code repository from GitHub.
git clone https://github.com/Benchokz/nodejs-on-ec2.git
Step 5: Install npm in the directory of the cloned code repository
- Get into the cloned repository by typing the following command.
cd nodejs-on-ec2
- Install npm by typing the following command.
npm install
Step 6: Start the application
- Start the application by typing the following command.
npm start
The command, if run successfully will finish up as follows.
Step 7: Check the display via the instance public IP on AWS
- Copy the public IP of your instance and paste it into a browser, and you will have the below result. Please note that this IP is the non-secure HTTP protocol that is mapped to port 80 by default in AWS.
Conclusion
The tutorial offers a comprehensive guide, outlining the sequential steps to initiate an EC2 instance on Amazon Web Service, generate a host, establish connectivity between the host and the instance through Termius, deploy Node Version Manager to simplify the installation of npm and essential dependencies for ensuring smooth access to a Node.js application, launch the application, and verify its accessibility via the instance's public IP address using the non-secure HTTP protocol.
Top comments (0)