The task is to write a utility for deploying the NodeJS application. Requirement is to run the utility from any computer. The problem is that pm2 does not take in mind that I have switched the working directory to the server:
process.chdir ('SERVER');
process.cwd (); // SERVER
But commands run on my workstation. Share furioku, how to let pm2 know that commands need to be run on the server?
Related
I uploaded nodejs project using nestjs and typeform to awsec2 and used pm2 startnpm -- start command.
My server is working well now.
Since then, however, the pm2 command has not been heard at all and the pm2 input speed has been very slow.
Why did this happen?
I have a demo project in AWS and then I created an AMI for it so that I can use it for auto-scaling. now I am looking for something that I can put in user text in my launch configuration which will let me start the server without going to ssh. I am trying out below, let me know where is my mistake.
#!/bin/bash
cd demo
node server.js
when I launch a new instance with my AMI and just do cd through SSH it works absolutely fine, however, I want to start the server with going to SSH.
These are common one can face when running node application without process manager on a remote server.
Let suppose the above script but what if a node application encounter error? so the application will be stopped, so better to use process manager which will take care of such thing and you will not need to do ssh.
You can use pm2. Which also have slack integration another interesting feature that will help to monitor the process.
You can also set Setup startup script.
Restarting PM2 with the processes you manage on server boot/reboot is
critical. To solve this, just run this command to generate an active
startup script:
run these command in the AMI, and pm2 will take care of the process on all instances.
pm2 startup
#And to freeze a process list for automatic respawn:
pm2 save
The situation is, I'm using Socket.io for a chat application using node js (AWS EC2 Linux).
For other applications, PM2 is working fine since I use Express with Node js.
PM2 does not work with Socket.io because here I'm using http to listen.
var http = require('http');
So I have to go with Forever.
But forever does not work with crontab in linux since I want the app started on system reboot.
I tried forever-service to start the app during reboot. But I get this error
forever-service must run as root.
So I tried with sudo forever-service install test
Then it throws sudo: forever-service: command not found
I want my Chat application to auto restart on system reboot. Please help.
I'm gonna deploy a Node.js mobile web application on two remote servers.(Linux OS)
I'm using SVN server to manage my project source code.
To simply and clearly manage the app, I decided to use Jenkins.
I'm new to Jenkins so it was a quite difficult task installing and configuring Jenkins.
But I couldn't find how to set up Jenkins to build remote servers simultaneously.
Could you help me?
You should look into supervisor. It's language and application type agnostic, it just takes care of (re-) starting application.
So in your jenkins build:
You update your code from SVN
You run your unit tests (definitely a good idea)
You either launch an svn update on each host or copy the current content to them (I'd recommend this because there are many ways to make SVN fail and this allows to include SVN_REVISION in the some .JS file for instance)
You execute on each host: fuser -k -n tcp $DAEMON_PORT, this will kill the currently running application with the port $DAEMON_PORT (the one you use in your node.js's app)
And the best is obviously that it will automatically start your node.js at system's startup (provided supervisor is correctly installed (apt-get install supervisor on Debian)) and restart it in case of failure.
A node.js supervisord's subconfig looks like this:
# /etc/supervisor/conf.d/my-node-app.conf
[program:my-node-app]
user = running-user
environment = NODE_ENV=production
directory = /usr/local/share/dir_app
command = node app.js
stderr_logfile = /var/log/supervisor/my-node-app-stderr.log
stdout_logfile = /var/log/supervisor/my-node-app-stdout.log
There are many configuration parameters.
Note: There is a node.js's supervisor, it's not the one I'm talking about and I haven't tested it.
per Linux OS, you need to ssh to your hosts to run command to get application updated:
work out the workflow of application update in shell script. Especially you need to daemonize your node app so that a completed jenkins job execution will not kill your app when exits. Here's a nice article to tell how to do this: Running node.js Apps With Upstart, or you can refer to pure nodejs tech like forever. Assume you worked out a script under /etc/init.d/myNodeApp
ssh to your Linux OS from jenkins. so you need to make sure the ssh private key file has been copied to /var/lib/jenkins/.ssh/id_rsa with the ownership of jenkins user
Here's an example shell step in jenkins job configuration:
ssh <your application ip> "service myNodeApp stop; cd /ur/app/dir; svn update; service myNodeApp restart"
I have just gotten a VPS to bring my first node.js project online, but I am wondering where do I place the node files like app.js if I want it to be accessible at http://www.mywebsite.com:3000?
Right now, to host a website, I am using WHM to create a cPanel account, which creates /home/cpanelusername and my HTML/PHP files all go into /home/cpanelusername/public_html. Where does node.js files go to? Or did I get this step wrong as well?
On my Mac where I developed the node app, I simply cd into the directory containing the node file and run node app.js
You have to execute app.js file using the node binary, just like you do in local development. That means that you should probably make that execution a service call, the details of which depend on your linux distro. If it's not a service call, then executing it in ssh will mean that the app stops working once you log out of ssh.
For example, in Ubuntu server (which I use) I have an Upstart script which automatically runs my node.js app automatically on system start and log to /var/log. An example of the file, named /etc/init/myapp.js.conf is:
description "myapp server"
author "Me"
# used to be: start on startup
# until we found some mounts weren't ready yet while booting:
start on started mountall
stop on shutdown
script
# We found $HOME is needed. Without it we ran into problems
export HOME="/root"
exec node /home/me/myapp/myapp.js 2>&1 >> /var/log/myapp.log
end script
Replace names, etc. as necessary.
Edit to add: You can then start and stop your service by running:
sudo start myapp.js or sudo stop myapp.js