Archivio per la categoria 'Rails'

Install Ruby, Rails, Gem and FastCGI on DreamHost shared hosting

Mercoledì, 1 Marzo 2006

Updated: April-13-2006
The following instructions let you install a complete RubyOnRails + RubyGem + FastCGI Ruby on your shared hosted account.The script was created and tested on DreamHost but will probably be reusable for every hosting that include a shell access (ssh) to your account and support rails. This example assume that you are using bash as your shell.

Step 1 - Setup your shell environment

Login to your account using ssh and export the new PATH by editing the .bash_profile.

1. edit your .bash_profile:

$ pico ~/.bash_profile

2. copy and paste the following code at the end of your .bash_profile:

export PATH=$HOME/local/bin:$PATH
export PATH=$HOME/local/gems/bin:$PATH
export GEM_HOME=$HOME/local/gems

3. Save & Exit
Press [CNTRL+X]
Press [Y]
Press [ENTER]

4. Execute .bash_profile:

$ sh ~/.bash_profile

Step 2 - The Install Script

The following script will get you ruby 1.8.4 the latest ruby gems, the latest rails version as well as the gems with the C versions of fcgi and mysql. To use it:

1. Create the installrails script file:

$ pico ~/installrails

2. copy and paste the following script in the file you just created:

# setup directories
mkdir -p ~/local/usr/src/ruby
cd ~/local/usr/src/ruby

# Install readline
curl -O ftp://ftp.gnu.org/gnu/readline/readline-5.1.tar.gz
tar xzvf readline-5.1.tar.gz
cd readline-5.1
./configure --prefix=$HOME/local
make
make install
cd ..

# install ruby
curl -O ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.4.tar.gz
tar xzvf ruby-1.8.4.tar.gz
cd ruby-1.8.4
./configure --prefix=$HOME/local --with-readline-dir=$HOME/local/
make
make install
cd ..

# get rubygems
curl -O http://rubyforge.org/frs/download.php/5207/rubygems-0.8.11.tgz
tar xzvf rubygems-0.8.11.tgz
cd rubygems-0.8.11
$HOME/local/bin/ruby setup.rb config --prefix=$HOME/local
$HOME/local/bin/ruby setup.rb setup
$HOME/local/bin/ruby setup.rb install
cd ..

# Install RAILS
gem install rails --include-dependencies

# Install FastCGI
curl -O http://www.fastcgi.com/dist/fcgi-2.4.0.tar.gz
tar xzvf fcgi-2.4.0.tar.gz
cd fcgi-2.4.0
./configure --prefix=$HOME/local
make
make install
cd ..

# Install FastCGI & MySQL gem packages
gem install fcgi -- --with-fcgi-dir=$HOME/local
gem install mysql -- --with-mysql-config
gem  update

3. Save & Exit
Press [CNTRL+X]
Press [Y]
Press [ENTER]

4. Execute installrails script:

$ sh ~/installrails

The script will compile and install all the necessary packages. (time to take a cofee-break)

Step 3 - Test

You can test which bin executables are used in the shell by using:

$ which ruby
/home/myuser/local/bin/ruby

$ which gem
/home/myuser/local/bin/gem

$ which rails
/home/myuser/.gems/bin/rails

Step 4 - Setup Rails application

In the next steps we will learn how to setup a rails application in our new environment.

1. Create a new rails application:

$ rails myapplication

2. Change the permissions of the public directory of your rail app:

$ cd myapplication
$ chmod 0755 public
$ chmod 0755 public/dispatch*

3. Be sure that the first line of dispatch.cgi, dispatch.fcgi and dispatch.rb point to your local bin ruby (change YOURUSERNAME with your account name):

#!/home/YOURUSERNAME/local/bin/ruby

4. Add the next two lines so the top of dispatch.fcgi and dispatch.cgi looks like this (change YOURUSERNAME with your account name):

#!/home/YOURUSERNAME/local/bin/ruby
ENV[’GEM_HOME’] = ‘/home/YOURUSERNAME/local/gems’
require ‘rubygems’

5. Modify all instances of dispatch.cgi in the myapplication/public/.htaccess file to dispatch.fcgi. In the current version of rails, there is only one line to change:

From:

RewriteRule ^(.*)$ dispatch.cgi [QSA,L]

To:

RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

WARNING: for rails 1.1.x you don’t need this step:
5. Dreamhost as many others service providers, regularly kills off sleeping processes with their watchdog. This will kill your dispatch.fcgi processes, leading to Error 500s from time to time. You’ll need to make dispatch.fcgi ignore all TERM requests by change how it responds to them:

After require ‘fcgi_handler’, change the rest of dispatch.fcgi to read:

# Patched code
class MyRailsFCGIHandler < RailsFCGIHandler
SIGNALS = {
'TERM' => :exit_now,
}

def exit_now_handler(signal)
dispatcher_log :info, “ignoring request to terminate immediately”
end
end

MyRailsFCGIHandler.process! nil, 50

6. Switch to the production environment by uncommenting line 5 in myapplication/config/environment.rb which should look like:

ENV['RAILS_ENV'] ||= 'production'

Creative Commons License
This work is licensed under a
Creative Commons Attribution-NonCommercial-ShareAlike 2.0 Italy License.