Upgrade to Pro — share decks privately, control downloads, hide ads and more …

optimizing PHP-FPM, Nginx and kernel for high p...

Osman
May 25, 2016

optimizing PHP-FPM, Nginx and kernel for high performance

Osman

May 25, 2016
Tweet

More Decks by Osman

Other Decks in Technology

Transcript

  1. osman ungur software (still i can't find a good name

    yet) developer with sysadmin background freelance, consultant, site reliability engineer avid runner, cyclist
  2. java, php, a little bit python and erlang spring, hibernate,

    symfony, doctrine big data, nosql tools, scalability optimization, automation, monitoring
  3. today we will see problems with selling lemonade problems with

    running an app on server why tuning and capacity planning is important key configuration parameters
  4. out of resources haproxy[12947]: backend app_api_backend has no server available!

    nginx[432]: worker_connections exceed open file resource limit: 1024 [crit]: connect() to unix:/tmp/php5-fpm.sock failed
  5. worker_process is the backbone of nginx workers are not multi-threaded

    so they do not spread the per-connection across CPU cores usually 1 worker per CPU core
  6. worker_connections effectively limits how many connections each worker can maintain

    at a time if you grow in traffic you’ll want to eventually increase the amount of connections each worker can do
  7. keep_alive is a HTTP feature which allows user agents to

    keep the connection keep alive connections have such a huge impact on nginx performance You probably don’t need a keep alive time out value of 65 but 10-20 is definitely recommended
  8. enable tcp_nodelay and tcp_nopush these directives determine how the OS

    handles the network buffers and when to flush them to the end user disable if you are running in vagrant for dev purposes
  9. minimize i/o disable access logs send it over syslog or

    use logging capability of loadbalancer less i/o and easy to spot problems only enable `warn` level errors in logs
  10. use open_file_cache caches open file descriptors, their sizes and modification

    times information on existence of directories file lookup errors
  11. adjust buffers If the buffer sizes are set too low

    nginx will have to store the responses from upstreams in a temporary file which causes both write and read IO fastcgi_buffer_size, fastcgi_buffers, fastcgi_*_timeout
  12. use fastcgi_cache for storing and serving php-fpm responses only for

    `Public` and non-user specific 200 responses also cache 301's and 404's "Here be dragons"
  13. minimize listen backlog or disable if possible if you need

    backlog, you're doing wrong listen.backlog = 0
  14. php-fpm controls child processes based on different strategies and parameters

    you need to be aware how many processes used and waiting for sending responses
  15. `dynamic` process manager inconsistent memory and cpu usage child processes

    are set dynamically based on the parameters pm.start_servers, pm.*_spare_servers default values not tuned for production purposes
  16. `ondemand` process manager no children are created at startup children

    will be forked when new requests will connect very inconsistent memory and cpu usage can hogs your server quickly
  17. `pm.max_requests` number of requests each child process should execute before

    respawning limiting this avoids memory leaks you should use this
  18. use `pm.status_path` and proxy over nginx you can monitor a

    lot of important metric from this endpoint also you can see how many workers is busy and plan for changing worker process parameters
  19. use and optimize zend_opcache find good numbers for max_accelerated_files memory_consumption

    and interned_strings_buffer disable revalidation in production
  20. tune net.ipv4* and net.core* values in sysctl this parameters determines

    how OS handles buffers, time-waits, backlogs and timeouts use carefully!
  21. i don't have magic configurations all parameters based on your

    workload application characteristics and server configuration monitoring helps you to find good numbers
  22. wrapping things up problems with selling lemonade problems with running

    an app on server why tuning and capacity planning is important key configuration parameters