2010-04-07

Install production server with Ruby 1.9.1 / Gem 1.3.6 / Rails 3b / Passenger


Environment: x86 32bit celeron mini tower, 4GB ram, big mirrored harddisk, ubuntu 9.10

This time I wrote a bash-script that, if used on an ubuntu (tested for 9.10) pc will install ruby, rubgems, rails, apache, passenger and necessesary dependencies for a naked (as in freshly installed ubuntu) pc. 

It needs to be run as root and will download all packages, sources as needed and compile/install them in order. As I usually work with sqlite3, postgresql and an oracle database I also included the required installations for those. Just make sure if you dont want to #-comment the oracle-installation, that it will need the actual instantclient-zipfiles and the tnsnames.ora-file located in /tmp. The script will then find these (tested for instantclient_11.x) and install them accordingly.

Enjoy!

#Ruby 1.9.1 / Gem 1.3.6 / Rails 3b / Passenger
#!/bin/bash
red='\E[31;40m'
green='\E[32;40m'
cecho ()                     # Color-echo.
                             # Argument $1 = message
                             # Argument $2 = color
{
local default_msg="No message passed."
                             # Doesn't really need to be a local variable.
message=${1:-$default_msg}   # Defaults to default message.
color=${2:-$white}           # Defaults to black, if not specified.
  echo -e "$color"
  echo "$message"
  tput sgr0                  # Reset to normal.
  return
}
##
## Installation Script for user 'root' on an Ubuntu system to install a ruby on rails production server
##
## This script can be used to automatically install all you need to deploy rails apps on a so far ruby-clean machine.
## Be careful tho, it is not yet thoroughly tested and may contain bugs which may result in unexpected problems.
##
## Still, I hope I got it all right and it would work effortlessly through the whole installation.
## You can of course copy/paste all commands to your console and execute them step by step. If you do so,
## don't forget to replace variables by their intended values.
##
##    if you work through it manually replace occurences of
##        $INSTANTCLIENTDIR
##        $RUBYVERSION
##        $GEMSVERSION
        RUBYVERSION=1.9.1-p376
        GEMSVERSION=1.3.6
##    with the right content
##
## double-##-ed are comments, unindented they start a new installation chapter
## single-#-ed lines usually need some adaptation
## non-#-ed lines usually are copy/pasteable
##
## if you want to use oracle, then please predownload the oracle-instantclient files
## and drop them at /tmp and put the tnsnames.ora file there before running this script
## else disable the oracle installation lines by putting a # infront of the according lines
##
## start installation
    cecho "goto homedirectory" $green
    ## switch to homedirectory
    cd
## make sure no ruby/ruby-remains are on your system
    cecho "uninstall ruby-remains if installed" $green
    apt-get -y remove ruby irb ri
    apt-get -y autoremove
## get the builing package
    cecho "install build-essentials and git-core" $green
    apt-get -y install build-essential zlib1g-dev libssl-dev gzip zip unzip libzip1 libzip-dev git git-core
## download latest ruby (in this case 1.9.1-p376, check www.ruby-lang.org/en/downloads for latest)
    cecho "get Ruby v$RUBYVERSION" $green
    wget ftp://ftp.ruby-lang.org/pub/ruby/1.9/ruby-$RUBYVERSION.tar.gz
## unarchive and compile ruby
    cecho "unarchive and compile ruby $RUBYVERSION" $green
    tar -xzf ruby-$RUBYVERSION.tar.gz
    cd ruby-$RUBYVERSION
    ./configure
    make
## install ruby
    cecho "install ruby $RUBYVERSION" $green
    make install
    cd
## download rubygems
    cecho "get rubygems $GEMSVERSION" $green
    wget http://rubyforge.org/frs/download.php/69366/rubygems-$GEMSVERSION.zip
    cecho "unarchive and install rubygems $GEMSVERSION" $green
    unzip rubygems-$GEMSVERSION.zip
    cd rubygems-$GEMSVERSION
    ruby setup.rb
    cd
