Running under THIN

Posted by John
on Monday, 03 March 2008

As I really want to push THIN (the light mongrel rails server) and see how it really stands in a production environment I've decided to run this site on it for a week and see how the thing handles.

For the inquisitive, this is the command I ran to start it up...

thin start --port 8000 --environment production --servers 2

Starts 2 THIN rails servers at ports 8000 and 8001 under production mode.

Currently they're consuming about 64mb of memory (less than mongrel's 120mb).

Let me know how you think it handles.

Update

So far running pretty sweetly, much more responsive than Mongrel and the memory usage is down a heck of a lot. Considering using ArchLinux for my server box, have liked using that linux distribution.

'Thin' Rails Server + Fedora Cleanup

Posted by John
on Wednesday, 27 February 2008

Thin

Thanks to Thomas Glasgow, I've been told of a new lighter Mongrel web server called THIN. It's more-so a cut-down version of the Mongrel Web Server, with the added power of an Event machine and Rack parser.

Haven't tested it out, but intend to try it out on one of my apps, install it with...

sudo gem install thin

Thin - Rake Task

With the above I found a startup script similar to Monrel's .yml config file.

namespace :thin do
  namespace :cluster do

    desc 'Start thin cluster'
    task :start => :environment do
      `cd #{RAILS_ROOT}`
      port_range = RAILS_ENV == 'development' ? 3 : 8
      (ENV['SIZE'] ? ENV['SIZE'].to_i : 4).times do |i|
        Thread.new do
           port = ENV['PORT'] ? ENV['PORT'].to_i + i : ("#{port_range}
%03d" % i)
          str  = "thin start -d -p#{port} -Ptmp/pids/thin-#{port}.pid"
          str += " -e#{RAILS_ENV}"
          puts "Starting server on port #{port}..."
          `#{str}`
        end
      end
    end

    desc 'Stop all thin clusters'
    task :stop => :environment do
      `cd #{RAILS_ROOT}`
      Dir.new("#{RAILS_ROOT}/tmp/pids").each do |file|
        Thread.new do
          if file.starts_with?("thin-")
            str  = "thin stop -Ptmp/pids/#{file}"
            puts "Stopping server on port #{file[/\d+/]}..."
            `#{str}`
          end
        end
      end
    end

  end
end

copy and paste it into a .rake task in your apps /lib/tasks directory.

then fire with...

rake thin:cluster:start
rake thin:cluster:stop

and...

rake thin:cluster:start RAILS_ENV=production SIZE=10 PORT=8000
  • SIZE = cluster size (2 is usually enough)
  • RAILS_ENV = environment (production, development or test)
  • PORT = port range to start from (for 2 clusters it'll be 8000 and 8001)

Thanks to Stephen Celis for this.

Fedora Package Errors and Cleanups

Lately i've run into some problems with my Fedora package manager, inconsistent files and dropped repositories. However I think I've managed to cleanup this problem by first cleaning up my package managers metadata, db cache and doing a general tidyup with...

sudo yum clean all
sudo yum clean metadata
sudo yum clean dbcache

Then giving the once-over of my RPM repository,

sudo rpm -vv --initdb

This looked ok, but I wanted to make sure so I rebuilt my RPM repository,

sudo rm -f /var/lib/rpm/__db*
sudo rpmdb -vv --rebuilddb

Now looking a little better,

Want to install Beryl + Compiz to get the 3D effects and cubed desktop next.

Update on Beryl

After hunting around I found that Beryl has been dropped in favour of Compiz, so don't go hunting for it because you won't find it.

Linux mail, mongrel and others

Posted by John
on Sunday, 17 February 2008

Software Install Commands

sudo aptitude install postfix
sudo aptitude install sendmail

sudo aptitude remove postfix
sudo aptitude remove sendmail

install telnet (not installed by default)

sudo aptitude install telnet

Mail Commands

show pending mail in queue

mailq

access mail system

mail

pending mails stored in /var/spool

Sending Mail Manually with Telnet

telnet localhost 25

ehlo localhost
mail from: root@localhost
rcpt to: fmaster@localhost
data
Subject: hello
some content
.
quit

...the dot marks the end of the email

SendMail Commands

sudo /etc/init.d/sendmail start
sudo /etc/init.d/sendmail stop
sudo /etc/init.d/sendmail restart

config files saved in /etc/mail/config.cf

Mongrel Cluster Commands

mongrel_rails cluster::start
mongrel_rails cluster::stop
mongrel_rails cluster::restart

MySQL login

login to server

mysql -u root -p

Shutdown Server Gracefully

shutdown now and reboot

shutdown -r now

Login as Another

su anotheruser

Show Pending Jobs

jobs

Mongrel Cluster Restart

mongrel_rails cluster::restart -C /var/www/apps/[myapp]/config/mongrel_cluster.yml

replace [myapp] with your apps dir

CRONTAB + Mongrel - fire on restart

add above line to /home/deploy/start.sh

crontab -e

then add...

@reboot (sh /home/deploy/start.sh) > /dev/null 2>&1

save and should fire + restart on reboot

Kill Processes

find if firefox is running...

ps aux | grep firefox

kill firefox...

killall firefox

or specifically...

kill 19919  <- processid

NGINX

sudo /etc/init.d/nginx start
sudo /etc/init.d/nginx stop
sudo /etc/init.d/nginx restart

Need Xcode to Build Mongrel on Leopard

Posted by John
on Friday, 14 December 2007

Quick note, you'll need the latest build of Xcode (which comes with the Apple GCC compiler) to build mongrel server on Leopard.

It being all super 64-bit OS'ey ;-)

Get this from Apple Developer Connections, using your Apple Account.

Note: big download, around 600mb so get a coffee while you wait,

Once installed, run this in Terminal (open a new window with applekey+N),

sudo gem install mongrel

Not forgetting the ever-lovely deprec,

sudo gem install deprec

(which comes with capistrano 1.4.1)

And a of couple updates with...

sudo gem install rails
sudo gem update --system
sudo gem install tzinfo
sudo gem install RedCloth

(p.s. you don't need SSHKeyChain with Leopard, so skip installing that)

Enjoy,

How-To: Multiple Ruby Apps on the Same Box

Posted by John
on Friday, 24 August 2007

So you’ve deployed your first app to your nice lil’ Linux box via Capistrano, wahey!!

Now presuming you don’t really want to dedicate each server to one app alone, how would you get more than one app on there?

Here’s how,

Apache 2 .Conf

remote onto your server and edit the apache2 .conf file

/usr/local/apache2/conf/httpd.conf

  • look for a line saying ServerName
  • append the server’s IP address to the end of this
  • un-comment it

now when you try restarting apache remotely it’ll know where it is.

next check down the bottom of the file for:

  • Listen 80
  • Include conf/apps/
  • NameVirtualHost * :80

this tells Apache which port to listen to for incoming http requests and to load the config files in the conf/apps/ dir on startup.

Editing your Apps Deploy.rb

Now before you deploy your second app to the new server you must make sure it’ll use a different proxy port. Now by default your first app’l have used 8000 and 8001 so the obvious choice is to use 9000 and 9001.

So, navigate to your second ruby apps /config/ dir and edit it’s deploy.rb file

secondapp/config/deploy.rb

  • first off, you can copy & paste bits from your first apps deploy.rb to make your second, just remember to change the svn repository location to default (as you’ll make your own) and change your apps name.
  • look for => set :apache_proxy_port, 8000 and change it to 9000

deploy your 2nd app

in your second apps dir using the capistrano + deprec gems,

  • cap deprec_setup
  • cap setup_scm <= this’ll give you a new svn location for your app, add it to your deploy.rb file
  • cap deploy_with_migrations
  • cap restart_apache

afterwards you should have 2 ruby apps running on the same server!

If you get problems…

Now life isn’t perfect so chances are you’re gonna run into problems, so here’s some common commands and gotcha’s to help you,

Mongrel Complains .PID files already exist!

  • …ssh in and goto /var/www/apps/secondapp/current/tmp/ and run: rm *.pid

How Do I Re-Start Mongrel?

  • …navigate to app root & run: cap restart_mongrel_cluster

Second App keeps pointing to first, ARGH!!!

  • …ssh in and goto /usr/local/apache2/conf/apps/
  • …run: nano secondapp.conf
  • …make sure <VirtualHost *:80> and it’s balancemember is using 9000 and 9001

Restart Server?

  • …ssh in and run shutdown -r now

Restart Mongrel + Apache?

  • …navigate to first app and run: cap restart_mongrel_cluster
  • …goto second app my run: cap restart_mongrel_cluster
  • …run: cap restart_apache

If you still get problems drop me a line and i’ll try to help,

Best of luck,

John.