PHP error guide

PHP "Maximum execution time exceeded": Causes and Fixes

Error summary

The script ran longer than `max_execution_time`. Optimize the hot loop, move work to a queue/CLI job, or raise the limit for that specific long-running task.

What it means

The script ran longer than max_execution_time. Optimize the hot loop, move work to a queue/CLI job, or raise the limit for that specific long-running task.

What the error means

PHP’s execution timer aborted the request after the configured number of seconds (0 means unlimited on many builds).

Why PHP produces it

Web SAPIs use time limits to protect workers from stuck requests. External HTTP calls, huge imports, and accidental infinite loops are typical triggers.

PHP version notes

set_time_limit() / max_execution_time interact with OS signals; FPM terminate timeouts may still win.

Most common causes

  • Slow remote HTTP/DB calls without timeouts
  • Large synchronous imports in a web request
  • Infinite or near-infinite loops
  • Low max_execution_time on shared hosting

Minimal examples

BAD — reproduces the problem

php
while (true) {\n    // accidental infinite loop\n}

FIXED — safer pattern

php
set_time_limit(120); // for a known batch boundary\nforeach ($batches as $batch) {\n    process_batch($batch);\n}

Step-by-step diagnosis

  1. Note the seconds value in the fatal message.
  2. Identify the line—often inside a loop or remote call.
  3. Check FPM request_terminate_timeout which can kill workers independently.
  4. Compare web vs CLI limits.

Fixes

Fix #1: Move heavy work off the request

Use a queue/cron/CLI worker for imports and reports.

bash
php bin/console app:import --file=data.csv

Fix #2: Set timeouts on I/O

Ensure cURL/DB calls cannot hang forever.

php
curl_setopt($ch, CURLOPT_TIMEOUT, 10);\ncurl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

Common mistakes when fixing it

  • Blindly setting max_execution_time=0 on public FPM pools

How to prevent it

  • Async jobs for long tasks
  • Hard timeouts on outbound HTTP

Web server / environment notes

Web pools vs CLI cron jobs need different budgets

Tags: fpm,apache,nginx,cli,cron,shared-hosting

Categories

Relevant ZendStudio.net tools