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.
Checking realpath_cache_size usage
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.
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:
<?php var_dump(realpath_cache_size()); ?>
Next, upload it to your web server (eg. “/var/www/html/”), visit yoursite.com/rpc_size.php and that page will return how much memory realpath cache is currently using. Like this:
int(178356)
So in my case, I’m using 178KB of realpath_cache. I had realpath_cache_size set to 4M and so I was able to reduce it to “realpath_cache_size = 1M” in php.ini. The default realpath_cache_size was increased to 4M (4096K). Prior to PHP 7.0.16 and 7.1.2, the default was “16K”
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.
PHP’s default:
realpath_cache_size = 4M realpath_cache_ttl = 120
Setting realpath_cache_size
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.
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
To view the contents of realpath_cache, use:
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.
Enjoy!
Originally published: Sep 6th, 2012 | Last updated: April 15th 2024