PHP max_execution_time Explained: Configuration, Timeouts, and Best Practices

Published: 2026-07-06
Google AdSense 广告位 (待申请通过后开启)

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

Operating System : AlmaLinux 10
PHP Version      : PHP 8.3
Web Server       : Nginx
PHP-FPM          : Enabled
Architecture     : x86_64
Last Tested      : July 2026

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:

Browser
    │
    ▼
Nginx
    │
    ▼
PHP Script Starts
    │
    ├── Database Query
    ├── File Processing
    ├── API Request
    └── Business Logic
          │
          ▼
30 Seconds Reached?
          │
     ┌────┴────┐
     │         │
    No        Yes
     │         │
     ▼         ▼
 Complete   Fatal Error

If the script exceeds the configured execution time, PHP terminates it automatically.

Check the Current Value

php -i | grep max_execution_time

Example:

max_execution_time => 30 => 30

Locate php.ini

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:

sudo nano /etc/php.ini

Find:

max_execution_time = 30

Example change:

max_execution_time = 120

Restart PHP-FPM:

sudo systemctl restart php-fpm

Verify the New Value

Run:

php -i | grep max_execution_time

Or create a simple PHP file:

<?php
phpinfo();

Search for:

max_execution_time

Recommended Values

| Application               |   Recommended Value |
| ------------------------- | ------------------- |
| Small websites            |          30 seconds |
| WordPress                 |          60 seconds |
| Laravel                   |      60–120 seconds |
| File uploads              |     120–300 seconds |
| Large import/export tasks | 300 seconds or more |

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:

Browser
    │
    ▼
PHP
    │
    ├── max_execution_time
    ├── memory_limit
    ├── upload_max_filesize
    └── post_max_size
    │
    ▼
Application

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

Technology Guides →

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

Southeast Asia Insights →