Tuesday, March 27, 2012

Setup Nginx on server for rails application.



To install nginx you would require passenger first:
install passengar:

$ gem install passenger

$ passenger -v

In order to let Passenger install Nginx, you need to install a dependency first.
$ sudo apt-get install libcurl4-openssl-dev
use rvmsudo than regular sudo to install nginx

$rvmsudo passenger-install-nginx-module
start nginx
$ sudo /opt/nginx/sbin/nginx
Configuration
$ sudo nano /opt/nginx/conf/nginx.conf (VPS)
$ sudo /etc/init.d/nginx reload (VPS)

configuration file example

server {
listen 80;
server_name www.yourdomain.com;
root /home/nagendra/test_app/current/public;
passenger_enabled on;

#charset koi8-r;

#access_log logs/host.access.log main;

#location / {
# root html;
# index index.html index.htm;
#}

Friday, March 16, 2012

Steps To Setup Ruby on Rails on ubuntu Server

Log into the server through ssh

ssh default-user@vps-ip-address

for example: root@ip_address

For security purpose never do any operation from root access thats why we should create a user:

#adduser nagendra

#adduser nagendra some-group-name

ex: adduser nagendra admin

Securing your SSH (server) configuration is the next step. Because every Unix system has a “root” user by default, you should disable “root” from logging in using SSH. This makes your system less vulnerable to brute force attacks.

$ nano /etc/ssh/sshd_config

Set PermitRootLogin to no, and reload the SSH configuration so that the changes take effect. Although “root” will be disabled from logging in in future, the current “root” user connection will be maintained.

$ /etc/init.d/ssh reload

Now, log out of your VPS as “root,” and log back in as the new user.

$ logout and $ ssh nagendra@ip_address

Update the server

$ sudo apt-get update 
$ sudo apt-get upgrade 
$ sudo apt-get dist-upgrade 
$ sudo reboot

ssh nagendra@ip-address

We need git and curl setup first

$ sudo apt-get install curl $ sudo apt-get install git-core

Install RVM(stands for Ruby Version Manager and

is “a command line tool that allows you to easily install,

and work with multiple Ruby environments, from interpreters to sets of Gems.)

$ bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)

Navigate to home  
$ cd $ nano .bashrc and add the below line 
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" load the script

$ source .bashrc Verify RVM  
$ type rvm | head -1 the shell should return that “rvm is a function.”
Setting up ruby gems 
$ rvm notes 
$ (sudo apt-get install 
build-essential bison 
openssl libreadline6 libreadline6-dev 
curl git-core zlib1g zlib1g-dev libssl-dev 
libyaml-dev libsqlite3-0 libsqlite3-dev 
sqlite3 libxml2-dev libxslt-dev autoconf 
libc6-dev ncurses-dev ) in one line
$ rvm list known 
$ rvm install 1.9.2 
Set default ruby version 
$ rvm --default use 1.9.2 check  
$ ruby -v 
$ gem -v 
also you can manually update 
$ gem update --system $ gem update 
Install Rails 
$ gem install rails -v 3.0.11(or any version that you need) 
$ rails -v

Monday, February 13, 2012

Installing the postgresql 9.1 on Ubuntu 11


Installation of Postgresql



1. $ sudo add-apt-repository ppa:pitti/postgresql

    1. $ sudo apt-get update and sudo apt-get install postgresql

    2. check if postgresql installed properly

      psql -V //you must get psql (PostgreSQL) 9.0.4

    3. finger postgres to get the installation overview

    4. lets login to postgres

      $su postgres

    5. psql // you would be logged in to psql terminal postgres=# select version();

Setting up root user for postgres

$ su postgres # switch to the user postgres

    $ cd /etc/postgresql/9.0/main     
