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

tom Tom, 8th August 2021

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

More from our blog

18a win Netty 2024 award for Activibees.com

18a win Netty 2024 award for Activibees.com

29.02.24

We are delighted to announce that 18a has been recognised for its outstanding work in the "Web Design Agency of the Year - UK" category at… Read →

Generating an Effective Content Security Policy with your Laravel React App

Generating an Effective Content Security Policy with your Laravel React App

27.02.24

I recently had an interesting problem to solve. I'd built a brand new author website on a shiny installation of Laravel 10, utilising its out-of-the-box… Read →

If your WordPress website looks broken, it could be because of this.

If your WordPress website looks broken, it could be because of this.

15.02.24

WordPress is the incredibly popular blogging-come-full-website platform that powers over 835 million websites* in 2024. It's functionality is extended by plugins, and one such very… Read →