pm2 is not working in amazon ec2 with node application - node.js

I have node application hosted in amazon ec2.
I used nginx to point it in my domain.
It runs perfectly when i use npm start to run the application.
But when i use pm2, it doesnt work.
pm2 list command shows that the app is running.
But url doesn't work. Even when i again run npm start app gets started that clearly states that the port is free. So pm2 is not actually running anything on that port. But pm2 says app is online.

I was running the wrong file from pm2. As it was express project, so i had to run bin/www
So the following command solve the issue:
pm2 start bin/www
Answered it here as well:
https://github.com/Unitech/pm2/issues/3252
Thanks

Related

Nodejs API doesn't work after reboot centos server

In my centos server, I have an API with node js and I started with pm2. pm2 status is online and It works correctly. but when server reboot, can't connect to the API although pm2 status is online.
I also try start node js like below
pm2 start index.js
pm2 startup
pm2 save
but It does not work until I restart pm2 ..
how can I fix it?
You have to do pm2 resurrect to start an application after "pm2 saving" it:
pm2 resurrect
Then put this command in a script file and configure it to run on server reboot. Read this Q and A for help. Also in this article there are some CentOS specific changes that need to be done.

Why pm2 is fine, but pm2 logs is not?

I'm using pm2 to keep tracking of what I do on backend with NodeJS. Until yesterday it all worked perfectly fine, but today, even if pm2 server is working fine (i can start the project server and i can navigate), pm2 logs just stopped for no reasons.
Here you can see the version I'm using (i already updated PM2 both on project and local as i find in other answers).
I usually use the command
pm2 start process.json
pm2 restart 0 --watch
pm2 restart 0
And in other prompt instance, for the logs i use
pm2 logs
For some reason PM2 is just tailgating the last 15 lines but it doesn't work real time like it used to do
I didn't find this exact problem on other questions (only questions about pm2 not working at all, but not about pm2 working half way). Any suggestions?
Apparently the version of node and pm2 were the source of the problem. I used this command to run pm2 using the node version explicitly
pm2 start process.json --interpreter=/home/ken/.nvm/v10.15.1/bin/node
This way you force pm2 to use settings for the version of node you've installed in your machine. Now both server and logs works fine.

How to deploy a NodeJS app without root access, so it stays online after server restarts?

I'm trying to deploy my NodeJS app. I tried npm forever and pm2, but I believe that my hoster restarts the server every night because my app is always offline the next day.
The thing is, that I have a shared hosting server on A2hosting and don't have root access, so these sudo commands don't work here... except you know how I can change that, haha...
I don't know what infos I need to provide for you to help me... so if you need to know something, tell me please!
Thank you so much!
I've used pm2 to do that.
npm install -g pm2
then you run the following command to ensure pm2 will run on startup:
pm2 startup
It will show you the command you have to run.
Finally, you initiate the process:
pm2 start server.js --name <service-name>
Freeze your process list:
pm2 save
More details on pm2 documentation: https://github.com/Unitech/pm2

Node.js app stops working as soon as the AWS Lightsail's Bitnami console is closed

I have developed a simple node.js application and I am trying to publish it on AWS Lightsail. I followed the instructions on this page: https://medium.com/#sharmasha2nk/aws-lightsail-bitnami-nodejs-letsencrypt-cf653573b8a1
I am connecting to Bitnami using the SHH console provided. After I start the app with node index.js command the app is up and running until I close the SHH console. As soon as I close it the application stops and “Service Unavailable” error is being displayed. How can I keep my app running on AWS even if my PC is shot down and the SHH console is closed?
I have tried restarting the apache server and AWS Lightsail instance. Neither of them helped.
httpd-app.conf file content:
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
httpd-prefix.conf file content:
Include "/opt/bitnami/apps/emsnotes/conf/httpd-app.conf"
Thanks.
I would recommend you pm2
Here's how to do it quickly:
install pm2 globally using sudo npm i -g pm2
navigate to the location of your index.js via ssh
run pm2 start index.js
This will keep node running even when you disconnect the ssh session or shutdown your development computer. The process will keep running on your server in the background. This is used for running node apps in production on a server.
You can read all about it here in its documentation.
I hope I could help you, please let me know if you have any question.
Karim
I had a look at the article and frankly i see nowhere that he talk about keeping the server up and running.
Basically he is using the Bitnami Node.js stack and if you look at the docs you will see that Bitnami use Forever.js to keep the apps running on their instance, Forever is a CLI tool to monitor and keep a Node instance running in the background.
Check the docs here : https://docs.bitnami.com/general/infrastructure/nodejs/administration/start-express-server/
Here is the Git for Forever, so can read more on it: https://github.com/foreverjs/forever

Running pm2 on Elastic Beanstalk in cluster mode

I've been trying to run pm2 on AWS Elastic Beanstalk's node web service environment but without luck. I start up the express api via: ./node_modules/.bin/pm2 start server.js -i 0 but the server never comes out of a Degraded state. I can run this same command locally just fine. I have ssh'd into the aws instance and looked in the logs, but I don't see any errors. It would be a big help if I could chat with someone that has successfully run pm2 on eb via cluster mode.
Thanks!
I have successfully deploy pm2 on aws elastic beanstalk, it has more than one way to achieve.
You may either add an installation command to the .elasticbeanstalk/config.yml file for pm2 global installation or simply install pm2 into your app and follow the instruction of the link below (recommended way).
https://gist.github.com/Unitech/4c8ea564aa8bf0a389c5
As for the first method, in your config.yml file, simply add the following line(the link above doesn't require this) :
container_commands:
0_install_pm2:
command: "npm install pm2 -g"
You need to manually handcode the start command for your app with this method.

Resources