• Translate

10/05/2018

Tutorial How to Run Node.js on the Apache Server

Tutorial Web Design |

Advertisement

If you have a website that runs in apache but also wants node.js to serve several urls on your website, then in this tutorial we will discuss a little how to use reverse proxy techniques to make apache run the node.js application on the same server.

↪ Concept

Because we cannot run node.js and apache to use the same port, we need to configure apache to act like a reverse proxy and forward the request to the node.js application for a particular url.

For example, if you already have an Apache server running on localhost and want to run the Node.js application on localhost/node, the path will look like this.
Concept How to Run Node.js on the Apache Server



↪ Implementation

First let's start the application node to monitor port 3000.
const express = require('express')
const app = express()
app.get('/', (req, res) => res.send('Hello World! from Node.js'))
app.listen(3000, () => console.log('Example app listening on port 3000!'))
This is an example of a simple application for serving http requests using Express and returning simple text that can certainly be seen in the browser if accessing localhost: 3000
a simple application for serving http requests using Express
Next we will make Apache re-route requests by using directives from proxypass. Just open the httpd.conf file and add the line below:
ProxyPass /node http://localhost:3000/
You can replace /node into whatever url you want to serve your node application. Then, make sure you have activated the mod_proxy and mod_proxy_http modules by removing the comment sign (#). 
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
Save the file, then restart your Apache server. Now when you access localhost, this page runs on the Apache server. Now try accessing localhost/node and see the results.
Run Node.js on the Apache Server

It should be noted that this approach is suitable for certain roles with a limited number of users. But if you want to have performance scalability, you must run Apache and node.js separately and use others like nginx to do the opposite.

Hopefully this gives you inspiration to apply to your work, and do not hesitate to comment on the columns that we have provided below.

Disclaimer: Images, Content of articles or videos that exist on the web sometimes come from various sources of other media. Copyright is fully owned by the source. If there is a problem with this matter, you can contact us here.
How to style text in Disqus comments:
  • To write a bold letter please use <strong></strong> or <b></b>.
  • To write a italic letter please use <em></em> or <i></i>.
  • To write a underline letter please use <u></u>.
  • To write a strikethrought letter please use <strike></strike>.
  • To write HTML code, please use <code></code> or <pre></pre> or <pre><code></code></pre>, and please parse the code in the parser box below.
Show Parser Box