List of Rails gem that i use often for App development

I used several gems for different purpose in different applications. After setting up ruby on rails development environment in a new machine, there will be several gems installed by default, such as

activerecord
activesupport
activesource
rake
rubyforge and so on….

These are important gems but i’m not going to deal with these gems now. Most often we need many other external gems for application development. Here i’m providing a list of necessary gems below.

BackgroundRB (For cron jobs in ruby on rails)

capistrano (For automating tasks via SSH on remote servers like software installation, application deployment, configuration management)

pdf-writer (For pdf report generation in ruby on rails)

clickatell (For sending sms)

chronic (For date parsing in ruby on rails)

postgres-pr (For postgreSQL database connectivity with ruby on rails)

ruby-openid (OpenID integration with existing application of ruby on rails)

will_paginate (For pagination)

chris-auto_complete_jquery (Auto complete using jquery)

There are several other useful gems which i’ll include in this list soon.

As a starter application i liked baseapp cause its a great app to start with and it has lots of default features like User authentication including password recovery, account activation, openID integration, will_paginate, admin interface with user management and default CSS-based UI with tabs.

2 Comments

Uninitialized constant ApplicationController and Migration problem in rails 2.3

Uninitialized constant ApplicationController...... error in rails 2.3

if you have recently upgraded your rails version to 2.3 or higher. you will get this error.

uninitialized constant ApplicationController…..

application.rb was renamed to application_controller.rb in Rails 2.3

To fix this error, rename your application.rb to application_controller.rb :)


Migration problem in ruby on rails.

During migration i often face problem which says ….syntax error, unexpected ‘\n’, expecting tCOLON2…blablabla

Whenever we use scaffold to develop any simple AMD tool, migration files generate automatically, and we often face such error during migration. we generally think that, those files are ok, but its not always true, you can easily fix those errors and it’s not such a big deal also :)

open that migration file from db/migrate/file_that_causing_syntax_error.rb

and search for syntax error, you will definitely find few syntax errors there. (generally you will see (comma) is misplaced in several lines).

def self.up
create_table :posts do |t|
t.string, :title
t.string, :body
t.datetime, :mod_date
t.timestamps
end
end

So try to fix those errors and migrate db again. I hope this will help.

Single file migration

You can also migrate a single file, to do this you have to mention the version number of that file, such as

rake db:migrate:up VERSION=20090421094006 (place your own file’s version_number)

rake db:migrate:up and rake db:migrate:down let you run the up and down operations of an individual migration.

You can get help regarding rails migration in the following link (one of my favourite rails migration cheatsheet)

http://dizzy.co.uk/ruby_on_rails/cheatsheets/rails-migrations

No Comments

Install and run a simple ruby on rails application in windows xp (Netbeans & Instant rails)

Few days ago i made a blog post regarding ruby on rails installation in Linux machine . But some of my friends told me to write a post regarding ruby on rails installation in windows machine. So here is what you need to  do, though I found it little difficult to setup ruby on rails environment in windows  :(

To install ROR in windows xp what you need is listed below

  • Instant Rails
  • Netbeans IDE

OR

  • cRuby Interpreter(C based ruby) or jRuby interpreter (java based ruby)
  • Webrick or Mongrel or Glassfish or Apache server.
  • Database (PosgreSQL, MySQL, Sqlite, Microsoft SQL server …..)

I like cRuby interpreter and mysql as i’m much familiar with C and MySQL. Ruby on Rails supports large number of databases and one of its main feature is database abstraction (Active Record) which allows one to change full database server anytime he/she wants and you don’t have to change a single line of code in the main application.

Anyway, You can use Instant rails (cRuby & MySQL) for installing Ruby on Rails in windows and Netbeans IDE 6.5 for developing Ruby on Rails application. Netbeans is a standalone IDE and it already comes up with jruby interpreter by default.

So install Instant rails and start mysql service, you don’t need to start apache service as we’ll use webrick server (default) for ROR application. Now install and start netbeans IDE.

Now you need to change ruby interpreter from Tools-> Options-> Ruby now browse ruby.exe file from Instant rails directory (where it was installed, most often the directory is, C:/instant rails/ruby/bin/ruby.exe)

Now in the Netbeans window open a new project, select ruby from category then select Ruby on rails Application from projects. Click on next button, type project name and open that project.

Now run the project from Netbeans window, It will automatically open the project in your default browser (if not, type localhost:3000 in the url of browser). There is a rails console in the Instant rails window, one can use that console to test or manage project too.

Ruby is on rails now. Installation part complete.

Ok, Now i’m considering the fact that ROR(Instant rails for windows) & netbeans IDE installed in your pc, follow the steps below to develop a simple rails application (an AMD tool) using scaffold method.

Step 1: start Instant rails (cruby and mysql) and Netbeans IDE

Step 2: In netbeans window open a new Ruby on rails project. Bunch of files will be created in the project folder.

Step 3: Change mysql database name, username and password (if you want different db name, user and password)

Step 4: Now create the database from netbeans. To do this, right click on the project folder select Run rake task->db->create. This will create the database in mysql.

Step 5: Again right click on the project folder, click generate. Select scaffold from the dropdown, type model name in a singular form (such as post) now in the action field type field names: field type that you want in the application you are developing. In my case its (title and body), so type title:string body:text in the action field. Now click OK.

This will generate necessary files with default code in controller, view, database migration and other necessary folders. In Database migrate->migrate folder, you will notice a file which contains info to generate database tables and fields in the database. Now, to create table with specified fields in the database we need to migrate database to current version.

Step 6: Right click on the project, select migrate database->To current version. This will create db table with specified fields.

Step 7: You are done. Now run the project from netbeans and type localhost:3000/posts (posts is the controller name) in the url of any explorer. you will see your application running.

VOILA!!

, , ,

1 Comment

etc/sudoers permission problem in ubuntu? $HOME/.dmrc ignored?

I use ubuntu at home, few days ago found a weird problem with permission, i was getting popup during login and was frustrated with this. I was going to reinstall my favourite OS(fedora). But later decided against it and found few ways to recover Ubuntu :)

