Enable 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.
How to enable http 2.0 on Apache 2.4.
This write up is based on Fedora 30 and Apache 2.4, but it should also work on CentOS 8 or RHEL 8.
Install the required packages.
sudo dnf install php-fpm mod_fcgid fcgi mod_http2
Ensure the correct mpm module is loaded.
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.
Edit the file /etc/httpd/conf.modules.d/00-mpm.conf.
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.
Edit the file /etc/httpd/conf.d/php.conf.
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 #
.
Edit your virtual hosts file:
Near the top of your virtual hosts file, under the “ServerName” parameter add the following:
Protocols h2 h2c http/1.1
Start and Enable the php-fpm service.
sudo systemctl enable php-fpm && sudo systemctl start php-fpm
Restart the httpd service:
sudo systemctl restart httpd
Check your logs to verify http/2.
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!