Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
I couldn't find a quick example of how to deploy a node.js app using Capistrano 3. This gist assumes you are using Capistrano 3, Upstart, Forever, ssh/forward agent, and an unprivileged user named 'deploy'. Hopefully this simple setup will help to get you started.
# config/deploy.rb
# probably a lot of ways to improve this...
set :application, 'my_app'
set :repo_url, ''
# should set up a deploy user
set :user, 'deploy'
set :deploy_to, '/var/www/my_app'
set :scm, :git
set :format, :pretty
set :log_level, :debug
set :pty, true
set :keep_releases, 5
namespace :deploy do
#TODO: Add stop task in upstart
desc "Stop Forever"
task :started do
on roles(:app) do
execute "forever stopall"
desc "Install node modules non-globally"
task :npm_install do
on roles(:app) do
execute "cd #{current_path} && npm install"
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
# This assumes you are using upstart to startup your application
# - be sure that your upstart script runs as the 'deploy' user
execute "sudo start node-upstart-script", raise_on_non_zero_exit: false
before :restart, 'deploy:npm_install'
# /etc/init/node-upstart-script.conf
description "myapp"
author "Your Name <>"
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
export HOME=/home/deploy
export NODE_ENV=production
cd /var/www/my_app/current/
exec sudo -u deploy forever start -l "/var/log/node/my_app.log" -a app.js >> /var/log/node/my_app.log 2>&1
end script
# config/deploy/production.rb
set :stage, :production
server '', user: 'deploy', roles: %w{web app}, my_property: :my_value
server '',
user: 'deploy',
roles: %w{web app},
ssh_options: {
# user: 'user_name', # overrides user setting above
# keys: %w(~/.ssh/id_rsa),
forward_agent: true,
auth_methods: %w(publickey password),
#password: 'use a key instead'
Copy link

hmontazeri commented Oct 22, 2014

Hi, im getting an error:

cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing on host XX:XX:XX:XX forever stopall exit status: 1

can you help on this one?

Copy link

hmontazeri commented Oct 22, 2014

okay i fixed it....
When no precess is running, it couldn't stop any process.

Copy link

vatsalGS commented Apr 5, 2016

@hmontazeri how you fixed it? I want to know how you are catching if forever stop returns error or success.

OP your code will not work on latest capistrano version as :restart task does not gets executed by default aymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment