Running NodeJs http-server forever with PM2 - node.js

My question is about running HTTP-server in combination with PM2.
The problem I face is that:
HTTP-server requires as input a folder which is the root of the website and a port number to run the website on.
PM2 doesn't recognize the HTTP-server command, even when HTTP-server is installed with the -g option.
So I tried the following (note the double dash which should pass the parameters to the HTTP-server script:
/node_modules/http-server/lib$ pm2 start http-server.js -- /home/unixuser/websiteroot -p8686
But it doesn't work.
I also tried:
http-server /home/unixuser/websiteroot -p8686
Which does work, but doesn't have the great support of pm2 ?
Any suggestions would be great, thanks!

You almost had it.
Check where http-server is located by executing:
$ which http-server
You should get something like this /usr/bin/http-server
Then cd to the directory you want to serve files from and execute:
$ pm2 start /usr/bin/http-server --name my-file-server -- -p 8080 -d false
--name my-file-server is optional, but -- is required to pass arguments through to the http-server command.

pm2 start <location>/http-server --name http-server -- -p <port> -d false
or
PM2 modules it self has in-build static file to be served, which is similar to http-server
https://pm2.keymetrics.io/docs/usage/expose/
pm2 serve <path> <port>

pm2 start 'http-server-spa websiteroot index.html 8080'

if we have a build generated by grunt,then go to its path and hit:
~/app/build/prod$ sudo pm2 start /usr/local/bin/http-server -p 8080
Now check app status at localhost:8080

Related

Nestjs with pm2, port still be used after killing the process with pm2 stop?

I deployed my Nest.js application on my VPS (Ubuntu 20.04 DO) using pm2 as process manager. Here is my step by step when I'm updating the app.
$ pm2 stop 1
$ kill -9 $(sudo lsof -t -i:4040)
$ npm run build
$ pm2 start 1
Notice that I must kill the port that the app use before I proceed to the build one, how to simplify it and become like these :
$ npm run build
$ pm2 reload 1
So I can deploy my Nest.js app gracefully and achieve at least only 1% downtime
Stop command keeps the app in the apps list while the delete command not.
I think you want something like
start.sh
#!/bin/bash
source .env
ENVIRONMENT="$NODE_ENV"
npm run build || exit
pm2 delete "$ENVIRONMENT"
pm2 start "npm run start:prod" --name "$ENVIRONMENT" --log-date-format 'DD-MM HH:mm:ss.SSS'
If you are not using different environments, an equivalent script would be
#!/bin/bash
npm run build || exit
pm2 delete my_application
pm2 start "npm run start:prod" --name my_application --log-date-format 'DD-MM HH:mm:ss.SSS'
Emitting the --log-date-format is perfectly fine. However, I have included it because I suspect it might become helpful when finding bugs in production down the line.

I want to know how to use ngrok in PM2

pm2 start ngrok http 5000
This is the command I've entered.
But it didn't work out as I wanted.
Please help me to use ngrok using PM2.
If you use ngrk http 5000, you will be able to use it until the terminal is closed.
It works normally.
I want to use this in PM2.
My operating system is Window10 Node-Js.
Thank you.
download / extract to same folder
https://ngrok.com/download
then run following
pm2 start pm2-ngrok.yaml
pm2-ngrok.yaml
apps:
- name : 'ngrok'
script: './ngrok'
args: 'http --hostname=YOURSUBDOMAINHERE.ngrok.io PORTHERE'
instances: '1'
autorestart: true
max_restarts: 10
max_memory_restart: '500M'
watch : false
error_file: 'err-prod.log'
out_file: 'out-prod.log'
log_file: 'combined-prod.log'
run
pm2 startup
pm2 save
to persist across reboots
I think you need to use -- to pass arguments to ngrok when you run it with pm2:
pm2 start ngrok -- http 5000
Related question: How to pass arguments to app using pm2?
So, first install Ngrok with NPM npm install ngrok -g
https://www.npmjs.com/package/ngrok
Then configure your Ngrok.yml acordingly.
After that, you can run your pm2 like in this example.
pm2 start npm --name app -- run develop
pm2 start npm --name ngrok -- run ngrok --all
pm2 monit
So pm2 start npm --name ngrok -- run ngrok --all runes, the yml configuration for all.

How to run 2 separate apps in PM2?

Right now I am using pm2 to run a node server app. I do that with pm2 start npm. This seems to be independent of the current directory.
I found some mentions online to use pm2 start npm --name "app_name" -- start. However, no matter what name I specify and directory I am inside, it always starts the same app.
Due to the nature of node, I don't run a single .js file and just type npm start in the current directory.
Edit: From my understanding, the problem seems to be that pm2 always starts /usr/bin/npm (Starting /usr/bin/npm in fork_mode (1 instance). So the --name flag doesn't matter much, ie. I can get a list of the same app with different names, and this app is node app A and sometimes node app B. I am kinda lost
What is happening is you have a PM2 app named npm, thus the confusion. You can list pm2 apps with pm2 ls
First, remove it using :
pm2 del npm
Then, start a new app, naming it :
pm2 start npm --name "app_name" -- start
Then, the second app (in the other directory) with :
pm2 start npm --name "app_name2" -- start
You can run Multiple apps using PM2 just follow below steps:
Firstly enter into directory A and start it on PM2 pm2 start server.js --name app-name
Save this in PM2 using pm2 save
Now to run other app you need enter into directory B and start app using
pm2 start server.js --name app-name
Save this process as well, and now check PM2 list using pm2 ls

NodeJS - npm start every time gets error Port is already in use

I am developing an application with NodeJS and Express and every time I have to npm start I get the error Port is already in use. I have to change the port in www file and npm start again.
I am using Mac BTW.
How to solve this issuse?
Is there any good module for live reload?
Strange, looks like you are not stopping the server before starting again.
A simple and painless way would be to use nodemon (https://nodemon.io). It acts on changes of your code and restarts the server. You will have to reload the browser yourself.
npm i -g nodemon
nodemon [app|server|yourservername].js
Live Reload goes a step further, as it triggers the browser to reload the page (using websockets). You may check out supervisor as suggested. I personally prefer lite-server, which requires no configuration to get live reload. Just start it in your applications root directory.
npm i -g lite-server
lite-server
Setup a dev script so you don't have to use npm start. This example uses supervisor https://www.npmjs.com/package/supervisor
Create a file called dev.sh
#!/bin/bash
ENV=dev \
node_modules/supervisor/lib/cli-wrapper.js \
--watch ., ../core \ # replace this with your paths to watch
--ignore-symlinks \
--ignore node_modules,public,client,data \
--extensions js \
--quiet \
--non-interactive \
--no-restart-on error \
--instant-kill \
app.js # your server script here
Then when you want to dev just run sh dev. This question deals with killing processes on nix: Find (and kill) process locking port 3000 on Mac

Start app as root with pm2

I have a daemon that must be run as root on startup.
I use pm2 to start other apps but can not figure out if it can start an app as root. Can it be done?
If not, what are my options?
I had problems with sudo pm2 start api, but this was since pm2 was already running without sudo privileges, therefor you need to run:
pm2 kill
sudo pm2 start api
This kills the pm2 deamon first, so that it starts in sudo, but then you need sudo for ALL pm2 commands afterwards, like: sudo pm2 ls
If you only need your daemon to be run as root in order to access a port number (such as 80 or 443), the pm2 documentation recommends using authbind. So, if you want the user yourusername to have access to port 80, run:
$ sudo apt-get install authbind
$ sudo touch /etc/authbind/byport/80
$ sudo chown yourusername /etc/authbind/byport/80
$ sudo chmod 755 /etc/authbind/byport/80
$ authbind --deep pm2 update
And then use authbind --deep pm2 instead of pm2. The documentation suggests setting up an alias.
I would recommend:
sudo pm2 start index.js
OR
pm2 start 'http-server' /var/www -p 80
sudo pm2 startup
pm2 save
OR
pm2 start 'which http-server' /var/www -p 80
To start it on your HTTP Port
Also, I always put -i 0 at the end - this starts up as many worker processes as you have cores. Check THIS
It is not always necessary to start PM2 as root. If you have PM2 as root and the cli module installed, security is a big risk. This is only required if you're starting your app on a port between 1 and 1024
Wasted about an hour
On AWS EC2 machine, one system was in inconsistent state due to earlier installations, that forced sudo elevations in the application for all commands to OS, like sh, etc.
pm2 was running as root:
ps aux | grep pm2
# root ... PM2 v4.1.2: God Daemon (/root/.pm2)
Now pm2 is running as ubuntu:
ps aux | grep pm2
# ubuntu ... PM2 v4.1.2: God Daemon (/home/ubuntu/.pm2)
Below commands worked:
sudo pm2 kill
sudo npm remove pm2 -g
sudo npm i -g pm2#latest
sudo pm2 update
sudo chown -R ubuntu:ubuntu /home/ubuntu/.pm2
Hope that helps
You should start pm2 as a root, (sudo pm2 start app.js), then your app will start as a root
First, install pm2 globally.
Then set root permissions to pm2 using this command
sudo chown ubuntu:ubuntu /home/ubuntu/.pm2/rpc.sock /home/ubuntu/.pm2/pub.sock
you might consider routing your traffic with iptables, since there is a reason behind this errror
sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3000

Resources