[ start | index | login ]

typo cluster

Created by david. Last edited by domingos, 344 days ago. Viewed 1,038 times. #9
[diff] [history] [edit] [rdf]
labels
attachments

Configure your Rails environment:

This Typo installation builds on the RimuHosting Ruby on Rails stack. Instructions (and a script) to set this up can be found at:

>>http://bliki.rimuhosting.com/space/knowledgebase/linux/miscapplications/ruby+on+rails

In addition to the standard stack, I recommend installing the mongrel_cluster and daemons gems which you can do via the following command:

[root@test ~]# gem install mongrel_cluster daemons --include-dependencies

Download Typo

Download the latest stable version of Typo from: >>http://rubyforge.org/frs/?group_id=555&release_id=10286

cd /var/www/
[root@test www]# wget http://rubyforge.org/frs/download.php/18342/typo-4.1.tgz
[root@test www]# tar xvf typo-4.1.tgz

Configure Typo database

I'll only setup a production database alias because this is for production use and I don't anticipate needing either the test nor development database definitions.

[root@test www]# vi /var/www/typo-4.1/config/database.yml
production:
        adapter: mysql
        database: typo
        username: DBUSER
        password: PASSWORD
        host: localhost

Take the info from the above configuration file and create a Typo database in MySQL: (Make sure to use the same credentials you supplied in the configuration file)

[root@test www]# mysql -u root
mysql> create database typo;
mysql> GRANT ALL PRIVILEGES ON typo.* to 'DBUSER'@'localhost' IDENTIFIED BY 'PASSWORD';

After you've created the database with the proper credentials, you'll need to import the Typo schema (and initial data) into your database. You'll use the built-in rake task to do this:

[root@test typo-4.1]# cd /var/www/typo-4.1/
[root@test typo-4.1]# rake migrate

Configuring Mongrel Cluster

Having Apache run in front of Mongrel is a great way to serve your application. You get the benefit of being able to take advantage of the various Apache modules and the convenience of Mongrel serving your Rails bits. :)

One drawback to Mongrel is that it will only service a single request at time. This is only a problem when you're servicing high loads with many concurrent requests. You can help to mitigate this issue by running multiple instances of Mongrel and then passing incoming connections around your cluster. For this reason, this configuration will reflect running cluster, but it will only specify a single Mongrel instance initially.

Each Mongrel process you run will consume additional memory (usually between 15MB and 45MB) and with that in mind, you'll want to instantiate only as many Mongrel instances as you need. (more on that at the end of this article)

Generating your mongrel configuration file is dead simple with the mongrel_cluster gem we installed earlier. To generate your config, issue the following command from the root of your Typo installation:

[root@test typo-4.1]# mongrel_rails cluster::configure -e production -p 8000 -a 127.0.0.1 -N 2 -c /var/www/typo-4.1

The above will run 2 Mongrel instances (-N 2) in production mode (-e production) on ports 8000 and above (-p 8000) and will only listen on the loopback address (-a 127.0.0.1).

Setting up an init.d script:

The mongrel_cluster gem comes with an init script for you to use. We'll create a symbolic link to that and then modify it to be executable:

[root@test typo-4.1]# ln -s /usr/lib/ruby/gems/1.8/gems/mongrel_cluster-0.2.1/resources/mongrel_cluster /etc/init.d/mongrel_cluster
[root@test typo-4.1]# chmod +x /etc/init.d/mongrel_cluster

The init script will load mongrel cluster configs found in /etc/mongrel_cluster. We'll create that directory and then create a symbolic link for our typo mongrel config so that it will be loaded when the init script is run:

[root@test typo-4.1]# mkdir /etc/mongrel_cluster
[root@test typo-4.1]# ln -s /var/www/typo-4.1/config/mongrel_cluster.yml /etc/mongrel_cluster/typo.yml

Apache configuration

What good is a mongrel instance that only listens on the local port? Well, we'll need to tell Apache how to handle incoming transactions. First we'll create a mod_proxy_balancer configuration (note this is only available in Apache 2.2+).

[root@test typo-4.1]# vi /etc/httpd/conf.d/proxy_balancer.conf
<Proxy balancer://mongrel_cluster>
    BalancerMember http://127.0.0.1:8000
    BalancerMember http://127.0.0.1:8001
</Proxy>

The BalancerMember definition should match your mongrel configuration (the port bit at the end). If you have specified multiple mongrels, you'll need to add a BalancerMember for each mongrel. If you're running 4, starting at port 8000, you'll need a member for 8001, 8002 and 8003.

Apache virtual host definition

The following virtual host definition will pass requests through to Mongrel where appropriate. In the case of cached pages and other static content (images/stylesheets/javascript), it will service those requests directly through Apache (which should help performance).

<VirtualHost *:80>
        ServerName YOURDOMAIN

<Directory "/var/www/typo-4.1/public"> Options FollowSymLinks AllowOverride None Order allow,deny Allow from all </Directory>

RewriteEngine On

# Rewrite index to check for static RewriteRule ^/(stylesheets|images|javascripts|favicon.ico) /$1 [QSA] RewriteRule ^/$ /index.html [QSA]

# Rewrite to check for Rails cached page RewriteRule ^([^.]+)$ $1.html [QSA]

# Redirect all non-static requests to cluster RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]

# Logging ErrorLog logs/yourdomain-error_log CustomLog logs/yourdomain-access_log common </VirtualHost>

Benchmarking

Typo implementing some caching which keeps your server from regenerating content you serve over and over. The first number below shows the speed of the above setup running on our MiroVPS2 (160MB) VPS:

Cache Enabled:
[root@none ~]# httperf --server YOURDOMAIN --port 80 --uri / --num-conns 2000
Reply rate [replies/s]: min 44.2 avg 48.4 max 50.6 stddev 2.4 (8 samples)

Cache Disabled: [root@none ~]# httperf --server YOURDOMAIN --port 80 --uri / --num-conns 2000 Reply rate [replies/s]: min 1.4 avg 1.8 max 2.0 stddev 0.1 (228 samples)

The above demonstrates the power of caching with this setup! If 4x requests per second isn't fast enough for you, you can simply add more mongrel instances to your custer as described above and you should be able to handle even more.

no comments | post comment
Powered by snipsnap.org Found a mistake in a howto? Let us know via an email to p.blikibugs at rimuhosting com.