Topic: How to install Thin Web Server in Linux in 2 Minutes & 10 Easy Steps

How to install Thin Web Server in a Fedora/RedHat Linux Server in 2 Minutes !!!

This tutorial/tip will show you how to install Thin web server in Fedora/RedHat Linux in 2 Minutes

First lets see if we have Thin installed.
[me@fedora me]$ gem list | grep thin

1.- Lets Install Thin web server.
As root:
[root@fedora ~]# gem install thin

or with sudo:
[me@fedora phonebook]$ sudo gem install thin

To verify if Thin is installed:
[me@fedora me]$ gem list |grep thin
thin (0.8.0, 0.7.1, 0.7.0)

To show the version of Thin:
[root@fedora ~]# thin -v
thin 0.8.0 codename Dodgy Dentist

2.- Lets create a Rails Application

[me@fedora me]$ rails phonebook -d mysql
# Note in Rails 2.0.2 the default database is sqlite.... but I love MySQL make sure to setup properly.

3.- [me@fedora me]$ cd phonebook


4.- For the Quick & Dirty:
thin start -e development -p 3000 -s 5 -d

Where:
-e == Environment in this case development
-p == TCP/IP Port in this case 3000
-s == Instances of the webserver in this case 5
-d == Daemonize to run in background

[me@fedora phonebook]$ thin start -e development -p 3000 -s 5 -d
Starting server on 0.0.0.0:3000 ...
Starting server on 0.0.0.0:3001 ...
Starting server on 0.0.0.0:3002 ...
Starting server on 0.0.0.0:3003 ...
Starting server on 0.0.0.0:3004 ...
[me@fedora phonebook]$

We just start 5 instances of the Thin Web Server on ports [3000..3004]
Open your browser on:
http://127.0.0.1:3000
http://127.0.0.1:3001
http://127.0.0.1:3002
http://127.0.0.1:3003
http://127.0.0.1:3004

All the PID's are located in: phonebook/tmp/pids/
[me@fedora phonebook]$ ls -l tmp/pids/
-rw-r--r-- 1 me me    5 Apr  7 21:48 thin.3000.pid
-rw-r--r-- 1 me me    5 Apr  7 21:48 thin.3001.pid
-rw-r--r-- 1 me me    5 Apr  7 21:48 thin.3002.pid
-rw-r--r-- 1 me me    5 Apr  7 21:48 thin.3003.pid
-rw-r--r-- 1 me me    5 Apr  7 21:48 thin.3004.pid

4a.- I'm lazy and create a very simple shell script to stop this running processes.
my_stop.sh

[me@fedora phonebook]$ vi my_stop.sh

####################
#!/bin/bash

this_path=`pwd`
pids_path="$this_path/tmp/pids/*.pid"

for thin_pid in $pids_path
do
    echo "Stoping Thin Instance $thin_pid"
    thin stop -P $thin_pid
done
####################

4b.- Lets allow my_stop.sh to run:
[me@fedora phonebook]$ chmod 755 my_stop.sh

Lets stop the daemonize Thin web server.
[me@fedora phonebook]$ ./my_stop.sh
Stoping Thin Instance /mywww/rails/phonebook/tmp/pids/thin.3000.pid
Sending QUIT signal to process 24610 ...
Stoping Thin Instance /mywww/rails/phonebook/tmp/pids/thin.3001.pid
Sending QUIT signal to process 24614 ...
Stoping Thin Instance /mywww/rails/phonebook/tmp/pids/thin.3002.pid
Sending QUIT signal to process 24618 ...
Stoping Thin Instance /mywww/rails/phonebook/tmp/pids/thin.3003.pid
Sending QUIT signal to process 24622 ...
Stoping Thin Instance /mywww/rails/phonebook/tmp/pids/thin.3004.pid
Sending QUIT signal to process 24626 ...
[me@fedora phonebook]$

