摘要:

什么是apr?

    apr 是apache portable runtime 用上这个后可以有效的提高apahe处理静态页面的能力,将Tomcat线程数开到300以上的水平,然后模拟一大堆并发请求。如果不配APR,基本上300个线程狠快就用满,以后的请求就只好等待。但是配上APR之后,并发的线程数量明显下降,从原来的300可能会马上下降到只有几十,新的请求会毫无阻塞的进来。APR对于apache最大的作用就是socket调度。你在局域网环境测,就算是400个并发,也是一瞬间就处理/传输完毕,但是在真实的Internet环境下,页面处理时间只占0.1%都不到,绝大部分时间都用来页面传输。如果不用APR,一个线程同一时间只能处理一个用户,势必会造成阻塞。所以生产环境下用apr是非常必要的。
 

什么是XCache?

   XCache 是一个开源的 opcode 缓存器/优化器, 这意味着他能够提高您服务器上的 PHP 性能. 他通过把编译 PHP 后的数据缓冲到共享内存从而避免重复的编译过程, 能够直接使用缓冲区已编译的代码从而提高速度. 通常能够提高您的页面生成速率 2 到5 倍, 降低服务器负载.
 

安装环境和软件版本:

  系统版本:  redhat5.8

  httpd版本: httpd-2.4.4          下载地址:http://httpd.apache.org/download.cgi#apache24

  mysql版本: mysql-5.5.28         下载地址:http://www.mysql.com/downloads/mysql/5.5.html#downloads

  XCache版本:xcache-3.0.1         下载地址:http://xcache.lighttpd.net/wiki/Release-3.0.1

  php版本:   php-5.4.13           下载地址:http://cn2.php.net/manual-lookup.php?pattern=5.4.13&scope=quickref

 开发环境包必须先装上:Development Libraries     Development Tools
  

本文安装过程大致分为四部分安装:

 一,安装apr

 二,安装httpd

 三,安装mysql

 四,安装php

 五,安装 XCache

 

 

LAMP+ XCache安装过程:

一、解决依赖关系,安装apr

  httpd-2.4.4需要较新版本的apr和apr-util,因此需要事先对其进行升级。升级方式有两种,
  一种是通过源代码编译安装,一种是直接升级rpm包。这里选择使用编译源代码的方式进行.

  1. 一、解决依赖关系,安装apr  
  2.  
  3.   httpd-2.4.4需要较新版本的apr和apr-util,因此需要事先对其进行升级。升级方式有两种,  
  4.   一种是通过源代码编译安装,一种是直接升级rpm包。这里选择使用编译源代码的方式进行.  
  5.  
  6. 1、 编译安装apr  
  7.  
  8. # tar xf apr-1.4.6.tar.bz2  
  9. # cd apr-1.4.6  
  10. # ./configure --prefix=/usr/local/apr  
  11. # make && make install  
  12.  
  13. 2、 编译安装apr-util  
  14.  
  15. # tar xf apr-util-1.5.2.tar.bz2  
  16. # cd apr-util-1.5.2  
  17. # ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr    #### 因为这个包依赖上面的包 所以要指定apr 在那个位置,系统上默认也有apr   
  18. # make && make install  
  19.  
  20.  
  21.  httpd-2.4.4编译过程也要依赖于pcre-devel软件包,需要事先安装。此软件包系统光盘自带,因此,找到并安装即可。  
  22. # yum install -y pcre-devel 

 

