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 \
irb1.8 \
libbigdecimal-ruby1.8 \
libcurses-ruby1.8 \
libdbm-ruby1.8 \
libdl-ruby1.8 \
libdrb-ruby1.8 \
liberb-ruby1.8 \
libgdbm-ruby1.8 \
libiconv-ruby1.8 \
libopenssl-ruby1.8 \
libpty-ruby1.8 \
libracc-runtime-ruby1.8 \
libreadline-ruby1.8 \
librexml-ruby1.8 \
libruby1.8 \
libruby1.8-dbg \
libsdbm-ruby1.8 \
libsoap-ruby1.8 \
libstrscan-ruby1.8 \
libsyslog-ruby1.8 \
libtest-unit-ruby1.8 \
libwebrick-ruby1.8 \
libxmlrpc-ruby1.8 \
libyaml-ruby1.8 \
libzlib-ruby1.8 \
rdoc1.8 \
ri1.8 \
ri \
rdoc \
ruby \
ruby1.8 \
ruby1.8-dev \
libmysql-ruby1.8 \
ruby1.8-examples
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:
cd ~
tar zxvf rubygems-0.8.11.tgzruby setup.rbgem install rubygemsTo install Rails, as root do:
gem
install rails -v 0.13.0Note 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
cd
/opt
tar zxvf xampp-linux-1.4.14.tar.gzopt/lampp/lampp startFull 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.cdcollection.
As far as I can tell, you only need to give this user SELECT, UPDATE,
DELETE and INSERT privileges on the database.cdcollection
user can connect to the database, you need to update the mysql.user
table like this:USE mysql;
UPDATE user SET password = OLD_PASSWORD(<password>) WHERE
user='cdcollection';FLUSH PRIVILEGES;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).cds
table so Rails has something to display.We're now ready to create our Rails application.
rails ~/cdcollectionwhereis
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
adapter: mysql
database: cdcollection
host: localhost
username: cdcollection
password: <password>
socket: /opt/lampp/var/mysql/mysql.sock
socket
line 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.cds
table. Rails provides scripts for creating basic MVC code for doing
CRUD operations on a database table.cd ~/cdcollection
ruby script/generate scaffold cdThe 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/cdcollectionchown
-R nobody.users <path_to_home_dir>/cdcollectionchmod
-R g+w <path_to_home_dir>/cdcollectionTo 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 Rails API documentation is also very good, and you will find this online book handy if you are also trying to learn Ruby at the same time.
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
cd /opt
tar zxvf xampp-linux-devel-1.4.14.tar.gzNext thing you need is the source distribution of FastCGI:
tar
zxvf mod_fastcgi-2.4.2.tar.gzcd
mod_fastcgi-2.4.2mv
Makefile.AP2 MakefileMakefile
and change
the top_dir
variable so its value is /opt/lamppmakemake
installThis installs a mod_fastcgi
module into XAMPP's modules directory
(/opt/lampp/modules/mod_fastcgi.so).
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:
LoadModule
fastcgi_module modules/mod_fastcgi.so
<IfModule mod_fastcgi.c>
FastCgiIpcDir /opt/lampp/tmp/fcgi/
AddHandler fastcgi-script .fcgi
</IfModule>
You will also need to add the fcgi directory to your XAMPP installation and set its permissions, i.e.
mkdir
/opt/lampp/tmp/fcgi
mkdir /opt/lampp/tmp/fcgi/dynamic
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.gzcd
fcgi-2.4.0./configure
--prefix=/opt/fastcgimake
installNote that this installs the
development kit into /opt/fastcgi.
To install the Ruby bindings for FastCGI:
tar
zxvf ruby-fcgi-0.8.6.tar.gzcd
ruby-fcgi-0.8.6ruby
install.rb config --
--with-fcgi-include=/opt/fastcgi/include
--with-fcgi-lib=/opt/fastcgi/libruby
install.rb setupruby
install.rb installThe last step is to configure the Rails application to make use of
FastCGI. You can do this by editing the ~/cdcollection/public/.htaccess
file as follows:
Change the line:
RewriteRule ^(.*)$ /dispatch.cgi?$1 [QSA,L]
to
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...
Comments
Make errors on Ubuntu Edgy
I attempted to install mod_fastcgi using the method shown above and this is what I got:
$ make top_dir=/opt/lampp
/opt/lampp/build/libtool --silent --mode=compile gcc -pthread -O6 -I/opt/lampp/include -L/opt/lampp/lib -DLINUX=2 -D_REENTRANT -D_GNU_SOURCE -D_LARGEFILE64_SOURCE -I/opt/lampp/include -I. -I/home/lampp/apache/httpd-2.2.3/srclib/apr/include -I/home/lampp/apache/httpd-2.2.3/srclib/apr-util/include -I/opt/lampp/include -prefer-pic -c mod_fastcgi.c && touch mod_fastcgi.slo
mod_fastcgi.c: In function 'init_module':
mod_fastcgi.c:270: error: 'ap_null_cleanup' undeclared (first use in this function)
mod_fastcgi.c:270: error: (Each undeclared identifier is reported only once
mod_fastcgi.c:270: error: for each function it appears in.)
mod_fastcgi.c: In function 'process_headers':
mod_fastcgi.c:725: warning: return makes pointer from integer without a cast
mod_fastcgi.c:729: warning: assignment makes pointer from integer without a cast
mod_fastcgi.c:739: warning: assignment makes pointer from integer without a cast
mod_fastcgi.c:768: warning: initialization makes pointer from integer without a cast
mod_fastcgi.c:838: warning: return makes pointer from integer without a cast
mod_fastcgi.c:842: warning: return makes pointer from integer without a cast
mod_fastcgi.c: In function 'set_uid_n_gid':
mod_fastcgi.c:1022: warning: passing argument 1 of 'memcpy' makes pointer from integer without a cast
mod_fastcgi.c:1024: warning: assignment makes pointer from integer without a cast
mod_fastcgi.c:1033: warning: assignment makes pointer from integer without a cast
mod_fastcgi.c:1034: warning: assignment makes pointer from integer without a cast
mod_fastcgi.c: In function 'do_work':
mod_fastcgi.c:2321: error: 'ap_null_cleanup' undeclared (first use in this function)
mod_fastcgi.c: In function 'create_fcgi_request':
mod_fastcgi.c:2479: warning: assignment makes pointer from integer without a cast
mod_fastcgi.c:2492: warning: assignment makes pointer from integer without a cast
mod_fastcgi.c: In function 'apache_is_scriptaliased':
mod_fastcgi.c:2534: warning: initialization makes pointer from integer without a cast
mod_fastcgi.c: In function 'post_process_for_redirects':
mod_fastcgi.c:2559: warning: passing argument 1 of 'ap_internal_redirect_handler' makes pointer from integer without a cast
mod_fastcgi.c: In function 'check_user_authentication':
mod_fastcgi.c:2682: warning: assignment makes pointer from integer without a cast
mod_fastcgi.c:2700: warning: comparison between pointer and integer
mod_fastcgi.c: In function 'check_user_authorization':
mod_fastcgi.c:2749: warning: assignment makes pointer from integer without a cast
mod_fastcgi.c:2765: warning: comparison between pointer and integer
mod_fastcgi.c: In function 'check_access':
mod_fastcgi.c:2809: warning: assignment makes pointer from integer without a cast
mod_fastcgi.c:2826: warning: comparison between pointer and integer
make: *** [mod_fastcgi.slo] Error 1
Is there anyway that I can possibly download a precompiled mod_fastcgi.so?
Hello there. Thanks for the
Hello there. Thanks for the comment. Have a look at my reply to the same error message report below. Unless you are intending on running in a shared hosting environment or with an ancient Apache, Mongrel plus a mod_proxy (and maybe mod_proxy_balancer) is a far better solution for running your Rails applications than Apache plus FastCGI. FastCGI is generally considered a moribund project, and it leaks memory all over the place as well as spawning errant processes. Mongrel is far superior. The scripts for compiling Mongrel, mod_proxy etc. for XAMPP are part of the AxleGrease project (the above instructions are about 18 months old now, and Rails deployments are generally moving off FastCGI onto Mongrel).
fastcgi make errors
Hi,
RubyonRails is running on XAMPP on Ubuntu dapper.
I have found this post when googling for a howto for installing fastcgi on XAMPP & ruby.
Downloaded the mod_fastcgi tarball, but trying to do make, i get following errors:
mod_fastcgi.c: In function 'apache_is_scriptaliased':
mod_fastcgi.c:2534: warning: initialization makes pointer from integer without a cast
mod_fastcgi.c: In function 'post_process_for_redirects':
mod_fastcgi.c:2559: warning: passing argument 1 of 'ap_internal_redirect_handler' makes pointer from integer without a cast
mod_fastcgi.c: In function 'check_user_authentication':
mod_fastcgi.c:2682: warning: assignment makes pointer from integer without a cast
mod_fastcgi.c:2700: warning: comparison between pointer and integer
mod_fastcgi.c: In function 'check_user_authorization':
mod_fastcgi.c:2749: warning: assignment makes pointer from integer without a cast
mod_fastcgi.c:2765: warning: comparison between pointer and integer
mod_fastcgi.c: In function 'check_access':
mod_fastcgi.c:2809: warning: assignment makes pointer from integer without a cast
mod_fastcgi.c:2826: warning: comparison between pointer and integer
etc.
Do you have any idea what i am missing?
With kind regards,
Franky
Hi Franky. To be honest, I
Hi Franky. To be honest, I haven't done too much compilation of FastCGI on Dapper: I prefer to use Mongrel now instead. I don't really see a reason for using FastCGI unless you are setting up mass hosting of Rails apps. on Apache, e.g. in a hosting provider setting (e.g. Dreamhost and Site5 take this approach). If you are setting up on your own servers, I would recommend Apache 2.2, mod_proxy_balancer and Mongrel instead. If you are stuck with an older Apache, you can use mod_proxy plus a load balancer like Pound (see the howto at http://blog.codahale.com/2006/11/07/pound-vs-pen-because-you-need-a-load...). If you are working with XAMPP on Linux, you can use my AxleGrease project (http://rubyforge.org/projects/rorox/), which will do all the hard stuff for you: it includes a recent Apache, Rails, mod_proxy_balancer, Mongrel, etc.. Hope this helps.
Thanks
Hi,
Thanks, works great!
Great projects you do elliot, lot of people will be happy for this, so do i :-)
I use fastcgi on production servers (RHEL). On ubuntu (laptop), the set up of xampp/rails without fastcgi was a bit slow,
thanks for the mongreil solution.
Again, nice project !
Franky
I found the install to be...
I found the install to be quite cumbersome for MAC OS X, however once I got it running it has been great. However, recently I have encountered some socket issues. Have you experienced any such issues? Do you know where I can find some help in order to resolve? Also, do you have any ruby apps running on the web. I would love to check it out.
Haven't noticed any socket issues. What...
Haven't noticed any socket issues. What kind of thing do you mean? I haven't got anything on the web yet, but have been working on some clip log software (for storing and categorising audio interview excerpts). I'll be putting it up on this site when it's done, and if I get the time I'll write some documentation!