天天看点

httpd六之源码编译lamp并能够实现xcache为php加速

本文主要讲述如何通过源码来编译LAMP,并可以通过xcache能给为php加速,本文使用的是三台机器,本处不考虑单点故障的问题,关于负载均衡的问题,将在后续推出,敬请期待!!!

一、环境规划

用途

ip地址

安装应用

下载地址

前段服务器

192.168.1.200

httpd2.4.9

http://apache.fayea.com/apache-mirror//httpd/httpd-2.4.9.tar.bz2

后端php与服务器

192.168.1.201

php5.4.26

http://cn2.php.net/get/php-5.4.26.tar.bz2/from/this/mirror

xcache服务器

xcache

http://xcache.lighttpd.net/pub/Releases/3.0.4/xcache-3.0.4.tar.bz2

数据库服务器

192.168.1.202

mysqld5.5.36

http://mirrors.sohu.com/mysql/MySQL-5.5/mysql-5.5.35-linux2.6-x86_64.tar.gz

本文所做实验的机器的系统都为centos6.5,并都已经安装好开发包组“Server Platform Development”和“Development tools”

二、编译安装httpd

关于编译安装httpd2.4.9请移步至本人的博客http://wangfeng7399.blog.51cto.com/3518031/1379373,本处就不再做累赘。

三、编译安装mysqld

1)、获取源程序、解压、创建软连接,此处创建软连接的好处在于在以后为mysql升级做便利。

1

2

3

<code>[root@localhost ~]# tar xf mysql-</code><code>5.5</code><code>.</code><code>36</code><code>-linux2.</code><code>6</code><code>-x86_64.tar.gz -C /usr/local/</code>

<code>[root@localhost ~]# cd /usr/local/</code>

<code>[root@localhost local]# ln -sv mysql-</code><code>5.5</code><code>.</code><code>36</code><code>-linux2.</code><code>6</code><code>-x86_64 mysql</code>

2)、建议:在生产环境中应该将数据库建在lvm上,底层的lvm应该通过硬raid来搭建。本文建立了一个10G的lvm的逻辑卷,并实现开机自动挂载/data下

<code>在/etc/fstab中添加</code>

<code>/dev/mydata/data        /data                   ext4    defaults        </code><code>0</code> <code>0</code>

3)、挂载后再/data目录创建mydata目录来存放数据文件

<code>[root@localhost local]# mkdir /data/mydata</code>

4)、创建mysql系统用户和系统组,并使其不能登陆系统

<code>[root@localhost local]# groupadd -r mysql</code>

<code>[root@localhost local]# useradd -r  -g mysql -s /sbin/nologin mysql</code>

5)、为了安全起见,我们将mysql目录下的内容的属组改为mysql

<code>[root@localhost local]# chown .mysql mysql -R</code>

6)、安装数据库

<code>[root@localhost mysql]# scripts/mysql_install_db --datadir=/data/mydata/ --user=mysql</code>

  查看/data/mydata下是否会生成默认数据库

<a href="http://s3.51cto.com/wyfs02/M01/23/01/wKioL1MtmSGAWgmbAADUK62F2qA324.jpg" target="_blank"></a>

7)、导出mysql的二进制文件、头文件、库文件、帮助文档

4

5

<code>[root@localhost mysql]# echo </code><code>"export PATH=/usr/local/mysql/bin:$PATH"</code> <code>&gt; /etc/profile.d/mysql.sh</code>

<code>[root@localhost mysql]# echo </code><code>"/usr/local/mysql/lib"</code> <code>&gt; /etc/ld.so.conf.d/mysql.conf</code>

<code>[root@localhost mysql]# ln -sv </code><code>include</code> <code>/usr/</code><code>include</code><code>/mysql</code>

<code>`/usr/</code><code>include</code><code>/mysql</code><code>' -&gt; `include'</code>

