天天看點

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,如需轉載請自行聯系原作者