Apache Server taking longer time - php

I have deployed my PHP API on a Linux based server. It's users are 40000-50000. Server has Disk space 20GB and out of that, 15GB is used. I have used MySQL for Database. GZip is enabled and the response JSON on http://example.com/getData is nearly equal to 100KB. Initially, it used to take 2-3 seconds on each getData call (which includes querying from data table and processing on PHP), but now it is taking more than 15 seconds for a single request. I have checked the CPU usage by TOP command in linux. This is the CPU status:
top - 09:30:44 up 14:44, 3 users, load average: 1.16, 2.54, 2.28
Tasks: 222 total, 1 running, 221 sleeping, 0 stopped, 0 zombie
Cpu(s): 6.2%us, 1.5%sy, 0.0%ni, 92.2%id, 0.0%wa, 0.0%hi, 0.0%si, 0.2%st
Mem: 2051652k total, 1576444k used, 475208k free, 204408k buffers
Swap: 0k total, 0k used, 0k free, 456548k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
15942 www-data 20 0 320m 14m 6480 S 1 0.7 0:04.50 apache2
15356 mysql 20 0 1320m 52m 7448 S 1 2.6 1:00.19 mysqld
15973 www-data 20 0 321m 15m 6444 S 1 0.8 0:06.75 apache2
15811 www-data 20 0 318m 12m 5648 S 1 0.6 0:04.33 apache2
28989 root 20 0 96716 3928 2948 S 1 0.2 0:00.03 sshd
15806 www-data 20 0 323m 17m 6756 S 0 0.9 0:04.10 apache2
15807 www-data 20 0 321m 16m 6708 S 0 0.8 0:04.83 apache2
15817 www-data 20 0 322m 16m 6416 S 0 0.8 0:06.19 apache2
15832 www-data 20 0 320m 14m 6416 S 0 0.7 0:04.11 apache2
15838 www-data 20 0 320m 14m 6416 S 0 0.7 0:04.05 apache2
15849 www-data 20 0 321m 15m 6416 S 0 0.8 0:06.15 apache2
15851 www-data 20 0 321m 15m 6424 S 0 0.8 0:04.26 apache2
15853 www-data 20 0 320m 15m 6740 S 0 0.8 0:05.45 apache2
15856 www-data 20 0 323m 18m 6644 S 0 0.9 0:04.68 apache2
All the queries running time is less than 0.01 second and the microtime difference between end and start of the getData function is 0.51.
What may be the reasons of this slow response on browser?

I bet for DNS lookups in apache logs. Verify “HostnameLookups” are off in your apache config.

It could probably be the logs that is stored in your system , it holds records of application activities ex time, date and much more , that's a major thing in system logs as it reduces the performance of any application activities.

Related

Magento 1.9.1 php-fpm High CPU Usage but Low Memory Usage

I am running 2 pretty standard Magento websites in a cloud with 16 vCPU and 16 vRAM. But today I found out that my sites are running horribly slow with almost 100% vCPU all the time, but only 10% RAM. I can't figure out what the problem is and no idea where to check first.
This is my top command:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
22778 k6719876 20 0 307m 38m 4516 R 3.0 0.2 0:06.77 php-fpm
20862 enji 20 0 121m 61m 4148 R 2.7 0.4 0:13.05 lmtp
22785 k6719876 20 0 381m 48m 6192 R 2.7 0.3 0:05.99 php-fpm
22937 k6719876 20 0 314m 45m 4500 R 2.7 0.3 0:04.62 php-fpm
23872 enji 20 0 116m 59m 3992 R 2.7 0.4 0:00.61 lmtp
1471 mysql 20 0 2475m 221m 8572 S 2.3 1.4 1:42.89 mysqld
22777 k6719876 20 0 313m 44m 4500 R 2.3 0.3 0:05.37 php-fpm
22792 k6719876 20 0 320m 51m 4512 R 2.3 0.3 0:06.83 php-fpm
22809 k6719876 20 0 315m 46m 4572 R 2.0 0.3 0:06.73 php-fpm
23447 enji 20 0 402m 32m 6140 R 2.0 0.2 0:02.40 php-fpm
24095 enji 20 0 332m 30m 4392 S 2.0 0.2 0:00.31 php-fpm
24017 enji 20 0 324m 24m 4276 S 1.7 0.1 0:00.56 php-fpm
23971 root 20 0 139m 4648 1256 S 0.3 0.0 0:00.01 cpanel_php_fpm
23981 root 20 0 13180 1184 864 R 0.3 0.0 0:00.08 top
1 root 20 0 19284 1496 1292 S 0.0 0.0 0:00.12 init
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd/967975
PHP-FPM often peak to 10% CPU each

PHP-FPM not responding when CPU is 100% in low priority processes

