Optimising PHP Configuration and PHP-FPM for Your Website

optimising the php and php fpm configuration for your website
Reading Time: 4 minutes

Table of Contents

Configure correctly PHP and PHP-FPM is essential to ensure that your website functions efficiently and stably. In the following, we will go through the suggested settings, explaining each parameter and providing tips on how to optimise your configuration according to visits and website consumption.

PHP configurations

memory_limit: 2048M

Function: The parameter memory_limit defines the maximum amount of memory that a PHP script may use. If a script exceeds this limit, it will be terminated.

Council:

  • For low traffic sites: A value of 128M-512M is generally sufficient.
  • For medium to high traffic sites with complex content: A value of 1024M-2048M may be necessary.
  • Verification: Make sure the server has enough physical memory to support this setting. Use monitoring tools to check memory utilisation and adjust the value if necessary.

max_execution_time: 512

Function: The parameter max_execution_time determines the maximum time in seconds during which a script may run before being terminated. This prevents malfunctioning scripts from consuming resources indefinitely.

Council:

  • For low traffic sites: A value of 30-60 seconds is often sufficient.
  • For medium to high traffic sites: A value of 60-120 seconds may be adequate.
  • For complex scripts: If your scripts take longer (e.g. for intensive processing), you can increase the value, but try to optimise the code to reduce execution time.

max_input_time: 512

Function: The parameter max_input_time specifies the maximum time in seconds during which a script may parse input data (e.g. form data or loaded files).

Council:

  • For low traffic sites: A value of 30-60 seconds is adequate.
  • For medium to high traffic sites: A value of 60-120 seconds is recommended.
  • Monitoring: If your scripts are analysing large amounts of data, check the time required and adjust the value accordingly.

post_max_size: 128M

Function: The parameter post_max_size defines the maximum size of data that may be included in a POST request.

Council:

  • For sites with small uploads: A value of 8M-32M is sufficient.
  • For sites with medium uploads: A value of 32M-64M is adequate.
  • For sites with large uploads: 128M or more, ensuring that upload_max_filesize is less than post_max_size.

upload_max_filesize: 256M

Function: The parameter upload_max_filesize defines the maximum size of an uploaded file.

Council:

  • For sites with small files: A value of 2M-16M is sufficient.
  • For sites with medium files: A value of 16M-64M is adequate.
  • For sites with large files: 256M or more, make sure the value of post_max_size is greater than upload_max_filesize.

opcache.enable: on

Function: Enable opcode caching to improve the performance of your PHP application.

Council: Keep this setting enabled to improve performance. Be sure to configure other opcache settings correctly to maximise benefits.

disable_functions: opcache_get_status

Function: Disables certain PHP functions for security reasons.

Council: Disable only those functions that are not necessary for your site to increase security.

Common PHP Settings

include_path

Function: The list of directories in which PHP scripts search for included or required files.

Council: Keep the default path unless there is a specific reason to change it. Add paths only if they are necessary for the operation of your site.

session.save_path

Function: The directory where the PHP session files are stored.

Council: The default directory is generally adequate, but make sure it has the appropriate permissions to read and write. If your site uses sessions heavily, consider using a more powerful session storage solution such as Redis or Memcached.

error_reporting: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED

Function: The level of PHP error reporting.

Council: Maintain this configuration for the production environment to avoid non-critical error messages being displayed to end users.

display_errors: off

Function: Determines whether PHP errors are to be displayed as part of the output.

Council: Keep this setting disabled in production for security reasons. Only enable it in a development environment.

log_errors: on

Function: Enables access to PHP errors, recording them in a log file.

Council: Keep this setting enabled to be able to solve any problems. Check error logs periodically to identify and correct any bugs.

PHP-FPM Settings

pm.max_children: 12

Function: The maximum number of child processes that PHP-FPM can create.

Council:

  • For low traffic sites: 5-10 trials are sufficient.
  • For medium to high traffic sites: 10-20 processes may be necessary.
  • Monitoring: It monitors memory and CPU utilisation to determine the optimal number of processes.

pm.start_servers: 2

Function: The number of server processes at start-up.

Council:

  • For low traffic sites: 1-2 processes are adequate.
  • For medium to high traffic sites: 2-4 processes at start-up.
  • Monitoring: Check the server's start-up time and adjust the value if necessary.

pm.min_spare_servers: 2

Function: The minimum number of idle server processes.

Council:

  • For low traffic sites: 1-2 processes are adequate.
  • For medium to high traffic sites: 2-4 idle processes.
  • Monitoring: Maintain a sufficient number of idle processes to respond quickly to new users.

pm.max_spare_servers: 4

Function: The maximum number of idle server processes.

Council:

  • For low traffic sites: 2-4 processes are adequate.
  • For medium to high traffic sites: 4-6 idle processes.
  • Monitoring: Adjust according to load to avoid wastage of resources.

pm.max_requests: 500

Function: The maximum number of requests each child process can execute before being recycled.

Council:

  • For low traffic sites: 200-500 requests may be sufficient.
  • For medium to high traffic sites: 500-1000 requests are more suitable.
  • Monitoring: It monitors the behaviour of processes and adjusts the value to prevent memory leaks and ensure stability.

pm: dynamic

Function: It determines the behaviour of child process management.

Council: Keep dynamic for flexible management of child processes according to server load. In some cases, ondemand can be useful to save resources in times of low traffic.

Additional Suggestions

Resource monitoring

Use monitoring tools such as New Relic or server monitoring tools to keep an eye on memory and CPU utilisation.

  • Council: It monitors the server regularly to detect and quickly resolve any performance problems.

Caching

It implements server-side caching (e.g. Varnish) and/or application-side caching to reduce the load on the server.

  • Council: A good caching system can greatly improve site performance.

Regular updates

Ensure that all server components and libraries used are up-to-date to benefit from performance improvements and security fixes.

  • Council: Keep your software up-to-date to avoid security vulnerabilities and compatibility problems.

Using these configurations and tips, you should be able to effectively manage PHP-FPM memory and improve the performance of your website.

Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

G Tech Group was born conceptually in 2011 and entrepreneurially in 2013 from an idea of Gianluca Gentile its founder.

The aim was to create the first Social Web Agency not a classic web agency that deals with social but an agency that shares its resources and ideas with other agencies and also connects different agencies, creating a real network.

Recent Posts