Deploying your Node.js app to the cloud has never been easier. In 2024, AWS EC2 remains a top choice for developers seeking to deploy their applications. As an Infrastructure as a Service (IaaS), EC2 frees you from worrying about the physical layer of servers, allowing you to focus on what matters most: your application.
Ready to get started? You'll need these things:
🚀 An AWS account - Your launchpad to the cloud
🐙 A GitHub account
💻 Node.js on your local machine - The engine of your app
🌿 Git installed locally
🧠 Basic know-how of:
🟢 Node.js and Express - The dynamic duo of backend development
🔀 Git
🔐 SSH - The secret handshake of secure connections
🐧 Linux commands - Speaking the language of your server
Here are the top 3 benefits of using AWS EC2 for Node.js applications:
Instantly adjust your app's resources to match demand, ensuring smooth performance during traffic spikes without overpaying for unused capacity.
Choose from a variety of instance types optimized for different workloads, allowing you to fine-tune your environment for optimal Node.js performance.
Pay only for the computing power you use, with options to reserve instances for long-term savings, making it budget-friendly for projects of all sizes.
Solution 1: Setcap
Solution 2: Setting Up HTTPS with NginxStep 6: Running the Node.js App
This simplified process allows you to deploy your Node.js app to the cloud efficiently. Each step is essential for setting up a suitable environment for your app on EC2.
After creating your account, AWS will ask you to verify your identity. This typically involves providing a phone number for a verification code. This step is crucial for maintaining the security of your cloud infrastructure and preventing unauthorized access.
3.1: Sign in to AWS Management Console:
Navigate to EC2 Dashboard.
3.2: Select "Launch Instance" to create your new EC2 server.
3.3: Choose an Amazon Machine Image (AMI)
3.4: Choose Instance Type
Choose your server's power:
3.5: Key pair (login)
You can either select or create a New Key Pair.
3.6: Configure Instance
Configure instance settings, including network configurations, storage options, and security groups.
3.7: Add Tags (Optional).
3.8: Configure Security Group
If you want to use HTTPS for your website and have SSL certificates installed on your EC2 instance, ensure port 443 is allowed in your Security Group.
3.9: Review and Launch
3.10: Review your instance configuration and launch the instance
Replace the placeholders with your actual key pair path and instance DNS. This command establishes a secure connection to your cloud environment, allowing you to begin setting up your Node.js application.
Replace the placeholder repository name. If you're using an existing Node.js project, skip the following steps and go directly to step 4 Deploying and Managing Your Application.
To install or update nvm, use the following cURL or Wget command:
Running either command downloads a script that clones nvm to ~/.nvm and updates the appropriate profile file.
In your terminal window, paste or type the commands below:
The -y flag automatically uses default settings.
⏭️ Express is a popular choice for creating web applications with Node.
⏭️ Dotenv loads environment variables from .env file into Node.js's process.env object.
⏭️ npm uses the package.json file to identify a project and manage its dependencies. It includes essential project metadata.
Write your start script:
Basically, you can kick off your server by running npm start.
⏭️ Create a .env file
Create a .env file at the root, and add the below code: file at the root, add the below code:
⏭️ Make a new file called app.js and insert the code below
⏭️ Start the server
Execute the following command in your terminal:
⏭️ Push it to GitHub
To ensure your Node.js app runs smoothly in production, consider using a process manager like PM2. PM2 keeps your application running continuously and can automatically restart it if it crashes.
Here's a quick guide to deploying with PM2:
1. Install PM2 globally:
2. Navigate to your application directory
3. Start your app:
To check if your app is running, use this -
Open a web browser and go to your EC2 instance's public IP address or DNS name to access your Node.js application.
To deploy a Node.js app on EC2 using HTTPS, you need root privileges to bind to port 443. There are two solutions. Shorter Loop usesolution1.
To let your Node.js app use port 443 without root privileges, use the setcap command to give it the necessary permissions.
Consider using Nginx as a reverse proxy. It can handle HTTPS, terminate SSL/TLS, and forward requests to your Node.js app on a port that doesn't require root privileges.
Steps to Set Up Nginx as a Reverse Proxy:
1. Install Nginx:
First, install Nginx on your EC2 instance:
2. Configure Nginx:
Create or modify the Nginx configuration file (usually located in /etc/nginx/sites-available/default) to include the following:
3. Enable the Site and Restart Nginx:
4. Make sure you port 443 is enabled in aws security group.
5. Check the status
After setting up, verify that port 443 is listening:
Congratulations on deploying your Node.js application on AWS EC2 using PM2! This foundation opens up exciting possibilities for your projects. However, remember that AWS charges for active services, so be vigilant about your usage to avoid unexpected costs. Consider using an Elastic IP for a static address, but note that it's only free when associated with a running instance.
As you grow more comfortable with AWS, explore its ecosystem to enhance your app's performance and reliability. The cloud journey is ever-evolving, so keep experimenting and building. Just remember to monitor your resources closely and shut down unused services to keep costs in check.
Your next great innovation awaits – happy cloud computing!