Permission problem /etc/sudoers ?

start your computer in recovery mode.
(at boot press Esc for the grub menu and select: Ubuntu, kernel …….-c (recovery mode))

sudo chmod 0440 /etc/sudoers
sudo reboot

su must be setuid root to operate
you must issue the following command as root:
chmod 4755 /bin/su

######################################

Re: $HOME/.dmrc file is being ignored

Change permission of user directory and .dmrc file as well as chown those.

sudo chmod 644 /home/shabuz/.dmrc
sudo chown shabuz /home/shabuz/.dmrc
sudo chmod -R 700 /home/shabuz
sudo chown -R shabuz /home/shabuz

you can set 755, 766 and 777 as well.

######################################

Problem with GUI ? Stuck at the login screen??

If you don’t have access to your graphical (GUI) desktop to delete these folders in Nautilus or you’re stuck at the login screen, drop to a terminal by hitting CTRL + ALT + F1, login to your account, and run this command:

rm -rf .gnome .gnome2 .gconf .gconfd .metacity

UPDATE (2008.01.30): Keep in mind that this will only reset your Gnome-specific settings. If you are having problems with your video card, display, x-server, etc., this WILL NOT fix your problems.

Hope this will help :)

References:
http://ubuntuforums.org/
http://linuxfud.wordpress.com/2007/02/14/how-to-reset-ubuntugnome-settings-to-defaults-without-re-installing/

, , , ,

1 Comment

Configure Citycell Zoom ZTE CDMA USB Modem in Ubuntu & Fedora

Few months ago, I configured citycell zoom in my friend’s pc. That was Huawei modem and was much easy to configure. First tried to detect modem using wvdialconf in the terminal. Then just edited wvdial.conf file (/etc/wvdial.conf) with the detected path and after dialing wvdial cdma in the terminal it was successfully connected.

But recently in the eid vacation when i was in chittagong, i’ve bought citycell zoom with ZTE usb modem for using internet there. It was very much easy to setup and connect to internet in windows XP but failed to do so in ubuntu. First i tried to detect modem using wvdialconf. But It was showing a msg like failed to detect modem. then tried to grep tty using dmesg | grep tty but failed again, then tried to view modem using cat /proc/bus/usb/devices. But i haven’t found anything like devices in cat /proc/bus/usb/devices directory.

Anyway, Here is what you have to do to detect ZTE usb modem.

In ubuntu, you have to uncomment few lines to explore devices directory. So edit mountdevsubfs.sh file. ( ** No need to do this in fedora core, it will automatically detect devices in /proc/bus/usb/ directory)

sudo gedit /etc/init.d/mountdevsubfs.sh

and uncomment following lines from this file.

# Magic to make /proc/bus/usb work
mkdir -p /dev/bus/usb/.usbfs
domount usbfs “” /dev/bus/usb/.usbfs -obusmode=0700,devmode=0600,listmode=0644
ln -s .usbfs/devices /dev/bus/usb/devices
mount –rbind /dev/bus/usb /proc/bus/usb

Now start the service

/etc/init.d/mountdevsubfs.sh start and reboot your pc.

