domenica 31 maggio 2009

Hello World Wide Web: Part 2, deploying and testing

The binary helloweb is ready, now it's time to deploy it on the lighttpd web server and test it.

First, for convenience, we could move it to a directory that we decided to reserve to fastCGIs, for example /var/fastcgi.

Second we must instruct lighttpd to recognize helloweb has a fastCGI, so vi must set it up correctly; have a look at the directory /etc/lighttpd, it contains two other directories: conf-available and conf-enabled, the first one contains a collection of possible configuration files for different modules of the web server, the second one those effectively used. All the information about fastCGIs are in the file 10-fastcgi.conf and putting a symbolic link inside the conf-enabled directory makes this files actually used (after a web service restart).

A common 10-fastcgi.conf file looks like this:

server.modules += ( "mod_fastcgi" )

fastcgi.server = ( "/fastcgi/helloweb" =>;
((
"bin-path" => "/var/fastcgi/helloweb",
"socket" => "/tmp/helloweb.socket",
"check-local" => "disable"
)))

The line server.modules += ( "mod_fastcgi" ) loads the fastcgi module, the rest of the file instructs lighttpd about the usage of the fastcgi created in the previous post.
The instruction "/fastcgi/helloweb" informs the web server that an http request of URLs starting with /fastcgi/helloweb must be redirect to the fastcgi specified by the parameter "bin-path" (i.e. /var/fastcgi/helloweb).

Once modified the file, we must create a symbolic link to it in the directory conf-enabled:

ln -s /etc/lighttpd/conf-available/10-fastcgi.conf /etc/lighttpd/conf-enabled/10-fastcgi.conf

then we must restart the web server:

/etc/init.d/lighttpd restart

Now it's time to test the program: point your browser to the URL http://ip-of-your-server/fastcgi/helloweb where ip-of-your-server is the ip of the server running lighttpd (if you don't know your ip, simply digit ifconfig at the prompt). If everything went well an exciting ""Hello World Wide Web" will appear.

Note: I had some problem in deploying the fastcgi because initially I put "fastcgi.server =" and "( "/fastcgi" =>" in two different lines in the configuration file, joining them in a single line made everything working well.

References: http://redmine.lighttpd.net/wiki/lighttpd/Docs%3AModFastCGI

martedì 19 maggio 2009

Hello World Wide Web: Part 1, editing and compiling

In the last posts we set up the programming environment, the web server is running and finally we are ready to start with the first FastCGI application: what better than a web version of the classic Hello World, for the occasion renamed Hello World Wide Web? Let’s go!

First we must include the library fcgi_stdio needed by every FastCGI application:

#include “fcgi_stdio.h”

This isn’t the only possibility, the library fcgiapp is an alternative. What are the differences between fcgi_stdio and fcgiapp? The fcgi_stdio library is a layer on top of the fcgiapp library that implement also CGI functionalities, so an application developed with fcgi_stdio can run indifferently in CGI or FastCGI environment without recompilation.

According to the official FastCGI site using the library fcgi_stdio is preferable because the fcgi_stdio library offers several advantages:

Simplicity: there are only 3 new API calls to learn

Familiarity: If you are converting a CGI application to FastCGI, you will find few changes between CGI and FastCGI. We designed our library to make the job of building a FastCGI application as similar as possible to that of building a CGI application: you use the same environment variables, same techniques for parsing query strings, the same I/O routines, and so on.

Convenience: the library provides full binary compatibility between CGI and FastCGI. That is, you can run the same binary as either CGI or FastCGI.

The fcgiapp library is more specific to FastCGI, without trying to provide the veneer of familiarity with CGI.

After the #include, as usual for every C program, there is the main() function:

int main( int argc, char *argv[] ) { ... }

that is composed by a while loop waiting for every HTTP requests:

while( FCGI_Accept() >= 0 ) { ... }

FCGI_Accept function accepts a new request from the HTTP server (i.e. Lighttpd) and creates a CGI-compatible execution environment for the request.

Inside this while loop we must put the real program that generate the HTTP answer. An HTTP answer to a GET or a POST request must be composed of a header and the content (differently from other most popular languages for the web it's mandatory to write the header by-hand every time).

We can easily send both, header and content, to client writing them to the standard output as any regular CGI application, so in our first example we need only two lines of code:

printf( "Content-Type: text/plain\r\n\n\n" );

for header of the HTTP answer (note that the two new-lines , "\n\n", at the end of the string conventionally mark the end of the header) and

printf( "Hello World Wide Web\n" );

that represents what will appear on the browser.

That's all.

The full code of the example is:

#include "fcgi_stdio.h"
int main( int argc, char *argv[] )
{

while( FCGI_Accept() >= 0 )
{

printf( "Content-Type: text/plain\n\n" );
printf( "Hello World Wide Web\n" );
}
return 0;
}

We can save this code with "helloweb.c" and compile it with this command:

gcc -o helloweb -lfcgi helloweb.c

In the next post we will examine how to deploy helloweb on the web server.

References:
http://www.fastcgi.com/devkit/doc/fastcgi-prog-guide/ch2c.htm
http://support.zeus.com/zws/examples/2005/12/16/hello_world_in_perl_and_c

sabato 2 maggio 2009

FastCGI on Ubuntu Server 9.04 and VirtualBox 2.2.0

In the previous posts we have set up a full environment for FastCGI testing based on Ubuntu Server 8.10 running on an old version of VirtualBox (sorry, I don’t remember which version).

The following days the version 2.2.0 of VirtualBox and 9.04 of Ubuntu have been released, so, as soon as possible I retested everything with this new versions and, at the same time, I recorded the four steps of the preparation of the virtual environment and I published them on Youtube.

1 - Sun VirtualBox 2.2.0 installation on Windows XP

2 - Configuring VirtualBox 2.2.0 for Ubuntu Server 9.04

Only 2 changes compared with the previous installation:
- I didn’t need yet to enable PAE/NX
- I set the “base memory size” to 128 MByte to avoid “host memory low” crashes during the installation of Ubuntu (I think this problem doesn’t exist yet with the new version 2.2.2 of VirtualBox just released, if you experimented it let me know).

3 - Installing Ubuntu Server 9.04 on VirtualBox 2.2.0


4 - Updating Ubuntu Server 9.04

If you prefer you can download the VDI image of the installation here!
(it’s a 7-zip file of about 200 megabytes)