Reduce RAM usage for your WordPress websites

I’m using for most of my servers ServerPilot as the server management tool. With ServerPilot your server will be installed an optimized with Nginx and PHP-FPM. This configuration makes your server very fast.

If you host multiple WordPress websites on your VPS you need to check the RAM memory usage frequently. Even if your WP sites doesn’t have a lot of traffic, they might consume a lot of memory. One of the reason might be the dynamic PHP-FPM configuration, which is very good because each site will respond fast even after some time of inactivity. This happens because there is always at least one active task running inside the applications PHP-FPM pool. If your website becomes more active, it’s possible that a websites pool will activate (and keep) more than one tasks. At this moment your low-traffic website might consume more than 250MB of RAM memory!

NOTE, most configurations on this page are based on the settings for a VPS controlled by ServerPilot!

PHP-FPM dynamic or better ondemand?

The dynamic configuration is good for website with a decent traffic, but for smaller sites a short brute-force attack might consume all available RAM memory. The problem in that case is the PHP-FPM will reserver this higher memory amount for hours. If this is a problem on your server you use the ondemand configuration

PHP-FPM ondemand configuration for a ServerPilot controlled VPS

Start by by renaming this file (replace the php5.x with your app’s PHP version and replace your app name with APPNAME) :

/etc/php5.X-sp/fpm-pools.d/ APPNAME.d/main.conf

to:

/etc/php5.X-sp/fpm-pools.d/ APPNAME.d/main.custom.conf

Open/edit the renamed file and change the process manager configurations in that file with (this will keep/disable the old settings, just in case you like to change it later):

;pm = dynamic
;pm.start_servers = 1
;pm.max_children = 40
;pm.min_spare_servers = 1
;pm.max_spare_servers = 10

pm = ondemand
pm.max_children = 40
pm.process_idle_timeout = 3600s

Restart PHP-FPM now with:

service php5.x-fpm-sp restart

Disable wp_cron and use a real CRON job

I have noticed that in some situations (depends on the used WordPress themes and plugins) WordPress will execute the wp_cron.-php file very often. It’s much better to disable the wp_cron feature and to use a real CRON jog to execute that PHP file.

Open the wp-config.php file and add this row:

 define('DISABLE_WP_CRON', true);

Add a CRON job with (replace SRVUSER with the user name which is configured with the app):

 crontab -e -u SRVUSER

place this job into the file (replace also the right PHP version and the APPNAME):

*/5 * * * * php5.x-sp /srv/users/SRVUSER/apps/APPNAME/public/wp-cron.php

Disable PHP’s Opcache

The PHP Opcache is enabled by default since PHP version 5.5. If you like to save a bit of RAM memory, it’s possible to disable the Opcache feature. Note, this works the best if you use WP Super Cache in mod_rewrite mode!

To disable PHP’s Opcache, you can edit the following file(s) (replace the PHP version 5.5 or 5.6):

/etc/php5.x-sp/conf.d/opcache.ini

add the following line to the end of each of these files:

opcache.enable=0

and finally restart each PHP version:

sudo service php5.x-fpm-sp restart

Don’t tweak to much at the same time, some of the modifications might work better than others. If you like to share your own tweaks, please post them as a comment.

3 thoughts on “Reduce RAM usage for your WordPress websites”

  1. I found this post from Mattias Geniar where he gives some advice about how to optimize PHP-FPM for smaller websites: https://ma.ttias.be/a-better-way-to-run-php-fpm/

    His advice is also about to use “ondemand” instead of “dynamic” for low-traffic sites. His settings are a bit different and will use less RAM memory:

    pm = ondemand
    pm.max_children = 5
    pm.process_idle_timeout = 10s
    pm.max_requests = 200
    

    I totally agree with him because the value of 40 child processes is a bit high. Last week I got some serious problems where a site with WooCommerce installed has used 1.5 GB of RAM because of some visits by a nasty bot. Right now I’m testing one site with 5 child processes and experienced no problems while I did some WordPress updates. Maybe the value is a bit low, what do you think?

  2. Sometimes the wp_options table has a got lot of records where the value for “autoload” is set to “yes”. This column doesn’t have an index by default.
    Check how long it takes by executing this query in phpMyAdmin:

    SELECT option_name, option_value FROM wp_options WHERE autoload = 'yes';

    If this query takes several seconds, you need to add an index for the column “autoload”:

    ALTER TABLE `wp_options` ADD INDEX (`autoload`);

    I have seen several sites where this optimization was huge performance improvement for the database and RAM usage.

  3. I did some load tests with 50 virtual users to test the php-fpm settings: pm.max_children = 5

    The result for a cached page was: avg load 1,3 sec. and the result for the same page with cache disabled was avg load 2 sec.

    After that test I changed the max. number to 10 and 15. With the result for both settings that the avg load time went down to 1.8 secs. I think the value max. 10 child process is a good value for the tested website.
    Next time I will do the same test for a WooCommerce site :)

Comments are closed.