18a

How to configure php-fpm, Apache and HTTP/2 on AMI2 (Amazon Linux 2)

8th August 2021 03:00 by tom

I had a real battle getting HTTP/2 to work on my new AMI2 Lamp webserver this week. The Apache http2 module was installed but it didn't work alongside mod_mpm_prefork, so I needed to switch to the mod_mpm_event module. However this wouldn't start unless the server was using php-fpm. So what seemed like a simple task of just upgrading my HTTP requests to HTTP/2, turned into a little bit of work and searching on Google didn't offer much assistance as everyone else seemed to be using a Debian based linux which had a2enmod available.

The article is really just in note form for my own reference, but I hope it points you in the right direction if you're having problems getting it all to work.

php-fpm:

Make sure php-fpm is installed. PHP FPM is an article in it's own right, but the following might be useful:

> sudo yum install -y php-fpm

 

Once it's installed, make sure php-fpm is running as a service:

> sudo systemctl start php-fpm

 

Make sure it starts on reboot.

> sudo systemctl enable php-fpm

 

Make sure it's listening on 127.0.0.1:9000

> sudo vi /etc/php-fpm.d/www.conf

 

Edit the config file above (assuming that's where it is on your computer) and change the following:

;listen = /run/php-fpm/www.sock
listen = 127.0.0.1:9000

 

 

You can confirm if Apache is processing requests using PHP-FPM via the phpinfo() output.

 

Apache:

Make sure Apache is configured to use event MPM.

> sudo vi /etc/httpd/conf.modules.d/00-mpm.conf

 

#LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
LoadModule mpm_event_module modules/mod_mpm_event.so


Make sure PHP is processing with php-fpm

> sudo vi /etc/httpd/conf.d/php.conf

 

ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/html/$1DirectoryIndex /index.php index.php#<FilesMatch \.(php|phar)$>
# SetHandler application/x-httpd-php
#</FilesMatch>

 

UPDATE 19/5/2023: Alternatively you can leave it as listen = /run/php-fpm/www.sock in /etc/php-fpm.d/www.conf but if that's the case omit adding ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/html/$1 to /etc/httpd/conf.d/php.conf as well.


Important bit:
Make sure mod_php is not installed.

> sudo yum remove mod_php -y

 

This was the important one for me. The error message I was getting before doing the above was 'Apache is running a threaded MPM, but your PHP Module is not compiled to be threadsafe.'.

 

HTTP/2:

> sudo vi /etc/httpd/conf.d/ssl.conf

 

# enable HTTP/2, if available
Protocols h2 h2c http/1.1
H2Direct on

 

Useful commands:

> sudo service php-fpm reload
> sudo service php-fpm restart
> sudo service httpd restart

 


Useful resources:

https://wiki.alpinelinux.org/wiki/Apache_with_php-fpm
https://donjajo.com/fix-apache-running-threaded-mpm-php-module-not-compiled-threadsafe-fedora/
https://cwiki.apache.org/confluence/display/HTTPD/PHP-FPM
https://computingforgeeks.com/install-apache-with-ssl-http2-on-rhel-centos/
https://stackoverflow.com/questions/71875777/enable-http2-in-apache-2-4-53
https://httpd.apache.org/docs/2.4/mod/mod_http2.html