Run Node.js on the Apache server
If you have a website that runs on apache but also want node.js to server several urls on your website, then in this tutorial we will discuss a little how to use a reverse proxy technique to make apache be able to run node.js applications 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 requests to the node.js application for certain urls.
Example
if you already have an Apache server running on localhost and want to run the Node.js application on localhost / node, then the flow will look like this.
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 you can certainly see in the browser if you access localhost: 3000.
Next we will make Apache re-route the request using directions from proxypass.
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 service your node application.
Then,
make sure you have activated the mod_proxy and mod_proxy_http modules by uncommenting them.
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
Save the file, please 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.
Note
That this approach is suitable for certain roles with a limited number of users. If you want to have performance scalability, you have to run Apache and node.js separately and use something else like nginx to do the opposite.
Hopefully it gives you inspiration to apply to your work, and don’t hesitate to leave a comment in the column that we have provided below.
Aw, this was an exceptionally nice post. Finding the time and actual effort to make a
really good article… but what can I say… I hesitate a
lot and don’t seem to get anything done.
It’s very straightforward to find out any matter on net as compared to textbooks,
as I found this piece of writing at this site.
Very good information. Lucky me I came across your site by chance (stumbleupon).
I’ve bookmarked it for later!
I am curious to find out what blog system you
are using? I’m experiencing some minor security problems with my
latest site and I’d like to find something more secure.
Do you have any recommendations?
It’s amazing to go to see this web page and reading the views of all friends concerning this article, while I am also
eager of getting experience.
That is very attention-grabbing, You are an excessively
skilled blogger. I’ve joined your rss feed and look forward
to in the hunt for extra of your magnificent post. Additionally, I have shared
your web site in my social networks
Its not my first time to visit this web page, i am browsing this site dailly and get good data from here everyday.
I visited several web sites but the audio
feature for audio songs present at this site is truly superb.
Nice post. I learn something new and challenging on blogs I stumbleupon everyday.
It will always be interesting to read through articles from other
writers and practice something from other sites.
Thank you for the good writeup. It in fact was a amusement account it. Look advanced to far added agreeable from you! By the way, how can we communicate?