Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Running Ruby apps on Heroku with Phusion Passenger

passenger heroku

  • Please log in to reply
No replies to this topic

#1 FooBarWidget

FooBarWidget

    Passenger

  • Members
  • 2 posts

Posted 11 September 2013 - 01:25 PM

Heroku is a popular hosting service among Rubyists. Heroku makes deploying Ruby web apps very easy and they even provide free accounts.

 

But when you run your app on Heroku, you still need an application server. Phusion Passenger is such an application server, designed to be fast, robust and lightweight. By combining Heroku with Phusion Passenger, you can boost the performance of your apps, utilize the available resources on your dynos much more efficiently and increase its stability. This tutorial teaches how you can use Phusion Passenger on Heroku.

 

Phusion Passenger for Heroku brings the power of Nginx to your dynos. Nginx is an extremely fast and lightweight web server that powers 10% of the Internet. All the cool guys are rapidly switching to Nginx. Phusion Passenger replaces Thin and Unicorn, and makes full use of Nginx to serve your Ruby apps faster and better.
 
nginx_passenger_eyecatcher.png
 
Here's a list of the benefits that using Phusion Passenger will bring you:
 
  • Static asset acceleration through Nginx
    Don't let your Ruby app serve static assets, let Nginx do it for you and offload your app for the really important tasks. Nginx will do a much better job.
  • Multiple worker processes
    Instead of running only one worker on a dyno, Phusion Passenger runs multiple worker on a single dyno, thus utilizing its resources to its fullest and giving you more bang for the buck. This approach is similar to Unicorn's. But unlike Unicorn, Phusion Passenger dynamically scales the number of worker processes based on current traffic, thus freeing up resources when they're not necessary.
  • Memory optimizations
    Phusion Passenger uses less memory than Thin and Unicorn. It also supports copy-on-write virtual memory in combination with code preloading, thus making your app use even less memory when run on Ruby 2.0.
  • Request/response buffering
    The included Nginx buffers requests and responses, thus protecting your app against slow clients (e.g. mobile devices on mobile networks) and improving performance.
  • Out-of-band garbage collection
    Ruby's garbage collector is slow, but why bother your visitors with long response times? Fix this by running garbage collection outside of the normal request-response cycle! This concept, first introduced by Unicorn, has been improved upon: Phusion Passenger ensures that only one request at the same time is running out-of-band garbage collection, thus eliminating all the problems Unicorn's out-of-band garbage collection has.
  • JRuby support
    Unicorn's a better choice than Thin, but it doesn't support JRuby. Phusion Passenger does.
 
More information about Phusion Passenger:
Creating a new app
 
Clone the demo repository and push it to Heroku. This demo is already configured to use Phusion Passenger.
 
    cd passenger-ruby-heroku-demo
    heroku create
    git push heroku master
    heroku open
 
Your app is now powered by Phusion Passenger!
 
Switching an existing app to Phusion Passenger
 
Phusion Passenger is a drop-in replacement for Thin and Unicorn and very easy to install.
 
Open your app's Gemfile. Remove the following lines of they exist:
 
    gem "unicorn"
    gem "thin"
 
Insert:
 
    gem "passenger"
 
Open your app's Procfile, or create one if you don't already have one. Remove lines like this:
 
    web: bundle exec ruby web.rb -p $PORT
    web: bundle exec unicorn -p $PORT
    web: bundle exec thin start -p $PORT
 
Insert:
 
    web: bundle exec passenger start -p $PORT --max-pool-size 3
 
Finally, bundle install, commit and deploy:
 
    bundle install
    git commit -a -m "Switch to Phusion Passenger"
    git push heroku master
 
Congratulations, you're now running on Phusion Passenger!
 
Configuration
 
Any configuration is done by customizing the arguments passed to the `passenger` command. The most important ones are:
 
  • --max-pool-size - The maximum number of worker processes to run. The maximum number that you can run depends on the amount of memory your dyno has.
  • --min-instances - If you don't want the number of worker processes to scale dynamically, then use this option to set it to a value equal to `--max-pool-size`.
  • --spawn-method - By default, Phusion Passenger preloads your app and utilizes copy-on-write (the "smart" spawning method). You can disable this by setting this option to `direct`.
  • --no-friendly-error-pages - If your app fails to start, Phusion Passenger will tell you by showing a friendly error page in the browser. This option disables it.
 
Next steps
 
 * Using Phusion Passenger on Heroku? Tweet about usfollow us on Twitter or fork us on Github.
 * Having problems? Please post a message at the community discussion forum.
 






Also tagged with one or more of these keywords: passenger, heroku

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users