Improving the click to deploy solution for WordPress on Google Cloud Platform

If you’re using this recipe to run WordPress on a virtual machine on the Google Cloud Platform (GCP) chances are high you’re experiencing problems. Here’s how to solve them.

My example is a customer’s website running on a small instance of type e2-small. This instance only has 2G of RAM and 2 vCPUs.

Main problem of the setup is that it runs Apache webserver using mpm_worker process management model with PHP integrated as Apache module. This way Apache spawns multiple processes with separate memory areas each running a copy of the PHP interpreter together with all the PHP code needed to executed WordPress. That is quite a lot. For the website i was analyzing Apache had a resident set size of 1,75G without much traffic. So there wasn’t much RAM left for mysql and once some requests came in it lead to mysqld being killed due to out-of-memory errors.

One could argue that running an website on such a small instance was prone for error from missing resources from the beginning. But from my experience i always try to increasing efficiency of a given runtime environment before increasing resources because problems persist and eventually increased resources will be also be maxed out.

So how to improve this setup? Just switch Apache to event based process management mpm_event and run PHP separately as PHP-FPM using FastCGI as interface between Apache and PHP. This way the memory footprint of Apache decreases dramatically and PHP’s usage resources can be managed independently by configuration.

So here is a quick recipe:

Activate Apache mpm_event and PHP-FPM

sudo su -
systemctl stop apache2
a2dismod php7.4
a2dismod mpm_prefork
a2enmod mpm_event
apt install php7.4-fpm libapache2-mod-fcgid
a2enconf php7.4
a2enconf php7.4-fpm
a2enmod proxy
a2enmod proxy_fcgi
apachectl configtest && systemctl restart apache2

Using this recipe the website i was analyzing went from having no RAM left to ~800M of RAM available. Monitoring will show how the website keeps up with traffic.