Rails Against The Machine

Just a mind dump. Why are you even reading this?

Wednesday, 19 September 2007

 

Installing rails on ubuntu (Dapper drake)

Deploying rails on Ubuntu (Dapper drake).

Well I can put it off no longer – I have to enter the “sandal and star trek T-shirt wearing” world of Linux and deployment. Soon I will be posting that Microsoft suck on Slashdot and making sniggering at newbies and calling myself 31337 Haxor. The fun filled days of actualy coding but a distant memory.

Part 1 – install a whole load of stuff

We are going to install a whole load of stuff. The default repository for Ubuntu lacks many of these so we need to enable the Universe repository.

So type: sudo gedit /etc/apt/sources.list

then uncomment the lines:

#deb http://us.archive.ubuntu.com/ubuntu dapper universe
#deb-src http://us.archive.ubuntu.com/ubuntu dapper universe

and save then update the package repository info and update all the existing packages. (this may take some time!)

sudo apt-get update
sudo apt-get dist-upgrade

So you can do everything remotely you should install SSH so you can remote in from the comfort of home (Capistrano needs it to work anyway)

sudo apt-get install openssh-server

now having done this we can install ruby and MySQL.

sudo apt-get install ruby ri rdoc mysql-server libmysql-ruby

 
You need to get Gem from the source forge and untar repository
 
sudo wget http://rubyforge.org/frs/download.php/20989/rubygems-0.9.4.tgz
tar -xvzf rubygems-0.9.4.tgz
cd rubygems-0.9.4
sudo ruby setup.rb
 
So we can now install rails with:  sudo gem install rails --include-dependencies
(or so we think ) ..but woe is us we get the following error message:
 
Bulk updating Gem source index for: http://gems.rubyforge.org
ERROR:  While executing gem ... (Gem::GemNotFoundException)
Could not find rails (> 0) in any repository
 

So we need to clear the gem cache so it looks in the right place:

First find the path with
gem env
then type the following to delete the cache and update it.
rm -f /path/to/gem/cache/source_cache
gem update

Now we try: sudo gem install rails --include-dependencies

and this time we succeed type the following to create a rails app by way of celebration.

rails hooray

Part 2 – install (or rather build) Mongrel

First we get a few packages required for building & compiling then we install Mongrel as a Ruby Gem

sudo apt-get install build-essential ruby1.8-dev ruby1.8
sudo gem install mongrel
 
test if it installed by going to the root of your rails app typing
mongrel_rails start –d
 
Checking if it is working by seeing if your rails application appears at http://localhost:3000  then stopping mongrel with
 
mongrel_rails stop
 
since this is a real deployment we will want a pack of mongrels so we install mongrel cluster to manage the instances
 
sudo gem install mongrel_cluster
 
Part 3 capistrano ‘fun’ .. and I mean fun in an ironic sence
 
First install Capistrano on your development machine
 
gem install Capistrano
 
Then ‘capify’ your application by typing:
 
cap --apply-to /path/to/my/app MyApplicationName
 
Create a ‘spinner script in the script directory.

Add some content e.g.



/Library/Rails/MYAPP/current/script/process/spawner -p 11000 -i 3
 
Which basically says start 3 mongrel instances on ports 11000 -11002
You then create a deploy script in deploy.rb e.g.
 
#to generate this file just run "capify ." from the app's root directory
#Get the list of commands with 'cap -T' 
require 'mongrel_cluster/recipes'
 
set :application, "myapp"
set :repository,  "svn://your.repository.com"
set :deploy_to, "/Library/Rails/#{application}"
 
set :mongrel_conf, "#{current_path}/config/mongrel_cluster.yml"
set :svn_username, Proc.new { "username --password svnpassword" }
#needed to avoid “no passwd entry for app! Error when starting mongrel
#also  ailed to open `nohup.out' permission denied errors
#essentially this just determines the user who sudo is
set :runner, "root" 
# If you aren't deploying to /u/apps/#{application} on the target
# servers (which is the default), you can specify the actual location
# via the :deploy_to variable:
# set :deploy_to, "/var/www/#{application}"
 
# If you aren't using Subversion to manage your source code, specify
# your SCM below:
# set :scm, :subversion
 
role :app, "ip.of.application.server "
role :web, "ip.of.web.server"
role :db,  "ip.of.db.server" :primary => true
 
#This bit is required due to spinner script woes 
#since you need to grant execute access to let the spinner script run
desc "Tasks to execute after code update"
task :after_update_code, :roles => [:app, :db, :web] do
  run "chmod +x #{release_path}/script/spin"
  run "chmod +x #{release_path}/script/process/reaper"
  run "chmod +x #{release_path}/script/process/spawner"
  run "chmod 755 #{release_path}/public/dispatch.*"
end
 
To use capistrano you need to install subversion command line client and openSSH on your servers



sudo apt-get install subversion

sudo apt-get install openssh-server (if not already done)



