Use nodejs to host multiple html websites instead of apache - node.js

I currently use Apache2 to host multiple websites for some friends, and as i've been experimenting with NodeJS I was wondering if it was possible to host these sites with NodeJs?
I wanted to have a folder structure like the following:
App
--> server
--> websites
`--> site1 (example1.com)
`--> site2 (example2.com)
With more people asking me to host there sites I need to be able to easily create a new site quickly with out restarting the server effecting other hosted sites.
Currently I use a bash script to create the folder structure for apache and add a new virtualhost in the apache conf file and finally reload apache.
So my main question is, should I even be looking at nodejs for this or stick with apache?
Any opinions, examples or tutorials would be great.

You're probably better off staying with apache (or possibly switching to nginx). In fact, best practice for production node.js servers is typically to run them behind apache/nginx through a reverse proxy. Few reasons for that:
You have to run node.js as root to give it access to port 80/443 (generally bad idea)
You're going to be very hard-pressed (and probably a lot of trial and error) to get the security, performance and stability of apache/nginx.

Related

How to run Node on one URL, on GoDaddy (shared hosting), along with Apache

I installed Node on my GoDaddy's Linux shared hosting plan via SSH. Running Node on a specific port number isn't a problem.
(I'm not at all clear about this ecosystem, which is why my question might sound stupid)
The primary (WordPress) website runs on Apache and I cannot disturb that permanently (that is, I cannot stop that website from running and enable Node on all ports)
Is there any way to run Node on one specific URL of the website, instead of a port number?
I read a few answers and a few articles and tried to modify the .htaccess file and enable reverse proxy, but I can't get it to work especially since I'm not even sure about how it works.

FeathersJS on server with multiple hosts

I have a VPS that runs apache on with many domains (all same IP), but I want to start running nodeJS/feathersJS for some of my sites.
I can't seem to figure out how to run multiple instances other than with different ports. However, all the feathers sites are available to all the others provided you add the port (even sites running an apache site).
Is there an easy way to limit domain-1.com to show feathersjs site, and domain-2.com to still use apache?
IS there something I am missing?
I am new to node and transitioning from a PHP person to a nodeJS person... so please forgive my ignorance.
I found some non-feathersJS modules that I could figure out to use, but there has to be an easier way then modifying feathersJS... no?
*Edit I found the apache proxy solution already and implemented it. However, now I need to make sure that the port that runs node isn't used on my other domains.
Example.com now using apache proxy stuff run localhost:3030 but so does anotherexample.com:3030
Is there a way to limit this?
I found some libraries that do this for node, but none that seem to be nicely implemented in feathersJS.
*edit again I think the mentioned vHost feathers thing is what I am looking for, will update when i test this.
There is different ways to go about it but one way would be to use mod_proxy for Apache. In your domain configuration you'd then point to the port where the application you want is running on:
ProxyPass / http://www.example.com:8001/
ProxyPassReverse / http://www.example.com:8001/
While putting an Apache or NginX proxy in front of a Node application (and to serve static content) is usually a good idea for higher traffic sites, for smaller projects you can also just use Node without having to worry about Apache. To host different apps on different domains, you can use the vhost Express middleware. An example how to set it up with Feathers can be found here.

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.

Where to run node.js

So I thought about giving node.js a try seeing the possibilities it has for a little test chat project (with mysql) I'm doing.
But what I couldn't find out is where to run the file from and whats most common.
What I currently have:
A FreeBSD server with latest Node and PHP 5.3.x
A vhost
some tutorials on how to start with node (which I looked through and got exited about)
knowledge on how to run it from terminal without having to keep my terminal open (screen)
So far so good.
What I need:
Some basic information of where to put the (lets say:) chat.js file.
Most logical port to run it on
So the web root (www) runs on a user (not root obviously). And the webroot has an underlying folder where I could put the script (away from visitors grabby little hands). This seems to me to be the most safe place to put it seeing nobody can get to it, which is probably what I want seeing I'm going to connect to a db and don't want my DB login data out there (I don't know how this works yet but I'll figure out db connect with node later, no answer required).
But if a file is not in the webroot, it seems to me a connection cannot be made from outside. Cause my webroot is configured to only allow 80 (or ssl on 443) incomming traffic, which is logical. Outgoing obviously has no problems.
All the examples that I found don't really help me. They just do everything on a local machine, which sucks for me cause I don't want to do that.
So what I would like to is the best practice for:
Where to put the file
port to run it on.
What is Node.js?
A lot of the confusion for newcomers to Node is misunderstanding exactly what it is. The description on nodejs.org definitely doesn't help.
An important thing to realize is that Node is not a webserver. By itself it doesn't do anything. It doesn't work like Apache. There is no config file where you point it to you HTML files. If you want it to be a HTTP server, you have to write an HTTP server (with the help of its built-in libraries). Node.js is just another way to execute code on your computer. It is simply a JavaScript runtime.
A nice tutorial How to Deploy Node JS Applications, With Examples
You'll need to have your non-node application on port 9000 (for
Apache, this will be in /etc/apache2/ports.conf and in your
sites-available file for your site), and you'll need your node
application to listen on 8080. You'll also need to set up DNS 'A'
records for the different hostnames you'll be using for your servers.
Companies like Heroku allow for automated deployment of apps from the desktop to the cloud.
Nodejitsu provides a tool called jitsu that makes deploying an Node.js application super simple. You can install jitsu with npm.
npm install jitsu -g
Heroku How To
Getting started with jitsu
Use monit, forever, upstart or systemd to start your node server. Use Varnish or HAProxy or Nginx (Nginx not work with websockets).
Ultimately you can stick it anywhere you want. I recommend running your application using Forever or similar instead of directly with Node. I usually keep my apps in /var/ and let each one run under a unique user. I do not recommend keeping them in your http root, as your .js files are should NOT be interpreted by Apache, php, etc.
To be clear - you do NOT need a traditional webserver, nor do you need php,mySQL or anything else. Node is all you need. It'll serve content directly - it IS the webserver.
Often times you'll have each app use a high port number (3000+) and use NGINX to proxy them all to different hostnames off of port 80 (allowing you to easily have multiple apps on a single machine). If you aren't using some sort of proxy, then 3000 is very default, but there is no correct or incorrect port, so long as you don't use a reserved port.

