How to host Node.js along with PHP

I already hosted my PHP in Linux Host, now i used node.js for Real time push notifications and read integration would be done by reddis. Now my question is where to host node.js code and how to run that code along with my php in linux hosting?

If you have something Like a VPS then you are free to install whatever you want.

A typical stack for running php alongside node.js would be

  • Nginx as the webserver, listening at port 80
  • PHP-FPM as the fastcgi server, listening at port 9000
  • Node app running at your desired port, let's say port 3000

In your nginx html block you define a php and a node backend

upstream php_app {
    server 127.0.0.1:9000;
}

upstream node_app {
    server 127.0.0.1:3000;
}

In your vhosts you point php files to fastcgi_pass to the php backend

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    try_files $uri =404;
    include /etc/nginx/fastcgi.conf;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_pass php_app;
}

And you can forward the requests on the /node subfolder to the node backend:

location /node/ {
    proxy_pass http://node_app;
    proxy_redirect off;
}

Which means the rest of the requests (for static files) are served directly by nginx.

There are several parameters to tune the behavior of your app including timeouts for php and node backends, which are independent of nginx timeouts. Also, since you said push notifications I guess you're thinking in something like a websocket server (like socket.io). In that case, you also need to allow the client to request a protocol switch from the node backend

location /node/ {
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_pass http://node_app;
      proxy_redirect off;
  }

Instead of using routes to proxy to node, I rather use different subdomains, but that's up to you.