How to handle timeouts with php5-fpm + nginx timeout php.ini - php

how to handle timeouts with PHP in php5-fpm + ngnix configurations?
I tried to make a simple script with just
sleep(60);
php.ini
max_execution_time = 30
fast_cgi
fastcgi_connect_timeout 60;
fastcgi_send_timeout 50;
fastcgi_read_timeout 50;
The script stops at 50s for timeout of the backend. What do I have to do to
enable the max_execution_time in php.ini
enable ini_set to change the execution time to 0 directly in the
script
Why does fast_cgi get to control timeouts over everything instead of php itself?

It was basically the fact that on Linux the timeout counts only for the actual "php work" and not for all stream function times and moreover not for sleep that's why I never reached the limit and fastgci timeout always kicked in. Instead on Windows the actual "human" time elapsed counts.
from the PHP doc:
The set_time_limit() function and the configuration directive
max_execution_time only affect the execution time of the script
itself. Any time spent on activity that happens outside the execution
of the script such as system calls using system(), stream operations,
database queries, etc. is not included when determining the maximum
time that the script has been running. This is not true on Windows
where the measured time is real.

Try using set_time_limit in your PHP code.

When use php-cgi(php-fpm) php.ini's max_execution_timewill not take effects, but
fpm configuration item request_terminate_timeout will handle script execution time .
In php-fpm.conf set this item like below:
request_terminate_timeout = 60s

Related

Ajax request is getting cancelled

In a php application. I am uploading 20-30 files at once. Each files is around 100-200MB. Means more than 2GB of data i am uploading on server.
Because it takes time around 20-30 mins to upload. One general ajax pooling job getting cancelled after some time.
I have following configuration:
upload_max_filesize = 4096M
post_max_size = 4096M
max_input_time = 600
max_execution_time = 600
During this process my CPU consumption goes only upload 10-20%. I have 32 GB RAM and 12 CORE Linux machine.
Application is running on PHP 8.0, APACHE 2, MYSQL 8, Ubuntu 20.
Can anyone suggest what else i can check?
max_execution_time: This sets the maximum time in seconds a script is
allowed to run before it is terminated by the parser. This helps
prevent poorly written scripts from tying up the server. The default
setting is 30. When running PHP from the command line the default
setting is 0.
max_input_time: This sets the maximum time in seconds a script is
allowed to parse input data, like POST and GET. Timing begins at the
moment PHP is invoked at the server and ends when execution begins.
The default setting is -1, which means that max_execution_time is used
instead. Set to 0 to allow unlimited time.
I think change it:
max_input_time = 1800 & max_execution_time = 1800 (30 minutes)

MYSQL The connection has timed out even I have defined in php.ini

I am running some reports and those reports takes more than 300 seconds to execute and finally display on browser.
I have already defined the max execution time in my code and also set the same in php.ini.
ini_set('max_execution_time', 500);
I am using MySQL Workbench to monitor the execution but on 300 sec browser shows
The connection has timed out. The server at localhost is taking too long to
respond.
I just need to extend it to 400-500 sec and all of my reports will start working smoothly.
How can I do that?
Have you tried this:
ini_set('max_execution_time', 0);
This set the maximum execution time to unlimited. You could set this right before calling your report and set it back to a regular value, e.g 300 right after your report is done.
Your web server can have other Timeout configurations settings that may also interrupt PHP execution. Apache has a Timeout Directive which has default value to 300 seconds. Try changing the directive in your conf (httpd.conf for Apache) file.
For more details , See Apache Timeout Directive doc.

Set ini max_execution_time doesn't work

