Last Updated: February 25, 2016
·
4.394K
· dsci

Enable PHP for a Subdomain in NGINX

Here I go again - with mostly a reminder to myself.

We're mainly using Rails applications and for it the NGINX/Unicorn setup. Recently I had to install the Piwik analytics platform which is a PHP application. I only want PHP support for its subdomain.

(For the following I assume that NGINX and PHP (and PHP-FPM, the FastCGI Process Manager for PHP of course) is already installed at the system.

The most basic setup for a NGINX vhost using PHP is:

server {
    listen 80;
    server_name piwik.example.de;
    root /home/brewster/web/piwik;
index index.php;
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

I had some trouble with

fastcgi_pass 127.0.0.1:9000;

so I checked if anything happens at port 9000

lsof -i tcp:9000

Nothing happens here, so I had to change the PHP-FPM config file, which is located (if you're using Ubuntu Linux) at

/etc/php5/fpm/php-fpm.conf 

At the very end I add these lines

[piwik]

listen = 127.0.0.1:9000
listen.backlog = -1
listen.allowed_clients = 127.0.0.1

; Unix user/group of processes
user = www-data

; Choose how the process manager will control the number of child processes.
pm = dynamic
pm.max_children = 50
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pm.max_requests = 100

; Pass environment variables
env[HOSTNAME] = $HOSTNAME
env[PATH] = /usr/local/bin:/usr/bin:/bin
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp

After restarting NGINX and PHP-FPM

/etc/init.d/php5-fpm start
/etc/init.d/nginx restart

I had Piwik up and running.