AWS Basic
- AWS Intro
- Launch EC2 Instance
- Elastic IP
- AWS Target Group
- AWS Application Load Balancer
- AWS Route53
- Amazon Certificate Manager
- Redirect HTTP to HTTPs
- Redirect WWW to Non WWW
- AWS Cloudfront
- Download S3 to Local
- Install Node.js & NPM in EC2
AWS Connection
AWS Wordpress
AWS Deploy App
Deploy Express.js Website on an EC2 Instance
Photo Credit to CodeToFun
🙋 Introduction
In the vast landscape of web development, deploying a express.js website on a cloud server is a common and essential task.
Amazon Elastic Compute Cloud (EC2) provides a scalable cloud computing solution, allowing developers to easily deploy and manage applications.
📚 Prerequisites
Before we begin, make sure you have the following prerequisites:
AWS Account and EC2 Access:
Ensure you have an AWS account with access to the EC2 service.
Running EC2 Instance:
Make sure you have an EC2 instance that is up and running.
Elastic IP Association:
Associate an Elastic IP with your EC2 instance to provide a static IP address.
File Transfer with Filezilla:
Set up Filezilla with a connection to your EC2 instance for easy file transfer.
Secure Shell Access with PuTTY:
Configure PuTTY to establish a secure shell connection to your EC2 instance.
Node.js and NPM Installation:
Ensure Node.js and NPM are installed on your EC2 instance for running your Node.js application.
🖥️ Creating a Simple Express.js App
Let's develop a straightforward express.js web application that comprises two files: the index.js, and a hello-world.html files.
Here the index.js is the core express.js file
const express = require('express');
const path = require('path');
const app = express();
const port = 80; // You can change this to any port you prefer
// Serve static files from the 'public' directory
app.use(express.static(path.join(__dirname, 'public')));
// Define routes
app.get('/', (req, res) => {
res.send('Hello, this is the root route!');
});
app.get('/hello-word', (req, res) => {
// Send the HTML file for the '/hello-word' route
res.sendFile(path.join(__dirname, 'hello-world.html'));
});
// Start the server
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});
Create a sample page named hello-world.html, and utilize express.js to serve this page when a GET request for '/hello-world' is made.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello World</title>
</head>
<body>
<h1>Hello, World!</h1>
<p>This is the hello.html file served by Express.js.</p>
</body>
</html>
🚀 Launching an EC2 Instance
After completing the prerequisites, proceed with the following steps to deploy your Express.js application on an EC2 instance.
Make sure that your EC2 instance is operational and running.
Photo Credit to CodeToFun
Establish a connection to the EC2 instance using PuTTY.
Photo Credit to CodeToFun
Make sure that Node.js and npm are installed on your Amazon Ubuntu Instance.
Photo Credit to CodeToFun
Create a directory on your Amazon Ubuntu EC2 instance with the provided command.
sudo mkdir express-website
Photo Credit to CodeToFun
Grant permission to the directory to enable file uploads.
Before granting permission, the directory name appears in blue color with transparent background.
After granting permission, the directory name appears in blue color with green background.
Run the following command to grant permission.
sudo chmod 777 /home/ubuntu/express-website
Photo Credit to CodeToFun
Transfer your website files and folders from your local machine to an EC2 instance using Filezilla.
Photo Credit to CodeToFun
Next, use PuTTY to globally install PM2. Use the following command to install PM2.
sudo npm i -g pm2
Photo Credit to CodeToFun
Again, use PuTTY to globally install Express.js. Use the following command to install Express.js.
sudo npm i -g express
Navigate to the website directory and initiate the application with pm2 by executing the following command:
sudo pm2 start index.js
Photo Credit to CodeToFun
Congratulations! We have successfully initiated our Express.js application using pm2. You can now close PuTTY.
Proceed to step 1, where you should copy the Public IPv4 DNS or Public IPv4 address, then paste it into the URL and press Enter.
Photo Credit to CodeToFun
Our Express.js application is running successfully on our Amazon Ubuntu EC2 instance.
🎉 Conclusion
Congratulations! You've successfully deployed a Express.js website on an EC2 instance. Remember to monitor your application, set up security measures, and consider using a domain name for a production environment.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (Deploy Express.js Website on an EC2 Instance), please comment here. I will help you immediately.