0

I've been struggling with the following problem for a while now and would very much appreciate your help.

My webserver completely crashes on a regular basis due to too many php threads. The number of php processes is stable within a normal range (e.g. 35 processes). The number of threads per process constantly increases over time. After a couple of hours the total sum of all threads in all php processes gets greater than 900 - and that's when normal server operations start to shut down.

Here is how I count the number of processes and threads:

ps axo pid,nlwp,cmd | grep "pool www"
15674   11 php-fpm: pool www
15675   13 php-fpm: pool www
15676    8 php-fpm: pool www
15677    7 php-fpm: pool www
15678   12 php-fpm: pool www

...and 30 more lines like that

I am running

  • a virtual server
  • 8 GB RAM
  • 4 vCores
  • ubuntu 16.04
  • nginx 1.10.3
  • php-fpm 7.0.3

my .../fpm/pool.d/www.conf looks like this (alphabetical order):

user = www-data
request_terminate_timeout = 600s
pm.start_servers = 20
pm.min_spare_servers = 20
pm.max_spare_servers = 35
pm.max_requests = 500
pm.max_children = 72
pm = dynamic
listen.owner = www-data
listen.group = www-data
listen = /run/php/php7.0-fpm.sock
group = www-data
env[TMP] = /tmp
env[TMPDIR] = /tmp
env[TEMP] = /tmp
env[PATH] = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
env[HOSTNAME] = $HOSTNAME

And my nginx configuration looks like this:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 1024;
    # multi_accept on;
}

http {
    client_max_body_size 501m;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 3;
    types_hash_max_size 2048;
    server_names_hash_max_size 8192;
    include /etc/nginx/mime.types;
    default_type application/octet-stream;  
    log_format  main  '$remote_addr - $host $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';     
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
    ssl_prefer_server_ciphers on;
    map $status $loggable {
        ~^404  0;
        default 1;
    }
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log ;
    gzip on;
    gzip_disable "msie6";
    application/xml+rss text/javascript;
    gzip_min_length 1100;
    gzip_vary on;
    gzip_comp_level 2;
    gzip_proxied any;
    gzip_types      text/plain text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;  
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
    limit_conn_zone $binary_remote_addr zone=addr:10m;  
    limit_conn addr 50; 
    fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=YOURAPP:100m inactive=60m;
    fastcgi_cache_key "$scheme$request_method$host$request_uri";    
    upstream php {
        server unix:/run/php/php7.0-fpm.sock;
    }    
    map $http_upgrade $connection_upgrade {
            default upgrade;
            '' close;
    }     
}


server {
    listen 443 ssl;
    server_name www.mydomain.com;
    root   /var/www/path/to/webfiles;       
    # ssl specs here 
    # some general location stuff here
    location ~ \.php$ {
        regex to split $uri to $fastcgi_script_name and $fastcgi_path
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        try_files $fastcgi_script_name =404;
        set $path_info $fastcgi_path_info;
        fastcgi_param PATH_INFO $path_info;
        fastcgi_index index.php;
        include fastcgi.conf;
        fastcgi_cache_bypass 1;
        fastcgi_no_cache 1;  
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;     
        fastcgi_buffer_size 128k;
        fastcgi_buffers 256 4k;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
        fastcgi_cache YOURAPP;
        fastcgi_cache_valid 200 60m;
        fastcgi_read_timeout 600s;
    }

}   

There seems to be a connection to the z-push service running on the server. It is used to sync server data (contacts, calendar, mail) with mobile devies. After shutting down that particular service the number of threads stops acting up. So far I was not able to find a configuration for z-push/nginx/php which allows me to use the synchronization without flooding the server. I tried using the "static" and the "ondemand" configuration for spawning php processes - but that didn't make any significant difference.

I would be grateful for any hints or clarifications.
Thank you in advance
Steve

Steve
  • 1
  • 1
  • Er, wait, how did you get PHP _threads_? – Michael Hampton Sep 06 '18 at 02:05
  • Honestly - I am open for the possibility that I am using the wrong terminology here. The parameter `nlwp` to the command `ps` returns this ever increasing value as mentioned above. That parameter is supposed to return the number of threads, right? – Steve Sep 06 '18 at 05:42
  • Yes, and it should always be 1, as threaded PHP should not be run as a web server. It's not safe. Many PHP extensions are not thread safe and will crash when PHP is run threaded. – Michael Hampton Sep 06 '18 at 12:26
  • Thank you, Michael Hampton, for pointing that out. During system configuration I never consciously enabled php threading. I will examine the system further to get more information about what spawns all these threads. Any ideas about what to look for are very much appreciated. – Steve Sep 06 '18 at 14:40
  • I'm not sure how you got such a thing. I'm not aware of any distribution of PHP where threading is built by default. You may wish to contact whoever installed PHP on your system to find out exactly what they did. – Michael Hampton Sep 06 '18 at 20:30

1 Answers1

0

The problem was somehow related to Kopano Core. When I updated that one month ago to version 8.6.82, all thread related problems were gone in an instance. Better late than never. Thank you all for your advice!

Steve
  • 1
  • 1