I have a web application with Apache and PHP on the back end. I am in the process of enhancing this with many new features and considering using node.js for the new work.
First of all, can PHP and node.js co exist on the same machine? I do not see why not. Second, can I just call node.js code directly from Javascript and return JSON back?
Since you can run PHP on the command line, and Node. js can easily exec programs through the command line, it's very much possible.
Due to the V8 engine, asynchronous execution, and real-time server interaction, Node. js offers a better execution speed and certainly outperforms PHP.
Node. js is a server-side JavaScript run-time environment. It's open-source, including Google's V8 engine, libuv for cross-platform compatibility, and a core library.
Note: PHP should be used in applications in which client does not have to interact with the server again and again and Node.js should be used for the applications which require a lot of interaction between client and server.
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.
We cannot serve the same request since both the Node JS server and Apache are configured to run on different ports. Although we can change the port numbers and run on different ones, computers are engineered to have only one process listening on one port and not more.
Node JS is a JavaScript runtime that creates server-side applications in JavaScript code. Node JS also enables us to create simple HTTP servers that we can use to serve static content through various HTTP methods. The HTTP module in Node JS allows us to create a server that listens to a specific port number and responds to the client.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With