I'm having a problem when CPU is 100% usage: PHP-FPM does not respond, even the processes using 100% of CPU are running in lowest priority (+19).
Let me explain: my site triggers an audio converter (ffmpeg) using inotify + a bash script. The converter is running using "nice -n 19". So, CPU usage is 100% but, in theory, PHP-FPM has priority (default 0 priority).
What happens: only PHP freezes. Apache can respond to files like JPG/CSS/JS. Linux stable, other SWs too. All .php returns "504 Gateway Timeout" (browser, Apache response). Apache opens a lot of php-fpm instances and keep them open.
30585 fedora 19 0.0 0.3 0:00.00 snippeter
30586 fedora 19 25.0 2.8 0:00.10 ffmpeg
15640 fedora 0 0.0 1.0 0:00.12 php-fpm
17174 fedora 0 0.0 1.0 0:00.10 php-fpm
20583 fedora 0 0.0 0.9 0:00.00 php-fpm
21072 fedora 0 0.0 0.9 0:00.00 php-fpm
21309 fedora 0 0.0 0.9 0:00.00 php-fpm
22601 fedora 0 0.0 0.9 0:00.00 php-fpm
23172 fedora 0 0.0 0.9 0:00.00 php-fpm
23277 fedora 0 0.0 0.9 0:00.00 php-fpm
24170 fedora 0 0.0 0.9 0:00.00 php-fpm
24823 fedora 0 0.0 0.9 0:00.00 php-fpm
24916 fedora 0 0.0 0.9 0:00.00 php-fpm
26589 fedora 0 0.0 0.9 0:00.00 php-fpm
%Cpu(s): 8.5 us, 4.5 sy, 85.6 ni, 0.0 id, 0.5 wa, 1.0 hi, 0.0 si, 0.0 st
Changing php-fpm priority (renice), no difference.
I don't know what to do... I want to run processes in background, even using 100% of CPU, but keeps PHP+Apache running normally.
Server: Fedora 29, PHP 7.2.
Found a solution!
The problem is not related to 100% CPU usage, or just one CPU. Since I have a loop to open each audio converter (bash file), and wait it to finish to run the next one, the PHP session is locked! So, when another PHP script, in the same session, try to execute, it cannot read the session file, so, PHP freezes (waiting to have access to the session file, until a "Gateway Timeout" error). That's why only PHP freezes, not other softwares.
To fix, I had to run this before the main loop:
session_write_close();
session_start(['read_and_close'=>true]);
So, it writes and closes the session file, and read it again once, without locking it.
This only works in PHP 7+ and, obviously, you cannot write any session data. Or, after the loop, just open the session again with session_start().

disable old version of php-fpm after upgrade from 7.0 to 7.1

I upgraded from php-fpm 7.0 to 7.1 but the 7.0 is still running. How do I disable it?
This is the output from top;
1251 ubuntu 20 0 464760 48424 40448 S 0.0 4.8 0:00.03 php
1002 root 20 0 506216 47480 39856 S 0.0 4.7 0:00.03 php-fpm7.1
1048 root 20 0 311664 29788 24032 S 0.3 2.9 0:00.03 php-fpm7.0
1176 www-data 20 0 506216 9396 1772 S 0.0 0.9 0:00.00 php-fpm7.1
1177 www-data 20 0 506216 9396 1772 S 0.0 0.9 0:00.00 php-fpm7.1
1173 www-data 20 0 311664 7252 1496 S 0.0 0.7 0:00.00 php-fpm7.0
1174 www-data 20 0 311664 7252 1496 S 0.0 0.7 0:00.00 php-fpm7.0
do this;
sudo systemctl disable php7.0-fpm.service
sudo reboot
the name of services can be different, in my case the name is without dot(.). to check the right service name just use
systemctl | grep php
will return:
● php-fpm.service loaded failed failed The PHP FastCGI Process Manager
php72-php-fpm.service loaded active running The PHP FastCGI Process Manager
in above case, to disable just use:
systemctl disable php72-php-fpm.service
You dont need to reboot. It's work perfectly without reboot.

How to use all CPU for FastCGI (PHP fast CGI one process use 100% CPU, but the total CPU usage is only 25% )

