$ date

Sat Nov 28 19:18:34 UTC 2015

CGI and FastCGI

What is CGI?

The tl;dr of the Common Gateway Interface (CGI) is that it is a way to run scripts and programs from a web server to generate web pages dynamically.

It's commonly assigned with Perl, which is a big part in many CGI apps, but it is in no way restricted to that.

You can make a CGI app with any language supporting environment variables, reading input and writing output (stdio).

Example App

A crude and small example CGI app that outputs the environment variables.

import sys
sys.stderr = sys.stdout
import os
from cgi import escape

print "Content-type: text/html"
print "<html><head><title>Situation snapshot</title></head><body><p>"
print "Running:"
print "<b>Python %s</b><br><br>" %(sys.version)
print "Environmental variables:<br>"
print "<ul>"
for k in sorted(os.environ):
  print "<li><b>%s:</b>\t\t%s<br>" %(escape(k), escape(os.environ[k]))
  print "</ul></p></body></html>"

What is FastCGI?

FastCGI is a newer, alternative version of CGI, mainly getting rid of the need to spawn a new process on each request, like CGI did.

It is binary-based and tends to be faster than CGI for the most part, but also has a bigger effort required to make something simple.


Look at php-fpm, one of the most popular FastCGI applications...

What should I choose?

It depends, really.

Sometimes, writing a simple CGI app is simpler and requires less "glue" to be applied to get stuff to just work. You can literally write a CGI app with everything.

On the other hand, FastCGI servers tend to be more officient due to not having to fork() every time, having the ability to just reuse the connections and server.

Carbon recently got a CGI Server handler, so you can host CGI apps with Carbon.

FastCGI may also come in the future if I can be arsed, but eh.. Effort. x.x

$ cd ..