二、编译安装httpd-2.4.4
  首先下载httpd-2.4.4到本地,而后执行如下命令进行编译安装过程:

  1. 二、编译安装httpd-2.4.4  
  2.   首先下载httpd-2.4.4到本地,而后执行如下命令进行编译安装过程:  
  3.     
  4. 1、    
  5. # tar xf httpd-2.4.4.tar.bz2  
  6. # cd httpd-2.4.4  
  7. # ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-mpms-shared=all --with-mpm=event  
  8. # make && make install  
  9.  
  10. ###初始化参数解释#####  
  11.  --prefix=/usr/local/apache         初始化默认安装目录  
  12. --sysconfdir=/etc/httpd             默认配置文件位置         
  13. --enable-so                         支持动态共享模块                         
  14. --enable-ssl                        支持ssl加密功能,支持使用https  
  15. --enable-cgi                        支持CGI  
  16. --enable-rewrite                    支持URL重写  
  17. --with-zlib                          
  18. --with-pcre   
  19. --with-apr=/usr/local/apr           指定apr目录  
  20. --with-apr-util=/usr/local/apr-util 指定apr-util目录  
  21. --enable-mpms-shared=all            编译所有的MPM模块  
  22. --with-mpm=event                    设置默认MPM模块为event  
  23.  
  24. #################  
  25. #################  
  26. 补充:  
  27.  
  28. (1)构建MPM为静态模块  
  29.  在全部平台中,MPM都可以构建为静态模块。在构建时选择一种MPM,链接到服务器中。如果要改变MPM,必须重新构建。  
  30.  为了使用指定的MPM,请在执行configure脚本 时,使用参数 --with-mpm=NAME。NAME是指定的MPM名称。  
  31.  编译完成后,可以使用 ./httpd -l 来确定选择的MPM。 此命令会列出编译到服务器程序中的所有模块,包括 MPM。  
  32.  
  33. (2)构建 MPM 为动态模块  
  34.  在Unix或类似平台中,MPM可以构建为动态模块,与其它动态模块一样在运行时加载。   
  35.  构建 MPM 为动态模块允许通过修改LoadModule指令内容来改变MPM,而不用重新构建服务器程序。  
  36.  在执行configure脚本时,使用--enable-mpms-shared选项即可启用此特性。当给出的参数为all时,  
  37.  所有此平台支持的MPM模块都会被安装。还可以在参数中给出模块列表。默认MPM,  
  38.  可以自动选择或者在执行configure脚本时通过--with-mpm选项来指定,然后出现在生成的服务器配置文件中。  
  39.  编辑LoadModule指令内容可以选择不同的MPM。  
  40.  
  41. ####################  
  42. ####################  
  43.  
  44. 2、修改httpd的主配置文件,设置其Pid文件的路径  
  45.  
  46.   编辑/etc/httpd/httpd.conf,添加如下行即可:  
  47.   PidFile  "/var/run/httpd.pid" 
  48.  
  49. 3、提供SysV服务脚本/etc/rc.d/init.d/httpd,内容如下:  
  50.  
  51. #!/bin/bash  
  52. #  
  53. # httpd        Startup script for the Apache HTTP Server  
  54. #  
  55. # chkconfig: - 85 15  
  56. # description: Apache is a World Wide Web server.  It is used to serve \  
  57. #          HTML files and CGI.  
  58. # processname: httpd  
  59. # config: /etc/httpd/conf/httpd.conf  
  60. # config: /etc/sysconfig/httpd  
  61. # pidfile: /var/run/httpd.pid  
  62.  
  63. # Source function library.  
  64. . /etc/rc.d/init.d/functions  
  65.  
  66. if [ -f /etc/sysconfig/httpd ]; then 
  67.         . /etc/sysconfig/httpd  
  68. fi  
  69.  
  70. # Start httpd in the C locale by default.  
  71. HTTPD_LANG=${HTTPD_LANG-"C"}  
  72.  
  73. # This will prevent initlog from swallowing up a pass-phrase prompt if  
  74. # mod_ssl needs a pass-phrase from the user.  
  75. INITLOG_ARGS="" 
  76.  
  77. Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server  
  78. with the thread-based "worker" MPM; BE WARNED that some modules may not 
  79. work correctly with a thread-based MPM; notably PHP will refuse to start.  
  80.  
  81. # Path to the apachectl script, server binaryand short-form for messages.  
  82. apachectl=/usr/local/apache/bin/apachectl  
  83. httpd=${HTTPD-/usr/local/apache/bin/httpd}  
  84. prog=httpd  
  85. pidfile=${PIDFILE-/var/run/httpd.pid}  
  86. lockfile=${LOCKFILE-/var/lock/subsys/httpd}  
  87. RETVAL=0  
  88.  
  89. start() {  
  90.         echo -n $"Starting $prog: " 
  91.         LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS  
  92.         RETVAL=$?  
  93.         echo  
  94.         [ $RETVAL = 0 ] && touch ${lockfile}  
  95.         return $RETVAL  
  96. }  
  97.  
  98. stop() {  
  99.     echo -n $"Stopping $prog: " 
  100.     killproc -p ${pidfile} -d 10 $httpd  
  101.     RETVAL=$?  
  102.     echo  
  103.     [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}  
  104. }  
  105. reload() {  
  106.     echo -n $"Reloading $prog: " 
  107.     if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/nullthen 
  108.         RETVAL=$?  
  109.         echo $"not reloading due to configuration syntax error" 
  110.         failure $"not reloading $httpd due to configuration syntax error" 
  111.     else 
  112.         killproc -p ${pidfile} $httpd -HUP  
  113.         RETVAL=$?  
  114.     fi  
  115.     echo  
  116. }  
  117.  
  118. # See how we were called.  
  119. case "$1" in 
  120.   start)  
  121.     start  
  122.     ;;  
  123.   stop)  
  124.     stop  
  125.     ;;  
  126.   status)  
  127.         status -p ${pidfile} $httpd  
  128.     RETVAL=$?  
  129.     ;;  
  130.   restart)  
  131.     stop  
  132.     start  
  133.     ;;  
  134.   condrestart)  
  135.     if [ -f ${pidfile} ] ; then 
  136.         stop  
  137.         start  
  138.     fi  
  139.     ;;  
  140.   reload)  
  141.         reload  
  142.     ;;  
  143.   graceful|help|configtest|fullstatus)  
  144.     $apachectl $@  
  145.     RETVAL=$?  
  146.     ;;  
  147.   *)  
  148.     echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}" 
  149.     exit 1  
  150. esac  
  151.  
  152. exit $RETVAL  
  153.  
  154. 4、而后为此脚本赋予执行权限:  
  155. # chmod +x /etc/rc.d/init.d/httpd  
  156.  
  157. 5、加入服务列表:  
  158. # chkconfig --add httpd  
  159.  
  160. 6、加入httpd 配置文件检测。新建 httpd.sh  
  161. #vim /etc/profile.d/httpd.sh     
  162.  加入此行: export  PATH=$PATH:/usr/local/apache/bin    
  163.  
  164.  
  165. 接下来就可以启动服务进行测试了。 

 

