mod_fcgid + PHP + apache lockup - php

I'm running a fairly typical LAMP stack with PHP running through mod_fcgid. I'd consider the server to be under "high load" given the amount of traffic it receives.
There is an intermittent problem, where Apache is reporting all connections to be in the "Sending content" state ("W" on the monitor) when accessing sites that rely on PHP.
There are no PHP errors to speak of, its as though PHP isn't actually getting called during these "lockup" periods. However, in the apache site logs I'm seeing the following:
(103)Software caused connection abort: mod_fcgid: ap_pass_brigade failed in handle_request function
[warn] mod_fcgid: can't apply process slot for /var/www/cgi-bin/php.fcgi
During this time I can still access sites that do not depend on PHP, such as the apache status and HTML-only virtual hosts (that don't have the PHP handler include).
The php.fcgi script has PHP_FCGI_MAX_REQUESTS=500 set, because I have read there is a race condition problem with PHP running in CGI mode. The fcgid.conf also has MaxProcessCount=15 set.
Has anyone else experience this bug, and if so how can it be resolved?

I managed to fix this one myself.
To solve this problem add in stricter checks in the FastCGI configuration for process hangs, and reduce the lifetime of your PHP instances:
IPCConnectTimeout 20
ProcessLifeTime 120
IdleTimeout 60
IdleScanInterval 30
MaxRequestsPerProcess 499
MaxProcessCount 100
Depending on your requirements, this can satisfy a well-configured server that has in excess of 50k hits per hour.
You will find the number of recorded defunct / "zombie" PHP processes increases significantly. This is good, however, as previously the processes would have simply become unresponsive and the FastCGI manager would have continued to pipe requests to them!
I would also advise removing all override directives from your php.fcgi script, as this can cause problems with your system. Try to manage as much as possible from the primary FastCGI configuration in Apache.

We went with Nginx + http://php-fpm.org/
Try "strace -p".
I also saw lock-ups happen when some PHP software were trying to request file from the same server it's running on (get_file_contents('http://localhost...'))

Related

php fpm ondemand vs dynamic vs static?

Visited web application from browser and got cant be reached 504 message last night.
On checking the clients server, I was able to see that the timeout error from the web server log:
The timeout specified has expired: [client xxxxxxxx] AH01075: Error dispatching request to : (polling)
I advised client that this was due to them not having configured the PHP FPM values.
Bascially due to the use of the dynamic process manager FPM Config. And that furthermore they should try using Ondemand setting. This relates to the PHP FPM values. And see how that goes.
​
The site in question is loading pretty fine from our end. But am wondering if this is the best advice. I.e. changing from dynamic to on-demand for a vps 8gb ram, 2 core, 180sd. Any recommendations. Which is best?
Clients is running a wordpress app. High traffic site.
The web application stack is NGINX + Apache2 Hybrid (I can use .htaccess) let me know what else you need to know.

PHP FastCGI - can't apply process slot for

I have fastCGI with PHP
However, server runs fine for several hours and then suddenly I have php error log full of:
mod_fcgid: can't apply process slot for /var/www/php-bin/website/php
At the same time, there is no spike in activity on the web, CPUs are not spiking, all seems normal based on the server usage.
If I restart apache, all is running ok again for several hours and then situation repeats.
I have tried to set higher values to fcgid settings:
FcgidMaxProcesses 18000
FcgidMaxProcessesPerClass 3800
but the problem stil persist.
What is interesting, I also have the second server with totally different setup and SW versions (FastCGI as PHP module), but the same problem sometimes (not so frequently) occurs there as well.
So I am wondering, can this problem be caused by some PHP script? On both servers, there are some PHP libraries that are the same.
In generall, how to track down what is causing this? On debug server, this problem is non-existing and on production, I cannot blindly change settings and reset server over and over again.

How to best troubleshoot apache webserver with lot of requests in "being processed" status

I am running a Linux Centos/Plesk box with a medium/high traffic Prestahop e-commerce website.
I use the stock-Plesk configuration with PHP 7.0 FPM served by Apache / Nginx as a reverse proxy. I only made some tweaks on FPM pool settings according to the server power basically to increase MaxChildren value to serve more requests.
From some days I am encountering occasional website slowdowns that I am trying to troubleshoot (website did not have any particular visits increases in last time).
I have already performed some checks on:
- server logs, can't see particular error_logs
- server load average (it is ok)
- I/O "wa" value is OK
- MySQL server has not slow queries during slowdowns (SHOW FULL PROCESSLIST is never returning long execution time queries)
- net stat (no DDOS / strange connections)
I installed mod_status form Apache and noticed from the server-status page that during slowdowns I have a high number of "requests currently being processed" in "W" status [sending reply] (During slowdowns I can have up to 70/80 of those requests for several seconds) so I can correlate the slowdowns to Apache being busy to deliver requests but I can't figure out why and what application/webpage component is the source of the problem.
My question here is some advice on how to understand the culprit of slowdowns (a PHP script ? a stuck external service during Apache request ?)
thank you.

I get 500 Internal Server Error after 30 seconds of running a script

The production server has CentOS 6.4 with Plesk 11.0.9. It is a dedicated server.
Server configuration:
* max_execution_time is set to 500 from Plesk and it shows up correctly in phpinfo
Problems:
* I get 500 Internal Server Error after 30 seconds of running any script
Is there any way I get a workaround this problem?
LE:
* the script I want to do this downloads from a remote location a file to user's browser. It might require even hours to complete the operation (for very slow internet connection clients). I really need this. And one more thing, the user must not see the source url.
I think you run Fast CGI. The CGI process has a maximum execution time. In fastcgi its the idle-timeout. After that you get an 500 Error because the backend is not responding in that time.
I don't know which module you use. But normally you can set the idle-timeout in your configuration.
mod_fastcgi idle timeout not work

php5-fpm children and requests

I have a question.
I own a 128mb vps with a simple blog that gets just a hundred hits per day.
I have nginx + php5-fpm installed. Considering the low visits and the ram I decided to set fpm to static with 1 server running. While I was doing my random tests like running php scripts through http that last over 30 minutes I tried to open the blog in the same machine and noticed that the site was basically unreachable. So I went to the configuration and read this:
The number of child processes to be created when pm is set to 'static' and the
; maximum number of child processes to be created when pm is set to 'dynamic'.
; **This value sets the limit on the number of simultaneous requests that will be
; served**
What shocked me the most was that I didn't know because I always assumed that a php children would handle hundreds of requests at the same time like a http server would do!
Did it get it right?
If for example I launch 2 php-fpm children and launch 2 "long scripts" at the same time all the sites using the same php backend will be unreachable?? How is this usable?
You may think: -duh! a php script (web page) is usually processed in 100ms- ... no doubt about that but what happens if you have pages that could run for about 10 secs each and I have 10 visitors with php-fpm with 5 servers so accepting only 5 requests per time at the same time? They'll all be queued or will experience timeouts?
I'm honestly used to run sites in Windows with Apache and mod_php I never experienced these issues because apparently those limits don't apply being a different way of using PHP.
This also raises another question. If I have file_1.php with sleep(20) and file_2.php with just an echo, if I run file_1 and then file_2 with the fastcgi machine the second file will request the creation of another server to handle the php request using 4MB RAM more. If I do the same with apache/mod_php the second file will only use 30KB more of RAM (in the apache server). Considering this why is mod_php is considering the "bad guy" if the ram used is actually less...I know I'm missing the big picture here.
You've basically got it right. You configured a static number of workers (and that number was "one") -- so that's exactly what you got.
But you don't understand quite how things typically work, since you say:
I always assumed that a php children would handle hundreds of requests
at the same time like a http server would do!
I'm not really familiar with nginx, but consider the typical mod_php setup in apache. If you're using mod_php, then you're using the prefork mpm for apache. So every concurrent http requests is handled by a distinct httpd process (no threads). If you're tuning your apache/mod_php server for low-memory, you're going to have to tweak apache settings to limit the number of processes it will spawn (in particular, MaxClients).
Failing to tune this stuff means that when you get a large traffic spike, apache starts spawning a huge number of heavy processes (remember, it's mod_php, so you have the whole PHP interpreter embedded in each httpd process), and you run out of memory, and then everything starts swapping, and your server starts emitting smoke.
Tuned properly (meaning: tuned so that you ignore requests instead of allocating memory you don't have for more processes), clients will time out, but when traffic subsides, things go back to normal.
Compare that with fpm, and a smarter web server architecture like apache-worker, or nginx. Now you have some, much larger, pool of threads (still configurable!) to handle http requests, and a separate pool of php-fpm processes to handle just the requests that require PHP. It's basically the same thing, if you don't set limits on how many processes/threads can be created, you are asking for trouble. But if you do tune, you come out ahead, since only a fraction of your requests use PHP. So essentially, the average amount of memory needed per http requests is lower -- thus you can handle more requests with the same amount of memory.
But setting the number to "1" is too extreme. At "1", it doesn't even matter if you choose static or dynamic, since either way you'll just have one php-fpm process.
So, to try to give explicit answers to particular questions:
You may think: -duh! a php script (web page) is usually processed in 100ms- ... no doubt about that but what happens if you have pages that could run for about 10 secs each and I have 10 visitors with php-fpm with 5 servers so accepting only 5 requests per time at the same time? They'll all be queued or will experience timeouts?
Yes, they'll all queue, and eventually timeout. The fact that you regularly have scripts that take 10 seconds to run is the real culprit here, though. There are lots of ways to architect around that (caching, work queues, etc), but the right solution depends entirely on what you're trying to do.
I'm honestly used to run sites in Windows with Apache and mod_php I never experienced these issues because apparently those limits don't apply being a different way of using PHP.
They do apply. You can set up an apache/mod_php server the same way as you have with nginx/php-fpm -- just set apache's MaxClients to 1!
This also raises another question. If I have file_1.php with sleep(20) and file_2.php with just an echo, if I run file_1 and then file_2 with the fastcgi machine the second file will request the creation of another server to handle the php request using 4MB RAM more. If I do the same with apache/mod_php the second file will only use 30KB more of RAM (in the apache server). Considering this why is mod_php is considering the "bad guy" if the ram used is actually less...I know I'm missing the big picture here.
Especially on linux, lots of things that report memory usage can be very misleading. But think about it this way: that 30kb is negligible. That's because most of PHP's memory was already allocated when some httpd process got started.
128MB VPS is pretty tight, but should be able to handle more than one php-process.
If you want to optimize, do something like this:
For PHP:
pm = static
pm.max_children=4
for nginx, figure out how to control processes and thread count (whatever the equivalent to apache's MaxClients, StartServers, MinSpareServers, MaxSpareServers)
Then figure out how to generate some realistic load (apachebench, siege, jmeter, etc). use vmstat, free, and top to watch your memory usage. Adjust pm.max_children and the nginx stuff to be as high as possible without causing any significant swap (according to vmstat)

Categories