<code>[root@localhost mysql]# . /etc/profile.d/mysql.sh</code>

  帮助文档在/etc/man.config中添加MANPATH /usr/local/mysql/man

8)、为mysql提供配置文件

<code>[root@localhost mysql]# cp support-files/my-large.cnf /etc/my.cnf</code>

<code># Try number of CPU's*</code><code>2</code> <code>for</code> <code>thread_concurrency</code>

<code>thread_concurrency = </code><code>2</code>  <code>//改成cpu的两倍</code>

<code>datadir= /data/mydata  </code><code>//数据库文件的存放位置</code>

9)、为mysql提供启动脚本,并使其能够开机自动启动

<code>[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld</code>

<code>[root@localhost mysql]# chkconfig --add mysqld</code>

<code>[root@localhost mysql]# chkconfig --list mysqld</code>

<code>mysqld          </code><code>0</code><code>:off   </code><code>1</code><code>:off   </code><code>2</code><code>:on    </code><code>3</code><code>:on    </code><code>4</code><code>:on    </code><code>5</code><code>:on    </code><code>6</code><code>:off</code>

10)、启动服务并尝试登陆

6

7

8

9

10

11

12

<code>[root@localhost mysql]# service mysqld start</code>

<code>Starting MySQL....                                         [  OK  ]</code>

<code>[root@localhost mysql]# mysql</code>

<code>Welcome to the MySQL monitor.  Commands end </code><code>with</code> <code>; or \g.</code>

<code>Your MySQL connection id </code><code>is</code> <code>1</code>

<code>Server version: </code><code>5.5</code><code>.</code><code>36</code><code>-log MySQL Community Server (GPL)</code>

<code>Copyright (c) </code><code>2000</code><code>, </code><code>2014</code><code>, Oracle and/or its affiliates. All rights reserved.</code>

<code>Oracle </code><code>is</code> <code>a registered trademark of Oracle Corporation and/or its</code>

<code>affiliates. Other names may be trademarks of their respective</code>

<code>owners.</code>

<code>Type </code><code>'help;'</code> <code>or </code><code>'\h'</code> <code>for</code> <code>help. Type </code><code>'\c'</code> <code>to clear the current input statement.</code>

<code>mysql&gt;</code>

11)、删除不必要的用户,并为登陆用户设置密码

<code>mysql&gt; drop user </code><code>''</code><code>@</code><code>'localhost'</code><code>;</code>

<code>Query OK, </code><code>0</code> <code>rows affected (</code><code>0.00</code> <code>sec)</code>

<code>mysql&gt; drop user </code><code>''</code><code>@</code><code>'localhost.localdomain'</code><code>;</code>

<code>mysql&gt; drop user </code><code>'root'</code><code>@</code><code>'localhost.localdomain'</code><code>;</code>

<code>mysql&gt; drop user </code><code>'root'</code><code>@</code><code>'::1'</code><code>;</code>

<code>mysql&gt; </code><code>set</code> <code>password </code><code>for</code> <code>'root'</code><code>@</code><code>'localhost'</code><code>=password(</code><code>'wodehao123'</code><code>);</code>

<code>mysql&gt; </code><code>set</code> <code>password </code><code>for</code> <code>'root'</code><code>@</code><code>'127.0.0.1'</code><code>=password(</code><code>'wodehao123'</code><code>);</code>

四、编译php

1)、编译php

  1、为了解决php的依赖关系,建议安装“Desktop Platform Development”,bzip2-devel  

<code>[root@localhost ~]# yum groupinstall </code><code>"Desktop Platform Development"</code> <code>-y</code>

<code>[root@localhost ~]# yum install -y bzip2-devel</code>

  2.解压编译

<code>[root@localhost ~]# tar -xf php-</code><code>5.4</code><code>.</code><code>26</code><code>.tar.bz2</code>

<code>[root@localhost ~]# cd php-</code><code>5.4</code><code>.</code><code>26</code>

