Upstart node.js app on dokku deployment (Digital Ocean) - node.js

I deployed my node.js app to Digital Ocean, using dokku (Docker powered mini-Heroku). The app is started by command in Procfile (web: node app.js).
How do I start it with Upstart, so that it restarts automatically after crashing?
Added:
I need it to be upstarted when I deploy with git push dokku master.

Edit the file /etc/init/node.conf and put the following code in. Change /opt/node_project/ to the path of your project. You'll need to be root when editing this file so open your editor with sudo.
description "Node server"
author "eagor"
# Stanzas
#
# Stanzas control when and how a process is started and stopped
# See a list of stanzas here: http://upstart.ubuntu.com/wiki/Stanzas#respawn
# When to start the service
start on runlevel [2345]
# When to stop the service
stop on runlevel [016]
# Automatically restart process if crashed
respawn
script
echo $$ > /var/run/node.pid;
exec node /opt/node_project/app.js
end script
post-stop script
rm -f /var/run/node.pid
end script
Now that you've created an Upstart config for your process you can start it from the command line:
$ sudo service node start
Upstart will monitor your process and will restart it any time it goes down.
It also redirects logs to /var/log/upstart/node.log.
Forever
The above works directly with node and would bypass Dokku. Seems like Upstart isn't the best way to handle this.
You should consider using the forever module. Add forever to your package.json dependencies. Then in your Procfile use this: web: ./node_modules/forever/bin/forever app.js

As of Dokku 0.7.0 it has restart policies built in:
https://github.com/dokku/dokku/blob/master/docs/deployment/process-management.md#restart-policies
e.g.
# only restart it on Docker restart if it was not manually stopped
dokku ps:set-restart-policy node-js-app unless-stopped

Related

Correct command for running node.js project with pm2 in ApplicationStart hook

I want to set up ApplicationStart hook for node.js project where pm2 is used as a process manager in aws ec2 server.
I checked some tutorials and the shell script for ApplicationStart hook contains commands for running the project by using:
node/npm
pm2
for example in this tutorial, the shell script contains:
npm start
pm2 start npm --name "covidapp" -- start
in this tutorial, the shell script contains:
pm2 start npm --name "myApp"
node app.js > app.out.log 2> app.err.log < /dev/null &
Why we are running the project two times? Why we just don't use pm2?
I've deployed several apps on EC2 using PM2 and in my experience there should be no need (or benefit) to use node app.js, npm start or similar.
As you are probably already guessing, the whole point of PM2 is to run the process(es).
My recommendation would be to create a PM2 ecosystem configuration with all needed configurations, number of processes, ENV vars etc. I personally prefer this way even when running only one single node application on the server.
https://pm2.keymetrics.io/docs/usage/application-declaration/
And start the process(es) using the configuration, eg.:
pm2 start ecosystem.config.js
I also recommend using PM2 startup generator to make sure PM2 is started on server reboot: pm2 startup
https://pm2.keymetrics.io/docs/usage/startup/
Once you have the startup script generated. Start your processes using pm2 manually or by using a configuration file (see example above). Verify with pm2 status that all processes are running as expected and execute pm2 save to "snapshot" the current state. The saved state will now automatically respawn on reboot.

forever is not working as expected after closing terminal or console

I have installed forever on shared hosting Cpanel for node js application when I run forever start app.js, node js application works on the server.
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: app.js
But when I close terminal or console then node app stopped working. Any suggestions around it?
Closing the terminal will typically close the application running. Consider using tmux or screen to launch the app or also nohup.
Launching this way should be considered a short-term solution. You probably want to look at how your specific Linux distribution handles startup scripts and services.
like maldina said closing forever will stop your app consider
consider upstart (it runs tasks when the computer is started)
you basiclly create a conf file and place it in your init folder "var/etc/init"
the file content should look like this
#!upstart
description "my-app"
start on started mountall
stop on shutdown
# Automatically Respawn:
respawn
respawn limit 99 5
env NODE_ENV=production
exec node /somepath/myapp/server.js >> /var/log/myapp.log 2>&1
you can use the following commands to manage your app
sudo start my-app
sudo stop my-app
sudo restart my-app

