Skip to Content

Increase script execution time in Ubuntu and CentOS

How to increase script execution time in Ubuntu and CentOS?


Overview: This article explains how to increase the script execution time in Ubuntu and CentOS by modifying the max_execution_time directive in the PHP configuration. It covers updating the php.ini file for both Apache and CLI, using commands to automate the process, and adjusting the memory limit for optimal performance.


This directive, max_execution_time, specifies the maximum time (in seconds) that a PHP script is allowed to run before it is terminated.


Open the php.ini file and change the max_execution_time to 300 seconds (5 minutes).

Replace the PHP version from the below command.

root@ubuntu ~]# php -v

PHP 7.4.3 (cli) (built: Aug 17 2022 13:29:56) ( NTS )


root@ubuntu ~]# vim /etc/php/7.4/apache2/php.ini

max_execution_time = 300

memory_limit = 256M


root@ubuntu ~]# vim /etc/php/7.4/cli/php.ini

max_execution_time = 300

memory_limit = 256M


To change the script execution time in CentOS 7/8.

Open the php.ini file and change the max_execution_time to 300 seconds (5 minutes).

Replace the PHP version from the below command.

root@centos ~]# php -v

PHP 7.4.3 (cli) (built: Aug 17 2022 13:29:56) ( NTS )


root@centos ~]# vim /etc/php.ini

max_execution_time = 300

memory_limit = 256M

Note: 

Execute the following command to add the new maximum execution time for apache web server using single command

read -p "Enter new maximum execution time (e.g., 600): " time && sed -i "s/^max_execution_time =.*/max_execution_time = $time/I" /etc/php/$(php -v | head -n 1 | awk '{print $2}' | cut -d. -f1,2)/apache2/php.ini

Execute the following command to add the new maximum execution time for command line

read -p "Enter new maximum execution time (e.g., 600): " time && sed -i "s/^max_execution_time =.*/max_execution_time = $time/I" /etc/php/$(php -v | head -n 1 | awk '{print $2}' | cut -d. -f1,2)/cli/php.ini

Execute the following command to add new memory limit for apache web server

read -p "Enter new memory limit (e.g., 2G): " memory && sed -i "s/^memory_limit =.*/memory_limit = $memory/I" /etc/php/$(php -v | head -n 1 | awk '{print $2}' | cut -d. -f1,2)/apache2/php.ini

Execute the following command to add the new memory limit for command line

read -p "Enter new memory limit (e.g., 2G): " memory && sed -i "s/^memory_limit =.*/memory_limit = $memory/I" /etc/php/$(php -v | head -n 1 | awk '{print $2}' | cut -d. -f1,2)/cli/php.ini


Related Articles:

Correct php time

PHP fatal error

PHP extension ’mcrypt’ is required.

How to install and switch different versions of PHP in Ubuntu?

How to upgrade PHP to 7.4 on Centos 7