## install rails
    cecho "install rails3b dependencies needed to be done manually for rails3 beta" $green
    gem install tzinfo builder memcache-client rack rack-test
    gem install erubis mail text-format bundler thor i18n
    gem install rack-mount --version=0.4.0
    cecho "install rails3 beta" $green
    gem install rails --pre
## make sure all databases are installed and ready
## install ruby support for sqlite3, postgres
    cecho "install ruby support for sqlite3 and postgresql" $green
    apt-get -y install sqlite3 postgresql-8.4 libpq-dev libsqlite3-dev
    gem install sqlite3-ruby pg
##
## start oracle addition ## here is whats needed to connect this rails to oracle
##
## comment all lines of this chapter if you don't want/need oracle support
##
    ## first install some untold dependencies for ruby-oci8
    apt-get -y install libaio1 libaio-dev
    ## prepare a directory to hold the instantclient
    mkdir /opt/oracle
    ## adapt the following subparagraph to your version/system architecture
    ## have the oracle instantclient files downloaded as .zip files to /tmp (basic, jdbc and sdk are needed)
    ## NOTE:    this script was written for Version 11.2 of the instantclient and might need
    ##             adaptations for different versions
   
    cecho "trying to find instantclient components looking in /tmp" $green
    ## find oracle-instantclient components
    PARTS=`ls /tmp | grep instantclient-`
    ## install oracle-instantclient components
    cd /opt/oracle
    for ORAFILE in $PARTS
    do
            PART=`echo $ORAFILE | cut -d'-' -f 2`
            ARCH=`echo $ORAFILE | cut -d'-' -f 3`
            VERSION=`echo $ORAFILE | cut -d'-' -f 4`
            VERSION=${VERSION%.zip}
        cecho "unzipping instantclient component $PART for $ARCH v$VERSION" $green
        unzip /tmp/$ORAFILE
    done
    ## define the instantclient directory name
    INSTANTCLIENTDIR=/opt/oracle/`ls /opt/oracle | grep instantclient_`
    mkdir $INSTANTCLIENTDIR/network
    mkdir $INSTANTCLIENTDIR/network/admin
   
    ## place tnsnames.ora, have it located at /tmp
    cecho "trying to find your tnsnames.ora looking in /tmp" $green
    cp /tmp/tnsnames.ora $INSTANTCLIENTDIR/network/admin/tnsnames.ora
    ## set env-vars to environment file for permanent existence after reboot/relogon
    cecho "set oracle environment variables" $green
    echo ORACLE_HOME=$INSTANTCLIENTDIR >> /etc/environment
    echo TNS_ADMIN=$INSTANTCLIENTDIR/network/admin >> /etc/environment
    echo SQLPATH=$INSTANTCLIENTDIR >> /etc/environment
    echo LD_LIBRARY_PATH=$INSTANTCLIENTDIR >> /etc/environment
    ## GERMAN_GERMANY.UTF8 for my personal installation ... default would be US-ASCII, use what you need
    echo NLS_LANG=GERMAN_GERMANY.UTF8 >> /etc/environment
    ## set env-vars to actual session
    export ORACLE_HOME=$INSTANTCLIENTDIR
    export TNS_ADMIN=$INSTANTCLIENTDIR/network/admin
    export SQLPATH=$INSTANTCLIENTDIR
    export LD_LIBRARY_PATH=$INSTANTCLIENTDIR
    export NLS_LANG=GERMAN_GERMANY.UTF8
    ## ubuntu only
        echo # configure oracle instantclient libraries > /etc/ld.so.conf.d/oracle.conf
        echo $INSTANTCLIENTDIR >> /etc/ld.so.conf.d/oracle.conf
        ldconfig       
    ## set symbolic links
    cecho "add symlinks to oracle libs" $green
    ln -s $INSTANTCLIENTDIR/libclntsh.so.11.1 $INSTANTCLIENTDIR/libclntsh.so
    ln -s $INSTANTCLIENTDIR/libocci.so.11.1 $INSTANTCLIENTDIR/libocci.so
    ## install ruby support for oracle
    cecho "install ruby support for oracle" $green
    gem install activerecord-oracle_enhanced-adapter ruby-oci8
