Search code examples
phpwordpressamazon-web-servicesamazon-ec2fpm

Optimizing PHP-FPM Configuration for WordPress on Bitnami EC2 t3a.medium instance


I'm running a WordPress website on an AWS EC2 t3a.medium (4Gb Ram) instance using Bitnami. The server's RAM often gets completely filled, causing the server to crash and I need to restart it from ec2 console.

I've looked into tuning my php-fpm settings to mitigate this issue.

Here is the current configuration:

pm.max_children=60
pm.start_servers=40
pm.min_spare_servers=40
pm.max_spare_servers=45
pm.max_requests=5000

Maybe its could be too large! Here is the configuration I'm considering:

pm.max_children = 47
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers = 20
pm.max_requests = 1000
  1. Is this a reasonable configuration for a t3a.medium instance with 4GB RAM? Any suggestions for improvement?

  2. What are the best methods to monitor the system after applying these settings to ensure they're effective?

this is the htop command ordered by RAM Descending:

enter image description here

Thank you

MariaDB configuration file:

[mysqladmin]
user=****************

[mysqld]
skip_name_resolve
explicit_defaults_for_timestamp
basedir=/opt/bitnami/mariadb
port=3306
tmpdir=/opt/bitnami/mariadb/tmp
socket=/opt/bitnami/mariadb/tmp/mysql.sock
pid_file=/opt/bitnami/mariadb/tmp/mysqld.pid
max_allowed_packet=16M
bind_address=127.0.0.1
log_error=/opt/bitnami/mariadb/logs/mysqld.log
slow_query_log=1
slow_query_log_file=/opt/bitnami/mariadb/logs/mysqld_slowquery.log
long_query_time=10.0
character_set_server=utf8
collation_server=utf8_general_ci
plugin_dir=/opt/bitnami/mariadb/lib/plugin

[client]
port=3306
socket=/opt/bitnami/mariadb/tmp/mysql.sock
default_character_set=utf8
plugin_dir=/opt/bitnami/mariadb/lib/plugin

[manager]
port=3306
socket=/opt/bitnami/mariadb/tmp/mysql.sock
pid_file=/opt/bitnami/mariadb/tmp/mysqld.pid
!include /opt/bitnami/mariadb/conf/bitnami/memory.conf

memory.conf:

long_query_time = 1
query_cache_limit=2M
query_cache_type=1
query_cache_size=128M
innodb_buffer_pool_size=256M

Solution

  • You have your php and MariaDB server software running on the same machine, but you didn't show us any MariaDB configuration data. MariaDB can use a lot of RAM, and it's impossible to make detailed tuning suggestions without knowing more about that.

    That being said: You should consider two possible failure modes.

    One is memory leaks in your php / WordPress code. If these exist, your php processes ("children") will consume more and more RAM until they exit and respawn when they have handled pm.max_requests each. As an experiment you can reduce pm.max_requests to a much smaller number than 5000. 100 might be a good number to try. If your RAM exhaustion problem clears up, php / WordPress memory leaks were the problem. (Next you'll ask how to fix them, but that's another question, and answering it requires knowledge of which plugins you run and all that.)

    Another is simply too many simultaneous php processes for your RAM configuration. To test this hypothesis reduce your pm.max_children. Try cutting it in half. Sometimes, paradoxically, fewer processes can improve response time. That's because fewer concurrent requests cause less contention / congestion in your server software.

    Longer term, you might consider putting your MariaDB server on a different VM, if your traffic and revenue justify it.