Node equivalent to index.html

I'm coming from a LAMP background and want to test out node for production.
What seems a little confusing to me is that in Apache url's map to folders, and the server will automatically look for an index.html or index.php if you aren't re-writing urls.
What would the equivalent be in node?
I'm thinking it'd be something like checking the request url and matching it, then loading a specific node module which runs the app.
This might seems simple for a single app, but we run tons of client apps on our server so i'm used to having different frameworks in different folders and index.php just runs it.
to be more specific. i'm currently running a few codeigniter and wordpress installations on our server. so i'd want to run a few node apps/frameworkds in different 'subfolders'
With Node you don't really use Apache. It's similar to Ruby in that is runs its own web server.
However, you can probably get Apache to run Node files using mod_node. As far as I know this is non-standard though and you definitely lose the "non-blocking" advantages of Node. But for experimenting (and not load testing) it's fine.
Check out Express if you're looking for an MVC architecture written in Node.
If you're just looking to run the most basic sample web server, just run the example hosted on the main page: http://nodejs.org/
Lastly, I had that same issue you are experiencing where I have one box hosting a lost of stuff and Apache taking up port 80. The answer here is to use a reverse proxy like Nginx to run on port 80 and redirect traffic to Apache / Node / Ruby / etc. Best of both worlds and since Nginx is written non-blocking you still gain the benefits of node.
I actually wrote an in-depth blog article about this a few months ago:
http://readystate4.com/2011/07/15/nginx-apache-and-node-all-living-harmony/

Resources