How to Run Node.js server and Apache server together..

Chamindika Kodithuwakku
4 min readApr 18, 2020

--

Hi guys, Its really challenging to run an Apache server and node.js application on it at the same time. But it’s not hard to do with a reverse Proxy. Let’s do it in the easy way using reverse Proxy technique to make an Apache server able to run node.js application on any URL on Apache server.

What is Node.js?

Node.js is an open-source, cross-platform, JavaScript run time environment that executes JavaScript code outside of a web browser which is a server-side platform. Node.js server makes your application available to serve HTTP / HTTPS requests. Normally in Windows, Node.js run on 80 and 3000 ports but it can be changed as you preferred.

What is Apache server?

Apache Server is an open-source and free web server software. It is not a physical server, but rather a software that runs on a server. Apache is a cross-platform software, therefore it works on Unix,Linux and Windows servers. Apache server and the client communicate through HTTP protocol, and it is responsible for smooth and secure communication between two machines.

Why we cannot run both node.js and Apache server to listen the same port ?

Because these two are communicating through the HTTP protocols and each server has different ports to run. Since only one process can listen on any given port, both cannot run on same port directly. So, you will have to dispatch somehow. An easy solution is to run both together is to run node.js server and Apache server using reverse Proxy technique.

What is reverse Proxy technique?

A reverse proxy is a server that sits in front of web servers and forwards client requests to those web servers. A reverse proxy server is a type of proxy server that typically sits behind the firewall in a private network and directs client requests to the appropriate backend server. A reverse proxy provides an additional level of abstraction and control to ensure the smooth flow of network traffic between clients and servers.

Then how to use reverse proxy technique to solve our problem.

Since we cannot run both node.js server and the Apache server to listen on same port, we need to config Apache to act like a reverse proxy and pass the request to node.js application for a specific URL.

For example, if you have the Apache server running on localhost and want to run node.js application on localhost/node. This looks like as follows.

The Solution

First let’s start a simple node application to listen on port 3000. Here is a simple application listening to HTTP requests and return a simple “Hello World” text.

Now execute the app.js file to start the server and you will be able to see it on the browser if you navigate to localhost port 3000 (localhost:3000) as follows.

Next, go to xampp apache config and just head to the “Apache (httpd.conf)” file. We have to make the apache re-route the request by using ProxyPass and add “ProxyPass / node http://localhost:3000/” to the file as follows.

You can change the “/node” to whatever URL that you want to serve the node application. Also, you can change the “http://localhost:3000/” according to your node port.

Then, make sure that you have enabled “mod_proxy” and “mod_proxy_http” modules by removing the comment.

After that, Save the config file and restart apache server. Then let’s navigate to “localhost/node” and you can see node.js running on the same apache server.

WoW ! It is working! :)

This is how to use reverse proxy technique to make an apache server able to run node.js application on the same server.

This approach is suitable for a specific role with a limited number of users. But if you want to have the performance and scalability, you need to run Apache server and node.js server separately.

If you found it useful please hit that 👏 .

--

--

Chamindika Kodithuwakku
Chamindika Kodithuwakku

Responses (1)