Enable HTTP/2 on Apache2.4+ along with PHP8.3+ (Ubuntu)

CCarter

Final Boss ®
Moderator
BuSo Pro
Boot Camp
Digital Strategist
Joined
Sep 15, 2014
Messages
4,341
Likes
8,855
Degree
8
Alright so I was trying to figure out why my ancient websites had HTTP2 enabled but none of my newer sites could get it going, well long story short, it was because of Apache2 mpm_prefork which is needed for php to run on Apache. That will not allow HTTP/2.

(The mpm stuff has to do with multi-threading/processing - quite frankly I can't be bothered to understand it)

My ancient sites were all on NGINX: Enabling HTTP/2 on NGINX

The solution is to disable mpm_prefork and use mpm_event, and then use php-fpm (in my case php8.3-fpm) with Apache2.

Here are the commands you need:

Code:
: sudo add-apt-repository ppa:ondrej/apache2

: apt-get update
: apt-get install php-fpm
: apt-get install apache2

: a2dismod php
: a2dismod php8.3
: a2dismod mpm_prefork

: a2enmod proxy_fcgi
: a2enmod mpm_event
: a2enmod http2
: a2enmod ssl

: a2enconf php8.3-fpm

: service apache2 restart
: service php8.3-fpm restart

Within your /etc/apache2/sites-available/YOUDOMAIN.COM.conf file you need to make the following updates:

Code:
<VirtualHost *:443>

...

    Protocols h2 h2c http/1.1
    H2Direct on

    # PHP-FPM Setup
    <FilesMatch "\.php$">
        SetHandler "proxy:unix:/var/run/php/php-fpm.sock|fcgi://localhost/"
    </FilesMatch>

...

</VirtualHost>

This guide helped a lot (had to switch the php7.4 for 8.3): HTTP2 on Apache

Whenever you restart apache2, you'll also need to restart php-fpm using the exact php you are using:

Code:
: service apache2 restart
: service php8.3-fpm restart

I'm leaving this here for historical reference.
- CC

Edit:

How to test this with Curl:

Code:
 curl -I --http2 -s -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:123.0) Gecko/20100101 Firefox/123.0" https://www.yourDOMAIN.com/ | grep HTTP

Results should be:
Code:
HTTP/2 200

If you get HTTP/1.1 200 OK you have failed.
 
Back