Rails in production -- CentOS, Apache, MySQL - ruby-on-rails

I'm a newly converted LAMP stack developer and wondering how to deploy a rails version of our large scale app.
Our servers currently have:
CentOS
Apache 2.x
MySQL
I've read about products that aid in setting up with this stack (Passenger, etc) but I'm concerned with speed and server load (as our app is already a high trafficked one).
How are people deploying rails applications for production use on a similar setup?
How could I setup rails to use this existing stack without adding anything else (or only adding production scale components to it)? -- Sort of a LAMR of sorts (Linux Apache MySQL Rails)
NOTE: The setup will still serve existing projects that are built with PHP -- so the setup can't really break any existing stack functionality

Passenger is a good option and is already used by lots of high-traffic sites in production.

ad. 1. As you already said, Passenger + REE is popular choice.
ad. 2. You could try to run it with lightweight app server like thin or unicorn. http://articles.slicehost.com/2009/4/17/centos-apache-rails-and-thin

Is it possible to not use Passenger and deploy with just Apache and MySQL?
I don't believe you can run rails with only apache and mysql in such a way. Much like the way mod_php takes care of processing the PHP for Apache, a module like passenger (www.modrails.com) takes care of processing the Rails application for you.
You can use the PassengerEnabled off (more info) setting in the vhost to disable passenger for the PHP apps. (although your php app will work fine as long as it is not placed in your rails app without this setting.)
About production scale settings. I believe that Twitter used to use apache as a load balancer to a number of mongrel http-servers. (see this article for some extra information). As you can see, caching (e.g. memcached) can provide a huge boost to your app if implemented well.

Related

Config rails server on vps hosting