Create a startup script for meteor in linux server

I have followed some posts and tutorials as well to create a script to start meteor project when server restart. i have followed answer mentioned in : How to run meteor on startup on Ubuntu server
Then I gave executable permission to script with "chmod +x meteor-server.sh".
I have tried to put this script in /etc/init.d and /etc/init folders but meteor project does not start at the reboot. I'm using ubuntu 16.04.
I would be grateful if you can show me the fault that i have done. Following code is my "meteor.server.sh" script file.
# meteorjs - meteorjs job file
description "MeteorJS"
author "Jc"
# When to start the service
start on runlevel [2345]
# When to stop the service
stop on runlevel [016]
# Automatically restart process if crashed
respawn
# Essentially lets upstart know the process will detach itself to the background
expect fork
# Run before process
pre-start script
cd /home/me/projects/cricket
echo ""
end script
# Start the process
exec meteor run -p 4000 --help -- production
First of all, there's already a very good tool mupx that allows you to deploy meteor projects to your own architecture, so there's really no need to do it by yourself unless you have a very good.
If you really need to deploy manually, this will take several steps. I am not going to cover all the details because you're specifically asking about the startup script and the remaining instruction should be easily accessible in the Internet.
1. Prepare your server
Install MongoDB unless you are planning to use a remote database.
Install NodeJS.
Install reverse proxy server, e.g. Nginx, or haproxy.
Install Meteor, which we will use as the build tool only.
2. Build your app
I am assuming here that you already have the source code of your app put on the server to which you're planning to deploy. Go to your project root and run the following command:
meteor build /path/to/your/build --directory
Please note that if /path/to/your/build exists it will be recursively deleted first, so be careful with that.
3. Install app dependencies
Go to /path/to/your/build/bundle/programs/server and run:
npm install
4. Prepare a run.sh script
The file can be of the following form:
export MONGO_URL="mongodb://127.0.0.1:27017/appName"
export ROOT_URL="http://myapp.example.com"
export PORT=3000
export METEOR_SETTINGS="{}"
/usr/bin/env node /path/to/your/build/bundle/main.js
I will assume you put it in /path/to/your/run.sh. A couple of notes here:
This form of MONGO_URL assumes you have MongoDB installed locally.
You will need to instruct your reverse proxy server to point your app trafic to port 3000.
METEOR_SETTINGS should be the output of JSON.stringify(settings) of whatever settings object you may have.
5. Prepare upstart script
With all the preparations we've made so far, the script can be as simple as
description "node.js server"
start on (net-device-up and local-filesystems and runlevel [2345])
stop on runlevel [016]
respawn
script
exec /path/to/your/run.sh
end script
This file should go to /etc/init/appName.conf.
Finally i got it to work. I have used following 2 scripts to run meteor on the startup.
First i put this service file(meteor.service) in /etc/systemd/system
[Unit]
Description = My Meteor Application
[Service]
ExecStart=/etc/init.d/meteor.sh
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=meteor
[Install]
WantedBy=multi-user.target
I have called a scipt using this service. I put this following script(meteor.sh) in /etc/init.d
#!/bin/sh -
description "Meteor Projects"
author "Janitha"
#start service on following run levels
start on runlevel [2345]
#stop service on following run levels
stop on runlevel [016]
#restart service if crashed
respawn
#set user/group to run as
setuid janitha
setgid janitha
chdir /home/janitha/projects/cricket_app
#export HOME (for meteor), change dir to plex requests dir, and run meteor
script
export HOME=/home/janitha
exec meteor
end script
I make both these file executable by using
chmod +x meteor.service
chmod +x meteor.sh
And i have used following two commands to enable the service
systemctl daemon-reload
systemctl enable meteor.service
I used this configurations successfully
In /etc/init.d add a file called meteor.sh
#!/bin/sh
export HOME="/home/user"
cd /home/user/meteor/sparql-fedquest
meteor --allow-superuser
You must give executions permissions to meteor.sh
sudo chmod 644 meteor.sh
Also you must create meteor.service in /etc/systemd/system
[Unit]
Description =Portal of bibliographic resources of University of Cuenca
Author = Freddy Sumba
[Service]
ExecStart=/etc/init.d/meteor.sh
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=meteor
[Install]
WantedBy=multi-user.target
Also you must give permissions to meteor.service
$ sudo chmod 644 meteor.service
Then, we need add the service to this start each time that the server reboot
$ systemctl enable meteor.service
And finally start the service
$ service meteor start

