Pergunta

I have a dedicated VPS running ZPanel (a managed LAMP stack installation) on Ubuntu. This allows me to easily add domains and PHP websites to my server and have each of them sandboxed to different ftp logins + mysql users which is pretty awesome.

Today I started messing around with some NodeJS apps. I was really impressed with the speed of these apps so I thought I'd throw them on my server. I created a new hosting account within zpanel as per usual, then started the app within an SSH terminal. As expected, the NodeJS app runs smoothly and I can still update its code via a separate FTP login just like I can with my existing PHP sites - fantastic!

All good up to here, but now comes the tricky part: getting NodeJS and Apache running in parallel on the same server...

The problem is that my NodeJS app runs on port 8080 (user defined) and Apache runs on port 80 (by default)

e.g. http://domain.com/:8080 <-- having the port in the url is obviously pretty confusing for users.

What would be the best solution to have any new nodejs apps & PHP websites accessible from http://newdomain.com/ as well as any existing PHP websites accessible from http://domain1.com/ , http://domain2.com/ or http://domain3.com/ instead of using the port?

These are the options I have come up with so far:

  1. Creating a 'sites-available' file on Apache for each domain that runs a Node app (ZPanel doesn't create this file by default). I'm guessing this is the simplest option but I've read that proxying from apache to Node is a speed & performance bottleneck as Apache is spawning additional processes on each request and then Node is doing its processing too.

  2. Changing all Apache websites to run off port 8080 and making Node run on port 80 instead. Then running a proxy with Node (on port 80 - the default http port) which would redirect a client to the correct url + port depending on the domain they visit. (Is it possible to run a node app globally for all domains attached to a server?) This seems like the most time consuming option, but wouldn't performance be greatly improved and my current hosting architecture would remain running pretty well?

  3. Any other option that I haven't thought of?

Remember: I am running every website (Node or PHP) on a different domain. I haven't come across a solution as sophisticated as this yet so I thought it'd be quite an interesting question as I'm sure a few others would be interested in a workflow like this (especially with git integrated!)

Please ask if further explanation is needed with this question and I will make edits if necessary.

Thanks in advance.

Foi útil?

Solução

Definitely proxy your Node server through Apache. Not only can Apache serve static files faster than Node probably ever will, but it’s really not much of a bottleneck – the opposite, however, is not true. (Although Nginx is definitely my preferred option.)

Give it a try with Apache Benchmark!

$ ab -c 1000 -n 5000 http://127.0.0.1/

My personal numbers are:

  • 22,281 requests/sec for static Nginx files
  • 5,514 requests/sec for Node
  • 4,729 requests/sec for Node via Nginx

If you’re looking for something much higher, Haskell’s Warp gets about 12,000/sec. :)

Outras dicas

You probably won't be able to make node run on port 80 without super-user privileges, and giving your node app that kind of power is simply not a good idea. Much safer to have apache or (also my preferred) nginx listening on 80 and passing along the requests. (nginx is friendlier for sockets as well, and someday you'll want those).

On my dev box right now I've got nginx proxying for both apache and node apps. Because you can't have too many servers, I say ;)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top