Before I use nginx and php-fpm, I used Apache, so when I wanted only one of my cron jobs to run without time execution limitation, I used these lines in my PHP code:
set_time_limit(0);
ini_set('max_execution_time', 0);
but after I migrated from Apache to nginx, this code doesn't work. I know ways to change nginx.conf to increase maximum execution time.
But I want to handle this with php code. Is there a way?
I want to specify only one file that can run PHP code without time limitation.
Try This:
Increase PHP script execution time with Nginx
You can follow the steps given below to increase the timeout value. PHP default is 30s. :
Changes in php.ini
If you want to change max execution time limit for php scripts from 30 seconds (default) to 300 seconds.
vim /etc/php5/fpm/php.ini
Set…
max_execution_time = 300
In Apache, applications running PHP as a module above would have suffice. But in our case we need to make this change at 2 more places.
Changes in PHP-FPM
This is only needed if you have already un-commented request_terminate_timeout parameter before. It is commented by default, and takes value of max_execution_time found in php.ini
Edit…
vim /etc/php5/fpm/pool.d/www.conf
Set…
request_terminate_timeout = 300
Changes in Nginx Config
To increase the time limit for example.com by
vim /etc/nginx/sites-available/example.com
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_read_timeout 300;
}
If you want to increase time-limit for all-sites on your server, you can edit main nginx.conf file:
vim /etc/nginx/nginx.conf
Add following in http{..} section
http {
#...
fastcgi_read_timeout 300;
#...
}
Reload PHP-FPM & Nginx
Don’t forget to do this so that changes you have made will come into effect:
service php5-fpm reload
service nginx reload
or try this
fastcgi_send_timeout 50;
fastcgi_read_timeout 50;
fastcgi has it's own set of timeouts and checks to prevent it from stalling out on a locked up process. They would kick in if you for instance set php's execuction time limit to 0 (unlimited) then accidentally created an infinite loop. Or if you were running some other application besides PHP which didn't have any of it's own timeout protections and it failed.
I think that if you have php-fpm and nginx "you can't" set this time only from PHP.
What you could do is a redirect with the parameters indicating you where to continue, but you must control the time that your script is running to avoid timeout.
If your process runs in a browser window, then do it with Javascript the redirect because the browser could limit the number of redirects... or do it with ajax.
Hope that helps.
You can add request_terminate_timeout = 300 to your server's php-fpm pool configuration if you are tried all of solutions here.
ini_set('max_execution_time', 0);
do this if "Safe Mode" is off
set_time_limit(0);
Place this at the top of your PHP script and let your script loose!
Note: if your PHP setup is running in safe mode, you can only change it from the php.ini file.

PHP-FPM: pm.process_idle_timeout vs php_admin_value[max_execution_time]

I have set the FCGI idle timeout value to 120 with -idle-timeout 120 in my Apache site configuration to make sure Apache doesn't time out before my php scripts are finished executing, but now I'm wondering what the difference is between setting php_admin_value[max_execution_time] = 120 and pm.process_idle_timeout = 120s in a php-fpm .conf file?
Does one override the other? Is there a difference? Do I need to set both if I want to make sure my scripts don't time out before the length of time I specify?
Any explanation or reference would be helpful
Based on the comments from the file php-fpm.conf.default: pm.process_idle_timeout has nothing to do with script execution. It defines the time a spawned FPM child has to be idle (i.e. not handle a request) before it will be killed. This does not affect script execution in any way (not even sleep()).
PHP: Runtime Configuration:
The PHP INI setting max_execution_time defines the maximum process time (CPU time) in seconds after which script execution will be halted.
Note: Last time I checked (2 years ago), this did not apply to Windows where the elapsed real time counts.

Apache and or PHP Timeouts - Stumped.

I have a PHP script that when called via a browser it times-out after exactly 60 seconds. I have modified httpd.conf and set the Timeout directive to be 300. I have modified all PHP timeout settings to extend longer than 60 seconds. When I run the script from the command line it will complete. When I execute through browser each time after 60 seconds, POOF, timeout.
I have also checked for the existence of timeout directives in any of the .htaccess files. Nothing there.. I am completely stumped.
I am also forcing set_time_limit(0) within the PHP code.
I've been digging and testing for a week and have exhausted my knowledge. Any help is greatly appreciated.
You need to make sure you are setting a higher timeout limit both in PHP and in Apache.
If you set a high max_execution_time in php.ini your script won't timeout, however, if you are not flushing the output butter of the script's results to the browser on a regular basis, the script might time out on the Apache end due to a network timeout.
In httpd.conf do:
Timeout 216000
In php.ini do:
max_execution_time = 0
(setting it to 0 makes it never time out, like with a CLI (command line) script).
Make sure you restart Apache after you are done! On most linux distro's you can do this by issuing the command (as root):
service httpd restart
Hope this helps!
There are numerous places that the maxtime can be set. If you are using FastCGI, especially though something such as Virtualmin, there are an additional set of settings for max_execution_time that are hidden to you unless you have access.
In short, you will need to figure out all the places, given your PHP stack setup, there can be an execution time limiter, up those values, restart the server, and then do
set_time_limit(0);
for good measure.
Without more information about your specific setup and given my experience in dealing with execution time hangups in PHP, that's the most I can tell you.

Categories