Search code examples
phpperformanceapachehigh-availability

How check apache & php-fpm config is appropriate (not too high or too low)


I will have an event with 3k users on an app (php base).

I launch several instances in the cloud and install LAMP on it.[to make load test and choose on for the event]

On Ubuntu 18

enter image description here

I enable mpm_event and php7.4-fpm, (which seems to be the better configuration for high traffic with apache and php app).

I use this post which explain how tune your conf. Like this :

Here apache2 mpm event conf :

<IfModule mpm_*_module>
  ServerLimit           (Total RAM - Memory used for Linux, DB, etc.) / process size
  StartServers          (Number of Cores)
  MinSpareThreads       25
  MaxSpareThreads       75
  ThreadLimit           64
  ThreadsPerChild       25
  MaxRequestWorkers     (Total RAM - Memory used for Linux, DB, etc.) / process size
  MaxConnectionsPerChild   1000
</IfModule>

Here php7.4-fpm :

pm = dynamic            
pm.max_children         (total RAM - (DB etc) / process size)
pm.start_servers        (cpu cores * 4)
pm.min_spare_servers    (cpu cores * 2)
pm.max_spare_servers    (cpu cores * 4)
pm.max_requests         1000

My goal is : even if I rely of these method, I would saw some metric like :

  • --> You have too many thread (from apache worker or from phpfpm) unused open
  • --> All your thread (from apache worker or from phpfpm) are already busy and use

I already test: htop, glance, vmstat, sar to check io, cpu, ram but even with that it's not clear to me : Does my configuration is good for this machine with this load or should I increase/decrease something? Then I could be sure these configuration are good and start other subject : CDN, cache ...

How do you manage this ? thanks by advance,


Solution

  • As you noted, this depends on your script(s). We have this dynamically adjusted in our deploy scripts based on the server(s) being rolled up.

    The following script is based on running Apache, on Centos, on AWS infrastructure but could easily be adapted to what you are using.

    Basically:

    • set the size of apache processes
    • set the size of php process
    • scripts gets available memory, cores and does some crunching and then modifies the config.
    • we run this as part of stack roll up

    Primary Source / Based on:

    Steps:

    1. Calculate process size

    You need to know how many processes can run on your machine. So calculate the process size of your main CPU/memory drivers is necessary.

    cd /tmp
    curl https://raw.githubusercontent.com/pixelb/ps_mem/master/ps_mem.py --output ps_mem.py
    chmod a+x ps_mem.py
    sudo python ps_mem.py
    # Sample numbers:
    # 28.4 MiB + 103.0 KiB = 28.5 MiB memcached
    # 34.7 MiB + 9.5 KiB = 34.7 MiB amazon-cloudwatch-agent
    # 24.8 MiB + 18.0 MiB = 42.8 MiB httpd (15)
    # 69.1 MiB + 7.0 MiB = 76.0 MiB php (2)
    # 228.2 MiB + 46.0 MiB = 274.3 MiB php-fpm (36)
    

    Here you can see that there are 15 httpd processes, consuming a total of 43MiB, so each Apache process is using roughly 3MiB of RAM. The php-fpm process will use about 7.6MiB.

    1. Calculate Apache MaxRequestWorkers

    To be safe though, reserve 15% of memory for all other processes (in my case ~1.2GiB) and round up apache process size to 3MiB.

    MaxRequestWorkers = (Total RAM - Memory used for Linux, DB, etc.) / process size
    MaxRequestWorkers = (8000MB - 1200MB) / 3MB = 2,266
    
    1. Calculate php-fpm max-children

    To be safe though, reserve 1 GiB for all other processes and round up php process size to 8MiB.

    max_children = (Total RAM - Memory used for Linux, DB, etc.) / process size
    max_children = (8000MB - 1200MB) / 8MB = 850
    
    1. Here is the script we use, on roll up.
    #!/bin/bash
    
    # Creates a configuration script to run once final servers are up.
    PROCESS_SIZE_APACHE_MB=3
    PROCESS_SIZE_PHP_MB=8
    
    # Get some values from the server
    MEMORY_KB=`grep MemTotal /proc/meminfo | awk '"'"'{print $2}'"'"'`
    MEMORY_MB=$(($MEMORY_KB / 1024))
    MEMORY_AVAILABLE_MB=$(($MEMORY_KB / 1178))
    NUM_CORES=`nproc --all`
    echo "Memory: $MEMORY_MB MB"
    echo "Memory Available: $MEMORY_AVAILABLE_MB MB"
    echo "Num Cores $NUM_CORES"
    
    #Now do some calculations
    SERVER_LIMIT=$(($MEMORY_AVAILABLE_MB / $PROCESS_SIZE_APACHE_MB))
    echo "HTTP MPM Server Limit: $SERVER_LIMIT"
    
    #Convert Apache from mpm-prefork to mpm-worker
    #Set params
    #<IfModule mpm_*_module>
    #  ServerLimit           (Total RAM - Memory used for Linux, DB, etc.) / process size
    #   StartServers          (Number of Cores)
    #   MinSpareThreads       25
    #   MaxSpareThreads       75
    #   ThreadLimit           64
    #   ThreadsPerChild       25
    #   MaxRequestWorkers     (Total RAM - Memory used for Linux, DB, etc.) / process  size
    #   MaxConnectionsPerChild   1000
    # </IfModule>
    # /etc/httpd/conf.modules.d/00-mpm.conf
    
    echo "
    # LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
    # LoadModule mpm_worker_module modules/mod_mpm_worker.so
    LoadModule mpm_event_module modules/mod_mpm_event.so
    
    <IfModule mpm_*_module>
      ServerLimit           $SERVER_LIMIT
      StartServers          $NUM_CORES
      MinSpareThreads       25
      MaxSpareThreads       75
      ThreadLimit           64
      ThreadsPerChild       25
      MaxRequestWorkers     $SERVER_LIMIT
      MaxConnectionsPerChild   1000
    </IfModule>
    " > /etc/httpd/conf.modules.d/00-mpm.conf
    
    # Configure the workers
    # pm = dynamic
    # pm.max_children         (total RAM - (DB etc) / process size) = 850
    # pm.start_servers        (cpu cores * 4)
    # pm.min_spare_servers    (cpu cores * 2)
    # pm.max_spare_servers    (cpu cores * 4)
    # pm.max_requests         1000
    MAX_CHILDREN=$(($MEMORY_AVAILABLE_MB / $PROCESS_SIZE_PHP_MB))
    echo "Max Children: $MAX_CHILDREN"
    NUM_START_SERVERS=$(($NUM_CORES * 4))
    NUM_MIN_SPARE_SERVERS=$(($NUM_CORES * 2))
    NUM_MAX_SPARE_SERVERS=$(($NUM_CORES * 4))
    
    sed -c -i "s/^;*pm.max_children.*/pm.max_children = $MAX_CHILDREN/" /etc/php- fpm.d/www.conf
    sed -c -i "s/^;*pm.start_servers.*/pm.start_servers = $NUM_START_SERVERS/" /etc/php- fpm.d/www.conf
    sed -c -i "s/^;*pm.min_spare_servers.*/pm.min_spare_servers =  $NUM_MIN_SPARE_SERVERS/" /etc/php-fpm.d/www.conf
    sed -c -i "s/^;*pm.max_spare_servers.*/pm.max_spare_servers =  $NUM_MAX_SPARE_SERVERS/" /etc/php-fpm.d/www.conf
    sed -c -i "s/^;*pm.max_requests = 500.*/pm.max_requests = 1000/" /etc/php-> fpm.d/www.conf