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

Apache, Nginx and PHP-FPM

Apache, Nginx and PHP-FPM

a short and brief overview of installing a basic web server (apache or nginx) with php, configuration and some optimisation. Presented at PHPMelbourne

Colby Swandale

January 15, 2013
Tweet

More Decks by Colby Swandale

Other Decks in Technology

Transcript

  1. A  look  at  Apache,  Nginx  +  PHP-­‐FPM  
    Examples  are  based  on  Debian  6  environment  
    Colby  Swandale  
    h5p://www.github.com/colby-­‐swandale  

    View full-size slide

  2. What  is  it  and  features  of  Apache,  Nginx  

    View full-size slide

  3. The  Apache  HTTP  Server  Project  is  a  collaboraEve  soFware  development  
    effort  aimed  at  creaEng  a  robust,  commercial-­‐grade,  featureful,  and  freely-­‐
    available  source  code  implementaEon  of  an  HTTP  (Web)  server  
    In  2009  it  became  the  first  web  server  to  power  over  100  million  sites  
    First  released  in  1995,  Apache  is  based  on  public  domain  HTTP  daemon  
    developed  by  Rob  McCool  at  the  NaEonal  Center  for  SupercompuEng  
    ApplicaEons,  University  of  Illinois  
    About  Apache  

    View full-size slide

  4. Nginx  (engine  x)  is  a  HTTP  and  reverse  proxy  server,  as  well  as  a  mail  proxy  
    server  
    nginx  is  the  3nd  most  popular  web  server  (2nd  is  IIS)  and  powers  sites  such  as  
    ne[lix  and  wordpress.com  
    Released  in  2004  by  Igor  Sysoev,  nginx  provides  a  unique  combinaEon  of  
    web  server,  caching  proxy  and  load  balancing  soluEons  to  any  web  site  that  
    just  wants  to  be  consistently  efficient.    
    About  Nginx  

    View full-size slide

  5. Features  of  Apache  and  Nginx  
    Apache   Nginx  
    Basic  HTTP  Auth   Yes   Yes  
    SSL/TLS  (HTTPS)   Yes   Yes  
    Virtual  HosJng   Yes   Yes  
    IPV6   Yes   Yes  
    FCGI   Yes   Yes  
    SPDY   Yes*   Yes*  
    *  Through  3rd  party  patches  

    View full-size slide

  6. Problems  of  Apache  and  Nginx  
    Apache   Nginx  
    Can  be  a  bit  of  a  memory  hog  on  high  traffic  sites  
    loss  of  .htaccess.  Making  Nginx  not  a  good  ideal  
    soluEon  for  shared  hosEng,  there  is  no  alternaEve  at  
    the  Eme  of  this  presentaEon  
    Has  6  million  opEons  but  you  only  need  5  
    Complete  hands  off  approach  to  all  dynamic  scripEng  
    languages    ie:  PHP,  Ruby.  (This  an  also  be  considered  
    a  benefit)  
    Has  difficulty  handling  large  concurrent  connecEons  
    as  well  as  nginx  
    Doesn’t  have  as  many  features  of  Apache  
    Will  always  load  mod_php  into  memory  even  if  its  
    only  for  an  image  request  
    Very  li5le  3rd  party  modules  

    View full-size slide

  7. InstallaJon  and  ConfiguraJon  

    View full-size slide

  8. Installing  Apache  
     
    apt-­‐get  install  apache2  
     
    •  h5p://h5pd.apache.org/docs/2.2/install.html  for  compile  opEons  
    •  Installed  under  /usr/local/apache2  by  default  
     
    ./configure  
     
    make  
     
    make  install  
     

    View full-size slide

  9. Configuring  Apache  
    Listen  80  
     
     
    DocumentRoot  /www/example1  
    ServerName  www.example.com            
     
     
     
     DocumentRoot  /www/example2  
     ServerName  www.example.org    
     
     
    Full  list  of  direcEves  can  be  found  at  h5p://h5pd.apache.org/docs/2.2/mod/direcEves.html  
    Apache  is  configured  by  placing  direcEves  in  plain  text  configuraEon  
    files.  The  main  configuraEon  file  is  usually  called  h5pd.conf.  

    View full-size slide

  10. Installing  Nginx  
     
    apt-­‐get  install  nginx  
     
    h5p://wiki.nginx.org/InstallOpEons  for  compile  opEons  
    Installed  under  /usr/local/nginx  by  default  
     
    ./configure  
     
    make  
     
    make  install  
     

    View full-size slide

  11. Configuring  Nginx  
    user  www-­‐data  www-­‐data;  
    worker_proccesses  5;  
     
    events  {  
     worker_connecEons  1024;  
    }  
     
    h5p  {  
    server  {  
     listen                    80;  
     server_name          www.domain.com;  
     index                      index.html;  
     root                        /home/domain.com;  
    }  
    }  
    FantasEc  list  of  nginx  configuraEons  and  user  submi5ed  example  configuraEons  can  be  found  at  h5p://wiki.nginx.org/ConfiguraEon  
    List  of  all  direcEves  can  be  found  at  h5p://wiki.nginx.org/DirecEveIndex  

    View full-size slide

  12. PHP  with  Apache  +  Nginx  

    View full-size slide

  13. Installing  PHP  
     
    apt-­‐get  install  php5  
     
     
    ./configure  –with-­‐apxs2=/usr/local/apache2/bin/apxs    -­‐-­‐with-­‐mysql  
    make  
    make  install  
     
     
    For  obvious  reasons  we  need  php,  it  is  strongly  recommended  you  have  a  plan,  ie  installing  php  with  nginx  
    or  apache,  especially  if  installing  php  from  source  as  you  will  need  to  recompile  php  if  you  missed  
    something  ie  –enable-­‐fpm  if  using  nginx  or  –with-­‐apxs2  for  apache  

    View full-size slide

  14. PHP  is  not  available  by  default,  instead  an  apache  library  “libapache2-­‐mod-­‐php5”  is  needed  to  run  PHP  
    scripts  or  we  can  compile  the  mod_php  module  from  source  but  only  if  shared  modules  is  enabled  on    
    apache  
    Apache  
     
    apt-­‐get  install  libapache2-­‐mod-­‐php5  
     
     
    //  Apache  
    ./configure  -­‐-­‐enable-­‐so  
    make  
    make  install  
     
    //PHP  
    ./configure  –with-­‐apxs2=/usr/local/apache2/bin/apxs    -­‐-­‐with-­‐mysql  
    make  
    make  install  
     
     

    View full-size slide

  15. Again,php  not  available  by  default  and  unlike  apache  where  php  is  embedded,  PHP  needs  to  run  as  a  
    separate  process,  thankfully  with  php-­‐fpm    (available  aFer  5.3.3)  we  can  do  this  
    Nginx  
     
    locaEon  ~  \.php$  {  
     fastcgi_pass    unix:/usr/local/var/run/php-­‐fpm.socket;  
     //  or  
     fastcgi_pass  127.0.0.1:9000    
    }  
     
     
    apt-­‐get  install  php5-­‐fpm  
     
    Or  
     
    ./configure  –enable-­‐fpm  
    make  
    make  install  
     
     

    View full-size slide

  16. OpJmizaJons  (Nginx  only)  
    Gathered  from  marEnvordvald.com,  link  in  references  

    View full-size slide

  17. CPU  Affinity  
     
    Sewng  CPU  affinity  basically  means  you  tell  each  worker  which  CPU  core  to  
    use,  they’ll  then  use  only  that  CPU  core.  But  your  OS  cpu  is  far  be5er  at  
    handling  load  balancing  than  you  so  best  pracEce  is  to  not  touch  it  
     
    tcp_nodelay  and  tcp_nopush  
     
    These  two  direcEves  are  probably  some  of  the  most  difficult  to  understand  
    as  they  affect  Nginx  on  a  very  low  networking  level.  The  very  short  and  
    superficial  explanaEon  is  that  these  direcEves  determine  how  the  OS  
    handles  the  network  buffers  and  when  to  flush  them  to  the  end  user.  I  can  
    only  recommend  that  if  you  do  not  know  about  these  already  then  you  
    shouldn’t  mess  with  them.  They  won’t  significantly  improve  or  change  
    anything  so  best  to  just  leave  them  at  their  default  values.  

    View full-size slide

  18. Access  and  Error  Logs  
     
    Though  access  and  error  logs  can  be  useful  for  staEsEcs,  error  checking  etc,  
    it  can  be  quite  IO  intensive  so  try  not  to  access  logs  to  images  or  resources  in  
    general,  just  make  It  log  page  requests.  
     
     
    tcp_nodelay  and  tcp_nopush  
     
    These  two  direcEves  are  probably  some  of  the  most  difficult  to  understand  
    as  they  affect  Nginx  on  a  very  low  networking  level.  The  very  short  and  
    superficial  explanaEon  is  that  these  direcEves  determine  how  the  OS  
    handles  the  network  buffers  and  when  to  flush  them  to  the  end  user.  I  can  
    only  recommend  that  if  you  do  not  know  about  these  already  then  you  
    shouldn’t  mess  with  them.  They  won’t  significantly  improve  or  change  
    anything  so  best  to  just  leave  them  at  their  default  values.  

    View full-size slide

  19. Finally,  buffers  
     
    One  of  the  most  important  things  you  need  to  tweak  is  the  buffer  sizes  you  
    allow  Nginx  to  use.  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,  the  more  traffic  you  get  the  more  of  a  problem  this  becomes.  
     
    client_body_buffer_size  is  the  direcEve  which  handles  the  client  request  
    buffer  size,  meaning  the  incoming  request  body.  This  is  used  to  handle  POST  
    data,  meaning  form  submissions,  file  uploads  etc.  You’ll  want  to  make  sure  
    that  the  buffer  is  large  enough  if  you  handle  a  lot  of  large  POST  data  
    submissions.  
     
    fastcgi_buffers  and  proxy_buffers  are  the  direcEves  which  deal  with  the  
    response  from  your  upstream,  meaning  PHP,  Apache  or  whatever  you  use.  
    The  concept  is  exactly  the  same  as  above,  if  the  buffers  aren’t  large  enough  
    the  data  will  be  saved  to  disk  before  being  served  to  the  user.  NoEce  that  
    there  is  an  upper  limit  for  what  Nginx  will  buffer,  even  on  disk,  before  it  
    transfer  it  synchronously  to  the  client.  This  limit  is  governed  by  
    fastcgi_max_temp_file_size  as  well  as  proxy_max_temp_file_size.  In  
    addiEon  you  can  also  turn  it  off  enErely  for  proxy  connecEons  with  
    proxy_buffering  set  to  off.  (Usually  not  a  good  idea!)  

    View full-size slide

  20. That’s  all  folks!  

    View full-size slide

  21. References  
    •  h5p://news.netcraF.com/archives/2013/01/07/january-­‐2013-­‐web-­‐server-­‐survey-­‐2.html  
    •  h5p://h5pd.apache.org/ABOUT_APACHE.html  
    •  h5p://blog.marEnvordvald.com/2010/07/nginx-­‐primer/  
    •  h5p://blog.marEnvordvald.com/2011/02/nginx-­‐primer-­‐2-­‐from-­‐apache-­‐to-­‐nginx/  
    •  h5p://en.wikipedia.org/wiki/Comparison_of_web_servers  
    •  h5p://php.net/manual/en/install.unix.apache2.php  
    •  h5p://blog.marEnvordvald.com/2011/04/opEmizing-­‐nginx-­‐for-­‐high-­‐traffic-­‐loads/  

    View full-size slide