Set PHP realpath_cache_size ‘correctly’
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:
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.
realpath_cache_size = 4M realpath_cache_ttl = 120
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
Oh, and to view the contents of realpath_cache, use:
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 lots of calls to lstat.
Originally published: Sep 6th, 2012 | Last updated: January 4th 2023
I’ve never even heard of this setting. Does this only apply to Apache servers?
I currently am running a Litespeed server and an nginx server with PHP-FPM, wondering if this is something I should/can change.
It applies to PHP. See here
Thanks for this.
I had to clear the
open_basedirin my control panel (runcloud) in order to get this function to work.
I also notice that the value varies widely, depending on where I dump the variable. On a blank page, like in the code you shared, it tends to be small. When injected into a template within BuddyPress, it ranges from 30-100KB. So, I suppose I could trim the cache size from 4MB to 1MB as you did. But, it seems to me that that’s pretty inconsequential on most servers these days so won’t even bother.
The real question is with regards to
open_basedir- whether it should be on or off, and whether the
realpath_cacheactually makes a meaningful performance difference…
Good points. Realpath cache reduces disk lookups. So really, with storage speeds vastly improving over the years… NMVe/SSDs it’s probably not as big of gains as it was back in 2012 when I originally published this article.
Welcome to the forums.