Enabling PHP-FPM and HTTP/2 on Apache2

Something I’ve wanting to do a long time but never got around to: enabling HTTP/2 on my Apache webserver. HTTP/2 has been around for over 8 years now. It is supposed to be faster and cooler, and by now almost every browser out there supports it.

In my case it also meant switching setups for Apache and PHP. Apache’s http2-module doesn’t run on mpm_prefork; but mpm_prefork is required by Apache’s PHP module. (MPM: multiprocessing module, the way Apache receives and processes requests. Prefork is one MPM type, event and worker are others.) So I needed to switch from mpm_prefork to mpm_event and from mod_php to PHP-FPM.

Thanks to the handy commands a2enmod and a2dismod commands changing the setup is quite easy.

First install PHP-FPM:

sudo apt install php7.4-fpm

(yes I know that is a very old PHP version …)

Then disable and enable modules:

sudo a2dismod php7.4 mpm_prefork
sudo a2enmod mpm_event proxy_fcgi http2

Make sure to also enable the setenvif module if you hadn’t already done that. I ran sudo a2enconf php7.4-fpm as well, not sure whether that is required though.

Finally, restart Apache:

sudo service apache2 restart

I tried this first on a Debian server running on Windows WSL, and it worked perfectly well. I was slightly worried that “the new Apache” might run as a different user, but that turned out not be the case (saving me from chown-ing many, many files). Then, on the server you’re looking at I backed-up the /etc/apache2 folder and ran the same commands. This time restarting Apache threw an error:

Job for apache2.service failed because the control process exited with error code.
See "systemctl status apache2.service" and "journalctl -xe" for details.

None of the suggestions provided anything useful, so I had to quickly roll back the changes.

sudo a2dismod mpm_event proxy_fcgi http2
sudo a2enmod php7.4 mpm_prefork
sudo service apache2 restart

Time to scan the logs. There was nothing in Apache’s own error.log, but fortunately syslog came to the rescue:

Syntax error on line 8 of /etc/apache2/sites-enabled/some-site.conf:
Invalid command 'php_admin_value', perhaps misspelled or defined by a module not included in the server configuration.

Turns out that php_admin_value, php_value and php_flag are only recognized by mod_php, not by PHP-FPM. I was using some of them for setting things like upload_max_filesize and post_max_size, in vhosts and in .htaccess. But PHP-FPM does support .user.ini-files. So I created a bunch of such files and put them in the web roots of the affected sites. For a smooth transition I put the old vhost/htaccess commands in conditional blocks, like this:

<IfModule mod_php7.c>
    php_value post_max_size 9M
    php_value upload_max_filesize 8M

Disable and enable the modules again:

sudo a2dismod php7.4 mpm_prefork
sudo a2enmod mpm_event proxy_fcgi http2

This time I tested the new config with sudo apachectl -t. “Syntax OK”! So, fingers crossed:

sudo service apache2 restart

VoilĂ .

When the server ran out of inodes

A while ago I couldn’t add any files to a Debian server I control. It was pretty odd, as there was plenty of free disk space available (df -h will show that).

After I deleted a number of web cache files, I could add new files again. But soon the problem would return, leaving me wondering what was wrong.

Continue reading When the server ran out of inodes