Welcome to Centmin Mod Community
Become a Member

PHP-FPM Switch from TCP to UNIX domain sockets

Discussion in 'Centmin Mod Insights' started by RoldanLT, Jun 7, 2014.

  1. RoldanLT

    RoldanLT Well-Known Member

    3,975
    965
    113
    May 25, 2014
    Phillipines
    Ratings:
    +1,329
    Local Time:
    7:42 AM
    1.11
    10.2
    I just read this:
    What adjustments on centminmod should do to make this happen? :)

    Thanks!
     
  2. RoldanLT

    RoldanLT Well-Known Member

    3,975
    965
    113
    May 25, 2014
    Phillipines
    Ratings:
    +1,329
    Local Time:
    7:42 AM
    1.11
    10.2
  3. eva2000

    eva2000 Administrator Staff Member

    30,902
    6,908
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,404
    Local Time:
    9:42 AM
    Nginx 1.13.x
    MariaDB 5.5
    straight from https://plus.google.com/b/104831941868856035845/113497966974166918390/posts/3oSoWwRvPqz

    FYI, the way the automated menu stuff is done in Centmin Mod it relies or assumes PHP-FPM uses TCP. You can try switching but it might auto revert on PHP updates to TCP

    To switch from TCP to Unix sockets need to edit 2 files

    Both can be edited using command shortcuts outlined in FAQ 16 at http://centminmod.com/faq.html

    edit /usr/local/etc/php-fpm.conf (fpmconf)

    comment out with semicolon the line

    from
    Code:
    listen = 127.0.0.1:9000
    to
    Code:
    ;listen = 127.0.0.1:9000
    uncomment the line

    from
    Code:
    ;listen = /tmp/php5-fpm.sock
    to
    Code:
    listen = /tmp/php5-fpm.sock
    edit /usr/local/nginx/conf/php.conf (phpinc)

    uncomment the line

    from
    Code:
        fastcgi_pass   127.0.0.1:9000;
        #fastcgi_pass   unix:/tmp/php5-fpm.sock;
    
    to
    Code:
        #fastcgi_pass   127.0.0.1:9000;
        fastcgi_pass   unix:/tmp/php5-fpm.sock;
    
    Then restart PHP-FPM

    Code:
    service php-fpm restart
    or via command shortcut

    Code:
    fpmrestart
     
    Last edited: Jun 7, 2014
    • Like Like x 1
  4. Matt

    Matt Moderator Staff Member

    697
    322
    63
    May 25, 2014
    Sheffield, UK
    Ratings:
    +449
    Local Time:
    11:42 PM
    1.7.1
    MariaDB 10
    I've tried on both. Didn't notice any difference personally, but I highly doubt my sites are going to cause enough traffic on the socket to stress it out, so I've just left it on TCP
     
    • Like Like x 2
  5. RoldanLT

    RoldanLT Well-Known Member

    3,975
    965
    113
    May 25, 2014
    Phillipines
    Ratings:
    +1,329
    Local Time:
    7:42 AM
    1.11
    10.2
    Thanks for the steps :)
    What about this step?
    3. Setup upstream load balancing
     
  6. RoldanLT

    RoldanLT Well-Known Member

    3,975
    965
    113
    May 25, 2014
    Phillipines
    Ratings:
    +1,329
    Local Time:
    7:42 AM
    1.11
    10.2
    What about this line on /usr/local/etc/php-fpm.conf (fpmconf)
    Code:
    ;listen = /tmp/php5-fpm.sock
    Should I comment it out also?

    On /usr/local/nginx/conf/php.conf (phpinc)
    I see this:
    Code:
    fastcgi_pass   127.0.0.1:9000;
    #fastcgi_pass   unix:/tmp/php5-fpm.sock;
    
    So I need to change it to:
    Code:
    #fastcgi_pass   127.0.0.1:9000;
    fastcgi_pass   unix:/tmp/php5-fpm.sock;
    
    Right?
     
  7. eva2000

    eva2000 Administrator Staff Member

    30,902
    6,908
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,404
    Local Time:
    9:42 AM
    Nginx 1.13.x
    MariaDB 5.5
    You can try it out, just need to create additional PHP-FPM pools and their conf files in /usr/local/nginx/conf/phpfpmd/. There's a sample second pool in zip download at /config/php-fpm/php-fpm-min-2pools.conf to work off of - just look at the settings from the second pool downwards after and including the [www2] line or as below.

    Of course tuning each PHP-FPM pool optimally within the confines of your server resources and site usage requirements is left up to you :)

    Code:
    [www2]
    user = nginx
    group = nginx
    
    listen = 127.0.0.1:9001
    listen.allowed_clients = 127.0.0.1
    ;listen.backlog = -1
    
    ;listen = /tmp/php5-fpm2.sock
    ;listen.owner = nobody
    ;listen.group = nobody
    ;listen.mode = 0666
    
    pm = dynamic
    pm.max_children = 5
    ; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
    pm.start_servers = 1
    pm.min_spare_servers = 1
    pm.max_spare_servers = 3
    pm.max_requests = 5000
    
    rlimit_files = 65536
    rlimit_core = 0
    
    ; The timeout for serving a single request after which the worker process will
    ; be killed. This option should be used when the 'max_execution_time' ini option
    ; does not stop script execution for some reason. A value of '0' means 'off'.
    ; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
    ; Default Value: 0
    ;request_terminate_timeout = 0
    ; Default Value: 0
    ;request_slowlog_timeout = 0
    slowlog = /var/log/php-fpm/www-slow2.log
    
    pm.status_path = /phpstatus2
    ping.path = /phpping2
    ping.response = pong
    
    ; Limits the extensions of the main script FPM will allow to parse. This can
    ; prevent configuration mistakes on the web server side. You should only limit
    ; FPM to .php extensions to prevent malicious users to use other extensions to
    ; exectute php code.
    ; Note: set an empty value to allow all extensions.
    ; Default Value: .php
    security.limit_extensions = .php .php3 .php4 .php5
    Centmin Mod does have plans in place for multiple PHP-FPM pools as such for both TCP and Unix sockets ;)

    edit: changed the following lines

    Code:
    pm.status_path = /phpstatus2
    ping.path = /phpping2
     
    • Like Like x 1
  8. eva2000

    eva2000 Administrator Staff Member

    30,902
    6,908
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,404
    Local Time:
    9:42 AM
    Nginx 1.13.x
    MariaDB 5.5
    whoops yeah missed that, updated https://community.centminmod.com/threads/switch-from-tcp-to-unix-domain-sockets.307/#post-1431
     
    • Like Like x 1
  9. RoldanLT

    RoldanLT Well-Known Member

    3,975
    965
    113
    May 25, 2014
    Phillipines
    Ratings:
    +1,329
    Local Time:
    7:42 AM
    1.11
    10.2
    But on /usr/local/nginx/conf/php.conf (phpinc), it should be:
    Code:
    fastcgi_pass   127.0.0.1:9000;
    #fastcgi_pass   unix:/tmp/php5-fpm.sock;
    to
    Code:
    #fastcgi_pass   127.0.0.1:9000;
    fastcgi_pass   unix:/tmp/php5-fpm.sock;
    Right?

    I don't see any code that just: ;listen = /tmp/php5-fpm.sock
     
  10. eva2000

    eva2000 Administrator Staff Member

    30,902
    6,908
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,404
    Local Time:
    9:42 AM
    Nginx 1.13.x
    MariaDB 5.5
  11. RoldanLT

    RoldanLT Well-Known Member

    3,975
    965
    113
    May 25, 2014
    Phillipines
    Ratings:
    +1,329
    Local Time:
    7:42 AM
    1.11
    10.2
    Just tried it and display,
    502 Bad Gateway
    nginx

    :(
     
  12. eva2000

    eva2000 Administrator Staff Member

    30,902
    6,908
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,404
    Local Time:
    9:42 AM
    Nginx 1.13.x
    MariaDB 5.5
    which you tried switching to Unix socket or 2nd pool ?

    for 2nd pool might need to change

    Code:
    ;listen.owner = nobody
    ;listen.group = nobody
    to

    Code:
    listen.owner = nginx
    listen.group = nginx
     
  13. RoldanLT

    RoldanLT Well-Known Member

    3,975
    965
    113
    May 25, 2014
    Phillipines
    Ratings:
    +1,329
    Local Time:
    7:42 AM
    1.11
    10.2
    Unix socket.
     
  14. eva2000

    eva2000 Administrator Staff Member

    30,902
    6,908
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,404
    Local Time:
    9:42 AM
    Nginx 1.13.x
    MariaDB 5.5
    in /usr/local/etc/php-fpm.conf (fpmconf) you might need to

    change

    Code:
    ;listen.owner = nobody
    ;listen.group = nobody
    to

    Code:
    listen.owner = nginx
    listen.group = nginx
     
  15. eva2000

    eva2000 Administrator Staff Member

    30,902
    6,908
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,404
    Local Time:
    9:42 AM
    Nginx 1.13.x
    MariaDB 5.5
    Actually started testing what is outlined at http://www.softwareprojects.com/res...-and-php-fpm-for-high-traffic-sites-2081.html myself on this forum using the above outlined steps.

    Upstream load balancing between TCP port 9000 and 1x php-fpm Unix socket. Unix socket isn't as stable under high loads, so gave TCP a 75% weighting and PHP-FPM unix socket a 25% weighting and you can see some PHP-FPM stats at https://community.centminmod.com/pages/mstats/.

    Used Blitz.io for stress testing the forums but didn't have enough free credits at Blitz.io to complete testing, so no conclusive results and have to wait until next month when the free credits start over (each month) and will try and see what happens if I add a 2nd or more PHP-FPM unix sockets to the upstream mix.

    [​IMG]
    [​IMG] [​IMG]
    [​IMG] [​IMG]
     
  16. eva2000

    eva2000 Administrator Staff Member

    30,902
    6,908
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,404
    Local Time:
    9:42 AM
    Nginx 1.13.x
    MariaDB 5.5
    Finished testing using php-fpm unix sockets and no where near as stable as TCP, so switched to testing multiple TCP php-fpm pools :)
     
    • Like Like x 1
  17. RoldanLT

    RoldanLT Well-Known Member

    3,975
    965
    113
    May 25, 2014
    Phillipines
    Ratings:
    +1,329
    Local Time:
    7:42 AM
    1.11
    10.2
    Please also share how tos :D
    Thanks!
     
  18. eva2000

    eva2000 Administrator Staff Member

    30,902
    6,908
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +10,404
    Local Time:
    9:42 AM
    Nginx 1.13.x
    MariaDB 5.5
    basically same as above just

    instead of within each php-fpm pool /usr/local/nginx/conf/phpfpmd/YOURNEWPHPPOOLNAME.conf file

    Code:
    #fastcgi_pass   127.0.0.1:9000;
    fastcgi_pass   unix:/tmp/php5-fpm.sock;
    you specify a different TCP port each with own unique paths

    Code:
    fastcgi_pass   127.0.0.1:9001;
    slowlog = /var/log/php-fpm/www-slow2.log
    
    pm.status_path = /phpstatus2
    ping.path = /phpping2
    
     
  19. RoldanLT

    RoldanLT Well-Known Member

    3,975
    965
    113
    May 25, 2014
    Phillipines
    Ratings:
    +1,329
    Local Time:
    7:42 AM
    1.11
    10.2
    I tried to follow this again but, still 502 Bad Gateway.
     
  20. RoldanLT

    RoldanLT Well-Known Member

    3,975
    965
    113
    May 25, 2014
    Phillipines
    Ratings:
    +1,329
    Local Time:
    7:42 AM
    1.11
    10.2
    Works now :D
    Didn't know Nginx needs to be restarted also:D