I must configure my RoR vps hosting for my rails app, but on my localmachine i simple use webrick...
Are any ways to do this on server, so that it will be faster, and also, if it reboot's or memory down, or something that call's shutdown of server, then it automatically starts?
Also how about rails enterprise?
Now i have clean ubuntu server, and rvm on it
Follow this tutorial, it's great for beginner and will answer all your questions: http://coding.smashingmagazine.com/2011/06/28/setup-a-ubuntu-vps-for-hosting-ruby-on-rails-applications-2/
Which webserver to use depends on many things. Among others, there's Thin, Unicorn, Rainbows, Puma, and counting.
I suggest you look on google (or even here, I'm pretty this was answered before) for comparisons between ruby webservers, so that you can know what situation each of them are adapted to, and which one will fit your needs best.
For production deployment you need to install nginx or apache,
nginx is lightweight ,consider installing nginx,
Now there are various ways simple and easy to install is passenger with nginx
Or simply you can use nginx+unicorn or nginx+thin you can find various article just google it.

Phusion Passenger (for Dummies!)

I'm an experienced LAMP developer moving into Rails. I have a very stupid question to ask: what the hell does Phusion Passenger do?
I've read a lot of documentation, I've Googled, I've read Wikipedia, I've read Stack Overflow. I've even installed it and gotten it running on a development machine (with Apache). I still don't know what it's actually doing though.
Here's one guess: I think it's weird that the Apache document root points to /mywebapp/public/ instead of /mywebapp/, so I assume it has to do with making everything inside of /mywebapp/ accessible. (That's a wild guess though, based on the fact that I don't know how else that stuff is getting accessed.)
I've gathered that Passenger is revolutionary, groundbreaking, etc, etc, but what does it do!?
Sorry for the n00b question, everyone. Thanks!
Passenger is a system for preparing and launching instances of Ruby for use with Rack-based applications such as Ruby on Rails. Apache and nginx, the two supported web server platforms, cannot run Ruby like they can PHP, Perl, or Python because there's no built-in Ruby module that works as well as those do. This means Ruby tends to run as an independent group of processes that the web server will have to direct traffic through.
Rails tends to run as a persistent process because the start-up time for the whole stack is significant. Passenger launches new instances as they are required, and will kill off those that are no longer required. You can see this in the process list as they are clearly identified with "Passenger" and "Rails" prefixes.
One feature of Passenger is it will re-use a portion of the Rails stack so that creating additional processes is faster, cloning one instance instead of spinning up a new one from scratch. The loader is written in C++ and handles properly configuring and kicking off each Ruby process as efficiently as possible and also helps save memory by sharing it amongst different processes.
The reason you host things out of the public/ directory is to avoid exposing your application code-base. PHP needs to be configured properly to prevent people from simply browsing directories and downloading the source because there's no specific distinction between static content and executable scripts. A mis-configured server will gladly serve up raw .php files instead of running them, for instance.
Passenger isn't exactly revolutionary, but it does incorporate a number of essential features in a very convenient package. What makes it such a great thing is that it works very well and doesn't demand a lot of attention. Out of the box it's pretty much ready to go.
It serves ruby on rails applications (actually any rack application). I was used with version 2.x of passenger to integrate it directly in apache, but with the new version, which supports standalone execution, I prefer to run it in standalone mode (in conjunction with rvm)
It can be very useful both in development mode and in production mode and it speeds up execution of RoR application.
In order to deploy a RoR application I install it with its own gemset and then I install passenger in that gemset with gem install passenger (you can also safely skip gemsets, but they will isolate the application environment, so it's nice to have them). After that you can start the application with passenger start -a 127.0.0.1 -p 3081 -e production in the project root.
Then I configure an apache vhost to work in reverse proxy mode with a file like this one
<VirtualHost *:80>
ServerName your.server.org
ProxyPass / http://localhost:3081/
ProxyPassReverse / http://localhost:3081/
ProxyRequests Off
# Local reverse proxy authorization override
# Most unix distribution deny proxy by default (ie /etc/apache2/mods-enabled/proxy.conf in Ubuntu)
<Proxy http://localhost:3081/*>
Order deny,allow
Allow from all
</Proxy>
</VirtualHost>
and you're ok, you have your application deployed with its local config, it doesn't even need root privileges (for the passenger part).

Deploy Rails app to EC2

My setup: Rails 2.3.10, Ruby 1.8.7 on Windows
The last time I deployed a Rails app from Windows to Linux on Slicehost, I used Capistrano, Nginx, Mongrel, and SVN. That was 3 years ago, fast forward to now, I'm still on Windows for development and is now looking to deploy to EC2. A quick search turns up tools like Rubber and Chef which aren't easy to grasp with a quick read. It seems like Rubber and Chef are designed for multi-EC2 instances deployment which will be useful when I need to scale.
I'm also new to Passenger but it seems to be the default way to deploy Rails app nowadays, one thing that isn't so clear to me is whether Passenger is a replacement for Mongrel? In my old setup, I configured Nginx to forward the Rails requests to a cluster of Mongrel processes but I don't see anything like that for Passenger.
Any insights are much appreciated.
We use something like what you're describing for our production server: EC2 + Apache + Passenger. We haven't had any need to use the fancy deployment tools you describe - plain old Capistrano (plus capistrano-ext so we can use it for multiple environments) does the job just fine. I've looked at Rubber (not Chef), but deemed it needlessly automagical and too poorly documented, and I'm really not sure what it offers that can't be done just as well with roles in Capistrano.
Passenger has been great. It's an "overseer" that manages a collection of Mongrel-like workers (I had thought that the workers were Mongrels, but upon further reading, I don't think they are. The Passenger comparisons page even compares its RPS to a Mongrel cluster, so...), starting them up as needed, culling them under low loads, restarting them if they crash, etc. It's actually very similar to the Server + Mongrel Cluster you described, but probably a bit better, as Passenger has an understanding of the underlying workers that Nginx / Apache don't. And you'll have to make a few minor tweaks to get Capistrano playing nicely with Passenger.
And if possible, pair Passenger with Ruby Enterprise Edition (from the same guys who made Passenger). It's a much faster version of Ruby, mostly due to a rewritten, configurable garbage collector. You'll have to tune your GC settings to get the most out of it.
Hope this helps!
Both might help:
http://ginzametrics.com/deploy-rails-app-to-ec2-with-rubber.html
Hosting rails on ec2
Rubystack allows you to have the same Rails environment for development on Windows and for deployment on Linux. We also have EC2 images (scroll to the bottom) and it is completely free, so you may want to give it a try.
Also, this may not work for you, but depending on your requirements, you may want to go for a PaaS solution like Heroku

Is there a linode version of EC2 on Rails?

We really like the idea of hosting with Amazon EC2 and the excellent EC2 on Rails, but our cashflow isn't enough to justify a move to EC2. So we've decided to host with linode. Now we're trying to put together the best Rails server build.
For those that don't know, EC2 on Rails is an opinionated Ubuntu Linux Server image for Amazon's EC2 hosting service. Out of the box, it runs a standard Ruby on Rails application with little to no customization.
So, is there something like EC2 on Rails for linode?
We'd need at least the following:
Ruby 1.8.7
Ruby on Rails 2.3.8
MySQL 5
memcached
Automatically runs hourly, daily, weekly and monthly scripts if they exist in Rails application’s script directory
Local Postfix SMTP mail server
SSL support
Passenger + Ngnix or Mongrel + Apache
32 bit image Ubuntu
With a railsy setup, meaning fast and simple. So, anyone run across anything like EC2 on Rails, but for linode?
I have been using linode for my personal use for a while and I think no matter what, you are probably going to have to get your hands dirty. I kind of like it for exactly that reason, but that is just my taste. They do have stack scripts with some predefined setups. The closest I can find to what you are looking for is a ruby/apache/mysql script that is fairly customizable.
From the script:
Installs a fully functioning, ready to
go stack that's optimized specifically
for your Linode's resources. By
default, it creates a VirtualHost
using the reverse DNS of your Linode's
primary IP.
This installs a stack based on Ruby,
Apache, and MySQL. This also gives you
the options to install gems so that
you can be up and running Ruby on
Rails in no time.
Optionally creates a MySQL database
and user, and assigns that user grants
to the database.
You may use this as an example for
creating more VirtualHosts. Set up
VirtualHosts, install your sites,
point your domains to your Linode, and
you're set!
This script downloads and compiles the
source from Ruby's ftp. Along with
Ruby, the latest version of ruby gems
is installed and you have a choice of
initial gems to install. Rails and
passenger can be used to have a ready
to go Rails server. Once this script
has finished be sure to run
'passenger-install-apache2-module' to
complete the passenger install.
Note that this script may take 1.5 - 2
hrs depending on the gems that are
specified.
All details of this stack script are
logged to /root/log.txt and the stack
script is finish when the line
"StackScript Finished!" is written to
the log file.
You might have good luck talking to their support though, they might have some more secret sauce that I haven't found yet.
Nothing in that setup sounds EC2-specific beyond the name. It appears it is just convenient to use with EC2, because they let you grab a disk image from another account.
You are welcome to create your own StackScript to do this and contribute it to the community. Once a StackScript is written, it can be marked public and used by others. EC2 on Rails has a public GitHub repository from which you can get started, and honestly, I'd love to see things like this ported to the StackScript system.

When to use Mongrel as web server for Rails application?

When I started my first bigger Rails application, I chose to use Apache (with Passenger), because I already had some experience with it with PHP, but I don't have any deep knowledge of deploying Rails applications.
How would one choose web server for a Rails app?
Are there any main differences between Apache and Mongrel?
Or is it just a matter of taste?
As you know, two popular setups are:
Apache + passenger
Apache + mongrel (Apache sending requests to your mongrel cluster)
Yes, It is a matter of taste.
You made the good choice with Passenger for your first app as it is far easier to setup.
It is the #1 recommended option on ROR official Website.
Performance is also very good.
Using mongrel is still a good option but it is harder to setup and administrate.
You will need to make sure you are running your rails app against a cluster of mongrel and not only one mongrel instance (for performance/concurrent request reason). Meaning adminstration of your apache + administration of your mongrels.
There is no reason to use mongrel.
The reason you see lots of sites/tutorials using mongrel is.. it was a good choice before Passenger.
Passenger is better on the performance side, dead easy to install and no (for the 90% cases) managing is required.
You can also do Nginx + Mongrel cluster. (I don't recommend it though.) Go with Phusion Passenger. It's pretty easy to setup and it's fast. No reason not to use it.
I use Passenger with Nginx. Besides just the web server, also look into using Ruby Enterprise Edition. It's going to save you a good amount of memory. ~30%. Ruby Enterprise Edition.

Resources