Then check if the USB device is detected properly
cat /proc/bus/usb/devices

Try to find some output like this

T:  Bus=07 Lev=01 Prnt=01 Port=01 Cnt=01 Dev#=  2 Spd=12  MxCh= 0
D:  Ver= 1.01 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=16 #Cfgs=  1
P:  Vendor=19d2 ProdID=fffd Rev= 0.00
S:  Manufacturer=ZTE, Incorporated
S:  Product=ZTE CDMA Tech
C:* #Ifs= 3 Cfg#= 1 Atr=e0 MxPwr=100mA
I:* If#= 0 Alt= 0 #EPs= 3 Cls=ff(vend.) Sub=ff Prot=ff Driver=usbserial_generic
E:  Ad=81(I) Atr=03(Int.) MxPS=  16 Ivl=128ms
E:  Ad=8a(I) Atr=02(Bulk) MxPS=  64 Ivl=0ms
E:  Ad=0b(O) Atr=02(Bulk) MxPS=  64 Ivl=0ms
I:* If#= 1 Alt= 0 #EPs= 2 Cls=ff(vend.) Sub=ff Prot=ff Driver=usbserial_generic
E:  Ad=83(I) Atr=02(Bulk) MxPS=  64 Ivl=0ms
E:  Ad=06(O) Atr=02(Bulk) MxPS=  64 Ivl=0ms
I:* If#= 2 Alt= 0 #EPs= 2 Cls=ff(vend.) Sub=ff Prot=ff Driver=usbserial_generic
E:  Ad=82(I) Atr=02(Bulk) MxPS=  64 Ivl=0ms
E:  Ad=05(O) Atr=02(Bulk) MxPS=  64 Ivl=0ms

Now probe for this USB device
modprobe usbserial vendor=0×19d2 product=0×fffd

You are done!!

Now run wvdialconf in the terminal Or you can edit /etc/wvdial.conf file and paste these following lines.

[Modem0]
Modem = /dev/ttyUSB0
Baud = 230400
SetVolume = 0
Dial Command = ATDT
Init1 = ATZ
FlowControl = Hardware (CRTSCTS)
[Dialer cdma]
Username = waps
Password = waps
Phone = #777
Stupid Mode = 1
Inherits = Modem0

Now you can connect to internet by applying the following command in terminal.

wvdial cdma

Using the same procedure, i’ve configured this modem in my office (fedora) also.

Thats it! Enjoy. :)

Note: For Ubuntu intrepid 8.10 and Ubuntu 9.04 jaunty jackalope, Please see my comments, i hope that will help you to connect to Internet

, , ,

25 Comments

Problem with mysql connect to remote database?

Yesterday i found a peculiar problem. We have just transferred our application from our local server to another server. But after that this application wasn’t working properly :o .

Anyway, we have tried to locate the problem and found that it wasn’t connecting to the replica database (another mysql database) properly. But most interesting thing was i was successful to connect from command prompt using ssh using same connection parameter.

Then i wrote an script to test the connection and found that i can’t connect to remote database using mysql_connect X(

Anyway, after surfing a lot i’ve found a solution to solve the problem.

Open your my.cnf which is placed in /etc/ directory and find the following line.

bind-address=127.0.0.1

It allow only localhost to connect. So delete or comment out this line. It will solve the problem
But my porblem wasn’t that. infact that line wasn’t exist in my my.cnf file. Problem was with the extra security that apache provide now a days. apache doesn’t allow connetion to other network by default. So follow the instruction to activate network connection.

At first, verify that SELinux is enabled or not.

in order to varify type in your command prompt getenforce
You should see something like Enforcing
If you see a different result, type setenforce 1 in the command prompt

If you get an error, then you do not have Selinux turned on. Go to /etc/selinux/config and edit the config file to enable Selinux.

Now reboot the linux server
Once SELinux is enabled, do the following

Set SELinux to allow remote connections for HTTP

setsebool -P httpd_can_network_connect=1

Tha’ll do the trick.

, , ,

2 Comments

Install and run a simple application in ruby on rails (feodra & ubuntu)

Here is the Instruction that you have to follow to install ruby on rails. Instead of yum, use apt-get if you are using ubuntu.

Step 1: install ruby yum install ruby

Step 2: Install rubygems yum install rubygems

note: It will ask for installing dependencies, Press Y each time

Step 3: Now install rails gem install rails

You are done. :)
Now try to run a simple application using ruby on rails.

Create a folder in any directory (suppose its “myproject”) and our project name is simpleapp so from command prompt go to that folder (myproject).

now type

rails simpleapp -d mysql

i.e: -d mysql is for using mysql database. if u r using sqlite3 then just omit these text.