cp pg_hba.conf         pg_hba.conf.bak.original  
cp postgresql.conf       postgresql.conf.bak.original      
Make changes to pg_hba.config (authetification methods).
host    all         all       127.0.0.1/32       trust          
# md5 -> trust
Now we can start our postgres server 
$ sudo /etc/init.d/postgresql restart  
* Restarting PostgreSQL 9.0 database server   
$ sudo /etc/init.d/postgresql status Running clusters: 9.0/main 
Install Pgadmin3 tool for managing postgres
 $ sudo apt-get install pgadmin3   
# install the latest pgAdminIII 
type pgadmin3 in terminal to launch

Sunday, January 15, 2012

Thinking Sphinx for Rails 3

Step 1
Sphinx Installation:

wget http://www.sphinxsearch.com/downloads/sphinx-0.9.9.tar.gz
tar -xzf sphinx-0.9.9-rc2.tar.gz
cd sphinx-0.9.9-rc2/
./configure
make
sudo make install

Note: sphinx By default comes with Mysql configuration, to configure it with pgsql
./configure --prefix=/usr/local --with-pgsql --without-mysql

Step 2
Thinking Sphinx Installation

In you Gem File add
1. gem
'thinking-sphinx', :git => 'http://github.com/freelancing-god/thinking-sphinx.git', :require => 'thinking_sphinx'


2. run bundle install

rake thinking_sphinx:index – Create the index
rake thinking_sphinx:reindex – Reindex Sphinx without regenerating the configuration file
rake thinking_sphinx:start – Start up Sphinx's daemon
rake thinking_sphinx:stop – Shut down the daemon

Example Usage of Thinking sphinx
In your Model:
define_index do
indexes location
indexes [first_name, last_name], :as => :name, :sortable => true
end

let Sphinx know your fields that has to be indexed.

Thats it you can search now using Model.search("first_name last_name")



Monday, January 9, 2012

Thumbs up in Rails

1. instal the gem :

gem 'thumbs_up'

.2. Create and run the ThumbsUp migration:

rails generate thumbs_up
rake db:migrate

.3. setting the model that act as voter

class User < ActiveRecord::Base
acts_as_voter
end

setting the model that can be voted

class Comment < ActiveRecord::Base
acts_as_voteable
end

4. To save the Vote
voter.vote_for(voteable) # Adds a +1 vote
voter.vote_against(voteable)    # Adds a -1 vote
voter.vote(voteable, vote)
# Adds either a +1 or -1 vote: vote => true (+1), vote => false (-1)

voter.vote_exclusively_for(voteable)
# Removes any previous votes by that particular voter, and votes for.

voter.vote_exclusively_against(voteable) # Removes any previous votes by that particular voter, and votes against.

5. Other Useful methods

positiveVoteCount = voteable.votes_for
negativeVoteCount = voteable.votes_against
plusminus = voteable.plusminus # Votes for minus votes against.

voter.voted_for?(voteable) # True if the voter voted for this object.
voter.vote_count(:up | :down | :all) # returns the count of +1, -1, or all votes

voteable.voted_by?(voter) # True if the voter voted for this object.
voters = voteable.voters_who_voted

Note : ThumbsUp by default only allows one vote per user.

This can be changed by removing:

In vote.rb:

validates_uniqueness_of :voteable_id, :scope => [:voteable_type, :voter_type, :voter_id]

In the migration:
add_index :votes, ["voter_id", "voter_type", "voteable_id", "voteable_type"], :unique => true, :name => "uniq_one_vote_only"

Install Ruby 1.8 and rails 3 from source and repository

From Ubuntu repo:
you should add the ubuntu-on-rails ppa repository
sudo add-apt-repository ppa:ubuntu-on-rails sudo apt-get update

Now get the essential ruby packages:

sudo apt-get install ruby rubygems irb ri rdoc rake

The dependencies should look like:
irb1.8 libreadline-ruby1.8 libreadline5 libruby1.8 rdoc1.8 ri1.8 ruby1.8 rubygems1.8

But you need more packages!

sudo apt-get install build-essential ruby1.8-dev libopenssl-ruby

These extra packages are very important – odds are high that you will need them. Unfortunately they are not in the standard package dependencies, partly because of some licensing issues – readline and openssl have incompatible licenses.

