{"id":272,"date":"2022-01-14T09:24:58","date_gmt":"2022-01-14T07:24:58","guid":{"rendered":"https:\/\/greenhouse.cv.ua\/?p=272"},"modified":"2022-01-14T09:24:58","modified_gmt":"2022-01-14T07:24:58","slug":"enable-http-2-in-apache-2-4","status":"publish","type":"post","link":"https:\/\/greenhouse.cv.ua\/?p=272","title":{"rendered":"Enable HTTP\/2 in Apache 2.4"},"content":{"rendered":"\n<p>Based on https:\/\/gist.github.com\/GAS85\/8dadbcb3c9a7ecbcb6705530c1252831<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Requirements<\/h1>\n\n\n\n<ul class=\"wp-block-list\"><li>A self-managed VPS or dedicated server with Ubuntu 20.04 running Apache 2.4.xx.<\/li><li>A registered domain name with working HTTPS (TLS\/SSL). HTTP\/2 only works alongside HTTPS because most browsers, including Firefox and Chrome, don\u2019t support HTTP\/2 in cleartext (non-TLS) mode.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Step 1: Install Apache2<\/h2>\n\n\n\n<p>Per default it will be <a href=\"https:\/\/packages.ubuntu.com\/focal\/web\/apache2\">apache2 version 2.4.41<\/a> what is enought for http2 support.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  sudo apt install apache2<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 2: Tell Apache to use PHP FastCGI<\/h2>\n\n\n\n<p>You want to make Apache use a compatible PHP implementation by changing mod_php to php-fpm (PHP FastCGI). If your website or app breaks on FastCGI, you can always revert back to mod_php until further troubleshooting.<\/p>\n\n\n\n<p>Install PHP FastCGI module for PHP 7.4, it is <a href=\"https:\/\/packages.ubuntu.com\/focal\/php\/php-fpm\">default version for Ubuntu 20.04<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt install php7.4-fpm <\/code><\/pre>\n\n\n\n<p>Enable required modules, <strong>proxy_fcgi<\/strong> and <strong>setenvif<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo a2enmod proxy_fcgi setenvif<\/code><\/pre>\n\n\n\n<p>Enable <strong>php7.4-fpm<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo a2enconf php7.4-fpm <\/code><\/pre>\n\n\n\n<p><em>Disable<\/em> the <strong>mod_php<\/strong> module:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo a2dismod php7.4<\/code><\/pre>\n\n\n\n<p>Restart Apache:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo service apache2 restart<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 3: Change MPM from &#8220;prefork&#8221; to &#8220;event&#8221;<\/h2>\n\n\n\n<p>Since the default &#8220;prefork&#8221; MPM (Multi-Processing Module) is not fully compatible with HTTP\/2, you\u2019ll need to change Apache\u2019s current MPM to &#8220;event&#8221; (or &#8220;worker&#8221;). This is shown by the error message in Apache versions greater than 2.4.27 as \u2013 AH10034: The mpm module (prefork.c) is not supported by mod_http2.<\/p>\n\n\n\n<p>Keep in mind that your server requires more horsepower for HTTP\/2 than for HTTP\/1.1, due to the multiplexing feature and other factors. That said, smaller servers with low traffic may not see much difference in performance.<\/p>\n\n\n\n<p>First, disable the &#8220;prefork&#8221; MPM:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo a2dismod mpm_prefork <\/code><\/pre>\n\n\n\n<p>Enable the &#8220;event&#8221; MPM:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo a2enmod mpm_event <\/code><\/pre>\n\n\n\n<p>Restart Apache2 and PHP 7.4:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo service apache2 restart &amp;&amp; sudo service php7.4-fpm restart<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 4: Add a line to your Virtual Host file<\/h2>\n\n\n\n<p>Add the following line to your site\u2019s current Virtual Host config file. This can go anywhere between the \u2026 tags. If you want to serve HTTP\/2 for all your sites, add this to your global \/etc\/apache2\/apache2.conf file instead of per each individual site\u2019s Virtual Host file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Protocols h2 h2c http\/1.1<\/code><\/pre>\n\n\n\n<p><em>Explanation<\/em>: h2 is TLS-encrypted HTTP\/2, h2c is cleartext HTTP\/2, and http\/1.1 is ordinary HTTP\/1.1.<\/p>\n\n\n\n<p>Having http\/1.1 at the end of the line provides a fallback to HTTP\/1.1, while h2c is not strictly necessary.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Step 5: Enable the mod_http2 Apache module<\/h2>\n\n\n\n<p>Now you can enable the http2 module in Apache:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo a2enmod http2<\/code><\/pre>\n\n\n\n<p>Check Apache2 config and if no errors, restart Apache:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apachectl configtest &amp;&amp; sudo service apache2 restart<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Step 6 create http2.conf for entire Server HTTP2<\/h2>\n\n\n\n<p>Create a new http2.conf<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/apache2\/conf-available\/http2.conf<\/code><\/pre>\n\n\n\n<p>and add all the following rows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;IfModule http2_module&gt;\n    Protocols h2 h2c http\/1.1\n    H2Direct on\n&lt;\/IfModule&gt;<\/code><\/pre>\n\n\n\n<p>Enable the http2.conf by running<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo a2enconf http2<\/code><\/pre>\n\n\n\n<p>Check Apache2 config and if no errors, restart your Apache2<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apachectl configtest &amp;&amp; sudo service apache2 restart<\/code><\/pre>\n\n\n\n<p>and enhance your ssl-vhost file (default-ssl.conf):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo nano \/etc\/apache2\/sites-available\/default-ssl.conf<\/code><\/pre>\n\n\n\n<p>Amend in your configuration file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>...\nProtocols h2 h2c http\/1.1\nH2Push on\nH2PushPriority * after\nH2PushPriority text\/css before\nH2PushPriority image\/jpg after 32\nH2PushPriority image\/jpeg after 32\nH2PushPriority image\/png after 32\nH2PushPriority application\/javascript interleaved\n...<\/code><\/pre>\n\n\n\n<p>P.S. All in one command (you still have to edit your VirtualHost and ssl config):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update\nsudo apt upgrade\nsudo apt install apache2 php7.4-fpm \nsudo a2enmod proxy_fcgi setenvif\nsudo a2enconf php7.4-fpm \nsudo a2dismod php7.4 \nsudo service apache2 restart\nsudo a2dismod mpm_prefork \nsudo a2enmod mpm_event \nsudo service apache2 restart \nsudo service php7.4-fpm restart\nsudo a2enmod http2\nsudo service apache2 restart\nsudo echo \"&lt;IfModule http2_module&gt;\" &gt; \/etc\/apache2\/conf-available\/http2.conf\nsudo echo \"Protocols h2 h2c http\/1.1\" &gt;&gt; \/etc\/apache2\/conf-available\/http2.conf\nsudo echo \"H2Direct on\" &gt;&gt; \/etc\/apache2\/conf-available\/http2.conf\nsudo echo \"&lt;\/IfModule&gt;\" &gt;&gt; \/etc\/apache2\/conf-available\/http2.conf\nsudo a2enconf http2\nsudo apachectl configtest &amp;&amp; sudo service apache2 restart<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Based on https:\/\/gist.github.com\/GAS85\/8dadbcb3c9a7ecbcb6705530c1252831 Requirements A self-managed VPS or dedicated server with Ubuntu 20.04 running Apache 2.4.xx. A registered domain name with working HTTPS (TLS\/SSL). HTTP\/2 . . .<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[7],"tags":[],"class_list":["post-272","post","type-post","status-publish","format-standard","hentry","category-linux"],"aioseo_notices":[],"featured_image_src":null,"author_info":{"display_name":"Bernyk Dmytro","author_link":"https:\/\/greenhouse.cv.ua\/?author=2"},"_links":{"self":[{"href":"https:\/\/greenhouse.cv.ua\/index.php?rest_route=\/wp\/v2\/posts\/272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/greenhouse.cv.ua\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/greenhouse.cv.ua\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/greenhouse.cv.ua\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/greenhouse.cv.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=272"}],"version-history":[{"count":2,"href":"https:\/\/greenhouse.cv.ua\/index.php?rest_route=\/wp\/v2\/posts\/272\/revisions"}],"predecessor-version":[{"id":274,"href":"https:\/\/greenhouse.cv.ua\/index.php?rest_route=\/wp\/v2\/posts\/272\/revisions\/274"}],"wp:attachment":[{"href":"https:\/\/greenhouse.cv.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=272"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/greenhouse.cv.ua\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=272"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/greenhouse.cv.ua\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}