Anyway, after running this rails it will automatically create all the necessary files with a configuration file for mysql. Go to myproject/simpleapp/config/ directory. You will find database.yml file. Open the file you’ll the configuration for 3 database for 3 different mood. By default it always use development mood. So edit the configuration of development mood.

You will see that, it has configured the database name as simpleapp_development.

Edit this name or create new database in the mysql with this name or the edited db name.

Now we will try to build an AMD(add-modify-delete) tool using ruby on rails. For this initially we will use scaffold feature of ruby on rails.

In the command prompt go to your application directory and type

script/generate scaffold post title:string body:text

Once scaffolding is successful then migrate database. To migrate database type

rake db:migrate

Now just run the server and view your application. In order to run the server type

script/server

After running the server you will see that its listening to the port localhost:3000 or 127.0.0.1:3000 . That means you can view your application in that port of your web browser.

Open firefox and type localhost:3000/posts (posts is the controller name) to see your application.

yup. that’s all done. Isn’t it too simple ? :)

1 Comment

How to setup awstats in centOS

Installing awstats is little complex in centOS whereas i’ve installed awstats easily in my FC9 following this instruction

But failed to do the same in centOS. So here is the whole process to follow and setup awstats in centOS .

Step 1: Download the awstats rpm (awstats-6.6-1.el5.rf.noarch.rpm) from here and place it in your server

Step 2: Login to the server with SSH and run awstats from command prompt. To run awstats, go to that directory where you have placed the awstats rpm and type

rpm -ivh awstats-6.6-1.el5.rf.noarch.rpm

Step 3: Now go to the /etc/awstats/ directory and copy the file named awstats.localhost.localdomain.conf and paste it in the same directory with different name. The file name should be awstats.your_domain_name.conf. (let’s consider mine one is awstats.abc.conf). In order to do this, go to that specific directory and type

cp awstats.localhost.localdomain.conf /etc/awstats/awstats.Your_domain_name.conf

Step 4: Now Open that file and find the word “SiteDomain=”Localhost.Localdomain” in that file and replace the domain name with your domain name. suppose it was abc

SiteDomain = "abc"

Save that file when you are done and go back to command prompt.

Step 5: Now go to /var/www/awstats/ directory and create 2 file named

awstats062008.abc.txt (I.e: modify with current date) &

awstats062008.abc.bak (I.e: modify with current date)

Step 6: After that, run the following command which will create a html file.

perl awstats.pl -config=Your_domain_name -output -staticlinks > awstats.Your_domain_name.html

and finally run the following command in the command prompt.

perl awstats.pl -config=your_domain_name -update

Step 7: Now open a browser and in the URL and type

http://your_server_ip/awstats/awstats.pl?config=your_domain_name

Step 8: If page is not populating with proper records which indicates that your access_log is not populating properly. So open vhosts.conf file which is located in the /etc/httpd/conf.d/ direcotry and comment out 2 lines about access_log and error_log of that specific domain. Save and close that file and follow step6 again. Hope, everything will work properly now.

That’s all.

,

1 Comment

Necessary commands (tips) to tune apache

To search in the current directory and all sub directories for a file named httpd.conf
find . -name “httpd.conf” -print

To find some string or text, type
find . -exec grep “MaxClients” ‘{}’ \; -print
This command will search in the current directory and all sub directories. All files that contain the string with the path.

If you want to just find each file then pass it on for processing use the -q grep option. This finds the first occurrance of the search string. It then signals success to find and find continues searching for more files.
find . -exec grep -q “www.athabasca” ‘{}’ \; -print

Send 1000 Request to apache using apache benchmark
ab -n 1000 -c 200 -k YOUR_URL

To view error log of httpd. type
grep -i maxclient /var/log/httpd/error_log*
To view Process status type and load average type top and uptime respectively.

To open a file and search something(Here Example is: MaxClients) from there type
vi +/MaxClients /etc/httpd/conf/httpd.conf

To view total memory used by httpd, type
ps -ylC httpd –sort:rss

, , ,

2 Comments

How to tune Apache and Mysql

By default, Apache comes preconfigured to serve a maximum of 256 clients simultaneously. This particular configuration setting can be found in the file /etc/httpd/conf/httpd.conf

If your server has 2 GB of RAM, and you’re sharing your server with MySQL(true in my case), you’ll want to reserve about half of it for Apache (1 GB)

MaxClients: here is the process of determining MaxClients. type
ps -U apache -u apache u
See the number of apache process running in you command prompt.
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
apache 7694 0.0 0.3 42704 6680 ? S 18:30 0:00 /usr/sbin/httpd

