Hosting a Node.js and Express.js App on AWS EC2: A Step-by-Step Guide
Hosting a Node.js and Express.js application on AWS EC2 can be a powerful solution for deploying web services, APIs, or even full-fledged web applications. In this guide, we’ll walk through the process of launching and deploying a Node.js and Express.js app on an AWS EC2 instance running Ubuntu. By the end of this guide, you’ll have a fully functional web server running your Node.js and Express.js app that’s accessible from any browser.
Step 1: Launching an EC2 Instance:
- Go to the AWS Management Console and navigate to the EC2 service.
2. Launch a new EC2 instance by clicking the “Launch Instance” button.
3. Name your EC2 instance.
4. Choose an Ubuntu AMI for your instance.
5. Select the instance type and size that fits your needs.
The free tier is available for new AWS accounts and provides a small instance with limited resources.
6. Create a new key pair, and download the .pem
file to your local machine.
This key pair will be used to authenticate your SSH sessions to the EC2 instance
7. Set up a security group that allows incoming traffic on port 8000 (or any other port you choose to run your Node.js app).
This will allow your Node.js app to accept incoming HTTP requests
8. Launch the instance. Once launched, you will see the instance details, including the public IPv4 address. Note this down as we’ll need it later to SSH into the instance.
Step 2: Setting Up the Project on the EC2 Instance:
9. Open a terminal and navigate to the directory where your .pem
file is located.
10. Use the ssh
command to connect to the EC2 instance. Replace your-pem-file.pem
and xx.xxx.xx.xxx
with the IPv4 address you noted down earlier.
ssh -i "your-pem-file.pem" ubuntu@xx.xxx.xx.xxx
11. Once connected to the EC2 instance, run the following commands to install Node.js and npm.
sudo apt update
sudo apt install -y nodejs npm
12. Create a new directory for your project and navigate into it.
mkdir node-hello-world
cd node-hello-world
13. Initialize a new npm package. The npm init -y
command initializes a new package.json
file with default settings.
npm init -y
14. Install Express.js. Express.js is a popular web framework for Node.js that simplifies creating web applications and APIs.
npm install express
15. Create an index.js
file in the project directory and open it with your favorite text editor.
nano index.js
16. Paste the following code into the index.js
file and save it.
This is a simple Express.js application that listens on port 8000 and responds with “Node API” when you visit the root URL
const express = require('express')
const app = express()
const PORT = 8000
app.get('/', (req, resp)=>{
resp.send('Node API')
})
app.listen(PORT, ()=>{
console.log(`Listening to port ${PORT}`)
});
17. Start the server using the following command
node index.js
18 Check your API by visiting your instance’s public IPv4 address followed by port 8000 in a web browser. For example: your_public_IPv4:8000/
your_public_IPv4:8000/
Step 3: Running the Server in the Background:
19. To run your server in the background, you can use a tool like PM2.
PM2 is a process manager for Node.js applications that provides features such as process monitoring, automatic restarts, and log management.
20. Install PM2 globally using npm.
sudo npm install -g pm2
21. Start your server using PM2.
pm2 start index.js
22. Check if the server is running.
pm2 status
Visit your API in a web browser to verify that it’s still working.
Step 4: Conclusion and Further Learning:
You’ve successfully launched and deployed a Node.js and Express.js app on an AWS EC2 instance! This guide has provided a basic overview, but there is still a lot more to learn about AWS, Node.js, Express.js, and web development in general. Keep exploring and experimenting, and continue to expand your knowledge. Good luck!