Apache and Node - Js On The Same Server - Stack Overflow
Apache and Node - Js On The Same Server - Stack Overflow
I want to use Node because it's swift, uses the same language I am using on the client side, and
it's non-blocking by definition. But the guy who I hired to write the program for file handling
397 (saving, editing, renaming, downloading, uploading files, etc.), he wants to use apache. So, I must:
2. Figure out how to upload, download, rename, save, etc. files in node or
apache node.js
Share Improve this question Follow edited Oct 8, 2015 at 16:20 asked Mar 22, 2012 at 22:37
Michael Irigoyen Matt
22.7k 18 90 132 5,603 5 25 32
Great question!
There are many websites and free web apps implemented in PHP that run on Apache, lots of
773
people use it so you can mash up something pretty easy and besides, its a no-brainer way of
serving static content. Node is fast, powerful, elegant, and a sexy tool with the raw power of V8
and a flat stack with no in-built dependencies.
I also want the ease/flexibility of Apache and yet the grunt and elegance of Node.JS, why can't I
have both?
Fortunately with the ProxyPass directive in the Apache httpd.conf its not too hard to pipe all
requests on a particular URL to your Node.JS application.
Also, make sure the following lines are NOT commented out so you get the right proxy and
submodule to reroute http requests:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
Then you can access all Node.JS logic using the /node/ path on your url, the rest of the website
can be left to Apache to host your existing PHP pages:
Now the only thing left is convincing your hosting company let your run with this configuration!!!
Share Improve this answer Follow edited Jan 30, 2018 at 23:33 answered Sep 4, 2013 at 1:34
YakovL Steven de Salas
7,951 13 67 106 21.3k 9 75 82
7 This was a great answer, just wanted to add a link with a little more info on proxy pass that I used to make
this work. Check the comments as well.boriskuzmanovic.wordpress.com/2006/10/20/… – Alex Muro Oct 15,
2013 at 22:08
12 I tested putting "ProxyPass / 127.0.0.1:8000" inside a virtual host container and was able to successfully
redirect an entire domain group to a node instance. I also tested with "time wget..." to compare speed of
accessing node directly to accessing it over Apache. In 30 pairs of trials, the average difference was about
0.56ms. The lowest load time was 120ms for both direct and via Apache. The highest load time was 154ms
for direct and 164 via Apache. Not a significant difference. If I had the luxury of two IPs I would not route
through Apache, but for now I will stick with Proxypass – kaan_a Mar 19, 2014 at 15:59
5 Doesn't this proxy requests from Apache to Node, while it takes away the benefits of Node's non-blocking
nature? – Trace Sep 16, 2014 at 20:52
2 Hi @Basj, I dont have experience installing support for websockets myself. Having said that, Apache 2.4.6
appears to have support for proxying websockets traffic with using mod_proxy_wstunnel . I see you have
now found your answer, for others with same problem please refer to:
serverfault.com/questions/616370/… – Steven de Salas Dec 18, 2014 at 1:48
4 Where do I add this on debian based distributions? There's no httpd.conf file. – santi Jul 27, 2015 at 12:11
This question belongs more on Server Fault but FWIW I'd say running Apache in front of Node.js
is not a good approach in most cases.
86
Apache's ProxyPass is awesome for lots of things (like exposing Tomcat based services as part of
a site) and if your Node.js app is just doing a specific, small role or is an internal tool that's only
likely to have a limited number of users then it might be easier just to use it so you can get it
working and move on, but that doesn't sound like the case here.
If you want to take advantage of the performance and scale you'll get from using Node.js - and
especially if you want to use something that involves maintaining a persistent connection like
web sockets - you are better off running both Apache and your Node.js on other ports (e.g.
Apache on localhost:8080, Node.js on localhost:3000) and then running something like nginx,
Varnish or HA proxy in front - and routing traffic that way.
With something like varnish or nginx you can route traffic based on path and/or host. They both
use much less system resources and is much more scalable that using Apache to do the same
thing.
Share Improve this answer Follow edited Sep 17, 2015 at 12:34 answered Oct 6, 2014 at 20:18
Iain Collins
6,853 3 44 43
1 Do you have some numbers to back up your statement that nginx would be less resource intensive than
httpd? – RedShift Oct 3, 2018 at 18:30
I don't though it's quite dramatic. While I try not to link out in replies as links are fragile but you can find
some discussion and examples via Google – e.g. help.dreamhost.com/hc/en-us/articles/… … Apache is
great software but typically it's not a great approach in a context like this. – Iain Collins Oct 4, 2018 at 23:35
This answer sounds good, but then how to access to Node.js through httpS as it is already taken by Apache
? – Pierre Apr 29, 2019 at 15:38
2 Nginx is faster I agree, but then you have the overhead of an extra service to configure and manage. Since
the question asks for Apache and Node on the same server, it seems like Nginx is a bit of a third wheel.
– Steven de Salas Jun 3, 2020 at 10:42
CUSTOM is your newly created filename without extension, then enable proxy_http with the
command:
it should enable both proxy and proxy_http modules. You can check whether module is enabled
or not with:
After configuration and modules enabled, you will need to restart apache server:
Now you can execute node server. All requests to the URL/node will be handled by node server.
Share Improve this answer Follow edited Sep 20, 2016 at 9:50 answered Apr 3, 2016 at 10:36
krmld
1,308 15 9
I get internal server error (500) with no indication whatsoever what goes wrong. Any idea what may cause
it or where I can see some logs? I'm new to vps and linux/ubuntu in general. – DFSFOT Jun 4, 2021 at 17:53
1 If the first argument ends with a trailing /, the second argument should also end with a trailing /, and vice
versa. Otherwise, the resulting requests to the backend may miss some needed slashes and do not deliver
the expected results. – Mike Lowery May 6, 2023 at 20:56
Worked great. The only thing when I tried to add a second route to the express server (besides "/"), I
needed to change the .conf to ProxyPass /node http://localhost:9001 and in server.js I stuck to
app.get("/about",.... and I can navigate to myserver.com/node/about perfectly! – cdsaenz Feb 29 at
13:24
Running Node and Apache on one server is trivial as they don't conflict. NodeJS is just a way to
execute JavaScript server side. The real dilemma comes from accessing both Node and Apache
17 from outside. As I see it you have two choices:
1. Set up Apache to proxy all matching requests to NodeJS, which will do the file uploading
and whatever else in node.
2. Have Apache and Node on different IP:port combinations (if your server has two IPs, then
one can be bound to your node listener, the other to Apache).
I'm also beginning to suspect that this might not be what you are actually looking for. If your end
goal is for you to write your application logic in Nodejs and some "file handling" part that you
off-load to a contractor, then its really a choice of language, not a web server.
Share Improve this answer Follow edited Feb 19, 2016 at 19:01 answered Apr 21, 2013 at 0:16
LJones Yarek T
35 1 8 9,815 2 29 40
You can use a different approach such as writing a reverse proxy server with nodejs to proxy both
apache and all other nodejs apps.
11
First you need to make apache run on a different port other than port 80. ex: port 8080
Then you can write a reverse proxy script with nodejs as:
proxy.register("mydomain.me/blog", "http://mydomain.me:8080/blog");
proxy.register("mydomain.me", "http://mydomain.me:3000");
Share Improve this answer Follow edited Dec 16, 2019 at 18:36 answered Jun 14, 2016 at 18:34
wathmal
371 4 9
I combined the answer above with certbot SSL cert and CORS access-control-allow-headers and
got it working so I thought I would share the results.
9
Apache httpd.conf added to the bottom of the file:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
Apache VirtualHost settings (doc root for PHP is under Apache and SSL with Certbot, while
node.js/socket.io site runs on port 3000 - and uses SSL cert from Apache) Also notice the node.js
site uses the proxy for the folder /nodejs, socket.io, and ws (websockets):
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName www.example.com
ServerAlias www.example.com
DocumentRoot /var/html/www.example.com
ErrorLog /var/html/log/error.log
CustomLog /var/html/log/requests.log combined
SSLCertificateFile /etc/letsencrypt/live/www.example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/www.example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
RewriteEngine On
RewriteCond %{REQUEST_URI} ^socket.io [NC]
RewriteCond %{QUERY_STRING} transport=websocket [NC]
RewriteRule /{.*} ws://localhost:3000/$1 [P,L]
</VirtualHost>
</IfModule>
http.listen({host:'0.0.0.0',port:3000});
io.of('/nodejs').on('connection', function(socket) {
//optional settings:
io.set('heartbeat timeout', 3000);
io.set('heartbeat interval', 1000);
In this example when the JS loads, it will emit to the socket a "named-event" sending the data in
JSON to the node.js/socket.io server.
Using the io and socket on the server under path /nodejs (connected by client), receives the data
an then resends it as a broadcast. Any other users in the socket would receive the data with their
listener "named-event-broadcast". Note that the sender does not receive their own broadcast.
Share Improve this answer Follow answered May 28, 2020 at 19:29
Dr. Aaron Dishno
1,899 1 30 24
3 this worked for me when I made above entry in httpd-vhosts.conf instead of httpd.conf
I have XAMPP installed over my environment & was looking to hit all the traffic at apache on
port 80 with NodeJS applicatin running on 8080 port i.e.
http://localhost/[name_of_the_node_application]
I recently ran into this kinda issue, where I need to communicate between client and server using
websocket in a PHP based codeigniter project.
0
I resolved this issue by adding my port(node app running on) into Allow incoming TCP ports &
Allow outgoing TCP ports lists.
You can find these configurations in Firewall Configurations in your server's WHM panel.
Share Improve this answer Follow answered Jan 23, 2020 at 10:45
Paritosh Pandey
115 6
I was looking for the same information. Finally found the answer from the link on the answer
above by @Straseus
-1
http://arguments.callee.info/2010/04/20/running-apache-and-node-js-together/
Here is the final solution to run apache website on port 80, node js service on port 8080 and use
.htaccess RewriteRule
<IfModule mod_rewrite.c>
RewriteEngine on
# More complicated (the user sees only "benchmark.html" in their address bar)
RewriteRule ^benchmark.html$ http://arguments.callee.info:8000/node?action=benchmark
[P]
For the directory level redirect, the link above suggested (.+) rule, which requires one or more
character after the 'node/'. I had to convert it to (.*) which is zero or more for my stuff to work.
Share Improve this answer Follow answered Oct 12, 2013 at 0:30
pd1980
103 1 8
3 Just note that the [P] flag requires Apache's mod_proxy to be enabled. – Simon E. Jun 20, 2014 at 10:01
This is inefficient. Why invoke the Rewrite engine over a simple ProxyPass ? – Michael Irigoyen Oct 12,
2015 at 13:42
I am assuming that you are making a web app because you refer to Apache and Node. Quick
answer - Is it possible - YES. Is it recommended - NO. Node bundles it's own webserver and most
-5 websites run on port 80. I am also assuming that there is currently no Apache plugin which is
supported by Nodejs and I am not sure if creating a virtual host is the best way to implement
this. These are the questions that should be answered by developers who maintain Nodejs like
the good folks at Joyent.
Instead of ports, it would be better to evaluate Node's tech stack which is completely different
from most others and which is why I love it but it also involves a few compromises that you
should be aware of in advance.
Your example looks similar to a CMS or a sharing web app and there are hundreds of out of the
box apps available that will run just fine on Apache. Even if you do not like any readymade
solution, you could write a webapp in PHP / Java / Python or mix n match it with a couple of
ready made apps and they are all designed and supported to run behind a single instance of
Apache.
Now you are ready to decide on which techstack you are going to use. If your website will never
use any out of the thousands of ready made apps that require Apache, then go for Node
otherwise you must first eliminate the assumptions that I have stated earlier.
In the end, your choice of techstack is way more important than any individual component.
I completely agree with @Straseus that it is relatively trivial to use node.js file system api for
handling uploads and downloads but think more about what you want from your website in the
long run and then choose your techstack.
Learning Node's framework is easier than learning other frameworks but it is not a panacea. With
a slightly more effort (which may be a worthwhile endeavor in itself), you can learn any other
framework too. We all learn from each other and you will be more productive if you are working
as a small team than if you are working alone and your backend technical skills will also develop
faster. Therefore, do not discount the skills of other members of your team so cheaply.
This post is about a year old and chances are that you have already decided but I hope that my
rant will help the next person who is going through a similar decision.
Share Improve this answer Follow edited Apr 21, 2013 at 0:14 answered Apr 20, 2013 at 23:36
RHT
5,044 4 27 32