Monday, January 24, 2011

Run your rails application with apache2 and mongrel_clusters

Run your rails application with apache2 and mongrel_clusters

Install Apache 2.2 and enable the needed modules (url rewriting, proxy, proxy_balancer e proxy_http)

sudo apt-get install apache2

# enable your modules in apache
sudo a2enmod rewrite
sudo a2enmod proxy
sudo a2enmod proxy_balancer
sudo a2enmod proxy_http
sudo a2enmod proxy_connect

# if you not enabled any of the modules then may be errors occured while you restart your apache after adding the below configration
# Error may be encoutered it yoou have not enabled module rewrite is "Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration"

Install Mongrel, create a mongrel user, create the mongrel cluster
sudo gem install daemons gem_plugin mongrel mongrel_cluster --include-dependencies

You can continue with the same linux user to mongrel if you want otherwise you can create new user by
sudo /usr/sbin/adduser mongrel
This line creates new user mongrel withg same mongrel group
Now need to create one mongrel_cluster.yml
sudo mongrel_rails cluster::configure -e production -p 3010 -N 2 -c /home/user/projects/myapp -a 127.0.0.1 --user mongrel --group mongrel

here -e = your working environment
     -p = your port for run application
     -N = Number of instances of the application. here 2 instances for my application and port is 3010. so this application will work on 3010 and 3011 port
 --user = need to provide your linuxuser
--group = need to provide your linuxgroup

this line will create /config/mongrel_cluster.yml file with following command
user: mongrel
cwd: /home/user/projects/myapp
log_file: log/mongrel.log
port: "3010"
environment: production
group: mongrel
address: 127.0.0.1
pid_file: tmp/pids/mongrel.pid
servers: 2

now make sure that tmp and log folder has permission to the user/group which you have user in mongrel_cluster if not assign permission by
sudo chown -R mongrel:mongrel /path/to/app/tmp

# Now start mongrel clusters
sudo mongrel_rails cluster::start

Hopefully clusters is worked perfactly and you can get it run on multiple port 3010 and 3011
http://127.0.0.1:3010
http://127.0.0.1:3011

Now need to set proxy balancer for this app
For this need to create one host for you app
Go to sudo nano /etc/hosts

127.0.0.1 localhost
127.0.0.1 myapp

Now need to configure Apache configuration file (sudo gedit /etc/apache2/sites-available/default).

NameVirtualHost *:80

#we need this as on Ubuntu/debian by default Proxy is not allowed
<Proxy *>
  Order allow,deny
  Allow from all
</Proxy>

#Proxy balancer section (create one for each ruby app cluster)
  <Proxy balancer://myapp_cluster>
    BalancerMember http://myapp:3010
    BalancerMember http://myapp:3011
  </Proxy>

#Virtual host section (create one for each ruby app you need to publish)
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
        ServerName myapp  
        DocumentRoot /home/user/projects/myapp/public/
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /home/debian/chirag/mycalltime/public/>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

  #Rewrite stuff
  RewriteEngine On

  # Check for maintenance file and redirect all requests
  RewriteCond %{DOCUMENT_ROOT}/system/maintenance.html -f
  RewriteCond %{SCRIPT_FILENAME} !maintenance.html
  RewriteRule ^.*$ /system/maintenance.html [L]

  # Rewrite index to check for static
  RewriteRule ^/$ /index.html [QSA]

  # Rewrite to check for Rails cached page
  RewriteRule ^([^.]+)$ $1.html [QSA]

  # Redirect all non-static requests to cluster
  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
  RewriteRule ^/(.*)$ balancer://myapp_cluster%{REQUEST_URI} [P,QSA,L]

</VirtualHost>


After configure the default file need to restart apache
sudo /etc/init.d/apache2 restart

Cheers....
You get your app running on multiple instanses with proxy balancer
http://myapp

Tuesday, August 10, 2010

search gem from repository

If you want to find a gem in the repository then you can do it by following way

gem1.8 list -r | grep gem_name

Wednesday, June 30, 2010

Unpacked gem authlogic-2.1.3 in vendor/gems has no specification file. Run 'rake gems:refresh_specs' to fix this.

You can solve this issue by following way

> cd vendor/gems/authlogic-2.1.3
> gem specification authlogic > .specification

cheers,

Thursday, June 24, 2010

Get Session Data Available in Models - Ruby on Rails

Sometimes we really need session variables inside model. But as the rails framework defines (MVC) pattern. 

This pattern separates the context of the Web Application (Controller,View) from the Model. Model contains business logic of the web application. The Controller handles the interactions between the View and the Model. 

So you can not access sessions directly in your model. You can crack it by following way.

Create one module 
module Utility
  def current_user
    Thread.current[:user]
  end
 
  def self.current_user=(user)
    Thread.current[:user] = user
  end
end
 
Add following in application controller
 
class ApplicationController < ActionController::Base
  include Utility
  before_filter :set_user_session
 
  protected
  def set_user_session
    Utility.current_user = session[:user]
  end
end
 
And now get it in your model
 
class Account < ActiveRecord::Base
  include Utility
 
  def before_create
    unless allowed?(current_user)
      return false 
    end
  end
end
 
 

Tuesday, May 4, 2010

fetch all controllers and actions of the application

dirs = Dir.new("#{RAILS_ROOT}/app/controllers").entries
controller_hash = {}
dirs.each do |controller|
  if controller =~ /_controller/
    path = RAILS_ROOT + '/app/controllers/' + controller
    con = File.read(path).split('def ')
    con.delete_at(0)
    my_hash = Hash.new
    actions = con.collect{|c| c.gsub("\r","~").gsub("\n","~").split("~")[0].strip}
    controller_name = controller.gsub('.rb','')
    my_hash = {controller_name.to_s => actions}
    controller_hash = my_hash.merge(controller_hash)
  end
end
puts controller_hash.inspect

Friday, February 5, 2010

How to generate an SSH key in Linux?

You can generate a key in Linux using the ssh-keygen command.
You can run it in command line. You will be asked for a file in which the key should be saved to and for a passphrase (password) for the key:
This command will generate id_rsa public and private keys.

If you need to generate id_dsa keys then you need to run ssh-keygen -t dsa

Thursday, January 28, 2010

cannot open shared object file: No such file or directory

I got below error while i tried to run my application in fedora

libMagickCore.so.2: cannot open shared object file: No such file or directory – /usr/lib/ruby/gems/1.8/gems/rmagick-2.8.0/lib/RMagick2.so

To get solved of this error i executed
ldconfig /usr/local/lib

Contact Me for any help regarding rails/nodejs/php/mysql

Name

Email *

Message *