Was this helpful? Support me via buymeacoffee.com and help me create lots more great content!

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:

memory_limit = 256M

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:

max_execution_time = 120

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:

display_errors = Off

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:

upload_max_filesize = 64M

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:

post_max_size = 64M

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:

allow_url_fopen = Off

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.

Originally published at https://chrisshennan.com/blog/10-essential-phpini-tweaks-for-improved-web-performance