Autostart Node.js app (Ghost Blog) on server restart

After my preparation and installation of Ghost, I'm stuck with setting my DO Ubuntu to auto-start itself on server restart. I was suggested to use forever, and I do use it, however as far as I can understand from the concept of it; forever is just to keep the process running once it's started, and it vanishes (needs to be manually started) on each restart.
I'm looking for a solid solution that will keep multiple nodejs apps alive, even when the server is restarted or completely crashed.
For howtoinstallghost.com, allghostthemes.com, ghostforbeginners.com we use pm2 to keep Ghost running. We have a write up on how to setup pm2 here:
http://www.allaboutghost.com/keep-ghost-running-with-pm2/
DigitalOcean's one-click Ghost app use an Upstart script to have Ghost start on boot. It looks like:
description "Ghost: Just a blogging platform"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on runlevel [!12345]
# If the process quits unexpectedly trigger a respawn
respawn
setuid ghost
setgid ghost
env NODE_ENV=production
chdir /var/www/ghost
exec /usr/local/bin/npm start --production
pre-stop exec /usr/local/bin/npm stop --production
and it is installed to /etc/init/ghost.conf This has the added benefit of allowing you manage it like any other service on your server with commands like sudo service ghost restart
You want to set it up as an init.d script so that you can start it or stop it as a service (and set it up to autostart using chkconfig).
Details are here: https://help.ubuntu.com/community/UbuntuBootupHowto
Note that's in addition to using things like Forever and Monit to restart on service crash and so on.

Keep meteor running on amazon EC2

I have a simple meteor app that I'm running on an Amazon EC2 server. Everything is working great. I start it manually with my user via meteor in the project directory.
However, what I would like is for this app to
Run on boot
Be immune to hangups
I try running it via nohup meteor &, but when I try to log out of the EC2 instance, I get the "You have running jobs" message. Continuing to log out stops the app.
How can I get the app to start on startup and stay up (unless it crashes for some reason)?
Install forever and use a start script.
$ npm install -g forever
I have several scripts for managing my production environment - the start script looks something like:
#!/bin/bash
forever stopall
export MAIL_URL=...
export MONGO_URL=...
export MONGO_OPLOG_URL=...
export PORT=3000
export ROOT_URL=...
forever start /home/ubuntu/apps/myapp/bundle/main.js
exit 0
Conveniently, it will also append to a log file in ~/.forever which will show any errors encountered while running your app. You can get the location of the log file and other stats about your app with:
$ forever list
To get your app to start on startup, you'd need to do something appropriate for your flavor of linux. You can maybe just put the start script in /etc/rc.local. For ubuntu see this question.
Also note you really should be bundling your app if using it in production. See this comparison for more details on the differences.
I am using upstart on Ubuntu server which you should be able to easily install on Amazon linux.
This is roughly my /etc/init/myapp.conf:
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
respawn
respawn limit 99 5
script
export HOME="/home/deploy"
export NODE_ENV="production"
export MONGO_URL="mongodb://localhost:27017/myappdb"
export ROOT_URL=http://localhost
export MAIL_URL=smtp://localhost:25
export METEOR_SETTINGS='{"somesetting":true}'
cd /var/www/myapp/bundle/
exec sudo -u deploy PORT=3000 /usr/bin/node main.js >> /var/log/node.log 2>&1
end script
I can then manually start and stop myapp like this:
sudo start myapp
sudo stop myapp
I believe this package solves your problem: https://github.com/arunoda/meteor-up
which seems to use forever: https://github.com/nodejitsu/forever

Resources