Rails Forum
Rails Work - the best place to post and find great Ruby on Rails jobs.
Username
Password

You are not logged in.

New Posts in this thread
  • Index
  •  » Tutorials
  •  » Setting up Mongrel and mod_proxy on a CPanel server

#1 2006-08-21 16:51:19

adamp
Coach Class
Registered: 2006-06-20
Posts: 73

Setting up Mongrel and mod_proxy on a CPanel server

Having just moved to a new server and getting fed up with the quirks of getting FastCGI to work, I finally looked into Mongrel. It turns out it's pretty easy to set up, so I wrote this how-to on my blog and thought I'd share it here.

Installing Mongrel

This, amazingly, is a really simple three-step process as described on the Mongrel homepage:


Code :   - fold - unfold
  1. $ sudo gem install mongrel
  2. $ cd /home/USER/myrailsapp
  3. $ mongrel_rails start -d
I was expecting to need to do more, but this much seriously will give you a mongrel server running in three commands. There’s presumably lots of clever configuration you can do to improve performance, but this works for the basic server.

Step 1 installs mongrel as a Ruby Gem.

Step 2 gets you into your Rails app’s directory

Step 3 starts the mongrel server daemon to run in the background (you can stop it with mongrel_rails stop)

If you’ve just done the above, you might want to stop Mongrel and restart with a command similar to that below to put Mongrel on another port. By default it starts on 3000 (like WEBrick)


Code :   - fold - unfold
  1. $ mongrel_rails start -d -p 8000
This will start the mongrel server running on port 8000 (choose whatever you want).

What we can now do is set up Apache with mod_proxy and point the relevant part of our website at the Mongrel server…

Setting up mod_proxy

This again turned out to be easier than expected, though I think CentOS helps by having the module readily available. I’m not sure if all CPanel setups on other distros will have the same luck, though I would imagine they should.

The steps to install/set up mod_proxy are as described in this post on CPanel’s forums:


Code :   - fold - unfold
  1. $ cd /home/cpapachebuild/buildapache/apache_1.3.37/src/modules/proxy/
  2. $ /usr/local/apache/bin/apxs -i -c *.c
The above two steps generate the libproxy.so file and place it in the libexec directory for Apache’s use.

With this done, you’re ready to edit Apache’s config file, httpd.conf. You’ll find it in /etc/httpd/conf/httpd.conf

Open in a text editor (vi, pico, etc. - pico’s easiest for beginners), i.e. pico /etc/httpd/conf/httpd.conf and search for ‘LoadModule’ (ctrl+w in pico) to find the section where all the modules get loaded.

At the end of the LoadModule lines, add yours:


Code :   - fold - unfold
  1. LoadModule proxy_module libexec/mod_proxy.so
Below this, you should see a list of lines starting ‘AddModule’. Again, go to the bottom and add your own line:


Code :   - fold - unfold
  1. AddModule mod_proxy.c
Save the file and return to the shell. You can now restart Apache to make sure it’s working:


Code :   - fold - unfold
  1. /sbin/service httpd restart
As long as it tells you it’s restarted OK, you’re ready to point Apache at your Mongrel server. I set mine up as a sub-domain, so I’ll use that as an example (you can set the sub-domain up via CPanel).

Pointing Apache at Mongrel

Let’s say I want my app at adamsapp.supersonicfeet.com. I’ve already created the sub-domain via CPanel, so I just need to edit the Apache config, which means opening httpd.conf again:


Code :   - fold - unfold
  1. $ pico /etc/httpd/conf/httpd.conf
Do a search in the file for the sub-domain, ‘adamsapp.supersonicfeet.com’ and you should find a section of code wrapped in <VirtualHost> tags, e.g.


Code :   - fold - unfold
  1. <VirtualHost 1.2.3.4>
  2. ServerAlias www.adamsapp.supersonicfeet.com
  3. </VirtualHost>
There’ll probably be quite a lot of stuff in there, possibly including some <IfModule> tags. Find the </VirtualHost> tag and insert these two new lines just before (above) it:


Code :   - fold - unfold
  1. ProxyPass / http://www.supersonicfeet.com:8000/
  2. ProxyPassReverse / http://www.supersonicfeet.com:8000/
In the above, you’d replace supersonicfeet.com with your own domain name (not the sub-domain you’ve just created for the app) and 8000 with whatever port you started your Mongrel server on. These two commands tell the server to forward all requests to ‘/’ on the current VirtualHost (adamsapp.supersonicfeet.com) to the address http://www.supersonicfeet.com:8000/.

In other words, if a visitor enters http://adamsapp.supersonicfeet.com/ into their browser, Apache will see that request and forward it to http://www.supersonicfeet.com:8000/ (the Mongrel server), before returning the result to the visitor’s browser. The visitor will just see that they’re looking at http://adamsapp.supersonicfeet.com/ - they’ll see no trace of the weird port number.

