The max_execution_time directive in PHP controls the maximum time a script is allowed to run. Even if you call set_time_limit inside the script, if this value in the php.ini file is set too low, it will still restrict the execution time. The set_time_limit call only temporarily changes the maximum execution time for the current script, but if the max_execution_time in php.ini is lower than the desired time, the script will still be terminated.
Check and modify the max_execution_time setting in php.ini. If you want your script to run longer, increase this value. You can use the following command to check the current max_execution_time setting:
<span><span><span class="hljs-title function_ invoke__">phpinfo</span></span><span>();
</span></span>
Then, modify the corresponding setting in the php.ini file:
<span><span><span class="hljs-attr">max_execution_time</span></span><span> = </span><span><span class="hljs-number">300</span></span><span> </span><span><span class="hljs-comment">; Set to 300 seconds (5 minutes)</span></span><span>
</span></span>
After modifying, restart the web server (such as Apache or Nginx) for the changes to take effect.
Besides PHP configuration, web servers (like Apache, Nginx, etc.) may also have request timeout limits that can cause script execution to time out. For example, Apache’s Timeout directive or Nginx’s proxy_read_timeout settings might restrict the PHP script’s execution time.
Check and adjust the relevant timeout settings according to the web server you are using.
Apache: Locate and modify the Timeout directive:
<span>Timeout 300
</span>
Nginx: Add or modify proxy_read_timeout or fastcgi_read_timeout in the nginx.conf file:
<span><span><span class="hljs-attribute">proxy_read_timeout</span></span><span> </span><span><span class="hljs-number">300</span></span><span>;
</span><span><span class="hljs-attribute">fastcgi_read_timeout</span></span><span> </span><span><span class="hljs-number">300</span></span><span>;
</span></span>
Restart the server after modification to apply the new settings.
Some PHP scripts might experience long blocking operations, such as database queries or remote API requests. Even if set_time_limit modifies the max execution time, these blocking operations themselves may cause the script to time out.
Optimize database queries to ensure they complete within a reasonable time and avoid unnecessary locks.
Use asynchronous requests or batch processing to avoid long blocking times. For example, split API requests or database queries into smaller requests that take less time to process.
If the PHP script involves concurrent requests or multithreading, handling multiple tasks might exceed the time limits set for a single thread. Although set_time_limit can adjust the execution time of a single process, in concurrent scenarios, timeouts across multiple processes need to be managed synchronously.
Consider using a queue system (like RabbitMQ, Redis, etc.) to handle concurrent tasks. Break tasks into smaller subtasks and use asynchronous processing to avoid timeouts for single processes.
set_time_limit only applies to the current script. It does not affect external PHP requests or timeout limits of other scripts. If the script includes files via include or require, set_time_limit may not extend the execution time of code inside those files.
Make sure to call set_time_limit at the beginning of the script, or call it within every included file. To affect all files, consider placing set_time_limit in the entry point file (e.g., index.php).
In some environments (such as shared hosting or certain cloud providers), PHP script execution time may be limited by operating system level restrictions, which cannot always be changed via PHP configuration. For example, some OSes or container environments might terminate scripts if resource usage is too high.
Check OS-level limits like those set by the ulimit command to ensure there are no restrictions on process runtime. If changes are not possible, consider moving to an environment with fewer resource constraints, such as a dedicated server or private cloud hosting.
Some third-party services or components may impose execution time limits on scripts, especially when using external APIs or integrated services. For example, some APIs return errors after request timeouts, causing scripts to terminate.
Check the documentation of third-party services for any timeout settings.
Use proper error handling in your script to catch and manage timeout exceptions.