HTTP/2 on Amazon Linux instance

      1 Comment on HTTP/2 on Amazon Linux instance

Checking error_log

While I was checking my error_log, I noticed the following error:

The mpm module (prefork.c) is not supported by mod_http2. The mpm determines how things are processed in your server. HTTP/2 has more demands in this regard and the currently selected mpm will just not do. This is an advisory warning. Your server will continue to work, but the HTTP/2 protocol will be inactive.

Apparently this error is generated when mod_http2 module is enabled (for HTTP/2), but the underlying MPM module is set to prefork. In order to use HTTP/2 which can provide performance enhancements, I would need to change MPM setting to event. (More info about different MPM types and how you can check)
There isn’t much guidance for how to set this up on Amazon Linux, but since its architecture is most similar to CentOS, I was able to change the setting without any problem. So here’s how to do it.

Update Apache configuration

Assuming you have Apache version 2.4 or higher, update 00-mpm.conf under /etc/httpd/conf.modules.d:

$sudo vim /etc/httpd/conf.modules.d/00-mpm.conf

Comment out line and enable

#LoadModule mpm_prefork_module modules/
LoadModule mpm_event_module modules/

Save and restart httpd:

$sudo service httpd restart

Verify mpm is set to event:

$httpd -V | grep -i 'version\|mpm'
Server version: Apache/2.4.37 (Amazon)
Server MPM:     event

Set PHP to use FastCGI

In order to take advantage of event MPM, PHP has to run under FastCGI, so install it first. Check the version of PHP that you’re running and install the right version:

$php -v
PHP 7.0.33 (cli) (built: Jan  9 2019 22:04:26) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
$yum search fpm
Loaded plugins: priorities, update-motd, upgrade-helper
========================================= N/S matched: fpm =========================================
 php-fpm.x86_64 : PHP FastCGI Process Manager
 php54-fpm.x86_64 : PHP FastCGI Process Manager
 php55-fpm.x86_64 : PHP FastCGI Process Manager
 php56-fpm.x86_64 : PHP FastCGI Process Manager
 php70-fpm.x86_64 : PHP FastCGI Process Manager
 php71-fpm.x86_64 : PHP FastCGI Process Manager
 php72-fpm.x86_64 : PHP FastCGI Process Manager
 Name and summary matches only, use "search all" for everything.
$sudo yum install php70-fpm -y

As I was using PHP 7.0, that is the version that I installed.

Update php.conf to use FastCGI:

$sudo vim /etc/httpd/conf.d/php.conf

Comment out SetHandler application/x-httpd-php and add SetHandler “proxy:fcgi://”:

#SetHandler application/x-httpd-php
SetHandler "proxy:fcgi://"

Start php-fpm and set it to auto-start:

$sudo service php-fpm start
Starting php-fpm-5.6:                                      [  OK  ]
$chkconfig --add php-fpm
$chkconfig php-fpm on
$chkconfig --list php-fpm
php-fpm         0:off   1:off   2:on    3:on    4:on    5:on    6:off

Restart httpd service and check your phpinfo, if it shows Server API as FPM/FastCGI, then it’s running properly.

Enable HTTP/2 in Virtual Host setting

Lastly, in your virtual host setting, add Protocols h2 http/1.1 line at the top.

      Protocols h2 http/1.1
      DocumentRoot /var/www/html

Save and restart httpd (again).

Verify HTTP/2 is working

I’ve found best way to check if HTTP/2 is working properly is simply checking with a browser’s developer console. Enable developer console with F12 key and move to Network tab. Now access your site normally. In Firefox, click on the / entry at the top, the Version should show as HTTP/2.0. In Chrome, the Protocol column should show h2. If you don’t see the Protocol column, right click on any column header and check Protocol to enable it.


1 thought on “HTTP/2 on Amazon Linux instance

  1. Pingback: Tweaking PHP-FPM and Apache for Amazon Linux t2.micro instance - PeacefulAn

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.