Welcome to Centmin Mod Community
Become a Member

PHP-FPM How to troubleshoot & optimize PHP-FPM server?

Discussion in 'Nginx, PHP-FPM & MariaDB MySQL' started by eva2000, Aug 2, 2018.

Thread Status:
Not open for further replies.
  1. eva2000

    eva2000 Administrator Staff Member

    39,758
    8,769
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +13,494
    Local Time:
    3:19 AM
    Nginx 1.15.x
    MariaDB 5.5/10.x
    Centmin Mod is provide as is, so short of scripted related bugs or issues, any further optimisation to the web stack components - nginx, php-fpm, mariadb mysql, csf firewall etc or web app specific configurations are left to the Centmin Mod user to deal with. So I do not provide any free support for such. However, below steps can help - starting with enabling PHP-FPM status page output.

    FYI, if your web app supports it, you should update to latest PHP 7.1.x or 7.2.x version for performance. See Wordpress benchmarks here (for reference PHP 7.0.x is 2x faster than PHP 5.6.x already).

    Nginx 502 or 504 Bad Gateway Errors



    Bad gateway 502 /504 timeouts are usually related to Nginx timing out waiting on PHP-FPM to respond as PHP-FPM is overloaded or overwhelmed with requests, so may need to tune PHP-FPM values. It also maybe due to PHP-FPM in turn being queued and backed up waiting on MariaDB MySQL server to respond - so also need to look at MySQL.

    You'll need to tune your PHP-FPM settings with php-fpm main pool config file at /usr/local/etc/php-fpm.conf (overview of config files) and this is left up to end user to do but here's a thread for starters to enable PHP-FPM status page output outlined at
    Enabling PHP-FPM status also allows setting up 3rd party PHP-FPM status metric monitoring from services like:

    Checking PHP-FPM etc logs



    You'll also need to check into your PHP-FPM, Nginx and MariaDB logs which you can find as outlined at How to troubleshoot Centmin Mod initial install issues

    Server logs include Nginx, PHP-FPM, MariaDB MySQL error logs as well as others. You can find your Centmin Mod install/menu logs at FAQ 7 and server logs at FAQ 19 at Centmin Mod FAQ (most up to date info in FAQ so always read that first). Spoiler tag below has info too but may not be up to date.

    Some of Centmin Mod's installed software will have their own access and error logs which maybe useful for diagnosing errors or give info, notes, or warning notices.

    Note: There's no support provided by me for diagnosing such errors which may occur for various reasons including misconfiguration of installed php/mysql scripts or applications.

    In SSH2 telnet you can use tail command to view the last X number of lines in the file.

    For example for viewing last 10 lines in the file for:

    For Nginx access and error logs:
    Code:
      tail -10 /usr/local/nginx/logs/access.log
      tail -10 /usr/local/nginx/logs/error.log
    
    For specific domainname.com access and error log:
    Code:
      tail -10 /home/nginx/domains/domainname.com/log/access.log
      tail -10 /home/nginx/domains/domainname.com/log/error.log
    
    For other system error logs located at /var/log:

    list /var/log files in ascending time order so the most recently modified files are at the bottom
    Code:
      ls -lhrt /var/log
    
    Code:
    total 2.7M
    -rw------- 1 root  root    0 Aug 29 15:33 tallylog
    -rw------- 1 root  root    0 Aug 29 15:33 spooler
    drwx------ 3 root  root 4.0K Aug 29 15:35 samba
    drwxr-xr-x 2 root  root 4.0K Aug 29 15:35 mail
    -rw-r--r-- 1 root  500     0 Oct  8 18:13 dmesg.old
    -rw------- 1 root  500     0 Oct  8 18:13 boot.log
    -rw-r--r-- 1 root  500     0 Oct  8 18:14 dmesg
    drwx------ 2 root  root 4.0K Oct  8 18:14 httpd
    drwxr-xr-x 2 root  root 4.0K Oct  8 19:08 php-fpm
    -rw-rw---- 1 mysql root 2.3K Oct  9 12:38 mysqld.log
    -rw------- 1 root  root 9.2K Oct 26 10:48 yum.log
    -rw------- 1 root  utmp  94K Nov  7 22:59 btmp
    drwxr-xr-x 2 root  root 4.0K Nov  8 00:00 sa
    -rw------- 1 root  root 269K Nov  8 21:39 messages
    -rw------- 1 root  root 110K Nov  8 23:08 secure
    -rw-rw-r-- 1 root  utmp  43K Nov  8 23:08 wtmp
    -rw-r--r-- 1 root  root 144K Nov  8 23:08 lastlog
    -rw------- 1 root  root  69K Nov  8 23:08 lfd.log
    -rw------- 1 root  root 332K Nov  8 23:08 maillog
    -rw------- 1 root  500  1.6M Nov  8 23:10 cron
    
    For PHP-FPM error log:
    Code:
      tail -10 /var/log/php-fpm/www-error.log
    
    and/or
    Code:
      /var/log/php-fpm/www-php.error.log
    
    For MySQL / MariaDB error log:
    Code:
      tail -10 /var/log/mysqld.log
    
    For CSF firewall LFD log:
    Code:
      tail -10 /var/log/lfd.log
    
    For Mail log:
    Code:
      tail -10 /var/log/maillog
    
    For Cron job logs:
    Code:
      tail -10 /var/log/cron
    

    How to edit php.ini and php-fpm configuration files ?



    Centmin Mod install created command short cuts outlined here to allow you to quickly edit your /usr/local/lib/php.ini file and your /usr/local/etc/php-fpm.conf file. Full list of command shortcuts below:
    • Edit php.ini = phpedit ( /usr/local/lib/php.ini )
    • Edit my.cnf = mycnf ( /etc/my.cnf )
    • Edit php-fpm.conf = fpmconf ( /usr/local/etc/php-fpm.conf )
    • Edit nginx.conf = nginxconf ( /usr/local/nginx/conf/nginx.conf )
    • Edit (nginx) virtual.conf = vhostconf - only edits /usr/local/nginx/conf/conf.d/virtual.conf not the additional vhost domain.com.conf files added later
    • Edit (nginx) php.conf = phpinc ( /usr/local/nginx/conf/php.conf )
    • Edit (nginx) drop.conf = dropinc ( /usr/local/nginx/conf/drop.conf )
    • Edit (nginx) staticfiles.conf = statfilesinc ( /usr/local/nginx/conf/staticfiles.conf )
    • nginx stop/start/restart = ngxstop/ngxstart/ngxrestart
    • php-fpm stop/start/restart = fpmstop/fpmstart/fpmrestart
    • mysql stop/start/restart = mysqlstop/mysqlstart/mysqlrestart
    • nginx + php-fpm stop/start/restart = npstop/npstart/nprestart
    • memcached stop/start/restart =memcachedstop/memcachedstart/memcachedrestart
    • csf stop/start/restart = csfstop/csfstart/csfrestart

    Troubleshooting Tools



    However, there's many linux tools and scripts that can help you figure out what was causing the load issues and when.

    Tools and commands you will want to read up on and learn for basic system admin tasks and troubleshooting.
    Notes:
    However, Centmin Mod users are free to help each other out and ask questions or give answers on this community forum. My hopes are that this community forum evolves so that more veteran long time Centmin Mod users help new Centmin Mod users out :)
     
    Last edited: Aug 2, 2018
    • Winner Winner x 2
    • Like Like x 1
  2. eva2000

    eva2000 Administrator Staff Member

    39,758
    8,769
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +13,494
    Local Time:
    3:19 AM
    Nginx 1.15.x
    MariaDB 5.5/10.x

    Additional Information



    Online guides for optimal php-fpm config are predominantly incorrect as it isn't just about memory. Optimizing php-fpm configurations is based on monitoring and measuring php-fpm cpu and memory usage and your PHP web apps response time/process time and understanding what your php-fpm statistics output metrics mean as outlined at

    php-fpm.conf config options



    php-fpm.conf config options are located in /usr/local/etc/php-fpm.conf settings file for main php-fpm pool as outlined at https://centminmod.com/configfiles.html

    From above php.net php-fpm configuration manual

    php-fpm statistics status page



    From official centminmod.com site link above for php-fpm status output enabling https://centminmod.com/phpfpm.html#browserstatus. You can also monitor php-fpm status via setting up 3rd party PHP-FPM status metric monitoring from services like:
    Code (Text):
    lynx --dump  http://127.0.0.1/phpstatus
    pool:                 www
    process manager:      static
    start time:           13/May/2019:09:42:33 +0000
    start since:          306419
    accepted conn:        325
    listen queue:         0
    max listen queue:     0
    listen queue len:     511
    idle processes:       29
    active processes:     1
    total processes:      30
    max active processes: 12
    max children reached: 0
    slow requests:        0
    

    Centmin Mod 123.09beta01 on CentOS 7 also has command shortcut for php-fpm statistics output = fpmstats. Available due to the work I did here.
    Code (Text):
    fpmstats
    Processes active: 0, idle: 30, Requests: 325, slow: 0, Traffic: 0req/sec
    pool:                 www
    process manager:      static
    start time:           13/May/2019:09:42:33 +0000
    start since:          306740
    accepted conn:        327
    listen queue:         0
    max listen queue:     0
    listen queue len:     511
    idle processes:       29
    active processes:     1
    total processes:      30
    max active processes: 12
    max children reached: 0
    slow requests:        0
    


    Centmin Mod 123.09beta01 CentOS 7 Unique PHP-FPM Statistics Info



    FYI 123.09beta01 on CentOS 7 only that have been recently updated all switch to systemd php-fpm statistics supported service management so have additional statistics too. Notice the extra line in status output available only to CentOS 7 based 123.09beta01 and newer Centmin Mod installs.
    Code (Text):
       Status: "Processes active: 0, idle: 30, Requests: 325, slow: 0, Traffic: 0req/sec"

    full output
    Code (Text):
    systemctl status php-fpm
    ● php-fpm.service - PHP FastCGI Process Manager
       Loaded: loaded (/etc/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
      Drop-In: /etc/systemd/system/php-fpm.service.d
               └─50-CPUAccounting.conf, 50-CPUShares.conf, limit.conf
       Active: active (running) since Mon 2019-05-13 09:42:34 UTC; 3 days ago
     Main PID: 6821 (php-fpm)
       Status: "Processes active: 0, idle: 30, Requests: 325, slow: 0, Traffic: 0req/sec"
        Tasks: 32
       Memory: 275.2M
       CGroup: /system.slice/php-fpm.service
               ├─6821 php-fpm: master process (/usr/local/etc/php-fpm.conf)
               ├─6822 php-fpm: pool www
               ├─6823 php-fpm: pool www
               ├─6824 php-fpm: pool www
               ├─6825 php-fpm: pool www
               ├─6826 php-fpm: pool www
               ├─6827 php-fpm: pool www
               ├─6828 php-fpm: pool www
               ├─6829 php-fpm: pool www
               ├─6830 php-fpm: pool www
               ├─6831 php-fpm: pool www
               ├─6832 php-fpm: pool www
               ├─6833 php-fpm: pool www
               ├─6834 php-fpm: pool www
               ├─6835 php-fpm: pool www
               ├─6836 php-fpm: pool www
               ├─6837 php-fpm: pool www
               ├─6838 php-fpm: pool www
               ├─6839 php-fpm: pool www
               ├─6840 php-fpm: pool www
               ├─6841 php-fpm: pool www
               ├─6842 php-fpm: pool www
               ├─6843 php-fpm: pool www
               ├─6844 php-fpm: pool www
               ├─6845 php-fpm: pool www
               ├─6846 php-fpm: pool www
               ├─6847 php-fpm: pool www
               ├─6848 php-fpm: pool www
               ├─6849 php-fpm: pool www
               ├─6850 php-fpm: pool www
               └─6851 php-fpm: pool www
    
    Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.
    

    Centmin Mod 123.09beta01 on CentOS 7 also has command shortcut for php-fpm service status output = fpmstatus. Available due to the work I did here.
    Code (Text):
    fpmstatus
    ● php-fpm.service - PHP FastCGI Process Manager
       Loaded: loaded (/etc/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
      Drop-In: /etc/systemd/system/php-fpm.service.d
               └─50-CPUAccounting.conf, 50-CPUShares.conf, limit.conf
       Active: active (running) since Mon 2019-05-13 09:42:34 UTC; 3 days ago
     Main PID: 6821 (php-fpm)
       Status: "Processes active: 0, idle: 30, Requests: 325, slow: 0, Traffic: 0req/sec"
       CGroup: /system.slice/php-fpm.service
               ├─6821 php-fpm: master process (/usr/local/etc/php-fpm.conf)
               ├─6822 php-fpm: pool www
               ├─6823 php-fpm: pool www
               ├─6824 php-fpm: pool www
               ├─6825 php-fpm: pool www
               ├─6826 php-fpm: pool www
               ├─6827 php-fpm: pool www
               ├─6828 php-fpm: pool www
               ├─6829 php-fpm: pool www
               ├─6830 php-fpm: pool www
               ├─6831 php-fpm: pool www
               ├─6832 php-fpm: pool www
               ├─6833 php-fpm: pool www
               ├─6834 php-fpm: pool www
               ├─6835 php-fpm: pool www
               ├─6836 php-fpm: pool www
               ├─6837 php-fpm: pool www
               ├─6838 php-fpm: pool www
               ├─6839 php-fpm: pool www
               ├─6840 php-fpm: pool www
               ├─6841 php-fpm: pool www
               ├─6842 php-fpm: pool www
               ├─6843 php-fpm: pool www
               ├─6844 php-fpm: pool www
               ├─6845 php-fpm: pool www
               ├─6846 php-fpm: pool www
               ├─6847 php-fpm: pool www
               ├─6848 php-fpm: pool www
               ├─6849 php-fpm: pool www
               ├─6850 php-fpm: pool www
               └─6851 php-fpm: pool www
    
    Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.
    

    But basic gist is PHP-FPM optimization is calculated from continuous monitoring of php-fpm statistics and resource usage and make informed decisions on php-fpm options based on understanding of those options and how they play out in php-fpm statistics status output/monitored metrics.
     
    • Winner Winner x 1
    • Informative Informative x 1
..
Thread Status:
Not open for further replies.