Skip to Content

Increase script execution time in Ubuntu and CentOS

How to increase script execution time in Ubuntu and CentOS?


This directive max_execution_time specifies the maximum time in seconds that a PHP script is allowed to run before it’s 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

NOTE:  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