fbpx

How to Install Node.js and Nginx on Your EC2 Instance?

· >
install node.js & nginx on aws ec2 instance

Introduction

If you’re running a web application or website on AWS, chances are you’ll need to install Node.js and Nginx at some point. Node.js is an open-source, cross-platform JavaScript runtime environment that allows you to execute JavaScript code outside of a web browser. Nginx is a high-performance web server that can be used as a reverse proxy, load balancer, and HTTP cache. In this article, we’ll walk you through the steps to install Node.js and Nginx on an EC2 instance.

Prerequisites

Before we dive into the installation process, you’ll need to have the following:

  • An Amazon Web Services (AWS) account
  • An EC2 instance running a supported operating system (Ubuntu, Amazon Linux, or CentOS)
  • A user account on the EC2 instance with sudo privileges

Installing Node.js

Node.js is not available in the default package repositories for most Linux distributions. Therefore, we’ll need to add the NodeSource package repository to our system to install Node.js.

Step 1: Add the NodeSource package repository

To add the NodeSource package repository, you’ll need to execute the following command on your EC2 instance:

curl -sL https://deb.nodesource.com/setup_lts.x | sudo -E bash -

This command will download and execute a script that adds the NodeSource package repository to your system.

Step 2: Install Node.js

Once you’ve added the NodeSource package repository, you can install Node.js by running the following command:

sudo apt-get install -y nodejs

This command will install the latest LTS (Long-Term Support) version of Node.js on your system.

Step 3: Verify the installation

To verify that Node.js is installed correctly, you can run the following command:

node -v

This command will print the version number of Node.js that’s installed on your system.

Installing Nginx

Nginx is available in the default package repositories for most Linux distributions. Therefore, we can install Nginx using the package manager for our operating system.

Step 1: Install Nginx

To install Nginx, you can run the following command on your EC2 instance:

sudo apt-get install -y nginx

This command will install Nginx and all its dependencies on your system.

Step 2: Start Nginx

Once Nginx is installed, you can start the Nginx service by running the following command:

sudo systemctl start nginx

This command will start the Nginx service and enable it to start automatically at boot time.

How to fix common Nginx Web Server Errors

Must Read

Step 3: Verify the installation

To verify that Nginx is installed and running correctly, you can open a web browser and navigate to your EC2 instance’s public IP address. You should see the default Nginx welcome page.

Configuring Nginx as a Reverse Proxy for Node.js

Now that we have Node.js and Nginx installed on our EC2 instance, we can configure Nginx to act as a reverse proxy for Node.js.

Step 1: Create a Node.js application

Before you can install and configure Nginx on your EC2 instance, you need to create a Node.js application to serve as the backend for your website or web application. Node.js is a popular runtime environment that allows developers to run JavaScript code on the server-side.

To create a Node.js application, you first need to install Node.js on your EC2 instance. You can do this by logging into your instance using SSH and running the following command:

sudo apt-get update
sudo apt-get install nodejs

This will install the latest version of Node.js and its package manager, npm, on your EC2 instance.

Next, you can create a new directory for your Node.js application using the following command:

mkdir myapp
cd myapp

Once you’re in the directory, you can initialize a new Node.js application using npm with the following command:

npm init

This will prompt you to enter information about your application, such as its name, version, and dependencies. You can accept the defaults for most of the prompts, but you should specify the entry point of your application as app.js or another file that contains your application code.

Now that you’ve initialized your Node.js application, you can install any dependencies it needs using npm. For example, if you want to use the popular Express.js framework, you can install it using the following command:

npm install express

This will download and install the latest version of Express.js and any of its dependencies into a node_modules directory in your application.

With your Node.js application created and dependencies installed, you can now move on to configuring Nginx as a reverse proxy to your application.

Step 2: Configure Nginx as a reverse proxy

To configure Nginx as a reverse proxy, you’ll need to create a new Nginx server block by creating a new configuration file in the /etc/nginx/sites-available directory. For this tutorial, we’ll create a file called nodejs.conf.

Open the nodejs.conf file in a text editor and add the following configuration:

nginxCopy codeserver {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://localhost: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;
    }
}

This configuration tells Nginx to listen on port 80 for HTTP requests to example.com and proxy those requests to the Node.js application running on port 3000.

Step 3: Enable the new server block

Once you’ve created the nodejs.conf file, you can enable it by creating a symbolic link to the sites-enabled directory:

sudo ln -s /etc/nginx/sites-available/nodejs.conf /etc/nginx/sites-enabled/

This command will create a symbolic link to the nodejs.conf file in the sites-enabled directory, which tells Nginx to use this configuration.

Step 4: Restart Nginx

Once you’ve enabled the new server block, you can restart Nginx to apply the changes:

sudo systemctl restart nginx

This command will restart the Nginx service with the new configuration.

Step 5: Verify the configuration

To verify that Nginx is acting as a reverse proxy for Node.js, you can open a web browser and navigate to your EC2 instance’s public IP address or domain name. You should see the “Hello, World!” message from the Node.js application.

Conclusion

In this article, we’ve walked you through the steps to install Node.js and Nginx on an EC2 instance and configure Nginx as a reverse proxy for a Node.js application. By following these steps, you can host and scale web applications and websites on AWS with ease.

FAQs

1. Can I install Node.js and Nginx on a different operating system?

Yes, Node.js and Nginx can be installed on different operating systems. However, the installation process may vary depending on the operating system.

2. Do I need to install Nginx if I’m only using Node.js?

No, you don’t need to install Nginx if you’re only using Node.js. However, Nginx can be useful for load balancing, caching, and other web server functions.

3. Can I use a different web server instead of Nginx?

Yes, you can use a different web server instead of Nginx. However, the configuration may vary depending on the web server.

4. Do I need to configure a firewall for my EC2 instance?

Yes, it’s recommended to configure a firewall for your EC2 instance to restrict access to certain ports and services.
Subscribe
Notify of
guest
1 Comment
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Rasheed

You saved my day, subscribed!