Hosting a Nodejs server on AWS EC2

Hosting a Nodejs server on AWS EC2

Getting to production

Table of contents

No heading

No headings in the article.

Hosting a Node.js server on AWS EC2 with Nginx can be a great way to scale and manage your application. In this blog post, we will walk through the steps of setting up an EC2 instance, installing Node.js and Nginx, and configuring the server to handle incoming requests.

  1. Setting up an EC2 instance:
  • First, log in to your AWS account and navigate to the EC2 dashboard.

  • Click on the "Launch Instance" button to create a new instance.

  • Choose an Amazon Machine Image (AMI) that has Node.js pre-installed, such as the Node.js on Ubuntu 18.04 image.

  • Select the instance type and configure the settings as needed.

  • In the "Configure Security Group" step, make sure to open port 80 (for HTTP traffic) and port 443 (for HTTPS traffic) so that the server can handle incoming requests. It's important to note that you should choose the right instance type based on the resources your application needs. For example, if your application requires a lot of memory, you should select an instance type with more RAM. You should also choose the region that is closest to your target audience for better performance.

  1. Installing Node.js:
  • Connect to your EC2 instance using SSH.

  • Once connected, update the package manager by running the command: sudo apt-get update

  • Install Node.js by running the command: sudo apt-get install nodejs

  • Confirm the installation by running the command: node -v By default, Ubuntu 18.04 comes with version 8 of Node.js, if you need a different version you can install it from the NodeSource repository, or you could use a package manager like nvm to manage multiple Node.js versions.

  1. Installing Nginx:
  • Install Nginx by running the command: sudo apt-get install nginx

  • Start the Nginx service by running the command: sudo service nginx start

  • Confirm that Nginx is running by visiting the public IP address of your EC2 instance in a web browser. You should see the "Welcome to nginx" page. Nginx is an open-source web server and reverse proxy, it can handle a large number of concurrent connections and is known for its high performance and low resource consumption. It's a great choice for serving your Node.js application as it can handle the incoming requests and handle the load balancing.

  1. Configuring Nginx:
  • Stop the Nginx service by running the command: sudo service nginx stop

  • Open the Nginx configuration file by running the command: sudo nano /etc/nginx/sites-available/default

  • In the server block, update the location / block to proxy requests to your Node.js server.

  • Add the following lines to the location / block:

Copy code    proxy_pass http://127.0.0.1:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
  • Save and exit the file

  • Start the Nginx service again by running the command: sudo service nginx start By configuring Nginx to proxy requests to your Node.js server, it will handle the load balancing and distribute the incoming requests among multiple Node.js