Friday, May 14, 2010

Serving mercurial repositories with SCGI and Cherokee


I made a quick hack to be able to server mercurial repositories with SCGI served by Cherokee. I guess it would also work with other web servers, but I haven't tested it.

Requirements
  • Cherokee Web Server
  • Mercurial
  • python-flup
If you are using Debian it's as simple as

# aptitude install cherokee
# aptitude install mercurial
# aptitude install python-flup

Installing the Mercurial SCGI server

Fetch the Mercurial SCGI server here and copy it to a directory in your webserver. Change the owner and group if required and add the executable bit using chmod

Configuration options

The Mercurial SCGI server can be configured by using the following options:

  • HGWEBCONFIGPATH - Defines the path where the hgweb.config is located. If not defined, the default will be the directory where the Mercurial SCGI server file (hgwebdir-scgi) file is located
  • SCGIPORT - Defines the port in wich the SCGI server will listen. Defaults to 4000
  • BINDADDRESS - Defines the name/ip in which the SCGI server will listen. Defaults to localhost.
The format and contents of the hgweb.config file are documented here

Configuring Cherokee

Once you have the basics installed it's time to setup cherokee to use the SCGI handler to server the mercurial repositories.

Start your cherokee-admin and login the admin web page.

In this case we will run the SCGI server in the same machine and we will make Cherokee responsible for starting and stopping the server. For that you'll have to create a new information source. Click in Sources (top panel toolbar) and then add a new information source with localhost:4000 in the Connection field.

Change the Type from Remote Server to Local Interpreter. Fill the interpreter field with the path and name of the Mercurial SCGI server (for example /usr/local/bin/hgwebdir-scgi)

Uncheck Inherit Environment and then add a HGWEBCONFIGPATH variable with the directory where hgweb.config is located as value (for example /var/hg)

Now go to vServers to create a new rule to redirect requests to the SCGI server.

Click on the Behavior tab and then in Rule management use the Directory Rule Type and set it to the desired path (example /hgrepos). Then click on the Handler tab and select the SCGI handler.

Select a Balancer Policy and then choose the previously created Information Source
Click on Save and then choose Graceful restart and voila you'll be serving your mercurial repositories using SCGI.

As an extra you can add a new rule to serve the static content using the high performance Static content handler.

Enjoy!

Tuesday, May 11, 2010

Green Computing with Cherokee Web Server

Cherokee 1.0 was released yesterday after many years of constant polishing and optimizations. As you might already know if you are reading this entry, Cherokee focuses in two main areas.

  • Performance/low resource consumption
  • Easy to manage. If you are a casual user of a web browser you'll appreciate how easy is to configure it via it's web administrator user interface.
Following the trend of doing more with less, you might be interested in using Cherokee to serve more traffic using the same hardware, using less CPU, and thus less power, or maybe using your RAM in a more efficient way thanks to Cherokee's advanced cache.

So it's time to save the planet...

In this post I'll explain how to use Cherokee's features to improve your Pixelpost installation performance while reducing the usage of your server's resources.

First I recommend to use Cherokee's FCGI implementation to connect to your php-cgi process instead of using a simple cgi. The reason is simple, your server will not need to spawm a new process to serve each request, reducing the response latency and reducing the usage of your server's precious resources (like CPU).

The second one, that I think it's less obvious to many people is to use one of Cherokee's most valuable features. The I/O Cache used to serve static content.
When you configure Pixelpost with any web server (and this applies to all web applications), what you do is to set a rule in the web server to forward all requests from a given URL to the PHP (or any other technology) interpreter. In the case of Pixelpost there are some resources that are static (your photos being the most obvious ones) that are fetched by the PHP interpreter, forwarded to the web server, and then delivered to the final user (your visitor's web browser). Setting the following rules will improve the performance of your site reducing the number of CPU cycles needed to serve the content.

Add a Directory rule for /images and /templates with a Static Content handler and be sure to check the User I/O cache checkbox.

This will make Cherokee serve directly the static content (will bypass the PHP interpreter) and use a content cache that uses the same advanced algorithm that is used in Opensolaris' ZFS. You'll reduce the number of lookups and reads from your harddrive (again reducing latency and saving power) for free.

Now, I haven't posted any screenshots about the process. I challenge you to try to do it yourself and discover how easy it is to use the user interface. (hint. You'll find very useful resources in http://cherokee-project.com).

Go Green Cherokee!