Category Archives: Rails

Avoid Initial Slow Loading Times of Rails apps under Apache & Passenger

If you have enough RAM on your server, then you will probably want to avoid the lengthy booting times you experience after an application idles.

There are 2 solutions I could find used to solve this problem:

1. Use a cron job to access the application every couple of minutes to keep it in memory.

One of the ‘raw’ solutions used are setting up a cron job to automatically access the application every few minutes and therefore keep it in memory.

crontab -e

And use nano (or your favorite editor) to add this at the end of the cron jobs:

*/5 * * * * wget http://www.example.com > /dev/null

This issues a wget request every 5 minutes and trashes the response.

2. Setup passenger not to automatically kill the rails processes.

But, on the other hand, if you have access to the server’s configuration, a better approach is to modify a few of Passenger’s constants:

PassengerMaxPoolSize 30 (use 15 if you have a machine with 1GB, 30 if you have 2GB of RAM, etc.). This will enable more processes to be spawned if necessary.

PassengerPoolIdleTime 0 (using 0, application instances will not be shutdown unless it’s really necessary – when the available resources on the server are low)

On an Ubuntu machine, you would have to edit the /etc/apache2/httpd.conf file by adding these two lines:

PassengerMaxPoolSize 30
PassengerPoolIdleTime 0

Restart apache

sudo /etc/init.d/apache2 restart

And you should not have that slow boot problem anymore.

*3. Use nginx + thin

Read More

Windows Mac-alike Rails Development Environment

As I already been using e-texteditor (for quite a few years), which is a clone of Mac-only Textmate, I thought to give setting up a mac-alike rails development environment a go.

Since I had a ton of compatibility problems when developing under vanilla Windows (with Ruby1.9 and rails3, a lost of gems failed to work, etc.), I hope the Unix-path is much smoother.

Let’s see how we can get it going…

Step 1: Install cygwin

You will need in to select the following in addition to the the default selected configuration (italic = requirements for the Nokogiri gem):

  • make
  • gcc
  • libiconv
  • openssl
  • ruby
  • libiconv
  • libxml2
  • libxml2-devel
  • libxslt
  • libxslt-devel

Step 2: Use Console with Cygwin by default

Console was one of the tips I didn’t know about ( http://sourceforge.net/projects/console/ ), it’s much nicer than the old ‘cmd’, and most of all you can set it up to start a cygwin console by default

Quote from http://garbageburrito.com/blog/entry/391/a-macesque-rails-development-environment-on-windows:

You’ll want to setup cygwin as a Console Tab. Just go into the settings and click “Add” under “Tabs”. Use the following as the “Shell”.

c:\cygwin\bin\bash --login -i

If you want it to automatically open cygwin when you start Console, just move it to the top of the Tabs list.

Just in case you encounter errors with

rvm install 1.8.7

Checkout this post about readline: http://rvm.beginrescueend.com/packages/readline/.

rvm package install readline

Step 3a: Ruby 1.8.7

Ruby 1.8.7 is already installed in cygwin, so if you want to use this one, you’re ready to go.

Step 3b: If you want RVM

Follow the instructions from http://rvm.beginrescueend.com/rvm/install/

bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )

Modify ~/.bash_profile by appending at the very end, after all path loads etc:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm"  # This loads RVM into a shell session.

Install ruby with

rvm install 1.8.7
rvm install 1.9.2

Step 3c: Compile Latest Ruby (without RVM)

Download the Ruby 1.9 source from http://www.ruby-lang.org/en/downloads/ (downloaded the latest stable Ruby 1.9.2-p136 at the time of writing)

tar xvf ruby-1.2.2-p136.tar.gz
cd ruby-1.9.2-p136
./configure

(lasts ~5-10 minutes)

make -j 3

(use 3 if you have 2 cores, 5 if you have 4 cores, etc.)

make install
ruby -v

to confirm the new version

(find more in-depth instructions here: http://www.curphey.com/2010/05/installing-and-configuring-ruby-1-9-from-source-using-cygwin/)

Step 4: Avoid the default installation of ri and rdoc from ruby gems

To avoid the installing of documentation (as I usually check the online documentation):
create .gemrc file in your cygwin home directory (~) with the following contents:

gem: --no-ri --no-rdoc

Step 5: Install sqlite

Downloaded the latest sqlite tarball containing the amalgamation for SQLite 3 from http://www.sqlite.org/download.html.

tar xvf sqlite-autoconf-3070400.tar.gz
cd sqlite-autoconf-3070400
./configure
make -j 3

(use 3 if you have 2 cores, 5 if you have 4 cores, etc.)

make install

Step 6. Install Rails 3

gem install rails

Step 7. Create & start the first Rails3 app

rails new testapp
cd testapp
bundle install

(this will install sqlite3-ruby gem)

rails s

That’s it!

Read More