<code>[root@localhost php-</code><code>5.4</code><code>.</code><code>26</code><code>]# ./configure --prefix=/usr/local/php --</code><code>with</code><code>-mysql=mysqlnd --</code><code>with</code><code>-pdo-mysql=mysqlnd --</code><code>with</code><code>-mysqli=mysqlnd --</code><code>with</code><code>-openssl --enable-mbstring --</code><code>with</code><code>-freetype-dir --</code><code>with</code><code>-jpeg-dir --</code><code>with</code><code>-png-dir  --</code><code>with</code><code>-libxml-dir=/usr/ --enable-xml --enable-sockets --enable-fpm --</code><code>with</code><code>-config-file-path=/etc/ --</code><code>with</code><code>-config-file-scan-dir=/etc/php.d --</code><code>with</code><code>-bz2</code>

如果使用PHP5.3以上版本,为了链接MySQL数据库,可以指定mysqlnd,这样在本机就不需要先安装MySQL或MySQL开发包了。mysqlnd从php 5.3开始可用,可以编译时绑定到它(而不用和具体的MySQL客户端库绑定形成依赖),但从PHP 5.4开始它就是默认设置了。

--with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd

--with-openssl --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-bz2 加载openssl mbstring zlib bz2模块,创建freetype jpeg png存放目录

--with-libxml-dir=/usr/ libxml的查找目录

-enable-xml --enable-sockets --enable-fpm 默认开启xml sockets fpm模块

--with-config-file-path=/etc/ --with-config-file-scan-dir=/etc/php.d 生成的配置文件路径以及辅助文件的路径

<code>[root@localhost php-</code><code>5.4</code><code>.</code><code>26</code><code>]# make &amp;&amp; make install</code>

  3、为php提供配置文件

<code>[root@localhost php-</code><code>5.4</code><code>.</code><code>26</code><code>]# cp php.ini-production /etc/php.ini</code>

  4、为php-fpm提供服务启动脚本,并将其添加至服务列表中,并实现开机自启动,php通过php-gpm来管理

<code>[root@localhost php-</code><code>5.4</code><code>.</code><code>26</code><code>]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm</code>

<code>[root@localhost php-</code><code>5.4</code><code>.</code><code>26</code><code>]# chmod +x /etc/init.d/php-fpm</code>

<code>[root@localhost php-</code><code>5.4</code><code>.</code><code>26</code><code>]# chkconfig --add php-fpm</code>

<code>[root@localhost php-</code><code>5.4</code><code>.</code><code>26</code><code>]# chkconfig php-fpm on</code>

  5.为php-fpm提供配置文件

<code>[root@localhost php-</code><code>5.4</code><code>.</code><code>26</code><code>]# cp /usr/local/php/etc/php-fpm.conf.</code><code>default</code> <code>/usr/local/php/etc/php-fpm.conf</code>

  6.编辑php-fpm的配置文件

<code>listen = </code><code>192.168</code><code>.</code><code>1.201</code><code>:</code><code>9000</code>   <code>//监听地址</code>

<code>pm.max_children = </code><code>50</code>          <code>//允许启动的最大进程数</code>

<code>pm.start_servers = </code><code>5</code>          <code>//默认启动的进程数</code>

<code>pm.min_spare_servers = </code><code>2</code>      <code>//最小的空闲进程数</code>

<code>pm.max_spare_servers = </code><code>8</code>      <code>//最大的空闲进程数</code>

   7.启动服务,并查看端口是否处于监听状态

<code>[root@localhost php-</code><code>5.4</code><code>.</code><code>26</code><code>]# service php-fpm start</code>

<code>Starting php-fpm  done</code>

<code>[root@localhost php-</code><code>5.4</code><code>.</code><code>26</code><code>]# ss -tnlp |grep php</code>