## end oracle addition ##
## prepare system to install passenger
    cecho "install dependencies for passenger" $green
    apt-get -y install apache2 apache2-prefork-dev libapr1-dev libaprutil1-dev
## install passenger-gem
    cecho "install passenger gem" $green
    gem install passenger
## install passenger apache2-mod
    cecho "install passenger apache2-mod" $green
    passenger-install-apache2-module
## create passenger apache configuration
    ## ubuntu-specific (adapt to your needs)
    echo LoadModule passenger_module /usr/local/lib/ruby/gems/1.9.1/gems/passenger-2.2.11/ext/apache2/mod_passenger.so > /etc/apache2/mods-available/passenger.load
    echo PassengerRoot /usr/local/lib/ruby/gems/1.9.1/gems/passenger-2.2.11 > /etc/apache2/mods-available/passenger.conf
    echo PassengerRuby /usr/local/bin/passenger-ruby >> /etc/apache2/mods-available/passenger.conf
    a2enmod passenger
    apache2ctl restart
## adapt passenger-ruby wrapper
    cecho "create ruby wrapper for passenger" $green
    echo '#!/bin/sh' > /usr/local/bin/passenger-ruby
    echo 'export LD_LIBRARY_PATH=$INSTANTCLIENTDIR' >> /usr/local/bin/passenger-ruby
    echo 'export NLS_LANG=GERMAN_GERMANY.UTF8' >> /usr/local/bin/passenger-ruby
    echo 'exec "/usr/local/bin/ruby" "$@"' >> /usr/local/bin/passenger-ruby
## deploy your rails-apps
    ## create a new user 'rails'
    cecho "create new user for rails deployment" $green
    useradd -m -s /bin/bash -r rails
    ## dont forget to 'passwd rails' afterwards if you want the account to be ssh-able
    ## create app and apphook-directories
    cecho "create application deployment directories /apps and /hooks in /home/rails" $green
    mkdir /home/rails/apps
    mkdir /home/rails/hooks
    ##     create apache site setup
    echo "## generated by IGambins (igambin@brilliant.de) install-script for a ruby production environment" > /etc/apache2/sites-available/deployRails
    echo '' >> /etc/apache2/sites-available/deployRails
    echo '  ServerName tools' >> /etc/apache2/sites-available/deployRails
    echo '  DocumentRoot /home/rails/hooks' >> /etc/apache2/sites-available/deployRails
    ## in the following line replace app1...app3 by the app-names to be published
    ## don't forget to place the applications into the corresponding directories
    for RAILSAPP in hwmng
    do
        mkdir /home/rails/apps/$RAILSAPP
        mkdir /home/rails/apps/$RAILSAPP/public
         ln -s /home/rails/apps/$RAILSAPP/public /home/rails/hooks/$RAILSAPP
        echo "  RailsBaseURI /$RAILSAPP" >> /etc/apache2/sites-available/deployRails
     done
    echo '  ' >> /etc/apache2/sites-available/deployRails
    echo '    AllowOverride all' >> /etc/apache2/sites-available/deployRails
    echo '    Options -MultiViews' >> /etc/apache2/sites-available/deployRails
    echo '  ' >> /etc/apache2/sites-available/deployRails
    echo '' >> /etc/apache2/sites-available/deployRails
    ## enable apache site setup
    cecho "enable newly configured apache2-configuration 'deployRails'" $green
    a2ensite deployRails
    ## restart apache
    cecho "restart apache2" $green
    /etc/init.d/apache2 restart

Sources:

  • http://www.rubyonrails.org
    rails in general
  • http://oscardelben.com/install-rails-3
    rails3 installation on ubuntu
  • http://http://www.modrails.com/
    good old passenger