Apache and PHP-FPM 7.2 – Necessary Configuration Changes

apache-2.4fastcgiphp-fpmubuntu-18.04virtualhost

Until now, I was using Ubuntu 16.04.5 Server with Apache, Nginx as a reverse proxy, PHP 7.0 + FastCGI + FPM, everything updated to last version. Everything went fine.

This is one Apache virtualhost, for, say, x.com:

<VirtualHost *:8080>
    ServerName x.com
    ServerAlias www.x.com
    ServerAdmin x@mail.com
    DocumentRoot /home/user/x.com/site
    ErrorLog /home/user/x.com/logs/error.log
    CustomLog /home/user/x.com/logs/access.log combined
    <Directory /home/user/x.com/site>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
    </Directory>
    <IfModule mod_fastcgi.c>
        AddHandler php7.2-fcgi-x.com .php
        Action php7.2-fcgi-x.com /php7.2-fcgi-x.com
        Alias /php7.2-fcgi-x.com /usr/lib/cgi-bin/php7.2-fcgi-x.com
        FastCgiExternalServer /usr/lib/cgi-bin/php7.2-fcgi-x.com -socket /var/run/php/php7.2-fpm.x.com.sock -pass-header Authorization
        <Directory /usr/lib/cgi-bin>
            Require all granted
        </Directory>
    </IfModule>
</VirtualHost>

This is one FPM-pool .conf file for x.com:

[x.com]

user = user
group = www-data

listen = /run/php/php7.2-fpm.x.com.sock

listen.owner = www-data
listen.group = www-data

pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

Now I'm using the same but with Ubuntu 18.04.1 and PHP, FPM, etc are 7.2. It is a clean install, not an update from the other versions.

It seems to work fine, but I'm not entirely sure, because I've seen several people posting PHP-FPM 7.2 configurations like this:

<VirtualHost *:8080>
    ServerName x.com
    ServerAlias www.x.com
    ServerAdmin x@mail.com
    DocumentRoot /home/user/x.com/site
    ErrorLog /home/user/x.com/logs/error.log
    CustomLog /home/user/x.com/logs/access.log combined
    <Directory /home/user/x.com/site>
        Options -Indexes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>
    <FilesMatch \.php$>
        # 2.4.10+ can proxy to unix socket
        SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost/"
        # Else we can just use a tcp socket:
        #SetHandler "proxy:fcgi://127.0.0.1:9000"
    </FilesMatch>
</VirtualHost>

Now, this doesn't work for me, even if I delete the FPM individual .conf files. And I simply don't understand what the line SetHandler "proxy:unix:/var/run/php/php7.2-fpm.sock|fcgi://localhost/" does, or how one simple line can substitute all the bigger block of code that everybody's been using for FastCGI these last years.

Can somebody shed some light on this?

Thanks in advance.

Best Answer

Well, the difference is simple. Your working approach with handlers is being backed by mod_fastcgi whereas the second one requires mod_proxy_fcgi and mod_proxy within your configuration and proxies requests to FPM socket. The latter being preferred because it does not require separate configuration and memory overhead mod_fastcgi brings along. In your case, where everything you use of mod_fastcgi functionality is just FastCGIExternalServer it makes a little sense to bring in all the machinery mod_fastcgi has within.

Your problem is likely because your current configuration uses a fancy name for a socket, that is /var/run/php/php7.2-fpm.x.com.sock whereas your attempted config uses standard /var/run/php/php7.2-fpm.sock that does not exist on your system. You should use socket name exactly as it shown in your FPM configuration (and your working config) when attempting to use mod_proxy_fcgi and also remove |fcgi://localhost/ as your FPM is only listening on a UNIX socket.