bayu nugraha
4 min readAug 1, 2019

Create a Simple Reverse proxy for Node.Js with Apache Web Server.

This tutorial for basic CI/CD for a beginner who wanted to start with simple Gitlab pipeline.

  1. First, We will create a server with Amazon EC2 instance and Install :
    1. Apache Web Server
    2. Node.Js
    3. Pm2
    4. After we installed all the tools above, we will make Load Balancing with Apache, and reverse the Apache proxy to Node.Js

Prerequisites

Step 1: SSH to your EC2 Instance to Install Apache and run it.
Update instance, sudo yum update -y before installing Apache
Install Apache with sudo yum -y install httpd
Start the service sudo systemctl start httpd.
sudo systemctl enable httpd to enable Apache and check the status of Apache sudo systemctl status httpd.

Status Apache

Step 2: Install Node.js
sudo yum install -y gcc-c++ make to install depedency
sudo curl -sL https://rpm.nodesource.com/setup_10.x | sudo -E bash -
sudo yum install nodejs
node -v
npm -v

Status Node.js

create directory mkdir myapp
cd to directory that was we made cd myapp
sudo npm install
to install npm in our directory
mv -v package-lock.json package.json for rename package-lock, because json file will save on the package.json file
npm install express — save install express framework for node.js
create a new .js file, nano app.js paste this script to text editor :
var express = require(‘express’);
var app = express();

app.get(‘/’, function (req, res) {
res.send(‘Hello World!’);
});

app.listen(3000, function () {
console.log(‘Example app listening on port 3000!’);
});

Save ctrl + x if you using nano text editor.
Run the script with node app.js

app.js

Open browser with your ip address:3000 like on this picture:
Note: IP address you get from your server, in this case, I used EC2 instance from AWS.

Hello World!

Step 3: Install Pm2
Pm2 is a tool for production process manager for Node.js
sudo npm install pm2 -g
pm2 start app.js
to run app.js like this picture:
We use pm2 to run 2 applications node.js, if you don’t use pm2 when you close from node.js, node.js will stop running.

Pm2 Running

Step 4: Create Load Balancing with Apache
check status httpd: httpd -M
check 4 modules in the output
proxy_module (shared)
. . .
lbmethod_byrequests_module (shared)
. . .
proxy_balancer_module (shared)
. . .
proxy_http_module (shared)

Output

Also check on sudo nano /etc/httpd/conf.modules.d/00-proxy.conf
LoadModule proxy_module modules/mod_proxy.so
. . .
LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
. . .
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
. . .
LoadModule proxy_http_module modules/mod_proxy_http.so

Output

and restart after checking the results sudo systemctl restart httpd

Create new default for virtual host sudo nano /etc/httpd/conf.d/default-siteconf
<VirtualHost *:80>
ProxyPreserveHost On

ProxyPass / http://**.**.***.***:3000/
ProxyPassReverse / http://**.**.***.***:3000/
</VirtualHost>
Change with your ip and the port that you type on your code .js before, in this tutorial i use port 3000, then save it and restart sudo systemctl restart httpd
And check your ip address on browser and we success create Reverse Proxying from port 3000 to port 80.

Before Reverse Proxy
After Reverse Proxy

Step 5: Load Balancing Across Multiple Backend Servers
Create app2.js on the same directory with app.js and change port anything you want, in this case, I use port 2000 and don't forget to open that port.
Start app2.js with pm2 start app2.js

app2.js
Start app2.js

sudo nano /etc/httpd/conf.d/default-site.conf open this and change like this save it and restart apache sudo systemctl restart httpd

If you access http//:Your ip address in a web browser, you will see your backend servers' responses instead of the standard Apache page. If you followed Step 2, refreshing the page multiple times should show Hello world! and Hello world2!, meaning the reverse proxy worked and is load balancing between both servers.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

bayu nugraha
bayu nugraha

Written by bayu nugraha

Linux Administrator, Network Administrator, Cloud Engineer, DevOps Enthusiast, Docker, Jenkins, Git, Gitlab, Kubernetes, Ansible.

No responses yet

Write a response