These instructions will walk you through the process to get Thin up and running with Nginx as the HTTP server.
For a long time the recommended stack for Rails was based around Mongrel and Nginx - it's worth noting there are a variety of deployment methods (covered elsewhere in this Wiki).
Thin uses a combination of libraries from Mongrel, Event Machine and Rack. This combination is (apparently) faster than using Mongrel alone.
Installing Thin should be as simple as:
$ sudo gem install thin
You will also need to accept any dependencies for installation to succeed. To use Thin with Unix sockets you may need to install the EventMachine library:
$ sudo gem install eventmachine --source http://code.macournoyer.com
Each application will have a configuration file - this is similar to having a mongrel_cluster.yml in a Mongrel setup.
There are a number of options for how you configure your applications to be started/stopped - we prefer creating a directory for all Thin instances and then using the init script to control them. This is baked right in - to create the /etc/thin directory and install the init script simply run:
$ sudo thin install
Then, 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
To generate the config file you can run the following from within the root of your application:
$ sudo thin config -C /etc/thin/<config-name>.yml -c <rails-app-root-path> --servers <number-of-threads> -e <environment>
Replace <config-name> with the name of the configuration file, <rails-app-root-path> with the path to the root of your application, <number-of-threads> with the number of Thin processes to be started and <environment> with the environment in which to run the code. For example:
$ sudo thin config -C /etc/thin/myapp.yml -c /var/rails/myapp --servers 5 -e production
Or:
$ sudo thin config -C /etc/thin/myapp.yml -c /var/rails/myapp --servers 5 --socket /tmp/thin.myapp.sock -e production
This will generate a configuration file in the /etc/thin directory we created earlier. This file will contain something similar to:
--- pid: tmp/pids/thin.pid timeout: 30 log: log/thin.log max_conns: 1024 require: [] environment: production max_persistent_conns: 512 servers: 5 daemonize: true socket: /tmp/thin.myapp.sock chdir: /var/rails/myapp
You are now ready to start the thin service (which will start all of the Thin's configured in the /etc/thin directory):
on RedHat-like systems (you will need to substitute the relevant command to start the service on other distros):
$ sudo service thin start
If you already have Nginx installed (perhaps if you're moving over from Mongrel) then you can skip this step.
Some operating systems have binary versions of Nginx available.
http://www.kevinworthington.com/nginx-for-windows/ (0.6.35 at the time of writing) is compiled currently for 32-bit Windows systems only and has been tested for Vista, XP, and 2000.
The version available for your setup can be determined by using
$ apt-cache showpkg nginx
Then, to install, run
$ sudo apt-get install nginx
To install Nginx from source, grab the latest stable version of Nginx (0.6.35 at the time of writing) and build it:
$ curl -O http://sysoev.ru/nginx/nginx-0.6.35.tar.gz $ tar xzvf nginx-0.6.35.tar.gz $ cd nginx-0.6.35 $ ./configure --sbin-path=/usr/local/sbin --with-http_ssl_module $ make $ sudo make install $ sudo chmod +x /usr/local/sbin/nginx
This will put the binary at /usr/local/sbin/nginx (otherwise it will live under /usr/local/nginx/bin)
The configuration file will, by default, live at /usr/local/nginx/conf/nginx.conf - you can change this when building if desired.
We also use an init script to start and stop Nginx - courtesy of Geoffrey Grosenbach and Ryan Norbauer.
Grab the init script and install on your system as a service as before.
The next step is to configure Nginx. The sample configuration will setup a single site using a cluster of Thin's over the unix socket interface. To use this config, ensure the paths are correct for your system and change the hostname and you should be good to go!
Make sure you've started Thin and started Nginx and you should be able to access it over HTTP.
The first port of call is to check your Thins are running:
$ ps -ef | grep -i thin
If you can't see any Thin processes, then check in the logs under the log directory of your Rails application root.
If the Thins are running then the next step is to check Nginx is running:
$ ps -ef | grep -i nginx
If you can't see the Nginx processes, check in the logs, which depending on your configuration might be /var/log/nginx.error.log
If both Nginx and Thin appear to be running it's worth checking that the number of Thins matches the number in your Nginx configuration and also that the access method is the same (i.e. Unix Sockets or TCPIP)
Discussion
I need to make a couple changes to the init script
1. The current release of nginx (0.8.20) has changed the location of the PID file, causing the script http://gist.github.com/54030 to fail. Simply modify it as follows:
<pre>
+ NGINXHOME=/usr/local/nginx
! CONFIGFILE=$NGINXHOME/conf/nginx.conf ! PIDFILE=$NGINXHOME/logs/$NAME.pid
</pre>
2. The script also uses WIN/MAC style CRLF, which makes /bin/sh complain on Linux. Run dos2unix on the file would fix that.
hi there,
“Configure Nginx to talk to Thin” –> Can you please let us know where to save this file to?
thank you :)