r/PHPhelp • • Jan 18 '18

PHP average response time up from 113ms to 2500-7000ms.

Site works. Slow, though. Avg. response time (via loader.io) is suddenly up from the normal 100-113ms (with 2.500 test clients per minute) to the current 2500-6300ms (with 50-1200 test clients per minute, and aborted tests after a few seconds due to 50% failure threshold reached at that point, if I go above 50 clients/minute). No detected change in PHP settings, as they were kept during the last php repository upgrade. So, naturally, I'm puzzled (and baffled).

Anyway, I'm getting hammered constantly with these two errors/warnings over the last 24 hours:

nginx error.log

2018/01/18 10:25:28 [error] 668#668: *107499 connect() to unix:/run/php/php7.2-fpm.sock failed (11: Resource temporarily unavailable) while connecting to upstream, client: 162.158.79.73, server: www.example-site.dk, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.2-fpm.sock:", host: "www.example-site.dk"

In conjunction with PHP warnings:

php error.log

[18-Jan-2018 10:25:15] WARNING: [pool www] server reached pm.max_children setting (5), consider raising it

Changing the pm.max_children (to 25, for instance - I run 8 cores and 3GB of RAM), doesn't help a bit. Tests still fail quickly or run at ~7.000ms avg. response time. No new php scripts installed, either. But I guess an existing one could have become slow to execute and bogs down the system?

Anyway, when running non-PHP tests (with a simple index.html), nginx delivers the expected average response times of 100-120ms for 2500 test clients/minute.

So, PHP I'm looking at you.

root@example-site#> nginx -v

nginx version: nginx/1.13.8

root@example-site#> service nginx status

● nginx.service - nginx - high performance web server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2018-01-12 22:24:25 CET; 5 days ago
     Docs: http://nginx.org/en/docs/
 Main PID: 660 (nginx)
   CGroup: /system.slice/nginx.service
           ├─660 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.con
           ├─661 nginx: worker process
           ├─662 nginx: worker process
           ├─663 nginx: worker process
           ├─664 nginx: worker process
           ├─665 nginx: worker process
           ├─666 nginx: worker process
           ├─667 nginx: worker process
           └─668 nginx: worker process
Jan 12 22:24:24 example-site systemd[1]: Starting nginx - high performance web server...
Jan 12 22:24:25 example.site systemd[1]: Started nginx - high performance web server.

root@example-site#> php -v

PHP 7.2.1-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Jan  5 2018 13:54:13) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.2.1-1+ubuntu16.04.1+deb.sury.org+1, Copyright (c) 1999-2017, by Zend Technologies

root@example-site#> service php7.2-fpm status

● php7.2-fpm.service - The PHP 7.2 FastCGI Process Manager
   Loaded: loaded (/lib/systemd/system/php7.2-fpm.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2018-01-18 10:32:04 CET; 21min ago
     Docs: man:php-fpm7.2(8)
 Main PID: 20095 (php-fpm7.2)
   Status: "Processes active: 0, idle: 3, Requests: 78, slow: 0, Traffic: 0req/sec"
   CGroup: /system.slice/php7.2-fpm.service
           ├─20095 php-fpm: master process (/etc/php/7.2/fpm/php-fpm.conf)
           ├─20096 php-fpm: pool www
           ├─20097 php-fpm: pool www
           └─20100 php-fpm: pool www

www.conf (extract)

[www]
user = www-data
group = www-data
listen = /run/php/php7.2-fpm.sock

php.ini (extract)

cgi.fix_pathinfo=0

/etc/nginx/sites-available/#> vi example-site

server {
        listen 443 ssl http2 default_server; # managed by Certbot
        server_name www.example-site.dk;
        root /var/www/example-site.dk;
        index index.php index.html index.htm;
        location / {
                #try_files $uri $uri/ =404;
                try_files $uri $uri/ /index.php$is_args$args;
        }
        ssl_certificate /etc/letsencrypt/live/example-site.dk/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/example-site.dk/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
        ssl_client_certificate /etc/nginx/certs/cloudflare.crt;
        ssl_verify_client on;
        if ($scheme != "https") {
        return 301 https://$host$request_uri;
        } # managed by Certbot
        location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_intercept_errors on;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
        }
        location = /favicon.ico { log_not_found off; access_log off; }
        location = /robots.txt { log_not_found off; access_log off; allow all; }
        location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
        expires max;
        log_not_found off;
        }
        location ~* /wp-includes/.*.php$ {
        deny all;
        access_log off;
        log_not_found off;
        }
        location ~ /\.ht {
        deny all;
        }
    }

How do I find the culprit?

7 Upvotes

7 comments sorted by

5

u/h4ppyb1rthd4y Jan 18 '18

Have you checked the server load (using top or htop)?

It looks like you are running WordPress, and when we've seen a similar pattern it has been bots attacking xml-rpc.php.

1

u/fxfxfx Jan 19 '18

Thanks for the tip.

1

u/WarInternal Jan 19 '18

I've had to install fail2ban before to automatically firewall off people going after xmlrpc. Was actually pretty painless to set up.

1

u/Annh1234 Jan 19 '18

Redo the test with an empty PHP file. If that's fast then: Turn on MySQL slow query mode, and log the queries, add indexes where you need to. Log every MySQL/cache calls in your scripts, and at how many network calls you do per page load, if you have alot, batch the network calls.

0

u/[deleted] Jan 18 '18

We had a major server slow down once and it was because we added a join to a commonly used sql query where the two columns weren't indexed.

It took us forever to find. but once we indexed the two columns the server load went down substantially and almost instantly.

Sound like this may not be your issue, but it is worth looking at.

1

u/fxfxfx Jan 19 '18

It may not be this issue, no, but I appreciate any new ideas to mull over when stuck.

0

u/[deleted] Jan 18 '18

[deleted]

1

u/fxfxfx Jan 19 '18

Steep learning curve ahead. Just like any other day at the office. Didn't know abut strace and xdebug. Thanks.