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

Going Beyond mod_php

Going Beyond mod_php

A discussions of architecture options available for today's PHP deployments. Presented to Kansas City's PHP User Group.

Daniel Holmes

August 17, 2013
Tweet

More Decks by Daniel Holmes

Other Decks in Programming

Transcript

  1. Going beyond
    mod_php
    Understanding php-fpm

    View full-size slide

  2. Today’s Theme
    Making your PHP Server perform even better

    View full-size slide

  3. Questions to Answer today

    View full-size slide

  4. Questions to Answer today
    ● What could be better than Apache & mod_php?
    ● What is php-fpm? What does it look like?
    ● How does it help Apache be better?
    ● What kind of extra features are in php-fpm?
    ● What is "Opcode caching"?
    ● What is Nginix?
    ● But not exactly - How do I set it up?

    View full-size slide

  5. Apache is...
    Webserver

    View full-size slide

  6. Apache on the Server
    ps -ef | grep apache

    View full-size slide

  7. Apache is so much more
    Loaded Modules:
    core_module
    mpm_prefork_module
    http_module
    so_module
    auth_basic_module
    auth_digest_module
    authn_file_module
    authn_alias_module
    authn_anon_module
    authn_dbm_module
    authn_default_module
    authz_host_module
    authz_user_module
    authz_owner_module
    authz_groupfile_module
    authz_dbm_module
    authz_default_module
    ldap_module
    authnz_ldap_module
    include_module
    log_config_module
    logio_module
    env_module
    ext_filter_module
    mime_magic_module
    expires_module
    deflate_module
    headers_module
    usertrack_module
    setenvif_module
    mime_module
    dav_module
    status_module
    autoindex_module
    vhost_alias_module
    negotiation_module
    dir_module
    actions_module
    speling_module
    userdir_module
    alias_module
    rewrite_module
    cache_module
    suexec_module
    mem_cache_module
    version_module
    security2_module
    unique_id_module
    php5_module
    proxy_ajp_module
    ssl_module
    PHP is just one of many: apachectl -t -D DUMP_MODULES

    View full-size slide

  8. Apache is so much more
    Loaded Modules:
    core_module
    mpm_prefork_module
    http_module
    so_module
    auth_basic_module
    auth_digest_module
    authn_file_module
    authn_alias_module
    authn_anon_module
    authn_dbm_module
    authn_default_module
    authz_host_module
    authz_user_module
    authz_owner_module
    authz_groupfile_module
    authz_dbm_module
    authz_default_module
    ldap_module
    authnz_ldap_module
    include_module
    log_config_module
    logio_module
    env_module
    ext_filter_module
    mime_magic_module
    expires_module
    deflate_module
    headers_module
    usertrack_module
    setenvif_module
    mime_module
    dav_module
    status_module
    autoindex_module
    vhost_alias_module
    negotiation_module
    dir_module
    actions_module
    speling_module
    userdir_module
    alias_module
    rewrite_module
    cache_module
    suexec_module
    mem_cache_module
    version_module
    security2_module
    unique_id_module
    php5_module
    proxy_ajp_module
    ssl_module
    PHP is just one of many: apachectl -t -D DUMP_MODULES

    View full-size slide

  9. Apache + mod_php

    View full-size slide

  10. Apache + mod_php

    View full-size slide

  11. Apache + mod_php

    View full-size slide

  12. Apache is a great....

    View full-size slide

  13. Apache is a great...
    Webserver

    View full-size slide

  14. Apache + php-fpm

    View full-size slide

  15. Apache + php-fpm

    View full-size slide

  16. Apache + php-fpm

    View full-size slide

  17. Apache
    Now smaller, multi-threaded, consistent, and a lot more of them...

    View full-size slide

  18. php-fpm
    And our PHP pools are just waiting for scripts to run.

    View full-size slide

  19. What makes this better?
    ● Apache gets to do what it does best
    ● PHP becomes a service of flexible,
    restartable application pools

    View full-size slide

  20. php-fpm pools
    configured separately
    ● logging
    ● custom runtime environments
    ● resource allocation
    ● running as user/groups
    ● restrictions around what can access the pool

    View full-size slide

  21. configuring php-fpm pools
    [myphpadmz]
    user = www-phpadm
    group = www-phpadm
    listen = /tmp/php5-myphpadmz-fpm.sock
    listen.owner = www-data
    listen.group = www-data
    listen.mode = 0666
    listen.allowed_clients = 127.0.0.1

    View full-size slide

  22. configuring php-fpm pools
    pm = dynamic
    pm.max_children = 10
    pm.start_servers = 4
    pm.min_spare_servers = 2
    pm.max_spare_servers = 6
    pm.max_requests = 500
    chdir = /u01/myphpadmz/public_html
    security.limit_extensions = .php

    View full-size slide

  23. configuring php-fpm pools
    ; Pass environment variables like LD_LIBRARY_PATH.
    ; All $VARIABLEs are taken from the current environment.
    ; Default Value: clean env
    env[HOSTNAME] = $HOSTNAME
    env[PATH] = /usr/local/bin:/usr/bin:/bin
    env[TMP] = /tmp
    env[TMPDIR] = /tmp
    env[TEMP] = /tmp

    View full-size slide

  24. configuring php-fpm pools
    ;php_admin_value[sendmail_path] = /usr/sbin/sendmail -t -i -f
    [email protected]
    php_flag[display_errors] = off
    php_admin_value[error_log] = /var/weblogs/phpmyadmz/php.log
    php_admin_flag[log_errors] = on
    php_admin_value[memory_limit] = 128M

    View full-size slide

  25. configuring php-fpm pools
    ;access.log = log/$pool.access.log
    ;access.format = %R - %u %t "%m %r%Q%q" %s %f %{mili}d %{kilo}
    M %C%%
    ;slowlog = log/$pool.log.slow
    ;request_slowlog_timeout = 0
    ;request_terminate_timeout = 0
    ;rlimit_files = 1024
    ;rlimit_core = 0
    ;chroot =
    ;catch_workers_output = yes

    View full-size slide

  26. The missing Link?
    Naturally, Apache can’t
    talk to php-fpm
    It needs a module

    View full-size slide

  27. The missing Link
    FastCGI is the interface php-fpm
    expects to use with the Web
    Server

    View full-size slide

  28. The missing Link
    For Apache, is generally:
    ● mod_fcgi
    ● mod_fastcgi
    This can get confusing when you
    are looking at blogs, so listen up!

    View full-size slide

  29. mod_fcgi
    Newer, Bigger Brother
    Duplicates a lot already in php-fpm
    Useful for non php-fpm cgi’s
    Can be used with mod_suexec
    Available in most package managers

    View full-size slide

  30. mod_fastcgi
    Simple to configure
    Doesn’t manage it’s own pool
    Is simply a FastCGI interface
    May not come with your Package
    Manager, but easy to build

    View full-size slide

  31. mod_fastcgi in apache

    AddHandler php5-myphpa-fcgi .php
    Action php5-myphpa-fcgi /php5.myphpa.fcgi
    Alias /php5.myphpa.fcgi /usr/lib/cgi-bin/php5.myphpa.fcgi
    FastCGIExternalServer /usr/lib/cgi-bin/php5.myphpa.fcgi
    -socket /tmp/php5-myphpadmz-fpm.sock

    View full-size slide

  32. Kicking up the Heat
    Other things we can do:
    ● Adding an Opcode Cache
    ● Logging Slow PHP requests
    ● Replacing Apache?

    View full-size slide

  33. Adding an OpCode cache
    ● Full stack frameworks are big
    ● Wordpress is big
    ● Drupal is big
    ● 100+ php scripts need to be compiled per requests
    ● php's compiler is still FREAKY FAST
    ● But still, that's a lot of compiling

    View full-size slide

  34. Opcode Caching Options
    ● Zend's Opcache
    ○ Recently Open Sourced, built into PHP 5.5
    ● APC
    ○ LOTS of years of production use.
    ○ Additional software
    ○ APC can also act like a generic cache much like
    mcached...but that's a whole other talk

    View full-size slide

  35. Zend’s PHP’s Opcache
    Looking at Chris Jones' post, in 5.5 is should be as easy
    as:
    ; Adding the extension ...
    zend_extension=opcache.so
    ; and turning it on...
    opcache.enable=On

    View full-size slide

  36. APC - Alternate PHP Cache
    Install from PECL
    or your OS's
    package manager
    Can use a .php
    script to see
    statistics
    LOTS of blog
    posts on helping
    configure with your
    setup http://www.electrictoolbox.com/apc-php-cache-information/

    View full-size slide

  37. Logging slow PHP runs
    php-fpm’s slow query log

    View full-size slide

  38. Replacing Apache
    Apache is still a GREAT, mature, well tested server
    Now we get to ask the questions:
    ● Do we need or even use what it offers?
    ● Can we squeeze out more performance
    with something else?
    ● Is there something easier to configure?

    View full-size slide

  39. Nginx
    Was originally a Reverse Proxy -- so built for speed
    May take less memory (so more for php and mysql)
    Supports fastcgi natively
    Allows precise control with a simpler syntax
    Is not Apache -- so no .htaccess files
    Still "new" compared to Apache's maturity, but this doesn't
    mean experimental

    View full-size slide

  40. Nginx Config
    location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.
    ini
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;

    View full-size slide

  41. Nginx Config
    Like Apache, you can also configure Nginx to:
    ● route all non-matching requests through an index.php
    script (Wordpress, Drupal, etc)
    ● block access to config files, etc.
    ● configure extra modules to add functionality

    View full-size slide

  42. Nginx and php-fpm
    Notice the nginx processes are tiny both in resident and virtual sizes

    View full-size slide

  43. Want to dive deeper?
    Just ask!
    KC PUG is always looking for
    speakers and topic ideas
    Don’t be afraid to speak up!
    http://www.meetup.com/kcphpug/suggestion/

    View full-size slide

  44. Thank you!
    @dan_holmes
    http://kcpug.org

    View full-size slide