After getting all packages, there is another little step necessary: You have to add the gem path to your global PATH, so that executables new gems can be easily called from the command line. Do it by adding the following line to your ~/.bashrc:

export PATH=/var/lib/gems/1.8/bin:$PATH

Then restart your terminal or enter source ~/.bashrc

Ruby is ready now :)


From Source:

This way is recommended, when you want to have full control over your installation.

Do not get deterred, it is not that hard. Firstly, get the needed dev packages:


sudo apt-get install libruby1.8 libruby1.9
zlib1g-dev libssl-dev libreadline-dev build-essential

1.9

@wget http://ftp.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p0.tar.gz
tar -xzf ruby-1.9.3-p0.tar.gz

1.8

wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p334.tar.gz wget http://production.cf.rubygems.org/rubygems/rubygems-1.8.5.tgz tar -xzf ruby-1.8.7-p334.tar.gz tar -xzf rubygems-1.8.5.tgz

Now navigate to the extracted ruby directory and run:

./configure make sudo make install

To install both versions at the same time (without RVM), you can pass a --program-suffix option to one of the ./configurecommands before compiling.

You can check if this was successful with: ruby -v


Installing Rails

Choose a database, for example sqlite3

sudo apt-get install sqlite3 libsqlite3-dev sudo gem install sqlite3-ruby

or MySQL

sudo apt-get install mysql-server libmysqlclient-dev sudo gem install mysql

and install the framework:

sudo gem install rails

Common fixes while installation:

openssl (1.8)

/var/lib/gems/1.8/gems/rails-2.3.5/lib/initializer.rb:271:in `require_frameworks': no such file to load -- net/https (RuntimeError)
sudo apt-get install libopenssl-ruby

openssl (1.9.1)

/var/lib/gems/1.9.1/gems/rails-2.3.5/lib/initializer.rb:271:in `rescue in require_frameworks': no such file to load -- net/https (RuntimeError)
sudo apt-get install libopenssl-ruby1.9.1

ruby1.8-dev

ERROR: Failed to build gem native extensions. /usr/bin/ruby1.8 extconf.rb extconf.rb:8:in `require': no such file to load -- mkmf (LoadError)
sudo apt-get install ruby1.8-dev

ruby1.9.1-dev

ERROR: Failed to build gem native extensions. /usr/bin/ruby1.9.1 extconf.rb extconf.rb:1:in `require': no such file to load -- mkmf (LoadError)
sudo apt-get install ruby1.9.1-dev

build-essential

make: g++: Command not found
sudo apt-get install build-essential

wrong gem path settings

The program 'rails' is currently not installed. You can install it by typing: sudo apt-get install rails

Add export PATH=/var/lib/gems/1.8/bin:$PATH to the ~/.bash.rc file and restart your terminal (adjust the version number, if needed).

Monday, December 19, 2011

Adding BreadCrums in Rails 3 application

I recently came across a plugin that easily helps you add breadcrums to your app ad helps you customize it.

1. install the plugin in rails 3 app:

2. add this line in application.rb file
include BreadcrumbsOnRails::ControllerMixin

3. in controller:
class MyController
add_breadcrumb "home", :root_path
add_breadcrumb "my", :my_path
def index
# ...
add_breadcrumb "index", index_path
end
end

4. in views:
<%= render_breadcrumbs %>

Note:
1. changes in lib/breadcrums.rb
def render_element(element)
content = context.link_to_unless_current(compute_name(element), compute_path(element))
if @options[:tag]
@context.content_tag(@options[:tag], content.html_safe)
else
content.html_safe
end
end

2. changes in lib/controller_mixin.rb
def render_breadcrumbs(options = {}, &block)
builder = (options.delete(:builder) || Breadcrumbs::SimpleBuilder).new(self, breadcrumbs, options)
content = builder.render
if block_given?
concat(capture(content, &block)).html_safe
else
content.html_safe
end
end