LAMP 优化过程


                       周灵杰
  http://blog.netzhou.net
高性能
   非代码层面
   对系统的设计
   对开源软件方案的选择
   对系统的监控
   对服务器设置,优化
架
         构设计原则
   易于扩展
   结构简单
   采用成熟的开源软件及解决方案
   层次清晰
   先可用,抗住,再优化
   过度设计比不设计更龌龊
现代硬件架构
   CPU 转向多核,非提高 CPU 的主频
   内存是新的磁盘,大内存多核 NUMA
   磁盘,提高容量,非转速, IOPS 不高
   新型 SSD , PCI-E 型 SSD ,混合存储
   网卡的多通道, 10G
Web Server
   Nginx + php-fpm
   Nginx + php-cgi
   Apache + mod_php
   Apache + php-cgi
   …
Nginx Tips
   worker_processes
   worker_cpu_affinity
   open_file_cache max=xx inactive=xxs;
   open_file_cache_valid xxs;
   open_file_cache_min_uses 2;
   sendfile           on;
   keepalive_timeout xx;
   tcp_nopush on;
   tcp_nodelay on;
PHP-fpm Tips
   <value   name="listen_address">/dev/shm/php.socket</value>
   <value   name="max_children">512</value>
   <value   name="request_slowlog_timeout">1s</value>
   <value   name="slowlog">logs/slow.log</value>
   <value   name="rlimit_files">65535</value>
DB Cache
   Redis
   Memcached
MySQL
   MySQL Server tunning
   分库,分表
   索引 &Explain
   主从同步,读写分离
   Query Cache
   FlashCache 混合存储
OS
   EXT4 , XFS 文件系统
   CPU IO 调度算法
   IO 队列长度
   Raid 卡 Cache 策略,电池放电对于磁盘 IO 的影
    响
   多核大内存 NUMA
   网卡软中断多核( RPS , RFS , RSS )
   Iptables 调整
   Ulimit
TCP
   net.ipv4.tcp_max_syn_backlog = 65536
   net.core.netdev_max_backlog = 32768
   net.core.somaxconn = 32768

   net.core.wmem_default = 8388608
   net.core.rmem_default = 8388608
   net.core.rmem_max = 16777216
   net.core.wmem_max = 16777216

   net.ipv4.tcp_synack_retries = 1
   net.ipv4.tcp_syn_retries = 1

   net.ipv4.tcp_tw_recycle = 1
   net.ipv4.tcp_tw_reuse = 1
   net.ipv4.tcp_timestamps = 0

   net.ipv4.tcp_congestion_control = bic
   net.ipv4.tcp_ecn = 0

   net.ipv4.tcp_mem = 94500000 915000000 927000000
   net.ipv4.tcp_max_orphans = 3276800

   net.ipv4.tcp_keepalive_intvl = 15
   net.ipv4.tcp_keepalive_probes = 3
   net.ipv4.tcp_keepalive_time = 60

   net.ipv4.tcp_fin_timeout = 30
   net.ipv4.ip_local_port_range = 1024 65535
无处
              不在的 cache
   Browser cache
   Page cache
   PHP 的 Realpath Cache
   Nginx 的 facgi-cache
   PHP opcode ( APC,eAccelerator,XCache )
   DB Cache
监控
   Nagios
   Cacti
   PHP Slow function
   PHP Error Log
   Nginx Error Log
   Mysql Slow Log
   Redis Slow log
其它
   PHP extension
    GD < Imagemagic < Graphicsmagick
  memcached < memcache
  xCache < eAccelerator < APC
PHP Profile
  xdebug, xhprof
hiphop-php
…
Q&A

Lamp优化实践

  • 1.
    LAMP 优化过程 周灵杰 http://blog.netzhou.net
  • 2.
    高性能  非代码层面  对系统的设计  对开源软件方案的选择  对系统的监控  对服务器设置,优化
  • 3.
    构设计原则  易于扩展  结构简单  采用成熟的开源软件及解决方案  层次清晰  先可用,抗住,再优化  过度设计比不设计更龌龊
  • 5.
    现代硬件架构  CPU 转向多核,非提高 CPU 的主频  内存是新的磁盘,大内存多核 NUMA  磁盘,提高容量,非转速, IOPS 不高  新型 SSD , PCI-E 型 SSD ,混合存储  网卡的多通道, 10G
  • 6.
    Web Server  Nginx + php-fpm  Nginx + php-cgi  Apache + mod_php  Apache + php-cgi  …
  • 7.
    Nginx Tips  worker_processes  worker_cpu_affinity  open_file_cache max=xx inactive=xxs;  open_file_cache_valid xxs;  open_file_cache_min_uses 2;  sendfile on;  keepalive_timeout xx;  tcp_nopush on;  tcp_nodelay on;
  • 8.
    PHP-fpm Tips  <value name="listen_address">/dev/shm/php.socket</value>  <value name="max_children">512</value>  <value name="request_slowlog_timeout">1s</value>  <value name="slowlog">logs/slow.log</value>  <value name="rlimit_files">65535</value>
  • 9.
    DB Cache  Redis  Memcached
  • 10.
    MySQL  MySQL Server tunning  分库,分表  索引 &Explain  主从同步,读写分离  Query Cache  FlashCache 混合存储
  • 11.
    OS  EXT4 , XFS 文件系统  CPU IO 调度算法  IO 队列长度  Raid 卡 Cache 策略,电池放电对于磁盘 IO 的影 响  多核大内存 NUMA  网卡软中断多核( RPS , RFS , RSS )  Iptables 调整  Ulimit
  • 12.
    TCP  net.ipv4.tcp_max_syn_backlog = 65536  net.core.netdev_max_backlog = 32768  net.core.somaxconn = 32768  net.core.wmem_default = 8388608  net.core.rmem_default = 8388608  net.core.rmem_max = 16777216  net.core.wmem_max = 16777216  net.ipv4.tcp_synack_retries = 1  net.ipv4.tcp_syn_retries = 1  net.ipv4.tcp_tw_recycle = 1  net.ipv4.tcp_tw_reuse = 1  net.ipv4.tcp_timestamps = 0  net.ipv4.tcp_congestion_control = bic  net.ipv4.tcp_ecn = 0  net.ipv4.tcp_mem = 94500000 915000000 927000000  net.ipv4.tcp_max_orphans = 3276800  net.ipv4.tcp_keepalive_intvl = 15  net.ipv4.tcp_keepalive_probes = 3  net.ipv4.tcp_keepalive_time = 60  net.ipv4.tcp_fin_timeout = 30  net.ipv4.ip_local_port_range = 1024 65535
  • 13.
    无处 不在的 cache  Browser cache  Page cache  PHP 的 Realpath Cache  Nginx 的 facgi-cache  PHP opcode ( APC,eAccelerator,XCache )  DB Cache
  • 14.
    监控  Nagios  Cacti  PHP Slow function  PHP Error Log  Nginx Error Log  Mysql Slow Log  Redis Slow log
  • 15.
    其它  PHP extension GD < Imagemagic < Graphicsmagick memcached < memcache xCache < eAccelerator < APC PHP Profile xdebug, xhprof hiphop-php …
  • 16.