测试结果截图: 

WEB中的经典案例 《LAMP+XCache》让性能直线飙升_WEB中的经典案例 《LAMP+XCac

 

WEB中的经典案例 《LAMP+XCache》让性能直线飙升_WEB中的经典案例 《LAMP+XCac_02

 

 

三、安装mysql-5.5.28

  1. 三、安装mysql-5.5.28  
  2.  
  3. 1、准备数据存放的文件系统  
  4.  
  5. 新建一个逻辑卷,并将其挂载至特定目录即可。创建一个20G的磁盘类型为8e,这里不再做掩饰,  
  6. 使用逻辑卷是考虑到安全方面,也可以不适用,默认目录或者随便一个目录都可以。  
  7. 这里假设其逻辑卷的挂载目录为/mydata,而后需要创建/mydata/data目录做为mysql数据的存放目录。  
  8. #partprobe /dev/sda11     
  9. #pvcreate  /dev/sda11  
  10. #vgcreate  myvg /dev/sda11  
  11. #lvcreate -n mydata  -L 5G /dev/myvg  
  12. #mke2fs -j /dev/sda11  
  13. #vim /etc/fstab  
  14.  加入以下行:  
  15.  /dev/sda11              /mydata                 ext3    defaults        0 0  
  16. #mount -a  
  17.  
  18.  
  19. 2、新建用户以安全方式运行进程:  
  20.  
  21. # groupadd -r mysql  
  22. # useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql  
  23. # chown -R mysql:mysql /mydata/data  
  24. # chmod o-rx /mydata/data/  
  25.  
  26. 3、安装并初始化mysql-5.5.28  
  27.  
  28. 首先下载平台对应的mysql版本至本地,这里是32位平台,因此,选择的为mysql-5.5.28-linux2.6-i686.tar.gz。  
  29.  
  30. # tar xf mysql-5.5.28-linux2.6-i686.tar.gz -C /usr/local 
  31. # cd /usr/local/  
  32. # ln -sv mysql-5.5.28-linux2.6-i686  mysql  
  33. # cd mysql   
  34.  
  35. # chown -R mysql:mysql  .  
  36. # scripts/mysql_install_db --user=mysql --datadir=/mydata/data  
  37. # chown -R root  .  
  38.  
  39. 4、为mysql提供主配置文件:  
  40.  
  41. # cd /usr/local/mysql  
  42. # cp support-files/my-large.cnf  /etc/my.cnf  
  43.  
  44. 并修改此文件中thread_concurrency的值为你的CPU个数乘以2,比如这里使用如下行:  
  45. thread_concurrency = 2  
  46.  
  47. 另外还需要添加如下行指定mysql数据文件的存放位置:  
  48. datadir = /mydata/data  
  49.  
  50.  
  51. 5、为mysql提供sysv服务脚本:  
  52.  
  53. # cd /usr/local/mysql  
  54. # cp support-files/mysql.server  /etc/rc.d/init.d/mysqld  
  55. # chmod +x /etc/rc.d/init.d/mysqld  
  56.  
  57. 6、添加至服务列表:  
  58. # chkconfig --add mysqld  
  59. # chkconfig mysqld on 
  60.  
  61.  
  62.   为了使用mysql的安装符合系统使用规范,并将其开发组件导出给系统使用,这里还需要进行如下步骤:  
  63. 7、输出mysql的man手册至man命令的查找路径:  
  64.  
  65. 编辑/etc/man.config,添加如下行即可:  
  66. MANPATH  /usr/local/mysql/man  
  67.  
  68. 8、输出mysql的头文件至系统头文件路径/usr/include:  
  69.  
  70. 这可以通过简单的创建链接实现:  
  71. # ln -sv /usr/local/mysql/include  /usr/include/mysql  
  72.  
  73. 9、输出mysql的库文件给系统库查找路径:  
  74.  
  75. # echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf  
  76.  
  77. 而后让系统重新载入系统库:  
  78. # ldconfig  
  79.  
  80. 10、修改PATH环境变量,让系统可以直接使用mysql的相关命令。  
  81. #vim  /etc/profile.d/mysql.sh  输入以下内容  
  82.  export PATH=$PATH:/usr/local/mysql/bin  
  83.  
  84. 11、启动mysql,而后就可以启动服务测试使用了。  
  85.  #service mysqld restart 

 

