Sunday, November 7, 2010

Migrating to nginx from apache (with PHP + CGI support)

Was always aware of nginx, and several people mentioned it, eventually it hit home (especially after reading some interesting LOPSA posts on their mailing list.

For this example, I'll detail my setup for  PHP, CGI, nginx status page and restricting by address (IPv4 & IPv6).

The following is done on Debian Squeeze, I have tested the config with nginx version 0.7 & 0.8.

The following dependencies are required: php-fastcgi fcgiwrap spawn-fcgi php5-cgi

Although not in the Debian repository, I found php-fastcgi packaged, and a friend ended up packaging nginx 0.8 (0.7 is available in the repository).
php-fastcgi
nginx-0.8

I went with version 0.8 of nginx as it allows you to restrict access to sites using IPv6 addresses, as opposed to IPv4 only.

Firstly, the /etc/init.d/php-fastcgi wrapper must be modified - by default it listens on a port, and only starts one process - I want to use socket-based communication between nginx & start multiple processes (for me, 3 is enough).


LOGDIR=/var/log/php-fastcgi
PIDFILE=/var/run/$NAME.pid
SOCKET=/var/run/$NAME.socket
CHILDREN=3
DODTIME=1
DAEMON_OPTS=" -s ${SOCKET} -u ${USER} -g ${GROUP} -f ${PHP_CGI} -P ${PIDFILE} -C ${CHILDREN}"

The following is my nginx configs from /etc/nginx/sites-enabled/
localhost:

server {
        listen   127.0.0.1:80; ## listen for ipv4
        listen   [::1]:80;
        server_name  localhost;
        location /nginx_status {
                stub_status on;
                access_log   off;
                allow 127.0.0.1;
                allow ::1;
                deny all;
        }
}


stats.domain.com:
server {
        listen   80;
        listen   [::]:80;
        server_name  stats.domain.com;
        root   /var/www/stats;
        access_log  /var/log/nginx/stats.domain.com-access.log;
        location / {
                index  index.html index.php;
                allow 127.0.0.0/8;
                allow ::1;
                deny all;
        }
        location ~ \.php$ {
                gzip off;
                fastcgi_pass unix:/var/run/php-fastcgi.socket;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
        location /smokeping/ {
                index smokeping.cgi;
                gzip off;
                if ($uri ~ "/smokeping/smokeping.cgi") {
                        fastcgi_pass unix:/var/run/fcgiwrap.socket;
                }
        }
}
 I symlinked /usr/share/smokeping/www to /var/www/smokeping & also symlinked /usr/share/smokeping/cgi-bin/smokeping.cgi to /usr/share/smokeping/www/

3 comments: