11

I am using Capistrano to deploy my Rails application. whenever I deploy, changes would not be reflected on the browser, and I still need to restart nginx to update the site (running sudo /etc/init.d/nginx restart). I'm not really sure why but isn't it supposed to be updated after restarting application? (using touch /app/tmp/restart.txt)

Here's my deploy.rb

require "rvm/capistrano"
set :rvm_ruby_string, 'ruby-1.9.3-p194@app_name'
set :rvm_type, :user

require "bundler/capistrano"

set :application, "app_name"
set :user, "me"

set :deploy_to, "/home/#{user}/#{application}"
set :deploy_via, :copy

set :use_sudo, false

set :scm, :git
set :repository,  "~/Sites/#{application}/.git"
set :branch, "master"

role :web, '1.2.3.4'
role :app, '1.2.3.4'
role :db,  '1.2.3.4', :primary => true
role :db,  '1.2.3.4'

namespace :deploy do
 task :start do ; end
 task :stop do ; end
 task :restart, :roles => :app, :except => { :no_release => true } do
   run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
 end
end
2
  • are you sure about "restart" instead of graceful reload?
    – Anatoly
    Commented Aug 14, 2012 at 7:11
  • 1
    Using reload does seem to be better, but it appears the code was actually working after all. I was deploying it the wrong way (cap deploy:cold). I don't really need to reload/restart nginx using cap deploy.
    – gerky
    Commented Aug 14, 2012 at 7:24

3 Answers 3

5

You shouldn't have to restart or reload nginx. Just touching tmp/restart.txt should be enough to tell passenger to reload the app.

If you're using a recent version of capistrano, you can even drop entire 'namespace :deploy' part. Capistrano already touches tmp/restart.txt after a successful deploy.

3
  • Yup, it works, turns out I was using deploy:cold instead of just deploy.
    – gerky
    Commented Aug 14, 2012 at 19:14
  • Not true if using Passenger Standalone and deploying with Capistrano using /releases/XXX folders. Touching tmp/restart.txt doesn't seem to notice the change in symlink folder current/ ~> releases/newest-release
    – Mirko
    Commented Jan 17, 2013 at 14:43
  • Probably there's something custom with that deploy. By default, Capistrano links current with the latest release. Passenger standalone works just as passenger compiled with nginx or as an Apache mod: when it notices a file named current/tmp/restart.txt, it restarts. Commented Jan 21, 2013 at 21:37
4

I realized that the deployment setup matches http://coding.smashingmagazine.com/2011/06/28/setup-a-ubuntu-vps-for-hosting-ruby-on-rails-applications-2/

When I followed this tutorial(about a year ago), I installed slightly newer versions of nginx and passenger. From what I remember, I think these newer versions prompted me to use nginx as a service when I ran any type of init.d command. (Ubuntu 10.04)

Anyways I would switch out the code

run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"

to

run "#{sudo} service nginx #{command}"

And see if that works.

4
  • Yup, I got it from the same tutorial. It still doesn't seem to restart nginx. Btw, what's the difference between running nginx as a service and running it via /etc/init.d/nginx? I don't think deploy:restart is executed at all, I just see * executing 'deploy:start' at the end of the logs. I'm using the 'cap deploy:cold' command to deploy.
    – gerky
    Commented Aug 13, 2012 at 19:05
  • Running service is the new way to do things is all I know. You should only use :cold on the first deployment, all updates after should just be cap deploy, you can also run cap deploy:restart Heres a helpful list
    – hellocodes
    Commented Aug 13, 2012 at 19:11
  • ohh..right, I just found out that cap deploy:cold runs deploy:start instead of deploy:restart. I'll just use cap deploy, thanks!
    – gerky
    Commented Aug 13, 2012 at 19:15
  • @gerky you run it as a service if your Linux distro uses systemd, and you run it via /etc/init.d/ if you have a distro based on init.d. ewontfix.com/14
    – iconoclast
    Commented Aug 28, 2020 at 17:08
1

Maybe the problem is in how exactly you started Passenger. Capistrano points the symlink 'current' to the latest release. The task

run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"

is using that 'current' to place the restart.txt. But according to http://code.google.com/p/phusion-passenger/issues/detail?id=547 , Passenger is "pinned" to the 'current' it was started in, while the task writes 'restart.txt' to the current 'current', so to speak. So Passenger doesn't "see" that it's supposed to restart.

If you cd'ed to the then 'current' and started Passenger from there, it gets pinned to the directory the 'current' symlink points to at that point and doesn't follow the changes of the symlink. So you might need to get rid of the 'cd ... && passenger start...' and provide the path to Passenger directly. I extended the deploy:start and deploy:stop tasks you have in your recipie as well to say

task :start, :roles => :app, :except => { :no_release => true } do
  run "passenger start #{current_path} -a 127.0.0.1 -p 3000 -e production -d"
end
task :stop, :roles => :app, :except => { :no_release => true } do
  run "passenger stop #{current_path} -p 3000"
end

Not the answer you're looking for? Browse other questions tagged or ask your own question.