I have set up FastCGI to run my PHP site, and I found that when I generate a big file using the site, only one php-cgi process is working and its CPU usage is 100%. But the total CPU usage is only 25%.
Here is the "top -u user" output.
top - 13:50:35 up 3:58, 3 users, load average: 0.07, 0.20, 0.32
Tasks: 116 total, 1 running, 115 sleeping, 0 stopped, 0 zombie
Cpu(s): 25.0%us, 0.1%sy, 0.0%ni, 99.8%id, 0.0%wa, 0.0%hi, 0.1%si, 0.0%st
Mem: 35126760k total, 1090160k used, 34036600k free, 60644k buffers
Swap: 0k total, 0k used, 0k free, 289816k cached
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
7570 user 20 0 232m 13m 10m S 100.0 0.3 0:00.03 php-cgi
7571 user 20 0 234m 16m 10m S 0.0 0.0 0:00.22 php-cgi
7572 user 20 0 304m 86m 10m S 0.0 0.3 1:37.49 php-cgi
7573 user 20 0 234m 15m 9228 S 0.0 0.0 0:00.16 php-cgi
7574 user 20 0 232m 13m 9528 S 0.0 0.0 0:00.40 php-cgi
7594 user 20 0 232m 13m 10m S 0.0 0.0 0:00.02 php-cgi
7595 user 20 0 234m 14m 9492 S 0.0 0.0 0:00.09 php-cgi
7596 user 20 0 232m 13m 9184 S 0.0 0.0 0:00.06 php-cgi
7597 user 20 0 234m 15m 9500 S 0.0 0.0 0:00.03 php-cgi
7598 user 20 0 232m 10m 7116 S 0.0 0.0 0:00.02 php-cgi
7599 user 20 0 232m 13m 10m S 0.0 0.0 0:00.02 php-cgi
7601 user 20 0 326m 33m 11m S 0.0 0.1 1:58.60 php-cgi
7602 user 20 0 234m 14m 8988 S 0.0 0.0 0:00.14 php-cgi
7603 user 20 0 234m 12m 6800 S 0.0 0.0 0:00.15 php-cgi
7604 user 20 0 308m 13m 9848 S 0.0 0.0 0:00.13 php-cgi
The server is AWS m2.x2large instance and it got 4 vCPU (4 cores maybe). And now the FastCGI seems to only use one core.
I have no idea about how to set up FastCGI to let it use all 4 vCPU.
My current settings is FastCGI + suEXEC + PHP 5.5.11 + apache2.
Here is teh FastCGI configurations in apache:
FcgidIPCDir /usr/local/apache2/logs/mod_fcgid
FcgidProcessTableFile /usr/local/apache2/logs/mod_fcgid/fcgid_shm
FcgidIOTimeout 7200
FcgidMaxRequestLen 2097152
FcgidMaxProcesses 100
FcgidMaxProcessesPerClass 100
FcgidMaxRequestsPerProcess 5000
FcgidMaxRequestInMem 20971520
FcgidZombieScanInterval 5
FcgidIdleScanInterval 120
FcgidProcessLifeTime 3600
Here is the /cgi-bin/php.fcgi settings:
#!/bin/bash
# Shell Script To Run PHP5 using mod_fastcgi under Apache 2.x
### Set PATH ###
PHP_CGI=/usr/bin/php-cgi
PHP_FCGI_CHILDREN=4
PHP_FCGI_MAX_REQUESTS=5000
### no editing below ###
export PHP_FCGI_CHILDREN
export PHP_FCGI_MAX_REQUESTS
exec $PHP_CGI
Please help me on this.
Thank you very much in advance!
The process is using 100% of one core, which is 25% of your total processing power (4 cores). There isn't a way for a single thread to use more than 1 core, but it looks like FastCGI can be configured to use multiple threads. Refer to this question for more details.
Note that 1 request can only result in 1 thread, so if this is handling a single request, there doesn't seem to be a way to have that request handled in multiple threads.

Locating memory leak in Apache httpd process, PHP/Doctrine-based application

I have a PHP application using these components:
Apache 2.2.3-31 on Centos 5.4
PHP 5.2.10
Xdebug 2.0.5 with Remote Debugging enabled
APC 3.0.19
Doctrine ORM for PHP 1.2.1 using Query Caching and Results Caching via APC
MySQL 5.0.77 using Query Caching
I've noticed that when I start up Apache, I eventually end up 10 child processes. As time goes on, each process will grow in memory until each one approaches 10% of available memory, which begins to slow the server to a crawl since together they grow to take up 100% of memory.
Here is a snapshot of my top output:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1471 apache 16 0 626m 201m 18m S 0.0 10.2 1:11.02 httpd
1470 apache 16 0 622m 198m 18m S 0.0 10.1 1:14.49 httpd
1469 apache 16 0 619m 197m 18m S 0.0 10.0 1:11.98 httpd
1462 apache 18 0 622m 197m 18m S 0.0 10.0 1:11.27 httpd
1460 apache 15 0 622m 195m 18m S 0.0 10.0 1:12.73 httpd
1459 apache 16 0 618m 191m 18m S 0.0 9.7 1:13.00 httpd
1461 apache 18 0 616m 190m 18m S 0.0 9.7 1:14.09 httpd
1468 apache 18 0 613m 190m 18m S 0.0 9.7 1:12.67 httpd
7919 apache 18 0 116m 75m 15m S 0.0 3.8 0:19.86 httpd
9486 apache 16 0 97.7m 56m 14m S 0.0 2.9 0:13.51 httpd
I have no long-running scripts (they all terminate eventually, the longest being maybe 2 minutes long), and I am working under the assumption that once each script terminates, the memory it uses gets deallocated. (Maybe someone can correct me on that).
My hunch is that it could be APC, since it stores data between requests, but at the same time, it seems weird that it would store data inside the httpd process.
How can I track down which part of my app is causing the memory leak?
What tools can I use to see how the memory usage is growing inside the httpd process and what is contributing to it?
My hunch is that it could be APC, since it stores data between requests, but at the same time, it seems weird that it would store data inside the httpd process.
What's weird about that? That's exactly what APC does. The memory is shared between all httpd processes, though, so it's not as bad as it sounds. See Where does APC store its opcode and user variable cache? for details.

Categories