Setup NodeJS With Nginx For Multiple App And Multiple Domain - CentOS/RHEL 6/7
Linux NodeJs

Setup NodeJS With Nginx For Multiple App And Multiple Domain – CentOS/RHEL 6/7

NodeJs is an open source JavaScript built on Chrome’s JavaScript runtime environment. Is used for easily building fast, scalable network server-side applications. The platform runs on Linux, OS X, FreeBSD, and Windows. Yum repository is maintained by node.js and we will use the same to install NodeJS on Centos/RHEL 6/7.




Just in case, if you did miss Previous tutorials, You can read here:

Scenario:

Host OS: CentOS/RHEL 6/7
Host IP: 192.168.1.62
RAM: 4GB Memory
Network Port: 1GB/s

Prerequisites:

We are using the same server for running node js and for the front web server but you can use two different servers for the same work:

  • For App Working And Running: We will install NodeJS runtime with a node.js application, and PM2 for managing the running of these services.
  • Front Web Server: Well in this scenario you may use same or different server but I am using the same server for web also. We will use Nginx web server, which will act as a reverse proxy to your application. Public IP of this web server will be used to access your Node.js application.

NOTE: For this tutorial, I am using the single server for app and front web server but as I said it is possible to use two different servers.  You only need to make a few changes along the way.

With the help of the following diagram, you will be able to understand the working of reverse proxy and NodeJs setup

Nginx Reverse Proxy For NodeJs

Let’s Start

Step 1: Install Required Repository

To install NodeJS package and other required packages, we need to install EPEL repository and some other repository to full fill dependencies. First, we will add the NodeJs yum repository with our system provided by NodeJS official website. You also need development tools to build native add-ons to be installed on your system.

For CentOS/RHEL 6 Only




Step 2: Install NodeJS And NPM

After adding yum repository in your system(Required for CentOS/RHEL 6 only), we will run below given command o install NodeJS and NPM. Also, allow other dependencies to get installed with installation

Step 3: Verify Installed NodeJs and NPM Version

Well, we have successfully installed NodeJS and NPM and now we can verify the installation by checking its version by the following command. Check the official website for current versions.

For NodeJS Version

For NPM version

Step 4: Install Nginx For Reverse Proxy Server

We are going to install NGINX web server, which will be used a reverse proxy. Well, in this step we will only install and start the Nginx server. So follow these quick steps. You may also follow complete guide

Step 5: Create Test App  In NodeJs (Optional)

Well, this is the step which you may skip but test our server I am creating a test NodeJS script. Let’s say our app name in test.js. We will create this script in HTML directory of Nginx server. Follow the following step to do the same.

Now append following content in the test.js file

Save and quit the file using following :wq! . Also, we need to give permission for execution so use the below command.

Step 6: Run The NodeJs Test App(optional)

We have our Test App created so now we will run this app in the background and check that our installation is successful on not. So use below given command to run the app in the background.

Now your NodeJS server is running at http://127.0.0.1:8000/ and we can test the running node by hitting http://127.0.0.1:8000/ URL in the browser locally or use curl -ivs http://127.0.0.1:8000/ locally.

Well it’s a success but in a production environment we usually set up a reverse proxy for accessing our NodeJS app. so let’s do its

Nginx Reverse Proxy For NodeJs



Step 7: Configuring Nginx As Reverse Proxy Server

Now our application is running, and listening on a private IP address or LocalHost (127.0.0.1), we need to set up a way for your users to access it publicly. We all know that we have an already installed NGINX in Step 3 so, now we will be configuring this NGINX as the reverse proxy. NGINX will work as the front end for the app and serve all the request. Now, follow the series of instruction to set up the reverse proxy. You can just copy the location block into the server block of your choice (make sure the location does not conflict with any of your web server’s existing content).

And append the following line inside the file

Save and quit the file using :wq! And restart the web server to get the changes in effect.

Step 8: Verify Our Working

Now we only have to hit the domain name of the app or the IP of the app server because we just configured NGINX virtual host. Virtual Host will do the rest of task to access the app over port 80 and NGINX will pass away the request over port 8000 by itself. So now hit http://youDomaiName.com/ . well in my case I will in my case I will it http://192.168.1.62

If you can see below image then everything is working ok.

nodejs with reverse proxy




Read More In Related Article:

  1. Setup NodeJS With Nginx For Multiple App And Multiple Domain – CentOS/RHEL 6/7
  2. Learn NodeJS App Management And Getting Started With PM2 On RHEL/CentOS 6/7
Kapendra
Love to write technical stuff with personal experience as I am working as a Sr. Linux Admin. and every day is a learning day and Trust me being tech geek is really cool.
http://kapendra.com

Leave a Reply