I've done a lot of tinkering with Ruby on Rails recently, and thought it might be worth sharing my experiences with the wider world (I get a few hits on my website from people looking for "xampp+ruby", so thought I might be able to help those folks out).
This isn't a full tutorial, and I'll assume you understand how to create a database and tables in MySQL, and navigate your way round a command line. I also won't go into the ins and outs of Rails, but will point at far better tutorials that should help you get to grips with it.
Getting Ruby on Rails working is relatively straightforward. I'll talk about getting it going on Ubuntu, the Linux distribution I use for development work. The software you'll need to get started is as follows:
Once I installed this lot, I wrote my first Rails application to prove to myself it was working; so this tutorial will follow the same pattern, and I'll give some minimal instructions about creating a minimal application to prove Rails is working.
Then, optionally, you can install FastCGI to improve the performance of Rails applications (by default, Rails makes CGI applications). To get FastCGI working with Rails, you need the following:
I followed these instructions to install a full Ruby. If you are using Ubuntu, this boils down to running the following command as root:
apt-get install \
Rails can be installed via the gem tool for Ruby, which is kind of an apt for Ruby. It is possible that there are Debian packages for Rails, but I've been using the manual install method so I get the latest Rails.
Before you can install Rails, you will need to download and install gem first, like this:
tar zxvf rubygems-0.8.11.tgz
gem install rubygems
To install Rails, as root do:
gem install rails -v 0.13.0
Note that I'd suggest installing this version of Rails (0.13.0), as at the time of writing the latest version (0.13.1) is not supported under the version of Ruby packaged for Ubuntu. If you install this later version, it will refuse to work with the installed version of Ruby.
XAMPP is a really nice way of getting a LAMP stack up and working quickly. It comes packaged with Apache, MySQL, PHP (4 and 5), Perl, phpmyadmin, and phpsqlliteadmin, so you get a full stack up and running quickly. It's not suitable for production use out of the box, but can be tailored to make it secure.
To install XAMPP, follow these instructions:
cp xampp-linux-1.4.14.tar.gz /opt
tar zxvf xampp-linux-1.4.14.tar.gz
Full instructions for running it are given on the XAMPP website.
My first Rails application was built with help from these instructions, which are very good. However, I wanted to run my applications with Apache, rather than the Webrick built-in web server which Rails is bundled with. So here's my set of minimal instructions for writing a Rails application to run inside Apache.
cdcollection. As far as I can tell, you only need to give this user SELECT, UPDATE, DELETE and INSERT privileges on the database.
cdcollectionuser can connect to the database, you need to update the
mysql.usertable like this:
UPDATE user SET password = OLD_PASSWORD(<password>) WHERE user='cdcollection';
cds. Rails is quite exacting about what you call your database tables, and it's easiest to use plurals (this upsets me too, but let it pass). Give it three fields:
id(int, unsigned, not null, autoincrement, primary key),
title(varchar, 255 characters, not null),
artist(varchar, 255 characters, not null).
cdstable so Rails has something to display.
We're now ready to create our Rails application.
whereis ruby, which should give you the path you need to supply as the shebang for the scripts. You can do a global replace using this Ruby command:
ruby -i -pe 'gsub!("#!/usr/local/bin/ruby", "#!/usr/bin/ruby")' ~/cdcollection/public/dispatch.* ~/cdcollection/script/*
/usr/bin/ruby1.8, which works fine in Ubuntu.)
~/cdcollection/config/database.yml) so it looks like this:
development:Note the extra
socketline in this file: this is necessary because XAMPP's MySQL socket runs in a different location from the default, so you have to tell Ruby where the socket is.
cdstable. Rails provides scripts for creating basic MVC code for doing CRUD operations on a database table.
ruby script/generate scaffold cd
The last step is to link our directory into our XAMPP DocumentRoot and set its permissions, so we can do something with it:
ln -s <path_to_home_dir>/cdcollection/public /opt/lampp/htdocs/cdcollection
chown -R nobody.users <path_to_home_dir>/cdcollection
chmod -R g+w <path_to_home_dir>/cdcollection
To see the application, navigate to http://localhost/cdcollection/cds. Note that if you go to http://localhost/cdcollection/, you will just get the default "Rails is working" message.
Hopefully, you should see a list of the CDs you entered into your database, and be able to add, edit, update and delete them. This means Rails is working fine, using CGI to run.
You can now follow these excellent tutorials to improve your Rails application:
The next step is to improve performance of our Rails application so it runs under FastCGI, rather than standard CGI. (NB it is also theoretically possible to run Rails applications under mod_ruby, but I couldn't get this working.) Here's an explanation of FastCGI and what it's for.
Because I was running XAMPP, and there's no FastCGI package for XAMPP, I built one from the FastCGI source. The first thing you need to do is get the XAMPP Development package and unpack this on top of your XAMPP installation:
cp xampp-linux-devel-1.4.14.tar.gz /opt
tar zxvf xampp-linux-devel-1.4.14.tar.gz
Next thing you need is the source distribution of FastCGI:
tar zxvf mod_fastcgi-2.4.2.tar.gz
mv Makefile.AP2 Makefile
Makefileand change the
top_dirvariable so its value is
This installs a
module into XAMPP's modules directory
Now edit Apache's configuration file (/opt/lampp/etc/httpd.conf) so that this module is loaded. I did this by adding the following lines to the file:
AddHandler fastcgi-script .fcgi
You will also need to add the fcgi directory to your XAMPP installation and set its permissions, i.e.
chown -R nobody.root /opt/lampp/tmp/fcgi
chmod -R 777 /opt/lamp/tmp/fcgi
Note that this gives read/write/execute permissions on this directory to anybody. I'm sure there is a better way to do this, and welcome suggestions :)
You will need to reload Apache for this to take effect. Hopefully, you should now have mod_fastcgi installed and working. Check the error_log file just to make sure FastCGI is not throwing any errors.
We will also need the FastCGI development kit to install the Ruby bindings for FastCGI:
tar zxvf fcgi-2.4.0.tar.gz
Note that this installs the
development kit into
To install the Ruby bindings for FastCGI:
tar zxvf ruby-fcgi-0.8.6.tar.gz
ruby install.rb config -- --with-fcgi-include=/opt/fastcgi/include --with-fcgi-lib=/opt/fastcgi/lib
ruby install.rb setup
ruby install.rb install
The last step is to configure the Rails application to make use of
FastCGI. You can do this by editing the
file as follows:
Change the line:
RewriteRule ^(.*)$ /dispatch.cgi?$1 [QSA,L]
RewriteRule ^(.*)$ /dispatch.fcgi?$1 [QSA,L]
If you now return to your application in the browser, you should see a noticeable improvement in speed.
That's about all you need to do to get Rails running under FastCGI on Ubuntu. It's a bit scrappy and complicated at the moment, but hopefully before too long someone will start making the packages we need for this...