Maximum execution time 🐘 PHP

PHP Fatal error: Maximum execution time of 30 seconds exceeded

The script ran longer than max_execution_time and PHP stopped it.

Seen on: PHP

Meaning

PHP limits CPU time per request (default 30s; unlimited on CLI). Long imports, slow external calls or infinite loops hit it.

Common causes

  • Slow database queries
  • Synchronous calls to slow external APIs
  • Large imports/exports in a web request
  • Infinite loops

⚡ Quick fix

  1. Move long tasks to CLI/cron or a queue
  2. Optimize the slow query/API call
  3. set_time_limit(120) for known-long scripts
  4. Check the FPM/proxy timeouts too, or you’ll get a 504 instead

Detailed fix by platform

PHP

  1. php.ini: max_execution_time = 120. For FPM also request_terminate_timeout in the pool config.

Code examples

Run long work from the CLI (no time limit) via cron

bash
# crontab -e  → every 5 minutes, process the import queue
*/5 * * * * /usr/bin/php /var/www/app/bin/process-imports.php >> /var/log/imports.log 2>&1

Raise the limit only around a known-long block

php
set_time_limit(300);          // resets the timer to 300s from now
generateYearlyReport();
set_time_limit(30);           // back to normal

Also raise the PHP-FPM request_terminate_timeout and proxy timeouts, or you will get a 504 instead.

How to diagnose

  1. Location — Which file/line was executing when the limit hit (it is in the message)?
  2. Bottleneck — Time the DB queries and external HTTP calls in that request
  3. Limits — Compare max_execution_time, FPM request_terminate_timeout and proxy timeouts
  4. Placement — Should this work run in a queue/cron instead of a web request?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.