<code>LISTEN     </code><code>0</code>      <code>128</code>           <code>192.168</code><code>.</code><code>1.201</code><code>:</code><code>9000</code>                     <code>*:*      users:((</code><code>"php-fpm"</code><code>,</code><code>47352</code><code>,</code><code>7</code><code>),(</code><code>"php-fpm"</code><code>,</code><code>47353</code><code>,</code><code>0</code><code>),(</code><code>"php-fpm"</code><code>,</code><code>47354</code><code>,</code><code>0</code><code>),(</code><code>"php-fpm"</code><code>,</code><code>47355</code><code>,</code><code>0</code><code>),(</code><code>"php-fpm"</code><code>,</code><code>47356</code><code>,</code><code>0</code><code>),(</code><code>"php-fpm"</code><code>,</code><code>47357</code><code>,</code><code>0</code><code>))</code>

2)、配置在虚拟主机中指出fcgi

  1.修改主配置文件使其能够支持php

<code>启动fcgi模块</code>

<code>LoadModule proxy_module modules/mod_proxy.so</code>

<code>LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so</code>

<code>添加支持php的功能</code>

<code>AddType application/x-httpd-php .php</code>

<code>AddType application/x-httpd-php-source .phps</code>

<code>添加主页</code>

<code>DirectoryIndex index.php index.html</code>

   2.在虚拟主机中使其能够将客户端发来的php请求发送到192.168.1.201上

<code>&lt;VirtualHost *:80&gt;</code>

<code>    </code><code>DocumentRoot </code><code>"/www/html/wangfeng7399"</code>

<code>    </code><code>ServerName www.wangfeng7399.com</code>

<code>    </code><code>ServerAlias wangfeng7399.com</code>

<code>    </code><code>ErrorLog </code><code>"/var/logs/wangfeng7399_error_log"</code>

<code>    </code><code>CustomLog </code><code>"/var/logs/wangfeng7399_access_log"</code> <code>combined</code>

<code>    </code><code>ProxyRequests Off</code>

<code>    </code><code>ProxyPassMatch ^/(.*\.php)$ fcgi:</code><code>//192</code><code>.168.1.201:9000</code><code>/www/html/wangfeng7399/</code><code>$1</code>

<code>&lt;</code><code>/VirtualHost</code><code>&gt;</code>

   说明:

   ProxyRequests Off:关闭正向代理

   ProxyPassMatch:把以.php结尾的文件请求发送到php-fpm进程,php-fpm至少需要知道运行的目录和URI,所以这里直接在fcgi://192.168.1.201:9000后指明了这两个参数,其它的参数的传递已经被mod_proxy_fcgi.so进行了封装,不需要手动指定。

   3.测试

     在1.201的/www/html/wangfeng7399的目录中创建index.php

<code>&lt;?php</code>

<code>   </code><code>phpinfo();</code>

<code>?&gt;</code>

<code>~                                                                                    </code>

<code>~</code>

<a href="http://s3.51cto.com/wyfs02/M01/23/00/wKiom1MtvO6DImtzAAKBsU1fsjI184.jpg" target="_blank"></a>

测试成功

 4.为php登陆远端数据的权限,需要在192.168.1.202上为其授权

<code>mysql&gt; grant all privileges on *.* to </code><code>'root'</code><code>@</code><code>'192.168.1.201'</code> <code>identified by </code><code>'wodehao123'</code><code>;</code>

<code>mysql&gt; flush privileges;</code>

 5.修改inde.php查看数据库是否能够连接正常

<code>  </code><code>$link = mysql_connect(</code><code>'192.168.1.202'</code><code>,</code><code>'root'</code><code>,</code><code>'wodehao123'</code><code>);</code>

<code>  </code><code>if</code><code>($link)</code>

<code>   </code><code>echo </code><code>"very good!!!!"</code><code>;</code>

<code>  </code><code>else</code>

<code>    </code><code>echo </code><code>"FUCK!!!!"</code><code>;</code>

<code> </code><code>mysql_colse();</code>