Equally, say they went to http://adamsapp.supersonicfeet.com/news/2006/08/21 - Apache would be showing them the result of http://www.supersonicfeet.com:8000/news/2006/08/21 but without changing the address that the visitor sees at the top of their browser. Apache is basically handing off control of any address under adamsapp.supersonicfeet.com to the Mongrel server running on port 8000 (our Rails app).

By my count, that’s seven basic steps to set up Mongrel and mod_proxy on your server as well as point a sub-domain at the Mongrel server you’re running. All this extra text has just been me rambling (and hopefully explaining some things a bit better). Feel free to ask any questions, please just bare in mind that I quite possibly won’t know the answer if it goes beyond the above steps smile

Last edited by adamp (2006-08-21 16:52:17)

Offline

 

#2 2006-09-05 18:24:02

davidf8
Ticketholder
Registered: 2006-09-05
Posts: 1

Re: Setting up Mongrel and mod_proxy on a CPanel server

Thanks for the tutorial. It was so easy to follow, I was up and running in about 10 minutes.

After researching this topic more it seems the mod_proxy setup is pretty basic. From my understanding this setup is forwarding all requests for both ruby/rails files and static content to mongrel. Is this correct? How could I alter this setup so mongrel only handles rails/ruby requests and Apache handles everything else?

Offline

 

#3 2006-09-05 19:03:07

Dieter Komendera
First Class
From: Zistersdorf, Austria
Registered: 2006-08-10
Posts: 104
Website

Re: Setting up Mongrel and mod_proxy on a CPanel server

I’ve basically the same setup at my debian 3.1 server.
You have just one little problem: When your server reboots, the mongrel servers aren’t starting at boot time. So your sites will be offline until you start them manually.

The solution is to write your own init script. Here is my script, maybe you can use it:


Code :   - fold - unfold
  1. #!/bin/bash -e
  2.  
  3. . /lib/lsb/init-functions
  4.  
  5. MONGREL=/usr/bin/mongrel_rails
  6. RUNAS=mongrel
  7.  
  8. PORT=3001
  9. ENVIRONMENT=production
  10.  
  11. APPNAME=YourApplicationName
  12. APPDIR=/var/www/mongrel/path/to/railsroot
  13.  
  14. SUCOMMAND=”su $RUNAS -c”
  15. case “$1&#8243; in
  16. start)
  17. log_begin_msg “Starting mongrel application: $APPNAME”
  18. $SUCOMMAND “$MONGREL start -c $APPDIR -e $ENVIRONMENT -p $PORT -d -a 127.0.0.1&#8243;
  19. ;;
  20. stop)
  21. log_begin_msg “Stopping mongrel application: $APPNAME”
  22. $SUCOMMAND “$MONGREL stop -c $APPDIR”
  23. ;;
  24. restart)
  25. log_begin_msg “Restarting mongrel application: $APPNAME”
  26. $SUCOMMAND “$MONGREL restart -c $APPDIR”
  27. ;;
  28. reload|force-reload)
  29. ;;
  30. esac
  31.  
  32. exit 0
The basic idea is from this blog:
http://schwuk.com/articles/2006/06/13/h … ian-stable

But it’s currently down.. oh the irony…


My homepage: http://www.komendera.com/
Working at: http://www.wollzelle.com/
My blog: soaked and soaped http://soakedandsoaped.com/

Offline

 

#4 2006-09-05 20:06:48

adamp
Coach Class
Registered: 2006-06-20
Posts: 73

Re: Setting up Mongrel and mod_proxy on a CPanel server

Irony indeed!

Thanks for the init script, Dieter. I hadn't got around to sorting a proper way of auto-starting/restarting yet as I'm currently only running a test app this way.

davidf8: My knowledge of mod_proxy is basic to say the least, but a quick look at the docs shows that you can exclude specific paths from the redirection, like so:

Code :   - fold - unfold
  1. ProxyPass /apache_stuff !
  2. ProxyPass / http://www.supersonicfeet.com:8000/
Which would send (continuing my example from above) anything at http://adamsapp.supersonicfeet.com/ and below to Mongrel, except anything below http://adamsapp.supersonicfeet.com/apache_stuff - that would get handled by Apache as normal.

It looks like a long-winded solution if you have lots you need to separate, but you should be able to list exclusion commands as above with the ! argument.

If your app/site/requirements allow, then you could save time by putting anything Apache needs to handle in a /static or similar folder and just adding the exception for that before the ProxyPass directive, e.g.

Code :   - fold - unfold
  1. ProxyPass /static !
  2. ProxyPass / http://www.supersonicfeet.com:8000/

Last edited by adamp (2006-09-05 20:07:18)

Offline

 
  • Index
  •  » Tutorials
  •  » Setting up Mongrel and mod_proxy on a CPanel server

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson