In this post I will show you how to serve CGI scripts with Nginx on CentOS 6 using fcgiwrap.

 

As there’s no fcgiwrap package for CentOS 6.0, you must build it yourself. First install some prerequisites:

yum groupinstall 'Development Tools'
yum install fcgi-devel

Now you can build fcgiwrap:

cd /usr/local/src/
git clone git://github.com/gnosek/fcgiwrap.git
cd fcgiwrap
autoreconf -i
./configure
make
make install

This installs fcgiwrap to /usr/local/sbin/fcgiwrap.

 

1a: Install the spawn-fcgi package which allows you to run fcgiwrap as a daemon:

yum install spawn-fcgi

1b: If you are already using spawn-fcgi for php, copy the init.d script and sysconfig of spawn-fcgi

cp -R /etc/init.d/spawn-fcgi /etc/init.d/spawn-fcgi2
cp -R /etc/sysconfig/spawn-fcgi /etc/sysconfig/spawn-fcgi2

Next change the following lines in your spawn-fcgi2 file in /etc/init.d/ to look like this:

prog="spawn-fcgi2"
config="/etc/sysconfig/spawn-fcgi2"

Then modify your new spawn-fcgi configuration in /etc/sysconfig/ to look like this:

FCGI_SOCKET=/var/run/fcgiwrap.socket
FCGI_PROGRAM=/usr/local/sbin/fcgiwrap
FCGI_USER=nginx
FCGI_GROUP=nginx
FCGI_EXTRA_OPTIONS="-M 0700"
OPTIONS="-u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET -S $FCGI_EXTRA_OPTIONS -F 1 -P /var/run/spawn-fcgi.pid -- $FCGI_PROGRAM"

Now you can start it like any other service. Don’t forget to add it to chkconfig via chkconfig -levels 2345 spawn-fcgi on. (or chkconfig -levels 2345 spawn-fcgi2 on depending on your configuration)

The last thing you need to do is to write the nginx configuration.

location /cgi-bin/ {
gzip off;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

If you want to user another method, visit http://www.howtoforge.com/serving-cgi-scripts-with-nginx-on-debian-squeeze-ubuntu-11.04.