Working at a marketing firm, a lot of our client work doesn't end up as mission critical, high bandwidth sites. We end up pushing out projects that are either corporate sites requiring content control or content management apps that are used by a small subset of their employees.
Because of this, we have always deployed sites in a controlled, shared environment. We have our own boxes that our client work sits on. As we deploy more sites powered by Ruby and Rails, I've been on a mission for a nice deployment stack.
Since RailsConf, I've come to a satisfying decision of Apache 2.2, mod_proxy_balancer, and Mongrel. My next task was to get this stack working with multiple Rails apps in a shared environment. Luckily, getting this working is very trivial.
If you've read Coda's motivating post on getting this stack up and running, then some of this will be familiar to you. I'd suggest reading this first. My tips just build off of it.
Getting multiple Rails apps working in a shared environment is basically all up to Apache. Because your sites will be driven individually by Mongrels on separate ports, Apache proxies to them and vhosts become your friend.
mongrel.common
Like Coda, I kept my configuration DRY by creating a single file called mongrel.commmon. This contains all the configuration that every Rails app will need when being proxied from Apache. There's no need to repeat this stuff in every vhost.
RewriteEngine On
# Check for maintenance file and redirect all requests
RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
RewriteCond %{SCRIPT_FILENAME} !maintenance.html
RewriteRule ^.*$ /system/maintenance.html [L]
# Rewrite index to check for static
RewriteRule ^/$ /index.html [QSA]
# Rewrite to check for Rails cached page
RewriteRule ^([^.]+)$ $1.html [QSA]
# Deflate
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml application/xhtml+xml text/javascript text/css
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
First, we have Apache keep a look out for our maintenance.html file which is created by our nice disable_web Capistrano task. If its found, all traffic coming through Apache will be routed to it.
Next, we're using mod_rewrite to make sure Apache serves out our index files and cached pages. Having Apache serve our cached static files rather than our Mongrels, speeds everything up quite a bit.
The last thing we're telling Apache to do is to compress our static files to speed up and lower our outbound bandwidth. Everything gets decompressed on the clients browser. More on mod_deflate here.
site.vhost
The other file we need to get our new domain set up on our shared server is the domain's specific vhost file. Everything in this file will be contextual specifically to the domain.
<VirtualHost 62.44.22.11:80>
Include conf/mongrel.common
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /home/dev/apps/domain.com/current/public
ErrorLog logs/domain.com_errors_log
CustomLog logs/domain.com_log combined
<Directory "/home/dev/apps/domain.com/current/public">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
<Proxy balancer://domain.com_cluster>
BalancerMember http://127.0.0.1:8000
BalancerMember http://127.0.0.1:8001
</Proxy>
#Redirect all non-static requests to cluster
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://domain.com_cluster%{REQUEST_URI} [P,QSA,L]
</VirtualHost>
First off, add the ip address that your vhost will sit on listening for traffic. This will usually be the ip address of your server, and most importantly the ip that your domain resolves to. People familiar with shared hosting will recognize this. In addition, make sure you add...
NameVirtualHost 62.44.22.11:80
This allows you to use name based virtual hosting with Apache.
The next thing we do is include our nice abstracted configuration, mongrel.common. Then we add the typical things an Apache vhost file would have, ServerName, alias, logs, etc. We make sure we point the DocumentRoot at the public folder of our sites so Apache can serve our static files correctly.
Then we define our Mongrel cluster. Define a name for our cluster, and tell Apache where our Mongrels are running (these are defined in your config/mongrel_cluster.yml). Next, tell Apache which cluster you want your traffic proxied to via mod_rewrite. See the named cluster?
And that's it. Include the vhost file via your httpd.conf Apache conf file, and your new domain is ready to go. All you have to do to set up another site on this same server is (after doing the normal Rails portion setup. You ARE using Capistrno right?) to create a new vhost file and restart Apache.
I've probably used bit more words than needed to go over this, but I just wanted to show that its easy to use this great deployment stack in a shared environment.
Popularity: 15% [?]





About
Mmm, Del.icio.us
Thanks for the guide - this is pretty much what we're doing at the moment (minus capistrano - we're still hosting more java than rails stuff at the moment).
One aspect of shared hosting of rails app I'm stumbling on is the management of multiple mongrel processes - how are you handling the start/restart of all of these apps during say, a system restart or recovery of a crash? At the moment we're looking at custom bash scripts to manage this, is there anything better?
Keeran
--
http://www.beanlogic.co.uk
http://www.getflowing.com
http://www.keeran.com
Yah, thats been something i've been meaning to look at soon. I believe Coda touches on it in his tutorial, by symlinking the apps' mongrel conf to a central directory. Then a script will launch all of them at boot. Its actually the next thing I'll be looking at. But I've been enjoying a vacation for the last week :)
Great Stuff,
Just the article I've been looking for
Tuesday, May 22, 2007 (Akron...
``I'm very much like my father's side of the family, physically and in many other ways,'' Kriete said. ``My dad...
REVIEW: âCinderellaâ casts a spell...
REVIEW: âÂÂCinderellaâ casts a spellShreveport Times, LA -20 hours agoThe Ugly Stepsisters, played by Sarah Kelley and Carolyn Murphy, lent the show...
Bowflex Introduces a Space-Age Home...
Bowflex Introduces a Space-Age Home Gym to Revolution-ize Strength ...Market Wire (press release)Â -6 hours ago"This new home gym design is...
[url=http://dgrin.com/member.php?u=47094]Buy cheap biaxin online[/url]
http://www.nojazzfest.com/chat/member.php?u=5220 Buy Celexa