<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Louis-Philippe Véronneau - php</title><link href="https://veronneau.org/" rel="alternate"></link><link href="https://veronneau.org/feeds/tags/php.atom.xml" rel="self"></link><id>https://veronneau.org/</id><updated>2018-12-22T00:00:00-05:00</updated><entry><title>A Tale of HTTP/2</title><link href="https://veronneau.org/a-tale-of-http2.html" rel="alternate"></link><published>2018-12-22T00:00:00-05:00</published><updated>2018-12-22T00:00:00-05:00</updated><author><name>Louis-Philippe Véronneau</name></author><id>tag:veronneau.org,2018-12-22:/a-tale-of-http2.html</id><summary type="html">&lt;p&gt;Around a month ago, someone mentioned the existence of &lt;a href="https://en.wikipedia.org/wiki/HTTP/2"&gt;HTTP/2&lt;/a&gt; in an IRC
channel I lurk in. For some reason, I had never heard of it and some of the
features of this new protocol (like mutiplexing requests without having to open
multiple TCP connections) seemed cool.&lt;/p&gt;
&lt;p&gt;To be …&lt;/p&gt;</summary><content type="html">&lt;p&gt;Around a month ago, someone mentioned the existence of &lt;a href="https://en.wikipedia.org/wiki/HTTP/2"&gt;HTTP/2&lt;/a&gt; in an IRC
channel I lurk in. For some reason, I had never heard of it and some of the
features of this new protocol (like mutiplexing requests without having to open
multiple TCP connections) seemed cool.&lt;/p&gt;
&lt;p&gt;To be honest, I had just finished re-writing the Puppet code that manages our
backup procedures and enabling HTTP/2 seemed like a productive way to
procrastinate before moving on to an another large project. How hard could this
be?&lt;/p&gt;
&lt;p&gt;Turns out it took me around 25 hours of work... Sit back and put on comfortable
slippers, for this is a tale of HTTP/2!&lt;/p&gt;
&lt;p&gt;&lt;a href="https://commons.wikimedia.org/wiki/File:The_Yule_Log.jpg"&gt;&lt;img src="/media/blog/2018-12-22/yule_log.jpg" title="The Yule Log" alt="The Yule Log" height="80%" width="80%" style="margin-left: 10%;"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;Cursed Be the HTTP/1.1&lt;/h2&gt;
&lt;p&gt;When I first looked up how to enable HTTP/2 on Apache it seemed a pretty simple
task. The documentation mentioned loading the &lt;code&gt;http2&lt;/code&gt; module and making sure to
prioritise the new protocol via a configuration file like this one:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="nb"&gt;Protocols&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;h2&lt;span class="w"&gt; &lt;/span&gt;h2c&lt;span class="w"&gt; &lt;/span&gt;http/1.1

&lt;span class="nb"&gt;H2Push&lt;/span&gt;&lt;span class="w"&gt;          &lt;/span&gt;&lt;span class="k"&gt;on&lt;/span&gt;
&lt;span class="nb"&gt;H2PushPriority&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;*&lt;span class="w"&gt;                       &lt;/span&gt;after
&lt;span class="nb"&gt;H2PushPriority&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;text/css&lt;span class="w"&gt;                &lt;/span&gt;before
&lt;span class="nb"&gt;H2PushPriority&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;image/jpeg&lt;span class="w"&gt;              &lt;/span&gt;after&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="m"&gt;32&lt;/span&gt;
&lt;span class="nb"&gt;H2PushPriority&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;image/png&lt;span class="w"&gt;               &lt;/span&gt;after&lt;span class="w"&gt;   &lt;/span&gt;&lt;span class="m"&gt;32&lt;/span&gt;
&lt;span class="nb"&gt;H2PushPriority&lt;/span&gt;&lt;span class="w"&gt;  &lt;/span&gt;application/javascript&lt;span class="w"&gt;  &lt;/span&gt;interleaved
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;This would of course have been too easy. Even if everything in Apache was set up
properly, websites kept being served as HTTP/1.1. I was obviously doing
something right though, since my websites were now sending a new HTTP header:
&lt;code&gt;Upgrade: h2, h2c&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;After wasting a good deal of time debugging TLS ciphers (HTTP/2 is &lt;a href="https://http2.github.io/http2-spec/#TLSUsage"&gt;incompatible
with TLS 1.1&lt;/a&gt;), I finally found out the problem was that we weren't
using the right multi-processing module for Apache.&lt;/p&gt;
&lt;p&gt;Turns out Apache won't let you serve HTTP/2 while using &lt;code&gt;mpm_prefork&lt;/code&gt; (the
default MPM), as it is not supported by &lt;code&gt;mod_http2&lt;/code&gt;. Even though there are
two other MPM you can use with Apache, only &lt;code&gt;mpm_prefork&lt;/code&gt; supports &lt;code&gt;mod_php&lt;/code&gt;.
Suddenly, adding support for HTTP/2 meant switching all our webapps built in
PHP to PHP-FPM...&lt;/p&gt;
&lt;h2&gt;Down the Rabbit Hole&lt;/h2&gt;
&lt;p&gt;&lt;img src="/media/blog/2018-12-22/mod_php.gif" title="A clip from Alice in Wonderlands" alt="A clip from Alice in Wonderlands" height="30%" width="30%" style="float:right;"&gt;&lt;/p&gt;
&lt;p&gt;For the longest time, a close friend has been trying to convince me of the
virtues of &lt;a href="https://wiki.apache.org/httpd/PHP-FPM"&gt;PHP-FPM&lt;/a&gt;. As great as it looked on paper, I never really
did anything about it. It seemed so ... complicated. Regular ol' &lt;code&gt;mod_php&lt;/code&gt; did
the trick just fine and other things required my attention.&lt;/p&gt;
&lt;p&gt;This whole HTTP/2 thing turned out to be the perfect excuse for me to dive into
it after all. Once I understood how FPM pools worked, it was actually
pretty easy to set up. Since I had to rewrite the Puppet profiles we're using to
deploy websites, also I took that opportunity to harden a bunch of things left
and right.&lt;/p&gt;
&lt;p&gt;PHP-FPM let's you run websites under different Unix users for added separation.
On top of that, I decided it was time for PHP code on our servers to be ran in
read-only mode and had to tweak a bunch of things for our Wordpress, Nextcloud,
KanBoard and Drupal instances to stop complaining about it.&lt;/p&gt;
&lt;p&gt;After too much time passed automating tasks in Puppet, I finally was able to
turn off &lt;code&gt;mod_php&lt;/code&gt; and &lt;code&gt;mpm_prefork&lt;/code&gt; everywhere and to enable &lt;code&gt;mpm_event&lt;/code&gt; and
&lt;code&gt;mod_http2&lt;/code&gt;. The speed bonus offered by PHP-FPM and HTTP/2 is nice, but more
than anything I'm happy this whole ordeal forced me to harden the way our
Apache servers deal with PHP.&lt;/p&gt;
&lt;p&gt;&lt;img src="/media/blog/2018-12-22/victory.png" title="Victory!" alt="Victory!" height="80%" width="80%" style="margin-left: 10%;"&gt;&lt;/p&gt;</content><category term="blog"></category><category term="debian"></category><category term="php"></category><category term="apache"></category></entry></feed>