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
- Move long tasks to CLI/cron or a queue
- Optimize the slow query/API call
set_time_limit(120)for known-long scripts- Check the FPM/proxy timeouts too, or you’ll get a 504 instead
Detailed fix by platform
PHP
- php.ini:
max_execution_time = 120. For FPM alsorequest_terminate_timeoutin 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>&1Raise 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 normalAlso raise the PHP-FPM request_terminate_timeout and proxy timeouts, or you will get a 504 instead.
How to diagnose
- Location — Which file/line was executing when the limit hit (it is in the message)?
- Bottleneck — Time the DB queries and external HTTP calls in that request
- Limits — Compare max_execution_time, FPM request_terminate_timeout and proxy timeouts
- 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.
Was this page helpful?
Report a correction or suggest an improvement
Last updated 2 Oct 2026