<a href="http://s3.51cto.com/wyfs02/M02/23/01/wKiom1Mtvr_C4_ZzAABrCoPZ5aM041.jpg" target="_blank"></a>

 6.安装phpMyAdmin

   详细安装步骤,请移步本人博客http://wangfeng7399.blog.51cto.com/3518031/1381359,为了确保我的图片能正常访问,建议在httpd的虚拟主机目录中也放一份

<a href="http://s3.51cto.com/wyfs02/M02/23/01/wKiom1MtzSCAXpJFAAGcwjpa2Rk198.jpg" target="_blank"></a>

 切记:如果要登陆测试的话,请修改config.ini.config文件

<code>$cfg[</code><code>'Servers'</code><code>][$i][</code><code>'host'</code><code>] = </code><code>'192.168.1.202'</code><code>;</code>

 7.做压力测试  

<code>[root@localhost wangfeng7399]# ab -c </code><code>100</code> <code>-n </code><code>1000</code> <code>www.wangfeng7399.com/phm</code>

<code>Concurrency Level:      </code><code>100</code>

<code>Time taken </code><code>for</code> <code>tests:   </code><code>4.160</code> <code>seconds</code>

<code>Complete requests:      </code><code>1000</code>

<code>Failed requests:        </code><code>0</code>

<code>Non-2xx responses:      </code><code>1000</code>

<code>Total transferred:      </code><code>469000</code> <code>bytes</code>

<code>HTML transferred:       </code><code>240000</code> <code>bytes</code>

<code>Requests per second:    </code><code>240.38</code> <code>[#/sec] (mean)</code>

<code>Time per request:       </code><code>416.012</code> <code>[ms] (mean)</code>

<code>Time per request:       </code><code>4.160</code> <code>[ms] (mean, across all concurrent requests)</code>

<code>Transfer rate:          </code><code>110.09</code> <code>[Kbytes/sec] received</code>

五、安装xcache

   1.解压、编译、安装    

<code>[root@localhost ~]# tar xf xcache-</code><code>3.1</code><code>.</code><code>0</code><code>.tar.bz2</code>

<code>[root@localhost ~]# cd xcache-</code><code>3.1</code><code>.</code><code>0</code>

<code>[root@localhost xcache-</code><code>3.1</code><code>.</code><code>0</code><code>]# /usr/local/php/bin/phpize</code>

<code>[root@localhost xcache-</code><code>3.1</code><code>.</code><code>0</code><code>]# ./configure --enable-xcache --</code><code>with</code><code>-php-config=/usr/local/php/bin/php-config</code>

<code>安装结束时,会出现类似如下行</code>

<code>Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-</code><code>20100525</code><code>/</code>

  2.编辑php.ini,整合php和xcache

<code>[root@localhost xcache-</code><code>3.1</code><code>.</code><code>0</code><code>]# mkdir /etc/php.d</code>

<code>[root@localhost xcache-</code><code>3.1</code><code>.</code><code>0</code><code>]# cp xcache.ini /etc/php.d</code>

  3.编辑xcache的配置文件    

<code>extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-</code><code>20100525</code><code>/xcache.so</code>

注意:如果php.ini文件中有多条zend_extension指令行,要确保此新增的行排在第一位。

  4.压力测试  

<code>Time taken </code><code>for</code> <code>tests:   </code><code>1.059</code> <code>seconds</code>

<code>Requests per second:    </code><code>944.57</code> <code>[#/sec] (mean)</code>

<code>Time per request:       </code><code>105.868</code> <code>[ms] (mean)</code>

<code>Time per request:       </code><code>1.059</code> <code>[ms] (mean, across all concurrent requests)</code>

<code>Transfer rate:          </code><code>432.62</code> <code>[Kbytes/sec] received</code>

  可以看到比上次有明显提升

 由于本人水平有限,请各位大牛多多指正!谢谢

本文转自wangfeng7399 51CTO博客,原文链接:http://blog.51cto.com/wangfeng7399/1381688,如需转载请自行联系原作者