[Note the PID's surely will be different in your computer, but you get the idea ;-) ]

4c.- Just to verify there is no more Thin PID files in tmp/pids:
[me@fedora phonebook]$ ls -l tmp/pids/
total 0

This is a quick & dirty Tip on how to get going with Thin, but is not a scalable solution.
In order to make Thin webserver start/stop automatically just like any other Fedora/RedHat service follow the
following steps:

5.- Once Thin gem is installed lets install it into the FileSystem.

As root execute:
[root@fedora phonebook]# thin install
or with sudo:
[me@fedora phonebook]$ sudo thin install

>> Installing thin service at /etc/init.d/thin ...
mkdir -p /etc/init.d
writing /etc/init.d/thin
chmod +x /etc/init.d/thin
mkdir -p /etc/thin

To configure thin to start at system boot:
on RedHat like systems:
  sudo /sbin/chkconfig --level 345 thin on
on Debian-like systems (Ubuntu):
  sudo /usr/sbin/update-rc.d -f thin defaults
on Gentoo:
  sudo rc-update add thin default

Then put your config files in /etc/thin
[root@fedora phonebook]#

The folks at Thin did a great job here, few things will happen:

Should install /etc/init.d/thin for you. It works a lot like mongrel_cluster. It looks in /etc/thin for your thin config yaml files.

All the configuration yaml configuration files will be located at:
/etc/thin/rails_app1.yml
/etc/thin/rails_app2.yml
...
/etc/thin/rails_app3.yml

As many application as you want and your RAM can allow ;-)  [ Be careful here each instance will take about 20MB Ram and for VPS with 64Bit will take around 40M. So if you create about 10 instances at 20MB/instance will be 200MB very easy, lets said you configure 5 rails applications you will eat 1GB of RAM in no time. so use the instances wisely and conservative, its not about quantity is about quality smile.

6.- I recommend to use it similar to how mongrel cluster configuration is handled.

Create a thin.yml in your config folder of your rails app and then make a symlink to your /etc/thin folder such as /etc/thin/rails_appN.yml. This yaml file will look similar to mongrel_cluster with a few differences. Here's a sample configuration:
File: [ phonebook/config/thin_cluster.yml ]
---
user: me
group: me
chdir: /home/me/phonebook
log: log/thin.log
port: 3000
environment: development
pid: tmp/pid/thin.pid
servers: 5

Note: Jack Chu reports to be using a unix socket instead of a port and it works very nicely with nginx. "chdir" is the path of your rails app (similar to "cwd" in mongrel) and there's also some subtle differences like log vs log_file and pid vs pid_file. If you want to use a port instead of a socket, just use port: "3000" and so on.

Jack Chu - Sample configuration.
File: [ phonebook/config/thin_cluster.yml ]
---
user: www-data
group: www-data
chdir: /var/www/test.com/apps/redmine
log: log/thin.log
socket: /var/run/thin/redmine.sock
environment: production
address: 127.0.0.1
pid: /var/run/thin/redmine.pid
servers: 1

7.- Ok so we have our thin_cluster.yml file lets create the Symbolic Link to the /etc/thin directory:

As root
[root@fedora phonebook]# ln -s /home/me/phonebook/config/thin_cluster.yml /etc/thin/phonebook.yml
Or as a regular user using sudo:
[me@fedora phonebook]$ sudo  ln -s /home/me/phonebook/config/thin_cluster.yml /etc/thin/phonebook.yml

Note: Replace this path: [ /home/me/phonebook ] with the absolute path & name of your rails application.

8.- Once we have Thin installed, the properly configuration files thin_cluster.yml with their symbolic links to the /etc/thin directory lets activate Thin on start for Fedora:

As root:
[root@fedora phonebook]# /sbin/chkconfig --level 345 thin on

Just to verify that everything looks good:
[root@fedora phonebook]# /sbin/chkconfig --list |grep thin
thin            0:off   1:off   2:on    3:on    4:on    5:on    6:off

9.- Great now we can start Thin on boot or just like any other Fedora/RedHat service:

[root@fedora phonebook]# /etc/init.d/thin start

[root@fedora phonebook]# /etc/init.d/thin start
[start] /etc/thin/phonebook.yml ...
Starting server on 0.0.0.0:3000 ...
Starting server on 0.0.0.0:3001 ...
Starting server on 0.0.0.0:3002 ...
Starting server on 0.0.0.0:3003 ...
Starting server on 0.0.0.0:3004 ...

Great go a head and open your browser in any of the following URL's:
http://127.0.0.1:3000
http://127.0.0.1:3001
http://127.0.0.1:3002
http://127.0.0.1:3003
http://127.0.0.1:3004

Pretty Cool !!!.  You just create a Thin_Cluster in about 2 Minutes !!!.

[root@fedora phonebook]# /etc/init.d/thin stop
[stop] /etc/thin/phonebook.yml ...
Stopping server on 0.0.0.0:3000 ...
Sending QUIT signal to process 24833 ...
>> Exiting!
Stopping server on 0.0.0.0:3001 ...
Sending QUIT signal to process 24837 ...
>> Exiting!
Stopping server on 0.0.0.0:3002 ...
Sending QUIT signal to process 24841 ...
>> Exiting!
Stopping server on 0.0.0.0:3003 ...
Sending QUIT signal to process 24845 ...
>> Exiting!
Stopping server on 0.0.0.0:3004 ...
Sending QUIT signal to process 24849 ...
>> Exiting!

Note you can have as many symbolic links in /etc/thin pointing to different cluster configuration files thin_cluster.yml. thin start or stop will apply to all the applications.

10.- The only thing left to do is to setup a Virtual Host Directive in Apache and use te load_balancer option to distribute the pages on the cluster. Use the following configuration as a reference:

Assuming we try to setup the domain railsapplicationtip.com with the IP [123.123.123.123] using Apache Cluster Balancer we called: my_railsapp1.
URL: www.railsapplicationtip.com
IP: 123.123.123.123
Cluster Balancer: my_railsapp1

Edit the httpd.conf file and insert the following configuration:
########################################
<Proxy balancer://my_railsapp1>
BalancerMember http://127.0.0.1:3000
BalancerMember http://127.0.0.1:3001
BalancerMember http://127.0.0.1:3002
BalancerMember http://127.0.0.1:3003
BalancerMember http://127.0.0.1:3004
</Proxy>

<VirtualHost 123.123.123.123:80>
ServerAdmin webmaster@railsapplicationtip.com
ServerName railsapplicationtip.com
ServerAlias www.railsapplicationtip.com
RewriteEngine on
ProxyPass / balancer://my_railsapp1/
ProxyPassReverse / balancer://my_railsapp1/
ErrorLog logs/railsapplicationtip-error_log
CustomLog logs/railsapplicationtip-access_log common
</VirtualHost>
########################################

Start apache:
[root@fedora /]# /etc/init.d/thin start
[root@fedora /]# /etc/init.d/apache start

If you have the properly DNS configuration you should be able to open your browser at:

http://railsapplicationtip.com

Your Rails applications is working with a Thin Cluster and being allocated with the Apache Load_Balancer Module !!!.

Dinooz.
Brains R Like books only work when they R Open.

Re: How to install Thin Web Server in Linux in 2 Minutes & 10 Easy Steps

Awesome ! thanks !

Re: How to install Thin Web Server in Linux in 2 Minutes & 10 Easy Steps

Hi ,

I see you posted great instruction to install webserver

But I recommend that you should highlight the command for subscriber to easily use

Below is also one post for quickly virtual web server installation on linux :

http://vnluck.com/2012/01/how-to-setup- … 0-minutes/

Re: How to install Thin Web Server in Linux in 2 Minutes & 10 Easy Steps

Please help, I am trying to set up a rails app on Ubuntu 12.04, ruby, gems, rails, apache, thin, are all installed. If I start a thin cluster I can go to the website and the welcome page shows

domain.com:3000
domain.com:3001
domain.com:3002

all work so I know that thin is doing its thing, and I can get apache without proxy to load the index.htm (though without the assets working correctly as expected).

However I can't seem to get apache to actually pass the request off to the thin servers! I get a soft 404 (page can't be found) error or the It works page, and I don't know why.

here is the sites-available file

<VirtualHost *:80>

    ServerName myapp.mydomain.com
    DocumentRoot /home/sim/public_html/railsapp

    <Proxy balancer://thinservers>
        BalancerMember http://127.0.0.1:3000
        BalancerMember http://127.0.0.1:3001
        BalancerMember http://127.0.0.1:3002
    </Proxy>

    ProxyPreserveHost On
    ProxyPass /assets !
    ProxyPass / balancer://thinservers/
    ProxyPassReverse / balancer://thinservers/

    <Proxy *>
     Order deny,allow
     Allow from all
    </Proxy>
</VirtualHost>

Last edited by acooper86 (2012-07-01 15:46:54)