As of 2005-10-8, Debian “Etch” (currently known as “testing”) contains all the packages necessary to turn up Rails with Apache2 and Fastcgi without compiling anything from source or having to install and use gems. Here’s how to get it all working from a fresh Debian install.
These instructions were tested with Debian “Sarge”, which was the aliased to “stable” as of 2005-10-08.
Rails was at version 0.13.1-1
Apache2 was at version 2.0.54-4
libfcgi-ruby1.8 was at version 0.8.6-1
libapache2-mod-fastcgi was at version 2.4.2-6
postgresql was at version 7.4.7-6sarge1
When these instructions want you to run a command as yourself (not as root), they will just tell you to run the command:
$ ls
Commands you should run as root start with “sudo” :
$ sudo ls
If you don’t use sudo to run root commands, leave the “sudo” off.
Instead become root:
$ su root
If a command shows a path to the left of the $, it expects you to be in that directory:
~/rails$ ls
If a command doesn’t show a path to the left of the $, then it doesn’t matter what directory you are in:
$ ls
First install rails. This will install Ruby as well:
If you have a fresh installed Debian, and never ever did a “apt-get update” before, do it now:
$ sudo apt-get update$ sudo apt-get install rails
Create a rails environment in ~/rails (you can place it wherever you want; just adjust the instructions):
~$ rails ~/rails
Start Rails using Webrick:
~/rails$ script/server
You should see a few lines of log output. Point your browser to localhost:3000. If you see “Congratulations, you’ve put Ruby on Rails!,” then rails is working.
You can stop webrick (script/server) now by pressing Ctrl+C on your keyboard.
~$ sudo apt-get install apache2
Point your browser to http://localhost. You should see the default apache2 screen: “If you can see this, it means that the installation of the Apache web server software on this system was successful.”
Next, there are two ways to set up Apache to allow you to interact with rails: (1) enable URLs like “http://rails/” by creating a VirtualHost and editing the /etc/hosts file; and (2) enable URLs like “http://your_ip_address/” by replacing the default root of Apache with your rails path.
Choose the one that suits you and follow the appropriate set of instructions, below:
Enable mod-rewrite:
~$ sudo a2enmod rewrite
Configure a virtual host by creating /etc/apache2/sites-available/rails:
<VirtualHost *>
ServerName rails
DocumentRoot /home/wconrad/rails/public
</VirtualHost>
(Replace ”/home/wconrad” with the path to the directory containing the “rails” directory you created earlier).
Add the virtual host to apache’s configuration:
$ sudo a2ensite rails
As root, edit /etc/hosts. Add the name “rails” to the end of the line for 127.0.0.1.
127.0.0.1 localhost rails
Allow apache2 write to the log dir and log files.
~/rails$ sudo chgrp www-data log log/*
~/rails$ chmod g+w log log/*
Restart apache2:
sudo apache2ctl restart
Point your browser to http://rails. You should see the “Congratulations, you’ve put Ruby on Rails!” page.
Edit /etc/apache2/sites-available/default:
Change line that says:
DocumentRoot /var/www
to say:
Document Root /home/wconrad/rails/public
(Replace ”/home/wconrad” with the path to the directory containing the “rails” directory you created earlier).
Delete or comment out the block that begins:
<Directory /var/www/>
Add a new block that says:
<Directory /home/wconrad/rails/public>
Options ExecCGI FollowSymLinks
AddHandler cgi-script .cgi
AllowOverride all
Order allow,deny
Allow from all
</Directory>
Restart apache2:
sudo apache2ctl restart
Point your browser to http://your_ip_address. You should see the “Congratulations, you’ve put Ruby on Rails!” page.
Send your browser to http://rails/foo. You should get a “Routing Error” page. That’s ok, because there is no such page as “foo”. We are just using this URL to make sure that rails is working before (and after) we switch it to fastcgi. Note: You can bring up the http://rails page all day long, and it won’t tell you if fastcgi is horribly broken. That’s why we added ”/foo” to the url.
Install and enable mod-fastcgi and the ruby fcgi library:
$ sudo apt-get install libfcgi-ruby1.8 libapache2-mod-fastcgi
$ sudo a2enmod fastcgi
Edit ~/rails/public/.htaccess to switch rails to fastcgi. Change this line:
RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
To this:
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]
Restart apache2:
$ sudo apache2ctl restart
Send your browser to http://rails/foo. You should get a “Routing Error” page. If you do, then fastcgi is working.
Congratulations!
First install postgres:
$ sudo apt-get install postgresql
Now let’s make it so that rails (or any other user on this box) can connect to postgres. As root, edit /etc/postgresql/7.4/main/pg_hba.conf. Change this line:
local all all ident sameuser
To this:
local all all trust
This isn’t necessary a good way to set up postgres permissions. It’s just a quick way that works so you can get started.
Restart postgres so that the permissions change will take effect:
$ sudo /etc/init.d/postgresql-7.4 restart
Now create the rails user and the rails_development database:
$ sudo su postgres psql template1
template1=# create user rails password 'insecure';
template1=# create database rails_development with owner = rails;
template1=# \q
We will skip creating or configuring the test and production databases. You will need them eventually, but you don’t need them to get started developing.
Now, connect to postgres as the “rails” user and create a table:
$ psql -U rails rails_development
rails=# create table foos (id serial primary key, t text);
rails=# \q
Edit ~/rails/config/database.yml to tell rails how to connect to the development database. The “development” section should look like this:
development:
adapter: postgresql
database: rails_development
host: localhost
username: rails
password: insecure
Now restart apache so the database.yml change takes effect:
sudo apache2ctl restart
Finally, generate a scaffold for viewing and editing the table:
~/rails$ script/generate scaffold foo
Point your browser to http://rails/foos. You should see a page titled “Listing foos”. That’s it!
=====
libapache2-mod-fastcgi problem
using Debian sarge with testing sources: apt-get says no installation candidate. Did a apt-get search libapache2 | grep fcgi and it returned this package: libapache2-mod-fcgid.
====
The above problem is because you must set sources.list to allow non-free packages, something like:
<url of debian sources> main contrib non-free
Or see below.
====
wiki entry on using mod-fcgid (GPL) with Apache2 instead of mod-fastcgi (non-free)
http://wiki.rubyonrails.com/rails/pages/Debian+mod_fastcgi+Notes