you will also need to install on your development machine (if it is a windows one try gem install net-ssh tortoise does not count as a command line client)
 
Now create a directory for Capistrano to deploy to on your application server and give it write permissions so you don’t get the following error when yo do cap deploy:setup : “cannot create directory '/Library/Rails'”
 
sudo mkdir -p /Library

sudo chmod 777 -R /Library



Now lets create the production database on the db server with mysql and give access rights.
 
# On your database server
$ mysql -u root -p
Enter password: ********
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2080 to server version: 5.0.19
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> CREATE DATABASE myapplication;
Query OK, 1 row affected (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON myapplication.* TO 'myapplication'@'localhost' IDENTIFIED BY 'some_pass' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
 
So now go to the database.yml file and enter the username and password: (best practice is to manually add in the database.yml file rather than checking into version control)
 
production:
  adapter: mysql
  database: your_app_production
  username: your_app
  password: password
  host: localhost
 
So the suggestion is to add something like this to your deploy.rb script.
 

task :after_update_code, :roles => :app do

db_config = "#{shared_path}/config/database.yml.production"

run "cp #{db_config} #{release_path}/config/database.yml"

end
 
Ok so we think we can finally deploy from our development machine with 
cap deploy:setup
 
but we get the following error:
 
connection failed for: 172.xx.x.x (Net::SSH::AuthenticationFailed: myusername)
 
it seems that we are trying to ssh with the same name as my development machine user. So we add the following to the deploy script
set :user, 'remote-host-username'
 
if we continue to have problems we could try adding some more options as recommended by the ruby wiki e.g.
 
 ssh_options[:port] = 22
 ssh_options[:verbose] = :debug
 ssh_options[:username] = 'remote-host-username'
 ssh_options[:host_key] = 'ssh-dss'
 
Success! - If you go to your application server you will see that a directory structure has been created.
Now for the next stage:
 
Cap deploy:cold
 
This time it hangs on 
 
** [out] Authentication realm:  some_repository
 ** [out] Username:
 
So cap is assuming my ssh and svn usernames are the same. 
I haven’t figured out how to deal with this – it seemed easier to create a new  matching username and get it to work that way.
 
Ok if that worked you can go to your mongrel instances on 11000 ect and see your application. All that remains is to link up apache as a load balancer
 
Part 4 cast a magic spell
 
Walk round your computer three times backwards reciting the following incarnation
 
Linux linus god of the geek
God of confusion hear me speak
sudo sudo user bugs be gone 
Bless my reference let nothing go wrong
 
If that didn’t work your computer is probably next to a wall – move it to the center of the room
 
Part 5 apache proxy
 
You might think that all you need to do is
 
sudo apt-get install apache2
then check it is running on http://localhost
 
…but you would be wrong.  Since we want to use it as a load balancer and proxy to the mongrel instances we need to use the proxy module but with Apache2 you need to build the source to include the proxy module !!
 
"Invalid command 'proxy', perhaps mis-spelled or defined by a module not included in the server configuration"
 
So instead we use the following commands to build from source
 
wget http://apache.rmplc.co.uk/httpd/httpd-2.2.6.tar.gz

tar -xvf httpd-2.2.6.tar.gz
cd httpd-2.2.6
./configure --prefix=/usr/local/apache2 --enable-mods-shared=all --enable-deflate --enable-proxy --enable-proxy-balancer --enable-proxy-http
 
The key parameter here being “--enable-mods-shared=all” (see http://davidwinter.me.uk/articles/2006/10/17/building-apache-22-from-source-for-ubuntu-dapper/)
But alas it still doesn’t work,  we get the error “No recognized SSL/TLS toolkit detected” so we need to install an SSL toolkit (whatever that is)
 
sudo apt-get install openssl libssl-dev
 
try again
 
./configure --prefix=/usr/local/apache2 --enable-mods-shared=all --enable-deflate --enable-proxy --enable-proxy-balancer --enable-proxy-http
Make
sudo make install
 
Now edit the configuration file along the lines of http://blog.innerewut.de/2006/04/21/scaling-rails-with-apache-2-2-mod_proxy_balancer-and-mongrel
 
Sudo gedit /usr/local/apache/config/httpd.conf
 
so apache is now installed but when we try to start it with
/usr/local/apache/bin/httpd -k start


8)The dreaded Expected x.rb to define X error

This
turned out to be because I was using a plugin for this model to
validate emails all the require statements need to be in the init file
for the plug in e.g.

require 'RFC822'
require 'common_validations'

10) rails console doesnt work
sudo ln -s /usr/bin/irb1.8 /usr/bin/irb


Comments: Post a Comment

Subscribe to Post Comments [Atom]





<< Home

Archives

July 2007   August 2007   September 2007   December 2007   January 2008   February 2008   March 2008   April 2008   June 2008   July 2008   August 2008   October 2008   November 2008   January 2009  

This page is powered by Blogger. Isn't yours?

Subscribe to Comments [Atom]