how to setup nodejs webapp as service? - linux

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.

Related

Deploy a node js application on fedora workstation 36

I have a nodejs application that i wanted to deploy on a local server(which is running Fedora 36). I have developed the NodeJS side on another pc,and it works fine when the node is running. But when i copy the node server directory to the fedora serve and run it,it only works on local host. I couldnot access the server even if i'm in the same network.
I'm developing the app for a small locally connected PC and i donot want to use the hosting companies as well as Heroku. Is the it the firewall that is blocking my request to the server? What option do i have to host it locally? Is there a better way?
Your node.js server is running on a port determined at the end of the script usually. Sometimes 3000. but can be anything. The correct way for others to access is as you say...
http://your.network.ip.address:port/
or
example ip
http://192.168.0.3:3000
check your local ip and port where you run this server.
How could others, on a local network, access my NodeJS app while it's running on my machine?

Deploy ReactPHP on PHP host

I use the example for the chat server in ReactPHP. My server listens on port 8080
$socket = new React\Socket\Server(8080, $loop);
$server->listen($socket);
in my local PC. The written code is working correctly but when upload files into my Linux host, nothing works. I wrote a ticket to the support team from my hoster, they said that this is not possible in Linux. Is that correct?
ReactPHP core team member here. Your run-of-the-mill shared hosting won't be able to host this. You need your own server, VPS, or bare metal, to run ReactPHP as a server because you're dealing with a daemon process. And shared hosting generally doesn't support that.
My suggestion is to get a VPS somewhere and look into Supervisor to keep your process running and restart it when something happens to it. This also requires you to manage your own server with all the firewalling and networking knowledge that comes with it.

Deploy React website with node.js backend

I have developed website in react and node.js as intermediary to send and receive response.
Now i want to put this on a machine in lab , so everyone can access it.
Can i Install node on machine in lab and run same setup as on my dev machine and give ip of that server?
I tried searching deployment with node js an react, but everyone using AWS, or some external server after npm build.
How do I deploy my react app on lab machine with out actually copying code?
Two Things before you share your IP and PORT:
Have a process monitor setup in your lab machine, Check out pm2. This will help you run your application in the background,
monitor your application and start your application after restart.
Enable port in your firewall for everyone to use. if your lab system is Ubuntu then allow ufw for the port.
Now you can share your IP:PORT and everyone would be able to access it, provided they all are connected to the same network.
If you want everybody access your server, you can share your private IP with them, so others can access it using
yourip:port

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

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/

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.

Resources