测试mysql 结果截图:

WEB中的经典案例 《LAMP+XCache》让性能直线飙升_WEB中的经典案例 《LAMP+XCac_03

  

四、编译安装php-5.4.13

  1. 四、编译安装php-5.4.13  
  2.  
  3. 1、解决依赖关系:  
  4.  
  5. 请配置好yum源(可以是本地系统光盘)后执行如下命令:  
  6. # yum -y groupinstall "X Software Development"   
  7.  
  8. 如果想让编译的php支持mcrypt和mhash扩展,四个rpm包并安装之:  
  9. 下载网站:http://rpm.pbone.net/  直接在此网站搜索下载  
  10. libmcrypt-2.5.7-5.el5.i386.rpm  
  11. libmcrypt-devel-2.5.7-5.el5.i386.rpm  
  12. mhash-0.9.2-6.el5.i386.rpm  
  13. mhash-devel-0.9.2-6.el5.i386.rpm  
  14. #rpm -ivh *.rpm  
  15.  
  16.  
  17. 2、编译安装php-5.4.13  
  18.  
  19. 首先下载源码包至本地目录  
  20.  
  21. # tar xf php-5.4.13.tar.bz2  
  22. # cd php-5.4.13  
  23. # ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl  
  24.  --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring  
  25.  --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib   
  26.  --with-libxml-dir=/usr --enable-xml  --enable-sockets   
  27.  --with-apxs2=/usr/local/apache/bin/apxs --with-mcrypt    
  28.  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d   
  29.  --with-bz2  --enable-maintainer-zts  
  30.  
  31.  ##############  
  32.  ##############  
  33.   说明:  
  34.   1、这里为了支持apache的worker或event这两个MPM,编译时使用了--enable-maintainer-zts选项。  
  35.   2、如果使用PHP5.3以上版本,为了链接MySQL数据库,可以指定mysqlnd,这样在本机就不需要先安装MySQL或MySQL开发包了。  
  36.    mysqlnd从php 5.3开始可用,可以编译时绑定到它(而不用和具体的MySQL客户端库绑定形成依赖),但从PHP 5.4开始它就是默认设置了。  
  37. ###############  
  38. ###############  
  39.  
  40. # ./configure --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd  
  41. # make  
  42. # make test  
  43. # make intall  
  44.  
  45. 3、为php提供配置文件:  
  46.  
  47.  # cp php.ini-production /etc/php.ini  
  48.  
  49. 4、 编辑apache配置文件httpd.conf,以apache支持php  
  50.  # vim /etc/httpd/httpd.conf  
  51.  (1)、添加如下二行  
  52.    AddType application/x-httpd-php  .php  
  53.    AddType application/x-httpd-php-source  .phps  
  54.  
  55.  (2)、定位至DirectoryIndex index.html   
  56.    修改为:  
  57.     DirectoryIndex  index.php  index.html  
  58. 5、修改默认网页路径测试php  
  59.   vim /usr/local/apache/htdocs/index.php  
  60.    <?php  
  61.     phpinfo ()  
  62.    ?>  
  63.  如果想查看php是否连接到mysql 那么就加入下面 代码;  
  64.     <?php  
  65.      $conn=mysql_connect('localhost','root','');  
  66.      if ($conn)  
  67.      echo "Success...";  
  68.      else 
  69.      echo "Failure...";  
  70.      ?>  
  71.  
  72. 6、新启动httpd,或让其重新载入配置文件即可测试php是否已经可以正常使用。  
  73.  service httpd restart  
  74.   

 

