Running a FastCGI Application Server with a Lighttpd Front End
Running a FastCGI Application Server separately from the Web Server has been an increasingly popular topic. The FastCGI Wikipedia Article states some of the reasons for it -
“This separation allows server and application processes to be restarted independently — an important consideration for busy web sites. It also facilitates per-application security policies — important for ISPs and web hosting companies.”
The lighttpd web server includes the binary spawn-fcgi, which as the name suggests, allows you to spawn FastCGI processes. This can be done independently of the web server processes.
Lighttpd’s FastCGI module, mod_fastcgi is well documented here, but there are a lot of arguments to sift through and it can be a process of trial and error to get a working configuration.
In this example, we will review how to configure lighttpd and spawn-fcgi on separate servers. The web server which will run lighttpd and host all html, images, css, etc. files. The FastCGI server will host all PHP files.
Here is the relevant excerpt from lighttpd.conf on Server A: (the server hosting the html, images, css, etc)
server.modules = (
[ ... ]
"mod_fastcgi",
[ ... ]
)
[ ... ]
$HTTP["host"] =~ "(^|\.)iamtgc\.com$" {
server.name = "iamtgc.com"
server.document-root = "/lighttpd/" + server.name
fastcgi.server = ( ".php" =>
((
"host" => "192.168.25.120",
"port" => 9999,
"docroot" => "/php/iamtgc.com",
"check-local" => "disable"
))
)
accesslog.filename = "/var/log/lighttpd/" + server.name + "-access.log"
}
[ ... ]
On Server B (where the FastCGI processes are running) you start the FastCGI server like this:
# spawn-fcgi -f /usr/local/bin/php-cgi -a 192.168.25.120 -p 9999
Note, the php needs to exist in the docroot reference above, in this case /php/iamtgc.com. The bind address (-a) and port (-p) also correspond to the host and port variables referenced in lighttpd.conf on Server A.
Also note, php-cgi can be replaced with something like dispatch.fcgi in the case of Ruby on Rails or a similar fcgi file for Django as seen here. In both of these cases, other changes would be also required to Server A’s lighttpd.conf.