Set PHP realpath_cache_size 'correctly'

<?php
var_dump(realpath_cache_size());
?>

Enjoy!
realpath_cache_size = 4M
realpath_cache_ttl = 120

Note that if your using Apache Prefork-mpm mod_php to run PHP (the fastest when loading only PHP and not with static content) then the realpath_cache_size is used for each apache2 client launched. So if maxclients is set to 50 in your Apache2 config and you have, say 40 clients spawned, a setting of realpath_cache_size = 1M will apply 40 times! That was 2012 advice; if you are still using Apache, you should avoid mpm-prefork and instead use mpm_event or mpm_worker with PHP-FPM. Or switch to Nginx.
Originally published: Sep 6th, 2012 | Last updated: April 15th 2024
As you search the web, you’ll find blog posts telling you to set realpath_cache_size to 128K, 512K and even 1M. These suggestions are made blindly without knowing how much storage is being used. I thought to share the solution with those who find this article.
My advice, set yours to the new default for PHP 7+ of 4M, check storage using the instructions above and finally, reduce the size only if you have low server memory. For best performance, also tune realpath_cache_ttl. Again set it large, then tune it down to size.
PHP’s default:

Setting realpath_cache_size

int(178356)

In 2012, I started enabling PHP realpath_cache_size and realpath_cache_ttl for performance benefits. At the time, I followed the settings I found here (has since been deleted). It bugged me that I was blindly setting the cache size without knowing how much storage was actually being used. In this post, I will demonstrate how to view the size and contents of realpath_cache. Thus enabling you to tune settings based on your usage.
Here are my current realpath_cache settings in php.ini on a 3GB StackLinux VPS hosting this blog:
realpath_cache_size = 1M
realpath_cache_ttl = 300

Simply create a new PHP file. You can name it anything (I used: rpc_size.php), then paste the code example listed here into that file:

var_dump(realpath_cache_get());

If safe_mode or open_basedir are enabled in your php.ini, then see this bug (workaround at the bottom of the page). Basically, realpath cache is not used if safe_mode is on or if open_basedir restriction is enabled, thus causing many calls to lstat.

Similar Posts