I recently started transitioning all of the websites under my management from Apache to nginx (mainly to ease running my Python webapps via gunicorn, but that is another story).
Since nginx will not directly execute PHP (via either CGI or nginx-managed FastCGI), the first step was to get PHP running at all. I opted to run php-cgi via daemontools; my initial run script was fairly straight forward:
1 2 | #!/usr/bin/env bash exec php-cgi -b 127.0.0.1:9000 |
Couple this with a (relatively) straight forward nginx configuration and the sites will already start responding:
server {
listen 80;
server_name example.com
root /var/www/example.com/httpdocs;
index index.php index.html;
fastcgi_index index.php;
location ~ \.php {
keepalive_timeout 0;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$uri;
fastcgi_pass 127.0.0.1:9000;
}
}
The tricky part came when I wanted to run PHP under the user who owned the various sites. I could have (and perhaps should have) opted to spin up a copy of php-cgi for each user, but I decided to try something a little sneakier; PHP will set its own UID on each request.
