Setting up Ruby Rails with Apache 2.2, fastcgi and mySQL 5.0 on OpenSolaris 2008.11

  We recently started working on a Ruby on Rails project.  we decided to use OpenSolaris 2008.11 release as the dev environment.  One of the first task was to get the Ruby On Rails environment setup with Apache (with FastCGI ) and MySQL.  Here are the steps we used for setting this up.

Setting up mySQL 5.0  on OpenSolaris 2008.11

1.  Install  packages for mySQL 5.0    ( Step is executed as user with root privileges )

     # pfexec pkg  install SUNWmysql-base 
     # pfexec pkg  install SUNWmysql5

2.  Update 'mysql' user and directory ownership  ( Step is executed as user with root privileges )

     2.1 Change  the home directory for  'mysql' user to  '/usr/mysql'

     2.2  Make the mySQL 5.0 directories owned by 'mysql' user.
            #chown -R  mysql:mysql  /usr/mysql
            #chown -R  mysql:mysql  /var/mysql

     2.3  Set the password for 'mysql' account   
           #passwd mysql

     2.4 Add the  path  '/usr/mysql/bin'  to  PATH environment variable  in the .profile   for  user 'mysql'        

 3.  postInstall  configuration

      3.1 Run the mySQL initialization  and config
           #su -  mysql
           $ /usr/mysql/bin/mysql_install_db --user=mysql      

      3.2  Setup mysql startup script.  
           $ cp /usr/mysql/5.0/share/mysql/mysql.server  /usr/mysql/bin/
           $ chmod +x  /usr/mysql/bin/mysql.server

      3.3  Set  the values for 'basedir'  and 'datadir'   in   /usr/mysql/bin/mysql.server  as follows
             basedir=/usr/mysql/5.0
             datadir=/var/mysql/5.0/data

      Note :   An alternat option is to  use the svcadm command to enable the service 'svc:/application/database/mysql:version_50'

      3.4  Start  mySQL
            $ mysql.server start

      3.5  Drop Anonymous users
             $ mysql -u root
             mysql> drop user '';
             mysql> drop user ''@'localhost';
             mysql> drop user ''@'svr012';

       3.6 Set password for the 'root' user
             $ mysqladmin  -u root  password  'newpassword'
             $ mysqladmin -u root  -h  svr012  password  'newpassword' -p

        Note  : svr102  is the hostname  as returned  by the 'hostname' command

        3.7  Test mysQL 5.0  config.

             $ mysqlshow -u root -p
              Enter Password :             
+-----------+
| Databases |
+-----------+
| mysql     |
| test      |
+-----------+

Setting up  Ruby On Rails  on  OpenSolaris 2008.11

    The following  steps were performed as a user with 'root' privileges. 

1. Initial  Setup

     1.1  Install Ruby 1.8

           # pfexec pkg  install SUNWruby18

     1.2  Add the  path  '/usr/ruby/1.8/bin'  to  PATH environment variable  in the .profile

     1.3 Update to latest version of RubyGems
            # gem update --system    

     1.4  Install 'rails'

        # gem  install rails

2.  Fix the  'iconv' Error  for the rails  invocation.

       There is a known problem using the rails with the  use of 'iconv' library on OpenSolaris.  Due to  this the invocation of the 'rails' program fails with errros.   The  issue is  documented in detail at http://woss.name/2008/10/16/edge-rails-pre-22-iconv-transliteration-and-solaris/ 

    There is  a suggested Fix at http://rails.lighthouseapp.com/projects/8994/tickets/1396-framework-crashes-on-launch-on-solaris-with-invalid-encoding-asciiignoretranslit-utf-8 which worked fine  for us.

 As suggested the fix is  implemented by modifying the  'transliterate' function  in file /usr/ruby/1.8/lib/ruby/gems/1.8/gems/active-support-2.2.2/lib/active_support/inflector.rb(Line 275)

# Replaces accented characters with their ascii equivalents.
def transliterate(string)
  begin
    Iconv.iconv('ascii//ignore//translit', 'utf-8', string).to_s
    rescue Iconv::InvalidEncoding
  end
end


3. Fix the rbconfig.rb file

     The  ruby configuration file  rbconfig.rb  ( in directory /usr/ruby/1.8/lib/ruby/1.8/i386-solaris2.11 )  has some incorrect paths  on OpenSolaris.  

 You can obtain a correct version from   http://blogs.sun.com/prashant/resource/gcc/rbconfig.rb (for gcc).  Back up the original file and repalce it with  the downloaded file.   This step is required for installing the native gems like fcgi, mysql etc.

   # cd /usr/ruby/1.8/lib/ruby/1.8/i386-solaris2.11
   # mv rbconfig.rb rbconfig.rb.orig
   # wget http://blogs.sun.com/prashant/resource/gcc/rbconfig.rb


