Realpath cache size php ini

Set the «realpath cache size»

One of my extensions asks me to set the realpath_cache_size to a minimum of 512k. So do i have to set it inside of the .htaccess like:

realpath_cache_size = 512k 

And what are the consequences for the whole site, is it ok or too high? I don´t understand this cache size. Thanks for help.

1 Answer 1

The size can be change in php.ini file:

The line below determines the size of the realpath cache to be used by PHP.

This sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts for eating up all available memory on a server. Note that to have no memory limit, set this directive to -1.

Prior to PHP 5.2.1, in order to use this directive it had to be enabled at compile time by using —enable-memory-limit in the configure line. This compile-time flag was also required to define the functions memory_get_usage() and memory_get_peak_usage() prior to 5.2.1.

When an integer is used, the value is measured in bytes. Shorthand notation, as described in this FAQ, may also be used. See also: max_execution_time.

Performance Tuning Name Default Changeable Changelog realpath_cache_size «16K» PHP_INI_SYSTEM Available since PHP 5.1.0. realpath_cache_ttl «120» PHP_INI_SYSTEM Available since PHP 5.1.0. Here’s a short explanation of the configuration directives.

Читайте также:  Learning python 5th edition fifth edition

realpath_cache_size integer Determines the size of the realpath cache to be used by PHP. This value should be increased on systems where PHP opens many files, to reflect the quantity of the file operations performed.

The size represents the total number of bytes in the path strings stored, plus the size of the data associated with the cache entry. This means that in order to store longer paths in the cache, the cache size must be larger. This value does not directly control the number of distinct paths that can be cached.

The size required for the cache entry data is system dependent.

realpath_cache_ttl integer Duration of time (in seconds) for which to cache realpath information for a given file or directory. For systems with rarely changing files, consider increasing the value.

Источник

10 Essential PHP.ini Tweaks for Improved Web Performance

If you’re running a website or web application with PHP, you may have encountered issues with slow loading times, high memory usage, or other performance problems. Fortunately, there are several tweaks you can make to your PHP configuration file (php.ini) to optimize your scripts and improve your website’s performance. In this article, I’ll cover the top 10 most common changes you might need to make to your php.ini file for best performance.

Increase memory_limit

The memory_limit setting controls the maximum amount of memory that PHP can use. If you’re running scripts that require more memory than the default value, you’ll need to increase this limit. To increase the memory_limit, add the following line to your php.ini file:

This sets the memory_limit to 256 megabytes, which should be sufficient for most web applications.

Increase max_execution_time

The max_execution_time setting controls the maximum amount of time (in seconds) that a script can run before being terminated. If you’re running long-running scripts, you’ll need to increase this limit. To increase the max_execution_time, add the following line to your php.ini file:

This sets the max_execution_time to 120 seconds, which should be sufficient for most web applications.

Disable display_errors

The display_errors setting controls whether errors and warnings are displayed to the user. For security reasons, it’s best to disable this setting on a production server. To disable display_errors, add the following line to your php.ini file:

This disables display_errors, preventing error messages from being displayed to the user.

Enable opcache

OPcache is a caching engine for PHP that can significantly improve the performance of your scripts by reducing the amount of time it takes to parse and execute them. To enable OPcache, add the following lines to your php.ini file:

zend_extension=opcache opcache.enable=1

This enables OPcache and sets it to active.

Increase upload_max_filesize

The upload_max_filesize setting controls the maximum size of files that can be uploaded to your server. If you’re running a website that allows users to upload large files, you’ll need to increase this limit. To increase upload_max_filesize, add the following line to your php.ini file:

This sets the upload_max_filesize to 64 megabytes, which should be sufficient for most web applications.

Increase post_max_size

The post_max_size setting controls the maximum size of data that can be submitted via a form. If your forms submit large amounts of data, you may need to increase this limit. To increase post_max_size, add the following line to your php.ini file:

This sets the post_max_size to 64 megabytes, which should be sufficient for most web applications.

Disable allow_url_fopen

The allow_url_fopen setting controls whether PHP can access remote files via a URL. For security reasons, it’s best to disable this setting on a production server. To disable allow_url_fopen, add the following line to your php.ini file:

This disables allow_url_fopen, preventing PHP from accessing remote files via a URL.

Enable gzip compression

Enabling gzip compression can significantly improve the performance of your website by reducing the amount of data that needs to be transmitted between the server and the client. To enable gzip compression, add the following lines to your php.ini file:

zlib.output_compression = On zlib.output_compression_level = 5

This enables gzip compression and sets the compression level to 5.

Gzip compression typically has a range of compression levels, ranging from 1 (lowest compression, but fastest) to 9 (highest compression, but slowest). The default compression level is usually 6, which is a good balance between compression ratio and speed.

A higher compression level will generally result in a smaller compressed file, but will also take longer to compress and decompress. Conversely, a lower compression level will be faster but result in a larger compressed file. The optimal compression level will depend on the nature of the data being compressed and the trade-off between compression ratio and performance.

Enable realpath_cache

Realpath caching can improve the performance of file operations in PHP by reducing the number of disk operations required to resolve file paths. To enable realpath caching, add the following lines to your php.ini file:

realpath_cache_size = 16M realpath_cache_ttl = 120

This enables realpath caching and sets the cache size to 16 megabytes and the time-to-live to 120 seconds.

Enable session caching

Session caching can improve the performance of your website by reducing the number of disk operations required to store and retrieve session data. To enable session caching, add the following lines to your php.ini file:

session.save_handler = files session.save_path = /var/lib/php/sessions session.cache_limiter = public session.cache_expire = 180

This sets the session save handler to files, specifies the location of the session files, sets the cache limiter to public, and sets the cache expire time to 180 seconds.

It is worth noting that this will only work with a web application that runs on a single server. If you are optimising a web application that is load-balanced over multiple servers you will need to use a shared storage mechanism such as a database, shared file system, or memcached to store session data

Conclusion

By making these 10 changes to your php.ini file, you can optimize your PHP scripts and improve the performance of your website. While not every change may be necessary for your specific use case, implementing some or all of these tweaks can help you get the most out of your PHP application. Always remember to test your website after making any changes to ensure that everything is working as expected.

Subscribe to my newsletter.

. and receive the musings of an aspiring #indiehacker directly to your inbox once a month.

These musings will encompass a range of subjects such as Web Development, DevOps, Startups, Bootstrapping, #buildinpublic, SEO, personal opinions, and experiences.

You have been successfully added to my newsletter.

Источник

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:

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

PHP realpath cache size

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

Oh, and 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 lots of calls to lstat.

Originally published: Sep 6th, 2012 | Last updated: January 4th 2023

Источник

Оцените статью