The above indicates that a single httpd process is using 6.6 MB of RSS (Resident Set Size) memory (or non-swapped physical memory) and that it is using 42 MB of VSZ (Virtual Size) memory. This depends on the number of modules you have loaded and running in Apache.
As shared libraries are included in this number, it’s not 100 percent accurate. We can assume that half the RSS number is “real” memory. Let’s assume that each httpd process is using (6.6/2=3.3) 4 MB of memory. So if you have 1 GB ram then divide it with 4 MB of memory, which leaves room for around 256 concurrent httpd processes. Set MaxClients 256

Or

Somebody prefers to set MaxClients using following rule
MaxClients = 150 x RAM (GB)
So for example if you have 2 GB RAM (dedicated for apache) set this value to 300. In my case IT WILL BE 150

Or

Some individuals maintain that each httpd thread uses about 5 MB of “real” memory. So they determine by the following way..
Or MaxClients = RAM(MB)/5
So for example if you have 2 GB RAM (dedicated for apache) set this value to 409. In my case IT WILL BE 204(1 GB for apache)

Note: There is no reason for you to set it any higher unless you have a specific problem with this value. A high value can lead to a complete server hang in case of a DOS attack. A value too low can create timeout problems for your clients if the limit is reached

StartServers - Sets the number of child server processes created on startup. This setting depends greatly on the type of webserver you run. If you run low traffic websites on that server set it low to something like 5. If you have resource intensive websites on that server you should set it close to MaxClients.

MaxRequestsPerChild - Controls the number of request the a child serves before the child is killed. This should not be set too low as it will put an unnecessary load on the apache server to recreate the child. I suggest setting it to 1000. But we are going to use 2000 for handling heavy traffic load properly.

MinSpareServers and MaxSpareServers - MaxSpareServers and MinSpareServers control how many spare (unused) child-processes Apache will keep alive while waiting for more requests to put them to use. Each child-process consumes resources, so having MaxSpareServers set too high can cause resource problems. On the other hand, if the number of unused servers drops below MinSpareServers, Apache will fork. Leave those values to: MinSpareServers 5 MaxSpareServers 10

ServerLimit: Its better to keep Server limit same as the value of MaxClients.
MaxRequestsPerChild: I’ve Kept default apache value for this one.

So few changes need to be made in httpd.conf file which is located in /etc/httpd/conf/ directory

<IfModule prefork.c>
StartServers     140
MinSpareServers    5
MaxSpareServers   10
ServerLimit      150
MaxClients       150
MaxRequestsPerChild  4000
</IfModule>

[Note]: Response time depends on MaxClients. If you increase the MaxClients number, server will response more quickly for each request but  a high value can lead to a complete server hang.

Ab is a tool for benchmarking the performance of your Apache HyperText Transfer Protocol (HTTP) server. It does this by giving you an indication of how many requests per second your Apache installation can serve.

uptime command in your root login should not yield a load average above 1, and the server should respond to commands quickly
ab -n 10000 -c 200 -k http://your_url
-c = concurrent connections
-t = time limit
-n = # of requests

Keep tuning until you hit your maximum desired load average. For servers used interactively often, having a load above 3 is way too much to use the server comfortably. For servers used mostly as real servers, a maximum load average of 10 should be acceptable. More than that, and you’ll find yourself needing to reboot the server when experiencing heavy traffic conditions, because no terminal or remote console will respond quickly to commands, and managing the server will be impossible.

How to configure few things in php.ini file for supporting huge traffic

* Enable the compression of HTML by putting in your php.ini:
output_handler = ob_gzhandler
** Switch from file based sessions to shared memory sessions. Compile PHP with the –with-mm option and
set session.save_handler=mm

Configure mysql. Change my.cnf file for better performance.

The database parameters are tuned for systems with 1 GB RAM (for ISO CD images). If you have higher RAM, please change the following in the “my.cnf” MySQL configuration file under /etc/mysql or /etc directory.
For a machine running with 512 MB of RAM, you can set these to:
key_buffer=128M table_cache=1024 sort_buffer=64M read_buffer=2M record_buffer=4M
For a machine running with 1 GB of RAM, you can set these to:
key_buffer=256M table_cache=2048 sort_buffer=128M read_buffer=2M record_buffer=8M
For a machine running with 2 GB of RAM, you can set these to:
key_buffer=512M table_cache=3072 sort_buffer=256M read_buffer=2M record_buffer=8M
For a machine running with 4 GB of RAM, you can set these to:
key_buffer=1G table_cache=4096 sort_buffer=512M read_buffer=2M record_buffer=8M

, , , ,

2 Comments