Keep meteor running on amazon EC2 - linux

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

Related

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

how to run node js on dedicated server?

I am doing a chat app and integrating it on a website. When i execute teh command 'node index.js' on the local server everything works fine. But when i try installing node js on a dedicated server and try to execute the command 'nohup node index.js &' through ssh it gives following message.
nohup: ignoring input and appending output to `nohup.out'
I had followed the method mentioned in this site for installation of node js on server https://www.a2hosting.com/kb/installable-applications/manual-installations/installing-node-js-on-managed-hosting-accounts
Can someone help me, please?
You first need to install Node in a correct way. I wrote a tutorial about it: How to get Node 6.7.0 on Linux (of course you can use newer versions, just change the version in the commands).
Basically it's something like this - change the version to the one you like:
# change dir to your home:
cd ~
# download the source:
curl -O https://nodejs.org/dist/v6.1.0/node-v6.1.0.tar.gz
# extract the archive:
tar xzvf node-v6.1.0.tar.gz
# go into the extracted dir:
cd node-v6.1.0
# configure for installation:
./configure --prefix=/opt/node-v6.1.0
# build and test:
make && make test
# install:
sudo make install
# make a symlink to that version:
sudo ln -svf /opt/node-v6.1.0 /opt/node
I recommend building Node from source and always running make test but you can also install a binary package which is faster - just make sure you understand the issues with paths and hashbang lines if you do so - more info on that and more install options are described in my tutorial.
Then you need to make sure that your application is started every time the server is restarted. I recommend using Upstart if you can.
Using Upstart, save something like this in /etc/init/YOURAPP.conf:
# When to start the service
start on runlevel [2345]
# When to stop the service
stop on runlevel [06]
# If the process quits unexpectadly trigger a respawn
respawn
# Start the process
exec start-stop-daemon --start --chuid node --make-pidfile --pidfile /www/YOURAPP/run/node-upstart.pid --exec /opt/node/bin/node -- /www/YOURAPP/app/app.js >> /www/YOURAPP/log/node-upstart.log 2>&1
Just change:
YOURAPP to the name of your own app
/opt/node/bin/node to your path to node
/www/YOURAPP/app/app.js to the path of your Node app
/www/YOURAPP/run to where you want your PID file
/www/YOURAPP/log to where you want your logs
--chuid node to --chuid OTHERUSER if you want it to run as a different user than node
(make sure to add a user with a name from --chuid above)
With your /etc/init/YOURAPP.conf in place you can safely restart your server and have your app still running, you can run:
start YOURAPP
restart YOURAPP
stop YOURAPP
to start, restart and stop your app - which would also happen automatically during the system boot or shutdown.
For more info see those answers about:
Installing Node
Running Node on servers
You can also use systemd for that but there are some differences as the system is much more complicated and often leads to some problems.

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

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

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

Starting node server automatically when auto-scaling EC2 does not work

I would like to automatically run node server when instances are created (using forever). I am on Ubuntu 11.10 (Canonical), I followed the instructions here exactly on creating launch config using user script: http://alestic.com/2011/11/ec2-schedule-instance
I can't seem to get this to work. Below is my startup script:
#!/bin/bash
set -e -x
/home/MyUserName/node_modules/.bin/forever stopall
/home/MyUserName/node_modules/.bin/forever start node.js/app.js
The launch config is created using this cmd:
as-create-launch-config MyLC --image-id ami-b6a3f8f2 --user-data-file user-data-script.sh --instance-type m1.small
Found the issue, I have to run forever as the user, not root, wonder why...like so:
sudo -u MyUserName /home/MyUserName/node_modules/.bin/forever start node.js/app.js
Are you fully qualifying the app.js file? Could it just be this line?
/home/MyUserName/node_modules/.bin/forever start /home/MyUserName/node.js/app.js

Resources