Archive

Posts Tagged ‘capistrano’

passenger standalone with god on rvm

March 21st, 2011 3 comments

Some time ago I have migrated my server to nginx. Now I got first chance to setup rails application on it.

Searching for new experiences I have tried a new setup – passenger standalone on rvm monitored by god and cron  (see Q&A on the end).

Prerequisite to that guide is a working deployment script (putting files on server and db:migration working also rvm set up on server). Important is also to replace all occurrences of “/path/to/application” with your real path to application on server.

Nginx is also required, in case you do not have it already you can install it with following command (ubuntu server on linode):

sudo apt-get install nginx

Assuming you have an deploy script up and running you need to add few changes before deployment.

First add god configuration, in your RAILS_ROOT create file config/local.god:

passenger_config = {
:instances => (1..3),
:path => '/path/to/your/application/current'
}
God.watch do |w|
w.name = 'Passenger'
w.start = "ps -U $USER u | grep -v awk | awk '/nginx/ {print $2}' | xargs kill 2>/dev/null ;
passenger start #{passenger_config[:path]} -d  -e production \
-S #{passenger_config[:path]}/tmp/pids/passenger.socket --pid-file #{passenger_config[:path]}/tmp/pids/passenger.pid \
--min-instances #{passenger_config[:instances].min} --max-pool-size #{passenger_config[:instances].max}"
w.stop = "passenger stop #{passenger_config[:path]} --pid-file #{passenger_config[:path]}/tmp/pids/passenger.pid"
w.pid_file = "#{passenger_config[:path]}/tmp/pids/passenger.pid"
w.behavior(:clean_pid_file)

w.start_if do |start|
start.condition(:process_running) do |c|
c.interval = 5.seconds
c.running = false
end
end

end

Also new gems have to be added in Gemfile:

group :production do
gem 'passenger', '~>3.0.5'
gem 'god', '~>0.11.0'
end

Now deploy both files to server, they will be needed later on, and do not forget to run

bundle install --without=production

before deployment.

Second add an request to restart passenger after deploy:

touch /path/to/application/tmp/restart.txt

If your deployment script is capistrano you should use the following snippet:

namespace :deploy do
[:start, :stop].each do |method|
task method, :roles => :app, :except => { :no_release => true } do
run "local_god #{method} Passenger"
end
end
task :restart, :roles => :app, :except => { :no_release => true } do
run "touch #{File.join(current_path,'tmp','restart.txt')}"
end
end

So now lets go to server to finish configuration.

We need to install god and passenger (which we added on the beginning):

bundle install --without=development test cucumber

please note that cucumber is only required when you use it, skip this param if cucumber is only an tasty vegetable for you.

One of most important steps is to create god wrapper that will be used for starting application (replace my_ruby_1.9.2@my_application with your rvm identifier):

rvm wrapper my_ruby_1.9.2@my_application local god

Next we need to make test run of passenger, it will also compile nginx and notify of any problems if it finds any:

cd /path/to/application/current ; passenger start

Passenger will be started on port 3000 if there is no firewall on server (you missed to install it) then you can visit your browser and enter your server.address:3000. adding -p 8080 would server application on the port 8080 (welcome java users). To stop passenger just hit CTRL+C :)

When that works we can set up proxy in the main nginx configuration. On ubuntu (linode) the steps look as follows:

sudo vim /etc/nginx/available-sites/your-application.conf

and put the following content (do not forget to replace my_domain with your dns name of the server):

server {
listen   80;
server_name  my_domain;

access_log  /var/log/nginx/my_domain.access.log;
error_log  /var/log/nginx/my_domain.error.log;

location / {
proxy_pass        http://unix:/path/to/application/current/tmp/pids/passenger.socket;
proxy_set_header  X-Real-IP  $remote_addr;
}
}

after that (ESC :wq) you need to link configuration and restart server

sudo ln -s /etc/nginx/sites-available/your-application.conf /etc/nginx/sites-enabled/your-application.conf
sudo /etc/init.d/nginx restart

last step is to put local god into autorecovery mode with a simple cron job run crontab -e and enter the following content:

* * * * * ps aux | grep -v grep | grep -q "local\.god" || /home/user_name/.rvm/bin/books_god -c /path/to/application/current/config/local.god

That’s all, in one minute your server should be ready, do not forget that first call to passenger application might be sluggish, but it is only the first one when rails need to be initialized, rest of them is really fast.

To check the whole setup you could put small change in some view, deploy application, refresh your browsers (wait few seconds for rails startup) and check your results.

Q & A

Q: Why passenger, why not unicorn or thin ?

A: It is extremely easy to set up.

A: It is one of the fastest ruby servers (some say it is fastest one).

A: It takes far less memory then glassfish or torquebox.

Q: Why passenger standalone, not integrated into apache or nginx ?

A: It is integrated with nginx, but installation is automated – almost no manual steps needed.

A: It runs from user context, so it can be fully controled by user and does not affect other users.

A: It does not need to recompile main server – no dependencies for serving other pages, multiple version can be run at the same time.

Q: Why rvm?

A: It allows easy maintenance of ruby version for user installations.

A: It allows to separate gem repositories for multiple applications.

Q: Why God ?

A: It allows easy monitoring and management of processes – in our case manage passenger standalone.

Q: Why Capistrano ?

A: Really?

Q: Why do you kill nginx when starting passanger ?

A: It’s a hack, it might sometimes happen that passenger process is already stopped but nginx did not finished it’s operation yet, so to allow new process to start we kill the old one.

Q: Can I ask a new question, or provide more answers

A: I encourage you to ask questions and give answers, of course I will post only those that give any value.

easy capistrano remote invocation

May 6th, 2009 Comments off

Today I was coding just for fun … and wrote my own Capistrano script for deployment, during this I have found great way to invoke remote tasks.

The method is easy, add following code to your config/deploy.rb file:

set :sudo_call, ''
desc 'makes remote/rake calls to be executed with sudo'
task :use_sudo do
  set :sudo_call, 'sudo'
end

desc 'run rake task'
task :rake do
  ARGV.values_at(Range.new(ARGV.index('rake')+1,-1)).each do |task|
    run "cd #{current_path}; #{sudo_call} RAILS_ENV=production rake #{task}"
  end
  exit(0)
end

desc 'run remote command'
task :remote do
  command=ARGV.values_at(Range.new(ARGV.index('remote')+1,-1))
  run "cd #{current_path}; #{sudo_call} RAILS_ENV=production #{command*' '}"
  exit(0)
end

desc 'run specified rails code on server'
task :runner do
  command=ARGV.values_at(Range.new(ARGV.index('runner')+1,-1))
  run "cd #{current_path}; RAILS_ENV=production script/runner '#{command*' '}'"
  exit(0)
end

Now try your new tool with following commands:

cap rake db:migrate
cap use_sudo rake db:migrate
cap remote "tail -n 10 log/production.log"
cap use_sudo remote cat /etc/passwd
cap runner p User.all
cap runner "User.all.each{ |u| p u }"

In the third call I have used parentheses to hide “-n” form Capistrano, because it is its parameter, to see whole list of Capistrano parameters call it with “cap –help”. For the last command I have used parentheses again because now it contained bash special characters.

Did You liked this post or maybe not, vote on it at dzone.

Categories: Development, Linux Tags: , , , ,
Get Adobe Flash playerPlugin by wpburn.com wordpress themes