测试php是否成功:

WEB中的经典案例 《LAMP+XCache》让性能直线飙升_WEB中的经典案例 《LAMP+XCac_04

 

 

五、安装xcache,为php加速:
 

  1. 五、安装xcache,为php加速:  
  2.  
  3. 1、安装  
  4. # tar xf xcache-3.0.1.tar.gz  
  5. # cd xcache-3.0.1  
  6. # /usr/local/php/bin/phpize    ###这条命令意思是在制定的目录下执行phpize准备php扩展以便编译  
  7. # ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config  
  8. # make && make install  
  9.  
  10.  
  11. 安装结束时,会出现类似如下行:  
  12. Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-zts-20100525/  
  13.  
  14. 2、编辑php.ini,整合php和xcache:  
  15.  
  16. 首先将xcache提供的样例配置导入php.ini  
  17. # mkdir /etc/php.d  
  18. # cp xcache.ini /etc/php.d  
  19.  
  20.  
  21. #######注意#######  
  22.  安装xcache-3.0.1的时候下面步骤不用做,在此版本之前的版本下面的步骤要做  
  23. #######注意#######  
  24.  
  25. 说明:xcache.ini文件在xcache的源码目录中。  
  26.  
  27. 接下来编辑/etc/php.d/xcache.ini,找到zend_extension开头的行,修改为如下行:  
  28. zend_extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so  
  29.  
  30. 注意:如果php.ini文件中有多条zend_extension指令行,要确保此新增的行排在第一位。  
  31.  重复httpd 让其生效  
  32.   service httpd restart  
  33.  编辑index.php查看xcache 是否生效  
  34.   vim /usr/local/apache/htdocs/index.php   
  35.     <?php  
  36.      phpinfo();  
  37.     ?>  
  38.    
  39.  
  40.  
  41. 4、配置虚拟主机  
  42. (1),要使用虚拟主机必须先注销中心主机  
  43. #vim /etc/httpd/httpd.conf  
  44.    注释掉此行 DocumentRoot "/usr/local/apache/htdocs" 
  45.    启用此行 Include /etc/httpd/extra/httpd-vhosts.conf  
  46.    启用 mod_log_config 模块 ,大概在105行  
  47. (2),vim /etc/httpd/extra/httpd-vhosts.conf  
  48.      添加两台虚拟主机  
  49.     <VirtualHost *:80>  
  50.     ServerName www.a.com  
  51.     DocumentRoot "/www/a.com" 
  52.     <Directory "/www/a.com">  
  53.        Options none  
  54.        AllowOverride none  
  55.        Require all granted  
  56.     </Directory>  
  57.     ErrorLog "/var/log/httpd/a.com-error_log" 
  58.     CustomLog "/var/log/httpd/a.com-access_log" combined  
  59. </VirtualHost>  
  60.  
  61. <VirtualHost *:80>  
  62.     ServerName www.b.com  
  63.     DocumentRoot "/www/b.com" 
  64.     <Directory "/www/b.com">  
  65.        Options none  
  66.        AllowOverride none  
  67.        Require all granted  
  68.     </Directory>  
  69.     ErrorLog "/var/log/httpd/b.com-error_log" 
  70.     CustomLog "/var/log/httpd/b.com-access_log" common  
  71. </VirtualHost>  
  72.                                                 
  73.  
  74. (3)、创建所需要的目录  
  75.   mkdir /www/{a.com,b.com} -pv  
  76.   mkdir /var/log/httpd  -pv  
  77.  
  78. (4),为虚拟主机准备网页  
  79.    cd /www/a.com  
  80.    vim index.php  
  81.    <?php  
  82.    phpinfo()  
  83.   ?>  
  84.  
  85. 配置结束 开始测试吧  

 

xcache-3.0.1 测试结果: 

WEB中的经典案例 《LAMP+XCache》让性能直线飙升_WEB中的经典案例 《LAMP+XCac_05

 

 

配置结束!!!!!