nodejs or apache as first server - node.js

I have a website that runs on nodejs and for the blog this website uses wordpress(PHP). So I have decide to use either apache or nodejs as reverse proxy server. If I use node js as reverse proxy then I can easily scale my project by using PM2's cluster feature. But If I use apache then I can not use PM2 to scale up apache. Instead I have to tweak apache process pools and etc to scale up which I feel quite tedious. So what should I choose for reverse proxy apache or nodejs

Neither.
For reverse proxy, forget about Apache, you need to run Nginx, it's a tried and true service for reverse proxy setups and it works with PM2:
http://pm2.keymetrics.io/docs/tutorials/pm2-nginx-production-setup

Related

Load balancing in node server

I have created a node server using express. I using this architecture as follows:
-> I am serving node port as proxy on domain using apache.
-> I am using pm2 for handling node process. I have created two cluster and ran individually on different cores. (http://pm2.keymetrics.io/docs/usage/cluster-mode/)
My question is
Am i doing this correct way as production standard?
Do i need load balancing on apache level? because clusters will come
in picture after apache?
Am i correct?
Yes, that's correct architecture.
But Nginx and Pm2 go more hand in hand. Apache is okay too.

Can we use both NGINX and PM2 for node.js production deployment?

I am new to Node.js. I have built my first Node.js server. I am doing some research to improve performance of node js server in production. So I learned about NGINX and Process Manager(PM2).
NGINX:
It can load balance the incoming requests.
It can act as reverse proxy for our application.
PM2:
It can divide our application as clusters though it has in built load balancer.
We can monitor and restart application when crashed.
Can we use both for production?
Though load balancer is there in PM2 can I use only PM2?
What is the advantage of using NGINX over PM2?
If I use Load balancer using NGINX and clustering using PM2, will it give better performance than using only one (NGINX or PM2)?
This is a huge topic but let me help and give you some pointers.
Nginx is much more than just a reverse proxy. It can serve static content, can compress the response content, can run multiple apps on different port on the same VM and much more.
PM2 essentially helps you to scale throughput of your service by running it in cluster mode and utilizing all the cores of the box. Read this stackoverflow answer to understand more on this.
Now to answer your question
Can we use both for production?
Yes and you should. Nginx can run on port 80. PM2 can run on port 3000 (or whatever port) which can then manage traffic within the instances of the app.
gzip alone will make a huge difference in the app end user performance.
Here is a good article in case you need code help on how to set it up

Serve nodejs app from nginx like with php?

I use Nginx to serve my php applications for dev purposes.
On Ubuntu it works out of the box.
I want to do the same for Node.js apps.
Is this possible without doing nodejs app.js before?
How to achieve this in a single Nginx conf file?
PHP and node.js are oil and water. PHP requires a web server to run the .php files, however node.js typically creates its own web server. Since you are creating your own web server, in many cases you wouldn't find it necessary to serve your application from Nginx, however, if you truly insisted on "serving" it from Nginx, you would need to proxy it.
This is not possible without doing nodejs app.js before, due to the way node.js works.
This question best answers your question regarding proxy'ing via Nginx.
As a closing remark, its good to remember that node.js does in fact (in most cases) implement its own web server, and PHP does not.

node.js socket.io apache server

I have some sort of noob question regarding to node.js and socket.io. These two guys are usually needed to use websockets for server to browser communication. Once that my project gets done, do I have to upload my php files to my apache server. Now my question is do I have also to upload my node_modules directory to my apache server? along with socket.io directory?
In order to run your node.js process you need to have ability to execute own applications on server. Classic virtual server does not allow of such functionality. There is virtual server hosting for node.js specifically, check out nodejitsu, heroku or any other.
You can run node.js as web platform by it self, without of use of any apache, that will make your life much easier. But if you still need apache and PHP, then you need to consider proxying from apache to your node.js process. This still can be tricky as proxying WebSockets is not straight forward.
If you have access to Apache settings, then you can enable proxying for HTTP/HTTPS, read this: http://ronenagranat.blogspot.co.uk/2011/02/apache2-reverse-proxy-for-nodejs.html
For WebSockets, there is not much data available but here is possible solution: http://blog.cafarelli.fr/post/2013/04/26/Backporting-Apache-support-for-websockets-reverse-proxy-(aka-getting-GateOne-to-work-behind-Apache)
Although if you have own server and have ability to choose between web platform, I would recommend to look into nginx, as it has support for WebSockets proxying (might require module and nginx recompiling).

Expressjs to production

I am new to expressjs, I want to deploy an expressjs app to production. Based on my googling, here's the setup on rackspace I am thinking:
1 Load balancer + 2 server + Run app with forever
My questions are:
What engine shall I use to run the app? nginx?
how many app can I run per server?
Thank you.
If you are serving static files or using any of nginx's reverse proxy features, you can use nginx. But if not, since your servers are behind a load balancer, nginx isn't necessary at all.
The rule of thumb is one node.js/express.js process per core. Have a look at cluster to help you manage this. Make sure your load balancer knows about all the node.js processes you are running (and is not just load balancing between one IP/port pair on each server).
Update: Node.js now has cluster built in out of the box.
Also, if you are deploying on Ubuntu you can use upstart instead of forever if you like.
You need nodejs installed on your machine to run nodejs. nginx is a server used for reverse proxy and a load balancer. Also you can run the app through pm2 instead of forever which will handle all the clustering and running your app in background.

Resources