4.  Setup  FastCGI

       4.1  Download the  FastCGI 2.4.0 from  http://www.fastcgi.com/dist/fcgi-2.4.0.tar.gz  and extract to /downloads

       4.2  Install gcc
               # pfexec pkg install SUNWgcc

       4.3  Build & Install FasCGI 2.4.0
               # cd /downloads/fcgi-2.4.0
               # ./configure
               # make install            

                This step will install the FastCGI into  directories /usr/local/lib and  /usr/local/include

4.4  Install fcgi gem
                # gem install fcgi -- --with-fcgi-include=/usr/local/include --with-fcgi-lib=/usr/local/lib

  5.  Install mysql gem
               # gem install  mysql -- --with-mysql-include=/usr/mysql/include/mysql  --with-mysql-lib=/usr/mysql/lib/mysql              

  6.   Update rails app generator to generate the ".htaccess" file

In the newer versions of the rails ".htaccess" file is NOT created while generating  a rails application.  To fix this, add the line for generating the  ".htaccess" to  file "app_generator.rb"  in  directory /usr/ruby/1.8/lib/ruby/gems/1.8/gems/rails-2.2.2/lib/rails_generator/generators/applications/app/

         m.template "configs/routes.rb", "config/routes.rb"    //  Exists already
        m.template "configs/apache.conf", "public/.htaccess"

   7.  Setup  a sample  rails application  (blog)

    7.1  Generate a sample rails  application

            # cd  /export/home/vasanth/rubyapps
            #  rails blog  -d mysql

            Note :  The 'blog' directory is created under "rubyapps".  We refer to  this as the %RUBY_APP_DIR% in the sections below.

     7.2   Update the  %RUBY_APP_DIR%/config/.htaccess  file

              a. Replace the line "AddHandler fastcgi-script .fcgi"  by  "AddHandler fcgid-script .fcgi"

               b.  Replace the line "RewriteRule \^(.\*)$ /dispatch.cgi?$1 [QSA,L]"   by  "RewriteRule \^(.\*)$ /dispatch.fcgi?$1 [QSA,L]"

      7.3  Update the mySQL connection parameters in %RUBY_APP_DIR%/config/database.yml

      7.4  Invoke the Rake target to create the database.
               # rake db:create


 Setting up  Apache 2.2 with FastCGI  on  OpenSolaris 2008.11

  The following  steps were performed as a user with 'root' privileges. 

 1.  Install  packages for Apache2.2 and Apache FastCGI  plugin

       # pfexec  pkg install   SUNWapch22
       # pfexec  pkg install   SUNWapch22m-fcgid


   2.   Changes to  Apache 2.2 configuration ( /etc/apache2/2.2/httpd.conf)

       2.1  FastCGI configuration

               #cp /etc/apach2/2.2/samples- conf.d/fcgid.conf  /etc/apach2/2.2/conf.d/

               Add  the following line  in httpd.conf  under the section  'Dynamic Shared Object (DSO) Support'

               Include /etc/apache2/2.2/conf.d/fcgid.conf

         2.2  VirtualHost  and  Directory configuration for  Rails applications

                Add  the following lines at the end of  httpd.conf file.

               <Directory  %RAILS_APP_DIR% >
                     AllowOverride all
                     Allow from all
                     Order allow,deny
               </Directory>

               <VirtualHost \*:9090>
                    ServerName %HOSTNAME%>
                    DocumentRoot %RAILS_APP_DIR%/public
                    ErrorLog  %RAILS_APP_DIR%/error_log
                    Options Indexes ExecCGI FollowSymLinks
                    RewriteEngine On
               </VirtualHost>

       Notes :

                     1.   Repalce %RAILS_APP_DIR% with the actual  root directory of the rails application
                     2.   Replace  %HOSTNAME%  with  your actual hostname

           2.3  Add the directive for apache to  listen on port 9090

                   Add the following  line under the default Listen ( Listen 80 ) directive.

                   Listen  9090


           2.4  Restart Apache for the new configuration to take effect

                   # /usr/apache2/2.2/bin/apachectl   stop
                   # /usr/apache2/2.2/bin/apachectl   start

Testing the setup  on  OpenSolaris 2008.11

 1  Start Firefox and  open the URL http://hostname:9090/

 2. In the Welcome page, click  on the link "About your application's environment"

3.   You should see the following screen which displays the Ruby On Rails  environment information.

Ruby on Rails Environment




























Comments:

Thanks for this Vasanth.

Should the mysql package be SUNWmysql5 (without the hyphen)?

Also, doing a gem update --system took ages and then crashed. Installing 1.3.1 from scratch worked.

Cameron.

Posted by Cameron on January 16, 2009 at 02:19 PM IST #

Thanks Cameron for pointing out the typo error.

Yes the mysql package should be SUNWmysql5. I have updated the entry with this correction.

I did try the gem update --system on another system. This worked fine for me. But had to set the http_proxy environment as my system was behind a firewall.

--Vasanth

Posted by Vasanth on January 17, 2009 at 04:40 PM IST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Vasanth Bhat

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today