Deploying Your Express.js App on AWS EC2 with NGINX and a Custom Domain A Professional Guide
Deploying your web application can be a daunting task, but with AWS EC2, NGINX, and a custom domain, you can achieve a robust and scalable deployment. This guide will walk you through the entire process step by step.
Prerequisites
Before we begin, ensure you have the following:
- An AWS account
- A registered domain name
- Node.js and NPM installed locally
- Basic knowledge of SSH and the terminal
Step 1: Setting Up Your AWS EC2 Instance
- Launch an EC2 Instance:
- Log in to your AWS Management Console.
- Navigate to EC2 Dashboard and click "Launch Instance".
- Choose an Amazon Machine Image (AMI). We recommend the latest Ubuntu Server AMI.
- Select an instance type. The
t2.micro
is a good starting point as it is part of the free tier. - Configure instance details and add storage as needed.
- Add a tag (e.g., Name: MyExpressApp).
- Configure the security group. Allow HTTP (port 80) and SSH (port 22) traffic.
- Connect to Your Instance:
-
Once your instance is running, click on "Connect".
-
Use SSH to connect to your instance:
-
Step 2: Setting Up Node.js on EC2
-
Update your package lists:
-
Install Node.js and NPM:
-
Verify the installation:
Step 3: Deploying Your Express.js Application
-
Transfer your application to the EC2 instance:
You can use SCP to transfer files:
-
Install dependencies and start the application:
Replace
app.js
with the entry point of your application.
Step 4: Setting Up NGINX as a Reverse Proxy
-
Install NGINX:
-
Configure NGINX:
-
Open the default NGINX configuration file:
-
Replace the contents with the following:
Replace
your-domain.com
with your actual domain and ensure your Express.js app is running on port 3000. -
-
Restart NGINX:
Step 5: Setting Up Your Custom Domain
- Configure DNS Settings:
- Go to your domain registrar's website and find the DNS settings.
- Add an A record pointing to your EC2 instance’s public IP address.
- Verify the DNS Configuration:
- It might take a few minutes for DNS changes to propagate. You can use tools like
dig
ornslookup
to verify your domain points to your EC2 instance.
- It might take a few minutes for DNS changes to propagate. You can use tools like
Conclusion
Congratulations! Your Express.js application is now deployed on an AWS EC2 instance, served by NGINX, and accessible via your custom domain. This setup not only provides a professional and scalable solution but also leverages the power of AWS for reliable hosting.
Feel free to leave comments or questions if you need further assistance. Happy coding!