Sort:  

Part 1/8:

Hosting a Node.js Application on AWS EC2

Deploying web applications can seem daunting, especially for developers just starting their journey in cloud hosting. However, by utilizing Amazon Web Services (AWS) EC2 instances, you can seamlessly host your Node.js application on the internet. In this guide, we’ll walk through the essential steps involved in deploying a Node.js app connected to a PostgreSQL database hosted on an EC2 instance.

Setting Up Your EC2 Instance

To begin, log into your AWS Console and navigate to the EC2 dashboard. The first step is to launch an EC2 instance.

  1. Choosing the Instance Type:
  • After selecting to launch an instance, name your application (e.g., 'my web app').

Part 2/8:

  • For the base image, select Ubuntu as it’s a popular and user-friendly operating system.
  1. Selecting Instance Type:
  • The default T2 micro instance type is appropriate for this project. This option is especially useful if you are within the free tier of AWS.
  1. Setting Up Authentication:
  • Create a new key pair, which enables you to log in via SSH from your local machine. Name the key pair relevant to your region and save it.
  1. Configuring Network Settings:
  • Set up a firewall to control incoming traffic. Allow SSH (for your IP) and HTTP/HTTPS for web traffic, ensuring a secure connection.
  1. Launch Instance:
  • After confirming all settings, click Launch Instance to create your virtual machine.

Connecting via SSH

Part 3/8:

Once your instance is launched, you’ll need to connect to it using SSH:

  1. Use the downloaded key pair to establish a connection from your terminal. Move the key file to your SSH folder for better organization.

  2. Ensure the permissions on the key file are set correctly to enable a secure connection.

  3. Execute the SSH login command using the provided instance details. Upon successful connection, your terminal will indicate that you are now operating within the Ubuntu environment.

Initial Configuration

Perform some initial configurations to prepare your environment:

  1. Updating Packages:
  • It's crucial to update and upgrade Linux packages to maintain security and functionality.
  1. Installing Node.js:

Part 4/8:

  • Subsequently, proceed to install Node.js, as it serves as the runtime for your application. You can check the installation by running commands within the Node environment.
  1. Preparing Your Application Code:
  • Transfer your application files from your local machine to the EC2 instance. Using rsync allows you to efficiently sync your files, minimizing transfer size by only sending updated files.
  1. Building Your Application:
  • If you’ve written your application in TypeScript, compile it into JavaScript on the server using npm commands.

Setting Up PostgreSQL Database

With your application code in place, the next step is to set up your PostgreSQL database:

  1. Install PostgreSQL:
  • Run the necessary commands to install the PostgreSQL database on your EC2 instance.

Part 4/8:

  • Subsequently, proceed to install Node.js, as it serves as the runtime for your application. You can check the installation by running commands within the Node environment.
  1. Preparing Your Application Code:
  • Transfer your application files from your local machine to the EC2 instance. Using rsync allows you to efficiently sync your files, minimizing transfer size by only sending updated files.
  1. Building Your Application:
  • If you’ve written your application in TypeScript, compile it into JavaScript on the server using npm commands.

Setting Up PostgreSQL Database

With your application code in place, the next step is to set up your PostgreSQL database:

  1. Install PostgreSQL:
  • Run the necessary commands to install the PostgreSQL database on your EC2 instance.

Part 5/8:

  1. Create and Configure the Database:
  • Once installed, set up the database and roles needed for your application. Be sure to grant appropriate privileges to your new user.
  1. Migrations:
  • If you have existing migrations, run the necessary commands to set up your database schema.
  1. Connecting Application to Database:
  • Ensure your application has the correct environment variables pointing to your database.

Running the Application on the Server

With everything in place, it's time to run the application:

  1. Use npm start to launch your Node.js application, which will run it on port 3000.

  2. Adjust your EC2 security group settings to allow traffic on port 3000 temporarily for testing.

Setting Up a Reverse Proxy with Caddy

Part 6/8:

To make your application accessible via HTTP and HTTPS, you must set up a reverse proxy:

  1. Install a Reverse Proxy:
  • Caddy is an excellent choice due to its simplicity. Configure it to listen on port 80 and forward traffic to your Node application on port 3000.
  1. Obtain a Domain Name:
  • Integrate services like Route 53 to allocate a domain to your EC2 instance. Define routing rules to connect your domain to the instance IP address.
  1. Enforce HTTPS:
  • Caddy automatically manages SSL certificates, providing an additional layer of security.
  1. Final Testing:
  • Confirm that your application functions correctly over your custom domain using HTTPS.

Running Your Application as a Service with SystemD

To keep your application running in the background:

Part 6/8:

To make your application accessible via HTTP and HTTPS, you must set up a reverse proxy:

  1. Install a Reverse Proxy:
  • Caddy is an excellent choice due to its simplicity. Configure it to listen on port 80 and forward traffic to your Node application on port 3000.
  1. Obtain a Domain Name:
  • Integrate services like Route 53 to allocate a domain to your EC2 instance. Define routing rules to connect your domain to the instance IP address.
  1. Enforce HTTPS:
  • Caddy automatically manages SSL certificates, providing an additional layer of security.
  1. Final Testing:
  • Confirm that your application functions correctly over your custom domain using HTTPS.

Running Your Application as a Service with SystemD

To keep your application running in the background:

Part 7/8:

  1. Create Environment Variables:
  • Store crucial environment variables in a hidden directory, ensuring sensitive information is kept secure.
  1. Set Up a SystemD Service:
  • Create a service file to handle starting, stopping, and restarting your application automatically.
  1. Restart and Test:
  • After configuring the service, restart it through SystemD to ensure everything is running optimally.

Conclusion

Part 8/8:

While deploying a Node.js application to an AWS EC2 instance may appear complex at first, breaking the process down into manageable steps makes it straightforward. This foundational knowledge not only allows you to host web apps but also prepares you for scale and increased efficiency. As you grow more comfortable with these steps, automating the deployment process will further enhance your workflow for larger projects.

By following these steps, you'll have successfully hosted your Node.js application with a PostgreSQL backend, and understand the necessary components involved in creating a robust web application ecosystem on AWS.