Tuesday, September 10, 2013

Configure FPM on Apache2 for only one virtual host (Debian Jessie)

A friend of mine told me once that the most fearful parachute jump is the second one )

Purpose

I have apache2 and php5 and I want to configure them differently for different virtual hosts to have a testing environment on hand.
The annoying problem I've recently faced was configuration of FPM for some virtual hosts among others.
If you want to enable it for the whole server, there is no any problem. But if you want to have php configured as apache module for each one except few, there is a couple of things that are simple but not obvious.

Foreword

  • Apache2 + PHP5

Installation:
apt-get install apache2 apache2-doc apache2-utils 
apt-get install php5

  • PHP-FPM - FastCGI Process Manager

apt-get install libapache2-mod-fastcgi php5-fpm php5

  • Versions

I'm using Debian Jessie. It is important because the version of the OS influences the version of the application. Both they influence default configuration and features of your build.
  1. apache2 has the version 2.4.6-3
  2. php5-fpm has the version 5.5.1+dfsg-2
  3. php5 has the version 5.5.1+dfsg-2
To display version you can use the following command: apt-cache showpkg {PACKAGE}

Initial configuration

It's my configuration of the host that has to use PHP as Apache module (/etc/apache2/sites-available/testapache2.deb8s1.local.conf):
<VirtualHost *:80>
        ServerAdmin igor@deb8s1.local
        ServerName testapache2.deb8s1.local
        ServerAlias www.testapache2.deb8s1.local
        DocumentRoot /srv/www/igor/testapache2.deb8s1.local/public_html/
        ErrorLog /srv/www/igor/testapache2.deb8s1.local/logs/error.log
        CustomLog /srv/www/igor/testapache2.deb8s1.local/logs/access.log combined
</VirtualHost>

It's my initial configuration of the host that has to use FPM (FastCGI + PHP) /etc/apache2/sites-available/testfcgi.deb8s1.local.conf:
<VirtualHost *:80>
        ServerAdmin igor@deb8s1.local
        ServerName testfcgi.deb8s1.local
        ServerAlias www.testfcgi.deb8s1.local
        DocumentRoot /srv/www/igor/testfcgi.deb8s1.local/public_html/
        ErrorLog /srv/www/igor/testfcgi.deb8s1.local/logs/error.log
        CustomLog /srv/www/igor/testfcgi.deb8s1.local/logs/access.log combined
</VirtualHost>

It's the crucial moment of the /etc/apache2/mods-available/php5.conf config (remember it):
<FilesMatch ".+\.ph(p[345]?|t|tml)$">
    SetHandler application/x-httpd-php
</FilesMatch>

Let's check:
wget -O- testapache2.deb8s1.local/phpinfo.php | grep 'Server API' wget -O- testfcgi.deb8s1.local/phpinfo.php | grep 'Server API'

Configure FPM for a certain host

It is required to enable following modules:
a2enmod actions alias fastcgi
service apache2 restart
Then configure /etc/apache2/sites-available/testfcgi.deb8s1.local.conf
<VirtualHost *:80>
        ServerAdmin igor@deb8s1.local
        ServerName testfcgi.deb8s1.local
        ServerAlias www.testfcgi.deb8s1.local
        DocumentRoot /srv/www/igor/testfcgi.deb8s1.local/public_html/
        ErrorLog /srv/www/igor/testfcgi.deb8s1.local/logs/error.log
        CustomLog /srv/www/igor/testfcgi.deb8s1.local/logs/access.log combined
        <IfModule mod_fastcgi.c>
                <FilesMatch ".+\.ph(p[345]?|t|tml)$">
                        SetHandler php5-fcgi
                </FilesMatch>
                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
                <Location /php5-fcgi>
                        Require all granted
                </Location>
        </IfModule>
</VirtualHost>

Let's reload and check
service apache2 reload
wget -O- testapache2.deb8s1.local/phpinfo.php | grep 'Server API' wget -O- testfcgi.deb8s1.local/phpinfo.php | grep 'Server API'

About

Now, I think that I should describe the things that have made troubles for me.
it seems, that if you have a handler configured in the config file of some module, you have to reload it absolutely the same way (I can be wrong):

/etc/apache2/mods-available/php5.conf
<FilesMatch ".+\.ph(p[345]?|t|tml)$">
    SetHandler application/x-httpd-php
</FilesMatch>

From /etc/apache2/sites-available/testfcgi.deb8s1.local.conf
<FilesMatch ".+\.ph(p[345]?|t|tml)$">
    SetHandler php5-fcgi
</FilesMatch>

The second thing I faced was permission error. And the following fix was required:
<Location /php5-fcgi>
        Require all granted
</Location>

It's strange, but during my struggle I haven't found any guide that mentions these two issues in relation with FCGI, and I had to read articles about SetHandler, Alias and Action directives on the Apache site.

Conclusion

My friends tell: 'Use Nginx' )

Sources

No comments:

Post a Comment