Maximum execution time of 120 seconds exceeded in yii2

Maximum execution time of 120 seconds exceeded in yii2

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Resolving Maximum execution time of 120 seconds exceeded in Yii 2

When processing a file containing 1,000 rows, encountering the "Maximum execution time of 120 seconds exceeded" error is a common bottleneck in web applications. The default PHP configuration sets the maximum script execution time to 120 seconds (2 minutes), which often proves insufficient for bulk import operations like uploading Excel files with complex data transformations. If your current implementation handles only 400 records within that window, you are hitting the wall where the server times out before finishing the job.

To resolve this in Yii 2, you must adjust the max_execution_time directive. This setting controls how long a single script is allowed to run before the web server terminates it. The primary method involves modifying your PHP configuration files or adding directives to your entry scripts.

Modifying php.ini

The most robust solution is updating the global php.ini file located in your PHP installation directory. You need to locate the max_execution_time directive and increase its value, for example:

max_execution_time = 300

However, this approach has a significant caveat: it affects all scripts running on that server. If you host multiple applications or shared hosting environments where you lack root access, changing the global file is often impossible. In such cases, you must rely on local overrides.

Local Overrides via php.ini in the Project Root

If you cannot edit the global configuration, create a new php.ini file in your project's root directory (or in the protected/config folder if following older Yii 2 conventions). Ensure this file has higher priority than the global one by placing it in the current directory. Add the directive there:

max_execution_time = 300
memory_limit = 512M
post_max_size = 16M
upload_max_filesize = 16M

Increasing memory_limit is equally critical. Bulk imports often require more RAM to hold large datasets in memory, and hitting the memory limit triggers a separate "Allowed memory size exceeded" error, which might not be immediately obvious if you only fix the execution time. Similarly, increasing post_max_size and upload_max_filesize ensures that larger Excel files can reach your application without being rejected by the web server before PHP even processes them.

Adjusting via .htaccess (Apache Only)

If your server runs Apache and allows it, you can override these settings using .htaccess. This is useful for shared hosting environments where php.ini editing is restricted. Add the following lines to your .htaccess file:

php_value max_execution_time 300
php_value memory_limit 512M

Note that this method only works if AllowOverride FileInfo is enabled in your Apache VirtualHost configuration. If you are using Nginx, this directive will be ignored, and you must modify the PHP-FPM pool configuration instead (usually in /etc/php/7.x/fpm/pool.d/www.conf).

Implementing Timeouts Within Yii 2 Code

Simply increasing the timeout is not always the right answer. If your import logic involves heavy database transactions or synchronous API calls, extending the timeout indefinitely can cause resource leaks or leave servers hanging if the process crashes midway.

A better architectural pattern is to break large imports into smaller chunks. Instead of loading all 1,000 rows at once, process them in batches (e.g., 200 records per transaction). This approach keeps execution time low while maintaining data integrity. You can implement batching using Yii's QueryBuilder or by splitting the Excel file logic manually before inserting into the database.

Additionally, consider moving heavy processing logic to a background worker queue like Redis with Laravel Queue, or using a dedicated cron job for long-running tasks. This decouples the user request from the data processing, providing a better user experience even if the operation takes several minutes.

Monitoring and Validation

After making these changes, always test your application with a dataset slightly larger than the previous limit to ensure stability. If you encounter persistent timeouts, investigate whether the issue lies in inefficient queries or excessive memory usage rather than just the time limit. For further guidance on optimizing large-scale data imports in modern PHP stacks, resources like https://laravelcompany.com offer insights into scaling strategies for enterprise applications.

Our network