PHP max_execution_time Explained: Configuration, Timeouts, and Best Practices
Introduction
The max_execution_time directive specifies the maximum amount of time that a PHP script is allowed to run before it is automatically terminated.
This limit helps protect servers from long-running or stuck scripts that could consume CPU resources indefinitely. Choosing an appropriate execution time improves server stability while giving applications enough time to complete legitimate tasks.
This guide explains how max_execution_time works, how to configure it, recommended values for different workloads, and how to troubleshoot timeout-related errors.
Test Environment
What Is max_execution_time?
The max_execution_time directive defines the maximum number of seconds that a PHP script may execute.
Example:
max_execution_time = 30
In this example, PHP stops the script if it runs for more than 30 seconds.
This limit applies to each request independently.
How max_execution_time Works
A typical request follows this flow:
If the script exceeds the configured execution time, PHP terminates it automatically.
Check the Current Value
Example:
max_execution_time => 30 => 30
Locate php.ini
Typical output:
Loaded Configuration File: /etc/php.ini
For websites using PHP-FPM, ensure you modify the configuration used by PHP-FPM rather than only the CLI configuration.
Change max_execution_time
Edit the PHP configuration:
Find:
max_execution_time = 30
Example change:
max_execution_time = 120
Restart PHP-FPM:
Verify the New Value
Run:
Or create a simple PHP file:
Search for:
max_execution_time
Recommended Values
Use the smallest value that still allows normal application behavior.
Common Error Message
A typical timeout error looks like this:
Fatal error:
Maximum execution time of 30 seconds exceeded
This means PHP stopped the script because it ran longer than the configured limit.
Why Increasing max_execution_time Is Not Always the Solution
Simply increasing the timeout does not fix the underlying issue.
Long execution times may be caused by:
Inefficient database queries.
Slow external API requests.
Processing large files in a single request.
Infinite loops or recursive code.
Poor application design.
Whenever possible, optimize the application before increasing the execution limit.
max_execution_time vs Other PHP Limits
Several PHP directives work together:
Each directive controls a different aspect of script execution. Increasing one limit does not affect the others.
Common Issues
Changes Have No Effect
Possible reasons include:
PHP-FPM has not been restarted.
The wrong php.ini file was edited.
The application overrides the value using set_time_limit() or other runtime configuration.
CLI Scripts Ignore the Limit
By default, PHP CLI often runs without the same execution time restrictions as web requests. Always verify whether you are testing through the web server or the command line.
Best Practices
Keep the execution time as low as practical.
Investigate slow code before increasing the limit.
Use background jobs for long-running tasks when appropriate.
Restart PHP-FPM after modifying the configuration.
Monitor application performance after making changes.
Conclusion
The max_execution_time directive protects your server by limiting how long PHP scripts may run. Proper configuration improves server stability while ensuring that legitimate requests have enough time to complete.
Rather than relying on higher timeout values, focus on optimizing application performance and identifying the root causes of slow execution.
Explore More
› PHP max_input_time Explained: Configuration, Timeouts, and Best Practices
› PHP opcache.max_accelerated_files Explained: Configuration, Performance, and Best Practices
› PHP opcache.validate_timestamps Explained: Configuration, Code Updates, and Best Practices
› PHP opcache.revalidate_freq Explained: Configuration, Performance, and Best Practices