Enable http/2

By: Luke Rawlins Dec 19, 2019 | 3 minutes read
Share this:

Tags: apache, fedora, http/2

The http 2.0 protocol is designed for increased speed and performance. The protocol was published for release in 2015 and is supported by Apache 2.4 using the mod_http2 module.

Note: You will need to have a valid ssl cert for all practical purposes to implement http/2. Many web browsers including Firefox will not use http/2 on a site without an ssl cert. You can obtain a free ssl cert with letsencrpyt, check out https://certbot.eff.org for more info.

This write up is based on Fedora 30 and Apache 2.4, but it should also work on CentOS 8 or RHEL 8.

sudo dnf install php-fpm mod_fcgid fcgi mod_http2

In order to enabled http/2 in Apache you need to make sure that you are not using the mpm_prefork module. The prefork module is non-threaded and cannot handle the multiplexed connection that http/2 requires. For the best results you will want to switch to the mpm_event module, we will walk through how to make that change momentarily.

Uncomment the line:

LoadModule mpm_event_module modules/mod_mpm_event.so

Make sure to place a # in front of the line:

#LoadModule mpm_prefork_module modules/mod_mpm_prefork.so

You only want to have one mpm module loaded so make sure that mod_mpm_event is the only active module. Prefork and worker should be commented out.

Next edit /etc/httpd/conf.d/php.conf: - Look for the “SetHandler” parameter in the “local php-fpm” section shown below.

#
# Redirect to local php-fpm (no mod_php in default configuration)
#
<IfModule !mod_php5.c>
  <IfModule !mod_php7.c>
    # Enable http authorization headers
    SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1

    <FilesMatch \.(php|phar)$>
        SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost"
        #SetHandler "proxy:fcgi://127.0.0.1:9000"
    </FilesMatch>
  </IfModule>
</IfModule>

Ensure that the following parameter is active. Copy and paste it in under “<FilesMatch .(php|phar)$>” if it isn’t present.

SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost"

Ensure that “SetHandler proxy:fcgi://127.0.0.1:9000” in that section is commented out with a #.

Near the top of your virtual hosts file, under the “ServerName” parameter add the following:

Protocols h2 h2c http/1.1
sudo systemctl enable php-fpm && sudo systemctl start php-fpm

Restart the httpd service:

sudo systemctl restart httpd

Visit your website and click a few links, refresh the page a couple times, and then take a look at your web logs.

sudo grep "HTTP/2.0" /var/log/httpd/site_access.log

If all the above steps were completed successfully you should get some log entries returned that contain “HTTP/2.0”. If so congratulations your site is now supporting http/2!

Related Posts


Upgrade to Fedora 30 Complete!

In the last couple of days there has been some extended downtime on this site. That is because I’ve been working on migrating my blog from Ubuntu 16.04 to Fedora 30. I’m switching for lots of reasons. Some of the php packages I need for Wordpress have been getting a bit out of date on Ubuntu 16.04 and I wanted to have the most up-to-date stable release of php without needing to add a third-party repository and Fedora 30 comes with php7. Read more


Contact

If you’d like to get in touch, contact with me via email - or follow on Twitter.

[email protected]