User Guide - Ruby on Rails using Mongrel
- 01/12/2008 4:48 PM
Applicable Plans: All eApps General VPS plans
User Guide – Ruby on Rails Mongrel User Guide
Mongrel is a fast HTTP library and server for Ruby that is intended for hosting Ruby web applications of any kind using plain HTTP rather than FastCGI or SCGI. It is framework agnostic and already supports Ruby On Rails, Og+Nitro, and Camping frameworks – from mongrel.rubyforge.orgWhile mongrel is offered and supported, mod_rails is the recommended approach for deploying your Ruby on Rails applications on the eApps Hosting service |
Overview
This User Guide is designed to get you started with Mongrel Rails on your eApps hosting plan. This user guide only provides a brief overview, and is not a definitive resource. If you need more information than what is provided in this User Guide, you will want to reference the official Mongrel documentation. It is highly recommended that you at least read over the official documentation, to get a full understanding of what Mongrel does.The information in this User Guide is based on the assumption that you have already read the Ruby on Rails User Guide and have met all the Requirements. If you have not read that User Guide, please do so now. Reading and following that User Guide is the key to success with the information in this User Guide.
Mongrel is only available on plans using the CentOS 5 or greater operating system. To see what operating system (OS) you have, click on the Subscriptions icon from the My Account tab of your Control Panel. Then click on the name of the subscription you want to see. The OS for the subscription will be displayed near the top of the page. If you are not on a CentOS 5 plan, but would like information on updating your plan, please contact eApps Technical Support for more information.
Making your applications available with Apache mod_proxy
Starting your Ruby applications automatically at boot time
Providing High Availability for your Ruby Applications with mod_proxy_balancer
Links to other information
Making your applications available with Apache mod_proxy
If you have not done so, upload your application to your VPS, or create an application as explained in the Ruby on Rails guide. These examples assume that your application was created or uploaded to the /home/webadmin/example.com/html directory. Please adjust your user and path and application name as necessary.This is how to start your Ruby application with Mongrel:
[webadmin@example ~]$ cd /home/webadmin/example.com/html/newapp/ [webadmin@example newapp]$ mongrel_rails start -p 3000 -e production -d |
This will start the newapp application on port 3000 (-p 3000) using the production (-e production) Rails environment and will also make the Ruby server to run in background as a daemon (-d).
At this point the newapp application will be available on port 3000 on your VPS. If we create or upload another application named my1stapp we have to start it on another port because 3000 is already taken by the Mongrel server running for the newapp application:
[webadmin@example html]$ cd /home/webadmin/example.com/html/my1stapp [webadmin@example my1stapp]$ mongrel_rails start -p 3001 -e production -d |
Since the applications are running on non-standard ports, by default they are not available at the standard URLs of http://example.com/newapp and http://example.com/my1stapp. To allow them to be available at those URLs, without having to enter a port number, use mod_proxy to pass the requests from Apache to Ruby.
The mod_proxy settings have to be entered in the Custom Settings tab for the site. Log in to your Control Panel, and click on the System Tab. If necessary, click on the Select Another System (Subscription) link on the left and choose the correct Virtuozzo Container. Once the correct system name shows in the System Tab, click on the Site Tab. If you have more than one site, you may need to click on the Select Another Site link, and choose the correct site.
Once the correct site is selected, then click on Website Settings, then on the Custom Settings tab. Click Edit, and add the following directives. Remember this is just an example, please remember to use your application names.
ProxyPreserveHost On
ProxyPass /newapp/ http://localhost:3000/
ProxyPass /my1stapp/ http://localhost:3001/
Click Update to save the settings. Now the applications will be available at the http://example.com/newapp and http://example.com/my1stapp URLs.
Please see the mod_proxy User Guide for more information, as well as the official Apache mod_proxy documentation.
Starting your Ruby applications automatically at boot time
To ensure that your Ruby applications are correctly started and stopped using the Mongrel Rails library you will have to maintain a text file named /etc/sysconfig/rubyapps. Login to your VPS as the root user to create the file.[root@example ~]# cd /etc/sysconfig/ [root@example sysconfig]# vi rubyapps |
This is the syntax for the lines, one line per application:
/path/to/ruby/application port_number deployment_mode
The port numbers have to be unique in this file and also may not to be used by any other application. We recommend that you use ports between 3000 and 3100.
The deployment_mode corresponds to the Rails environment. It can be set to development, production or testing. You can find out more about Ruby environments from http://wiki.rubyonrails.org/rails/pages/Environments. In this script it is optional and when missing it will be set by default to development.
For the applications newapp and my1stapp, this file would have the following content:
/home/webadmin/example.com/html/newapp/ 3000 production /home/webadmin/example.com/html/my1stapp/ 3001 production |
Save and exit the file.
You will also need to download the rubyapps service from the eApps repo, and configure that to run when the system reboots. This will allow you to stop and start mongrel_rails as a service. Also, you will be able to stop and start the rubyapps service from the Control Panel, System Tab, Service Management, Services.
[root@example sysconfig]# cd /etc/init.d [root@example init.d]# wget http://repo.eapps.com/rails/rubyapps --10:12:32-- http://repo.eapps.com/rails/rubyapps Resolving repo.eapps.com... 69.89.14.200 Connecting to repo.eapps.com|69.89.14.200|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 5326 (5.2K) [text/plain] Saving to: `rubyapps' 100%[=======================================================>] 5,326 --.-K/s in 0.003s 10:12:32 (1.98 MB/s) - `rubyapps' saved [5326/5326] [root@example init.d]# ll rubyapps -rw-r--r-- 1 root root 5326 Mar 27 2009 rubyapps [root@example init.d]# chmod 755 rubyapps [root@example init.d]# ll rubyapps -rwxr-xr-x 1 root root 5326 Mar 27 2009 rubyapps [root@example init.d]# /sbin/chkconfig --level=3 rubyapps on [root@example init.d]# service rubyapps start|stop|restart|status (these are the commands to start, stop, restart and show the status of the mongrel_rails service) |
Providing High Availability for your Ruby Applications with mod_proxy_balancer
Using Apache mod_proxy_balancer (only available with Apache 2.2 and CentOS 5) you can also implement High Availability (HA) for your applications.To give an example of a High Availability scenario, consider an application that cannot be stopped for upgrade or maintenance. In this case, two instances of the application will be deployed on one VPS, and a third instance will be deployed on a different server, either a VPS at eApps or one totally outside the eApps service as a hot standby server.
This is just one example of one way to accomplish HA using Ruby on Rails and mod_proxy_balancer. Please consult the official Ruby on Rails and mod_proxy_balancer documentation and other online examples for more information. mod_proxy_balancer is a complex application, so be prepared to do a fair amount of research, reading and testing to get things working correctly. |
This example assumes you have a working Ruby on Rails application that has been tested and configured and is working correctly in the /home/webadmin/example.com/html directory.
First, stop mongrel_rails, then copy the application to a new location.
[webadmin@example html]$ cd /home/webadmin/example.com/html/my1stapp/ [webadmin@example my1stapp]$ mongrel_rails stop [webadmin@example my1stapp]$ cp -pR /home/webadmin/example.com/html/my1stapp /home/webadmin/example.com/html/my1stapp-2 (this should be on one line, with the space between my1stapp and /home) |
If for some reason you fail to stop the already running Mongrel instance for the original application (or if you just don't want to produce downtime for your currently running application) please make sure you delete the log/mongrel.pid file from the new directory (in this case /home/webadmin/example.com/html/my1stapp-2/ , the path is relative to this directory) after the copy process is complete. |
And now you start both instances, one on port 3000 and the second one on port 3001. See the section about Starting your Ruby applications automatically at boot time for an example of how to set up the same configuration for the HA load balanced applications.
[webadmin@example my1stapp]$ cd /home/webadmin/example.com/html/my1stapp/ [webadmin@example my1stapp]$ mongrel_rails start -p 3000 -e production -d [webadmin@example my1stapp]$ cd /home/webadmin/example.com/html/my1stapp-2/ [webadmin@example my1stapp-2]$ mongrel_rails start -p 3001 -e production -d |
mod_proxy_balancer can now be configured to load balance the HTTP requests to the http://example.com/my1stapp URL between these two instances, and also fail over to a third instance running on another VPS or server if both instances fail (or are stopped for some reason).
To do this, the mod_proxy_balancer settings now have to be entered in the Custom Settings tab for the site. Log in to your Control Panel, and click on the System Tab. If necessary, click on the Select Another System (Subscription) link on the left and choose the correct Virtuozzo Container. Once the correct system name shows in the System Tab, click on the Site Tab. If you have more than one site, you may need to click on the Select Another Site link, and choose the correct site.
Once the correct site is selected, then click on Website Settings, then on the Custom Settings tab. Click Edit, and add the following directives. Remember this is just an example, please remember to use your application names.
ProxyPreserveHost On
<Proxy balancer://my1stapp>
BalancerMember http://localhost:3000/ loadfactor=100 # Balancer member 1
BalancerMember http://localhost:3001/ loadfactor=80 # Balancer member 2
BalancerMember http://hotstandbyserver:3000/ +H # Balancer member 3
</Proxy>
ProxyPass /my1stapp/ balancer://my1stapp/
The Apache Mod Proxy Balancer will split the requests between the two instances of the application running on local host port 3000 and 3001 according to the loadfactor parameter (please refer to the mod_proxy_balancer User Guide for more details) and if both instances become unavailable it will automatically switch to the http://hotstandbyserver:3000/ with no downtime for your URL http://example.com/my1stapp/.
Of course, instead of localhost you can use another VPS or external server (please make sure you can access from the local VPS the other host on port 3000, 3001 or whatever port is used by the ruby server) like in the following example:
ProxyPreserveHost On
<Proxy balancer://my1stapp>
BalancerMember http://localhost:3000/ loadfactor=100 # Balancer member 1
BalancerMember http://another-eApps-VPS:3000/ loadfactor=80 # Balancer member 2
BalancerMember http://hotstandbyserver:3000/ +H # Balancer member 3
</Proxy>
ProxyPass /my1stapp/ balancer://my1stapp/
Links to other information
Ruby on Rails Wiki - http://wiki.rubyonrails.org/Information on scaling Rails with mod_proxy_balancer - http://blog.innerewut.de/2006/04/21/scaling-rails-with-apache-2-2-mod_proxy_balancer-and-mongrel
eApps mod_proxy_balancer User Guide - http://support.eapps.com/hsp/mod_proxy_balancer
eApps Ruby on Rails User Guide - http://support.eapps.com/hsp/rails
eApps mod_rails User Guide - http://support.eapps.com/hsp/mod_rails