If you have followed older guides to configure the server to install WordPress, then you have followed the guides to install Mod PHP with MPM prefork. presently, the usual mod_php
and prefork MPM
module of Apache2 does not work with HTTP/2 module. The default prefork MPM
is not fully compatible with HTTP/2. If you are running Apache alongside the mod_php
module, you need to switch to PHP-FPM. You need to uninstall the prefork MPM
and switch to the mpm_event
module which will be supported by HTTP/2. Apache2 is always easy. All you have to do is running some commands to do the things we mentioned above and remember what you have done. In case you want to switch back to mod_php
with MPM prefork
, that is just easy. In short – if you are following this guide then bookmark it.
How to Enable HTTP/2 on Apache 2.4 for PHP/WordPress
If you are not using the PPA from Ondřej Surý then do these steps :
1 2 3 | sudo add-apt-repository ppa:ondrej/apache2 sudo apt update sudo apt upgrade |
We are writing the commands for PHP 7.0, you can modify and use it for PHP 7.1, PHP 7.2, PHP 7.3, PHP 7.4 and so on. We are changing mod_php
to php-fpm
(PHP FastCGI). The setup will be somewhat like Nginx – you have to manually restart PHP-FPM and Apache2 for many settings to work.
---
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | sudo apt install php7.0-fpm # Enable proxy_fcgi and setenvif sudo a2enmod proxy_fcgi setenvif # Enable php7.0-fpm sudo a2enconf php7.0-fpm # Disable the mod_php module sudo a2dismod php7.0 # Restart Apache2 sudo service apache2 restart # Disable the "prefork" MPM sudo a2dismod mpm_prefork # Enable the "event" MPM sudo a2enmod mpm_event # Restart the services sudo service apache2 restart sudo service php7.0-fpm restart # Enable HTTP/2 module sudo a2enmod http2 # Restart Apache2 sudo service apache2 restart |
Basic optimization
Inside the /etc/apache2/mods-enabled/
directory, you’ll get the configuration files for mpm_event, http2 etc.
1 2 | cd /etc/apache2/mods-enabled/ ls |
You can open the file and modify :
1 | sudo nano /etc/apache2/mods-enabled/http2.conf |
You can run a configtest :
1 2 3 | sudo apachectl configtest # and restart apache sudo service apache2 restart |
This is all default enabled http2.conf file :
1 2 3 4 5 6 7 8 9 10 11 | <IfModule !mpm_prefork> Protocols h2 h2c http/1.1 H2Direct on H2Push on H2PushPriority * After 16 H2PushPriority * after H2PushPriority text/css before H2PushPriority image/jpeg after 32 H2PushPriority image/png after 32 H2PushPriority application/javascript interleaved </IfModule> |
Explanation is available on the official doc :
1 | https://httpd.apache.org/docs/2.4/mod/mod_http2.html |
Now open the mpm_event.conf
file :
1 | sudo nano /etc/apache2/mods-enabled/mpm_event.conf |
This is optimized settings for a 16GB server :
1 2 3 4 5 6 7 8 9 10 | <IfModule mpm_event_module> ServerLimit 2800 StartServers 4 MinSpareThreads 25 MaxSpareThreads 75 ThreadLimit 64 ThreadsPerChild 25 MaxRequestWorkers 2800 MaxConnectionsPerChild 1000 </IfModule> |
Now about PHP FPM (which is exactly like we do with Nginx) :
1 | nano /etc/php/7.0/fpm/pool.d/www.conf |
This should be enough for a 16GB server :
1 2 3 4 5 6 7 | pm = dynamic pm.max_children = 256 pm.start_servers = 20 pm.min_spare_servers = 10 pm.max_spare_servers = 20 ;pm.process_idle_timeout = 10s; pm.max_requests = 1000 |
Restart the services:
1 2 | sudo service php7.0-fpm restart sudo service apache2 restart |
How to revert back?
The easy way is to run the opposite commands, like a2dismod against previous a2enmod and vice-versa. However, it is practical to take a backup of all the configuration files of Apache, purge everything and freshly install Apache2 and PHP. Unlike Nginx, PHP-FPM does not hugely disturb with Apache.
Tagged With apache prefork http2