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) and max_input_vars to 10000.
Replace the PHP version from the below command.
root@ubuntu ~]# php -v
PHP 8.3.3 (cli) (built: Aug 17 2022 13:29:56) ( NTS )
root@ubuntu ~]# vim /etc/php/8.3/apache2/php.ini
max_execution_time = 300
max_input_vars = 10000
memory_limit = 256M
root@ubuntu ~]# vim /etc/php/8.3/cli/php.ini
max_execution_time = 300
max_input_vars = 10000
memory_limit = 256M
root@ubuntu ~]# systemctl restart apache2
To change the script execution time in CentOS 7/8, Alma linux 9, Rocky Linux 9
Open the php.ini file and change the max_execution_time to 300 seconds (5 minutes) and max_input_vars to 10000.
Replace the PHP version from the below command.
root@centos ~]# php -v
PHP 8.3 (cli) (built: Aug 17 2022 13:29:56) ( NTS )
root@centos ~]# vim /etc/php.ini
max_execution_time = 300
max_input_vars =10000
memory_limit = 256M
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
How to find PHP memory limit, max execution time, post max size, max_input_vars and upload max file size?
Related Articles:
PHP extension ’mcrypt’ is required.
How to install and switch different versions of PHP in Ubuntu?