Rails, Lighty and Plesk
September 20th, 2006
When you're trying to marry a server admin interface with a server config that's not about 5 years old (e.g. lighty and rails), you tend to fight the system. That is, the various control panel interfaces do not seem to play nicely with your custom setup out of the box. If you're like me, you tend to end up having your client's files scattered all over the place, config files here and there, rails apps in /var/www/lighttpd, static conetnt in /var/www/httpd/vhosts/client/http_docs, etc, etc.
This should be avoided because; (a) If your client backs up their site using a control panel type interface, they wont get any of the lighty conf or their rails app, (b) In a couple of months, unless you've taken copious notes, which is a pain in itself, you won't have a clue where all the relevant data for a particular client is, and (c) when it comes to upgrading your system, or the client's application, you'll probably miss something.
In this post, and others (linked below), I detail my attempt to create a uniform way of hosting rails apps for our clients, using Plesk. Plesk is real easy to use for keeping track of various admin related things, setting up mail, dns stuff, and has a nice interface for the client. that you can use for website admin. I'd like to make it easy for my and my forgetful brain to add a new app in a month.
System setup
Our server is a Fedora Core 4 machine, and it's real easy to get the system up to speed. A quick google found a real nice howto by Digital Media Minute. If you're starting with Plesk v8, then you'll already have a lot of the software you need. Note: If you're running FC < 4, I'd strongly recommend upgrading.
Setup lighttpd
I'm keeping the same permissions system as plesk for the rails apps. This means that lighttpd must belong to the psaserv group to be able to read your rails project. To achieve this edit /etc/group and change:
psaserv:x:2523:apache,psaftp,psaadm
to:
psaserv:x:2523:lighttpd,apache,psaftp,psaadm
You should set lighttpd to run at startup:
chkconfig --add lighttpd chkconfig --level 345 lighttpd on
See my post about lighttpd config for a full-blown vhost and subdomain setup, or see the rails wiki for a simple example.
Configuring a rails site
So here's what I do when setting up a new rails app with plesk:
1. Set up the vhost or subdomain with plesk control panel.
This will create the required directories in /var/www/vhosts/<your domain> or /var/www/vhosts/<your domain>/subdomain/<your subdomain>
2. Set up proxy pass.
We're going to leave Apache running, and just get it to pass requests to and from lighty for our railsified sites. This is pretty easy to do with plesk. Just add the following in /var/www/vhosts/<your domain>/conf/vhost.conf:
ProxyRequests Off ProxyPreserveHost On ProxyPass / http://0.0.0.0:81/ ProxyPassReverse / http://0.0.0.0:81/
If you want to also pass SSL to lighty then simply copy vhost.conf to vhost_ssl.conf and add the following line at the end.
RequestHeader set X_FORWARDED_PROTO 'https'
If you're configuring a subdomain then you need to edit the subdomain's conf, no need to touch the vhosts's conf.
Finally, to get plesk to recognise the new vhost.conf file you need to do the following (irrespective of whether it's a domain or subdomain):
/usr/local/psa/admin/sbin/websrvmng --reconfigure-vhost --vhost-name=<your domain>
3. Set up a rails directory
Make a rails directory in the client's site (/var/www/vhosts/<your domain>), and make it belong to the psaserv group, and to the user who owns the site. Also set the sticky bit on group. You want the permissions to look like this:
chmod 750 rails chmod g+s rails ls -al | grep rails drwxr-s--- 2 client psaserv 4096 Sep 26 09:15 rails
4. Deploy your rails app
Get your rails app, for example typo, into rails/ by whatever method you normally would. Make sure that lighttpd has write access to public/, log/ and tmp/.
chmod -R g+w tmp public log
5. Configure lighty for your app
Now you need to write the lighty conf for the app. If you're following my post about lighty config then you need to put the following in /var/www/vhosts/<your domain>/conf/lighttpd.conf:
var.rails-root = var.host-root + "/rails/typo" var.fcgi-min-procs = 1 # or whatever you like var.fcgi-max-procs = 2 # for these values include "rails.conf"
Otherwise edit /etc/lighttpd/lighttpd.conf and add something like the following:
$HTTP["host"] =~ "(^|www\.)domain\.com" {
var.rails-root = "/var/www/vhosts/domain/rails/typo"
server.document-root = var.rails-root + "/public"
server.errorlog = var.rails-root + "/log/lighttpd_error.log"
accesslog.filename = var.rails-root + "/log/lighttpd_access.log"
url.rewrite = ( "^/$" => "index.html", "^([^.]+)$" => "$1.html" )
server.error-handler-404 = "/dispatch.fcgi"
fastcgi.server = ( ".fcgi" =>
( "localhost" =>
( "min-procs" => 1,
"max-procs" => 2,
"socket" => var.rails-root + "/tmp/sockets/fcgi.socket",
"bin-path" => var.rails-root + "/public/dispatch.fcgi",
"bin-environment" => ( "RAILS_ENV" => "production" )
)
)
)
}
Finally, restart your lighttpd server:
service lighttpd restart
June 22nd, 2007 at 02:01 PM Wow this is exactly what I needed. Thank you.
August 9th, 2007 at 02:02 PM This looks good. But I wonder, if all requests are being passed through apache, how much are you saving by using lighttpd? Let me know your thoughts! hanks, Ron
March 4th, 2008 at 07:50 PM You just helped me solve something that I spent hours trying to figure out.... in my case, I wanted to proxy a subdomain I created in Plesk to a Paster server I had running on port 5000, and yep, that first section there worked perfectly. You sir, I applaud.
March 20th, 2008 at 04:39 PM Well I found this on the Plesk blog: 'Limited customization – It's very hard to efficiently implement per-tenant database schema customization in a single database, and it's not possible to use standard DBMS tools – like built-in indexing. Plus, the multi-tenant application cannot use script-based customization of the internal logic – what if a buggy script loops infinitely? SmoothSpan Bob Warfield and Unreasonable Men argued that customization capabilities are often excessive and there is little harm in removing much of that flexibility. For newly written applications, it might even be true. But if an ISV has a customer base to migrate to SaaS model, telling customers that their solutions will be broken because some of the customizations don't work anymore is hardly an option.' Apparently Plesk is not heading their own blog...LOL because we all are doing nothing but loosing functionality and the ability to see what is going on, on our sites. I'm paying an extra 10 bucks a month for this Plesk thing which basically has reduced my site to nothing. 1. AwStats which is just as important as Apache does not function at all. 2. Customizable httpd.include per domain does not work with vhost.conf and since Plesk is an unsecured script, (because it must run with open base directory on), we have lost HUGE functionality. You do not see this problem with or like this Cpanel at ALL! In the past 10 years I have had only 2 problems with Cpanel both related to the file manager and editor, very minor and were fixed in the next version. When I say fixed I don't mean they said it was fixed and it wasn't, it means it was fixed and has worked perfectly ever since. 3. If you change your MySql Passwords in phpMyAdmin i.e. outside of Plesk then you can not reach MySql any more. This implies that Plesk is storing your passwords outside the data base in unsecured fashion. There are many more too. I guess Plesk is like windows 3.1 and Cpanel is like linux Now if you think I'm a little more than perturbed with Plesk you'd be correct. However, I have spent the last three weeks messing around with Plesk because it doesn't work rather than having my attention on my business and customers where it belongs. Can Plesk comprehend the ramifications of placing UNTESTED, UVALIDATED and weak performing code (Plesk) on a server and claiming it to be a management utility?