can PHP and node.js run on the same server
You can run node and PHP on same server, and even on the same port. The key is to use a server like nginx in front listening on port 80, set up PHP in Nginx as you normally would (using php-fpm) and set up your Node instance to listen locally on some high port like 8081.
Then just configure nginx to proxy all the Node requests through to localhost:8081 using the directory name as a filter. You're essentially setting up nginx to treat Node requests a bit like it treats PHP requests: it forwards them off to some other daemon then handles the response as it comes back. Nginx is great at this. It will add a layer of security and it will boost performance because it's so good at managing many connections at once even when the backend doesn't.
Another benefit of this is that you can have multiple separate Node instances on different domains too, and use regular nginx rules to handle it all. You can also run it alongside other app servers like apps written in Go.
You'd benefit from Nginx's configurability, its SSL and HTTP/2 support and huge speed in serving static files, and not having to serve static files from your Node app (if you don't want to).
Yes, You can do it. If you server is an Ubuntu or Debian, follow these steps:
Open your terminal an write:
sudo curl -sL https://deb.nodesource.com/setup_8.x | bash - sudo apt-get install nodejs
If curl is not installed on your server:
sudo apt-get install curl
To your Node.js application not stop when you exit the Terminal without shutting down your instance, use a package called Forever.
npm install -g forever
If your site is uploaded and NPM and Forever are configured correctly, it is time to start the Node.js instance. If you’re using Express.js, run the following command to start a Forever instance:
forever start ./path/to/your/project
In the above command you'll notice I am feeding the ./bin/www script because that is what npm start launches for Express.js. Be sure to change the script to whatever your launch script is.
By default, the website (nodejs) is running at http://localhost:3000 which isn't ideal for remote visitors. We want to be able to access our site from a domain name processed by Apache. In your Apache VirtualHost file, you might have something that looks like the following:
<virtualhost *:80>
ServerName www.example.com
ProxyPreserveHost on
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
</virtualhost>
We are telling Apache to create a proxy that will get our Node.js http://localhost:3000 site every time the www.yousite.com domain name is hit. All assets and pages will use the www.yoursite.com path instead of http://localhost:3000 leading everyone to believe the website is being served no differently than any other.
However, by default, the Apache proxy modules are not enabled. You must run the following two commands if the modules are not already enabled:
a2enmod proxy
a2enmod proxy_http
You may be required to restart Apache after enabling these modules.
I get this information on The Poliglot Developer.
Yes, and yes. Node and Apache / PHP can co-exist on a single server.
The only issue you are likely to run into is that they cannot both listen on the same port. HTTP, by default, runs on port 80 and only one process can "listen" on a single port at any one time. You may therefore have to run the Node app on a different port (for example, 8080), which could bring in difficulties if any of your target users are restricted to only port 80.