Apache 2.4 virtualhost_alias with php5-fpm and proxy_fcgi

apache-2.4php-fpmvirtualhost

After moving my server from mod_php to mod_fastcgi and php5-fpm, I've found that everything works, except my wildcard virtualhost.

My wildcard virtualhost works as such:
*.dev.myserver.com translates to /var/www/clients/$1

I was previously doing this strictly with mod_rewrite, but in trying to make this set up work, I switched to mod_virtualhost_alias.

The initial problem was that none of the "sub sites" on this wildcard virtualhost were being sent to the PHP interpreter–the code would be returned to the browser.

I've managed to get it to work for "/" (index.php) and any URL with the php file in it. However, it does not work for "pretty" URLs (eg WordPress public side with mod_rewrite).

I've tried so many things.. here is what my virtualhost conf file for this vhost currently looks like:

<VirtualHost *:80>
        ServerAdmin webmaster@mysite.com
        ServerAlias *.dev.mysite.com
        VirtualDocumentRoot /var/www/clients/%1


        ErrorLog ${APACHE_LOG_DIR}/error.log
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined

        <Directory "/var/www/clients/%1">
                Options FollowSymLinks
                AllowOverride All

                RewriteEngine On
                RewriteCond %{HTTP_HOST}        [^.]+\.dev\.mysite\.com$
                RewriteRule ^(.+)               %{HTTP_HOST}$1  [C]
                RewriteRule ^([^.]+)\.dev\.mysite\.com(.*) proxy:unix:/var/run/php5-fpm.sock|fcgi://localhost//var/www/clients/$1$2  [E=ENV_CUST:$1]
                RewriteRule ^([^.]+)\.([^.]+)\.dev\.mysite\.com(.*) proxy:unix:/var/run/php5-fpm.sock|fcgi://localhost//var/www/clients/$2/$1$3    [E=ENV_CUST:$2,E=ENV_PROJ:$3]

                RewriteCond %{REQUEST_FILENAME} ^/((.*)(/.*)?)$
                RewriteCond %{DOCUMENT_ROOT}/%2.php -f
                RewriteRule !.*\.php$   proxy:unix:/var/run/php5-fpm.sock|fcgi://localhost/%{DOCUMENT_ROOT}/%1.php   [P]

                RewriteRule ^index\.php$ - [L]
                RewriteCond %{REQUEST_FILENAME} !-f
                RewriteCond %{REQUEST_FILENAME} !-d
                RewriteRule . index.php [QSA]

                <FilesMatch \.php$>
                        SetHandler "proxy:unix:/var/run/php5-fpm.sock|fcgi://localhost"
                </FilesMatch>
        </Directory>

</VirtualHost>

conf-enabled/php5-fpm.conf:

<IfModule mod_fastcgi.c>
AddHandler php5-fcgi .php
Action php5-fcgi /php5-fcgi
Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization

<Directory /usr/lib/cgi-bin>
    Require all granted
</Directory>

</IfModule>

mods-enabled/fastcgi.conf:

<IfModule mod_fastcgi.c>
  AddHandler fastcgi-script .fcgi
  #FastCgiWrapper /usr/lib/apache2/suexec
  FastCgiIpcDir /var/lib/apache2/fastcgi
</IfModule>

EDIT: More info

I did try setting up php5-fpm to listen on port 9000 instead of using unix sockets, but unix sockets seemed preferable. I'm open to changing that if there is no significant disadvantage (some production sites run on this server as well)

I've tried the solutions provided on these pages and various combinations of them trying to get this to work. I've tried too many things to list them all here or even remember them all, but if you look at these pages you'll have a pretty good idea:

apache 2.4 + php-fpm + mod_proxy_fcgi + alias

(see comments for full list, not enough reputation to post them all here)

Server is running Ubuntu 14.04, Apache 2.4.7

Any help greatly appreciated. I've found a temporary workaround by changing the WordPress permalink settings to not use clean URLs, but I'd really like to get the clean URLs working.

Best Answer

Author of one of those blog posts here :)

I'd try removing the rewrite rules from your virtual host configuration and trying to use the default rewrite rules provided by WordPress in a .htaccess file (without your extra proxy stuff in the rules).

I've a client with a setup like for WordPress websites and they don't have this problem, the setup is very similar to the one outlined in my blog post.