Following from the Ubuntu Intrepid - Mongrel and mongrel cluster article, we can now look at creating and configuring Apache to proxy to a mongrel cluster so we can serve our Ruby on Rails application.
Contents |
To get the most out of this article you need to have a couple of things preinstalled:
Firstly, you need Apache installed (Ubuntu Intrepid - Apache and PHP install) — if you don't require PHP then please feel free to leave that section out.
Secondly, you will need to have installed mongrels as per the mongrel and mongrel cluster article link above.
To start with, we will need a basic Ruby on Rails application. Move into your public_html folder (create one if you do not have one already):
cd ~/public_html
Then create a Rails application. We'll use the default sqlite database for this example:
rails railsapp
Apache will need the proxy and rewrite modules enabled.
Depending on your Apache install you may need to issue all the following commands:
sudo a2enmod proxy sudo a2enmod proxy_balancer sudo a2enmod proxy_http sudo a2enmod rewrite
Once done, reload Apache:
sudo /etc/init.d/apache2 force-reload
Well, for our simple application we're going to create a mongrel cluster consisting of 3 mongrels running on port 5000 in production mode. We'll also add a symlink so the cluster will restart if the Cloud Server is rebooted at any point.
Then we can create an Apache virtual host to serve the Ruby on Rails application.
I won't go into the details of explaining what the commands are in this section. Please refer to the main Ubuntu Intrepid - Mongrel and mongrel cluster article for that.
Ensure you are in the rails folder:
cd ~/public_html/railsapp
Then create a mongrel cluster file as such:
mongrel_rails cluster::configure -e production -p 5000 -N 3 -c /home/demo/public_html/railsapp -a 127.0.0.1
It's always a good idea to check the created file (config/mongrel_cluster.yml):
--- address: 127.0.0.1 log_file: log/mongrel.log port: "5000" cwd: /home/demo/public_html/railsapp environment: production pid_file: tmp/pids/mongrel.pid servers: 3
Looks good.
Now create a symlink to the /etc/mongrel_cluster folder. This ensures the cluster is restarted on a reboot:
sudo ln -s /home/demo/public_html/railsapp/config/mongrel_cluster.yml /etc/mongrel_cluster/railsapp.yml
Now all we need to do is start the cluster:
mongrel_cluster_ctl start
Done.
Now we can create the virtual host:
sudo nano /etc/apache2/sites-available/domain.com
he following will suffice for a basic application:
<VirtualHost *:80>
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /home/demo/public_html/railsapp/public
RewriteEngine On
<Proxy balancer://mongrel1>
BalancerMember http://127.0.0.1:5000
BalancerMember http://127.0.0.1:5001
BalancerMember http://127.0.0.1:5002
</Proxy>
# Redirect all non-static requests to thin
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://mongrel1%{REQUEST_URI} [P,QSA,L]
ProxyPass / balancer://mongrel1/
ProxyPassReverse / balancer://mongrel1/
ProxyPreserveHost on
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
# Custom log file locations
ErrorLog /home/demo/public_html/railsapp/log/error.log
CustomLog /home/demo/public_html/railsapp/log/access.log combined
</VirtualHost>
Nice and simple and, as you may have noticed, is the pretty much the same as the Apache vhost we created when using the Ubuntu Intrepid - Apache Rails and Thin.
There is a good reason they are the same — all they are doing is proxying rails requests to the 3rd party server. In this case, the requests are proxied to the mongrel cluster.
Now we must enable the vhost:
sudo a2ensite domain.com
Reload Apache:
sudo /etc/init.d/apache2 reload
If you get any port and NameVirtualHost errors then please read the Apache Virtual Host article which will take you through setting up said details.
All that's left is to navigate to your domain:
http://www.domain.com/
Where you will be greeted with the rails welcome page.
Setting up a virtual host to proxy to a mongrel cluster is fairly simple.
Although setting up and configuring mongrel, especially if you want them restart on a reboot, can be a bit complicated, once done it is a quick and powerful method for serving your Ruby on Rails application.
© 2011-2013 Rackspace US, Inc.
Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License

0 Comments
Add new comment