Can we use Apache Server for server side rendering of angular web apps or we have to stick with NodeJs - node.js

Currently I am running NodeJs server as background process in the host machine to achieve sender side rendering for my angular app.
On Linux for e.g. npm rum server & (ampersand is to put process in background)
But I am looking for solution like Apache Server that manages it start/stop with host reboot.

I think the best way for you to acheive what you're looking for is to use a management solution like PM2 or Forvever. These will pretty easily manage your solution in the background for you.

Rather than apache/nginx managing start stop of your node application, you may create service to run your node application. It will run without any manual intervention.
Start your Node app on some other port than 80, as your main web server might be running on that port.
Create a service file in /etc/init to start your Node app
Configure apache/nginx with reverse proxy to the node applicaiton
Start both of the services: 'service start nodeapp.conf' and 'service start apache2'
This would make your life for handling these services pretty easy.

Yes. You should be able to.
Start by creating a proper deployment directory - https://angular.io/guide/deployment
Then copy/ftp/whatever to the web server.
The tricky part is in your route controllers, etc. and getting all of the paths right if you end up deploying into a different directory than what you developed for.

You need to use Apache/Ngnix with NodeJS using proxy.
Look into this link, if this helps:
https://blog.daudr.me/painless-angular-ssr/

Related

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.

Always running process with IISNode

Does anyone know a way to have a JavaScript file or set of files always running under IISNode without the need for a client request? The idea would be to have scripts that behave as services, but have them running under IISNode.
Thanks!
csh3
How about trying node-windows, it allows Node.js applications to run as a windows service. A nice feature is that it also exposes a way to write to the EventLog.
It probably fits your scenario better considering that you need any of the IIS features other than the long running aspect of it.
Hope this points you in more applicable direction.
I guess you have some reason to use iisnode, but you are trying to run a service in iis which is not a good idea, if you want to run as service then run as service. how?
if you still insist to use iisnode then options are
use Application Initialization for IIS
Or write a scheduled job that pings your iisnode page
Or use pingdom like service to ping your iisnode application.

Running and managing nodejs applications on single server

Is there a good way to run and manage multiple nodejs apps on a single server?
I've been looking at haibu and nodester, but they seem a little complex for what I am trying to do.
I also was looking at forever and I think that may work with the config file and web gui, but I am not sure how I am going to handle passing the port information via ENV or arguments.
I use Supervisord & Monit, more details and configuration example here: Process Management at Bringr.
Moreover you can specify environnement variable directly from the supervisord configuration file (see sub-process environment). But I personally prefer to add these variables directly inside a ~/.bashrc on each machine.
If the port number isn't going to change for each application (but change between production & development environment). I'll recommend to specify them inside a config.json (or directly inside package.json). And the config.json will contain a different port number for each application depending on the environnement:
{
myapp:{
production:{port:8080},
development:{port:3000}
}
}
And inside myapp.js:
var config = require('./config');
app.listen(config.myapp[process.env.NODE_ENV].port)
With process.env.NODE_ENV declared in ~/.bashrc.
I wrote an app nodegod that I use for a handful deployments of maybe 10 apps each.
nodegod reads an app list from json. It has an internal state machine for each app that handles the app life cycle in a safe manner including restarts, and the web page features stop/start/debug.
The web interface uses web sockets, so that you can manage remote servers over ssh.
As you deploy over rsync, apps restart automatically.
Because nodegod monitors the stdout of other apps, you can capture an app's final breath like segfault and malloc errors.
I use a fork of http-proxy in front of a slew of express instances, so any number of apps can share a single server port per dns for both http and web sockets.
I wrote a haraldops module to read app configuration from outside the source tree. With that you can monitor and get emails whenever something's up with an app.
App configurations I keep in a git repo in the file system.
It's not rocket science, and it all fits very nicely together. Only node and json: SIMPLE gets more done.
If your server has upstart, just use it. I have no luck with forever and similar.
If you want to proceed with upstart, roco would be nice as deployment solution:
roco deploy:setup:upstart
roco deploy
We're constantly trying to improve forever and haibu at Nodejitsu. Seems like the approach you're looking for here is a .forever configuration file for complex options. This feature has been on our backlog for a while now
https://github.com/nodejitsu/forever/issues/124
Check back. I consider it pretty high priority after the next round of performance improvements.
These days I've taken to using dokku which is a OSS clone of heroku. Deploying is as simple as making sure your package.json contains a start script. For example:
"scripts": {
"start": "node index.js"
}
Sample App

Expressjs to production

I am new to expressjs, I want to deploy an expressjs app to production. Based on my googling, here's the setup on rackspace I am thinking:
1 Load balancer + 2 server + Run app with forever
My questions are:
What engine shall I use to run the app? nginx?
how many app can I run per server?
Thank you.
If you are serving static files or using any of nginx's reverse proxy features, you can use nginx. But if not, since your servers are behind a load balancer, nginx isn't necessary at all.
The rule of thumb is one node.js/express.js process per core. Have a look at cluster to help you manage this. Make sure your load balancer knows about all the node.js processes you are running (and is not just load balancing between one IP/port pair on each server).
Update: Node.js now has cluster built in out of the box.
Also, if you are deploying on Ubuntu you can use upstart instead of forever if you like.
You need nodejs installed on your machine to run nodejs. nginx is a server used for reverse proxy and a load balancer. Also you can run the app through pm2 instead of forever which will handle all the clustering and running your app in background.

how to setup nodejs webapp as service?

I have nodejs installed, npm installed, modules installed, and my app codes. On my dev machine I simply type node app.js in my app folder to start the dev server, but now it's the time to deploy it to a real server I got problem.
Where is the regular folder to place my app codes.
Which user should be use to run my nodejs app. and how to make the user only have permission to execute app codes, and 80, 443, 843 ports.
How to write the service script, and stop server by kill pid?
ports are determined by which port your app listens on. If you have physical access via ssh to a server and have root privileges etc then you can just treat it as a dev server.
I would recommend forever for keeping it running and maybe a writing a balancer to handle multiple node apps at once.
Permission handling has to be done based on connectivity. A user connects to your service and you authenticate it for its permission levels. This is done by hand.
The folder you place it is not very relevant.
If you have say a no.de server you can learn how to use their smart machines. THere are similar guides for say the Amazon EC2.
I recommend Monit with Upstart. You can read about that solution here and here. You can also make a simple load balancer in nginx.

Resources