In a classical PHP setup, the web server is separate from the application.
The setup looks like this:
[browser/client] => [apache/mod_php] => [index.php]
With node, things are different, because the web server is part of the application. So your setup looks like this:
[browser/client] => [node server.js]
So, what does that mean for deployment?
Usually it means, you need a supervisor that starts your application and restarts it if it crashes. When you copied over a new version of your application, you simply use the restart mechanism of your supervisor.
Some supervisors even restart automatically when they notice the application's code changed, which is similar to PHP's change-and-reload workflow.
A small selection of supervisors you could use follows:
But there are many alternatives.
If you'd start your application from the terminal on the server, it would normally only run until you terminate the terminal session. When the server restarts (maybe because of a power or hardware failure), you have to restart your application manually. Because of that, the supervisor should be
- Windows: configured as a service (Auto start node.js server on boot)
- Linux: I would simply install supervisord using the Linux distribution's package management and configure it to start the node application. Alternatively, you can hook up to the distribution's init system (create an init script). Different distributions often have different init systems.
Additionally, if you need
- more than one application running on one server, even node and PHP
- need some of the built-in behaviour of most webservers, like serving static content, caching, gzip, rate limiting, SSL termination etc.
you most certainly need a reverse proxy
in between your applications and the clients.
The setup would look like this:
/=> [apache/mod_php] => [index.php]
[browser/client] => [reverse proxy] => [node server1.js]
\=> [node server2.js]
Most web servers can also be configured to act like a reverse proxy. There are also specialized reverse proxies.