基于http的php模塊模式
一、需要準備的軟件:
apr-1.5.0.tar apr-util-1.5.3.tar httpd-2.4.9.tar mysql-5.5.33-linux2.6-x86_64.tar php-5.4.26.tar phpMyAdmin-3.2.5-all-languages.tar xcache-3.0.3.tar
1.關閉防火墻
~]# service iptables stop
~]# setenforce 0
2.配置yum源
~]# cd /etc/yum.repos.d/
]# wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
]# sed -i 's/$releasever/6/g' CentOS6-Base-163.repo
]# yum clean all
]# yum makecache
]# yum -y install epel-release.noarch
3.將準備的軟件放到/usr/local/src/下面
]# ll
total 204568
-rw-r–r–. 1 root root 813976 Jun 16 19:20 apr-1.5.0.tar.bz2
-rw-r–r–. 1 root root 695303 Jun 16 19:20 apr-util-1.5.3.tar.bz2
-rw-r–r–. 1 root root 4994460 Jun 16 19:20 httpd-2.4.9.tar.bz2
-rw-r–r–. 1 root root 186839926 Jun 16 19:20 mysql-5.5.33-linux2.6-x86_64.tar.gz
-rw-r–r–. 1 root root 12270535 Jun 16 19:20 php-5.4.26.tar.bz2
-rw-r–r–. 1 root root 3709673 Jun 16 19:20 phpMyAdmin-3.2.5-all-languages.tar.gz
-rw-r–r–. 1 root root 142503 Jun 16 19:20 xcache-3.0.3.tar.bz2
二、編譯安裝apache
1、解決依賴關系
httpd-2.4.9需要較新版本的apr和apr-util,因此需要事先對其進行升級。升級方式有兩種,一種是通過源代碼編譯安裝,一種是直接升級rpm包。這里選擇使用編譯源代碼的方式進行
安裝gcc庫:]# yum -y install gcc
(1) 編譯安裝apr
]# tar xf apr-1.5.0.tar.bz2
]# cd apr-1.5.0
]# ./configure –prefix=/usr/local/apr
]# echo $? #:看狀態返回值是否為0
]# make && make install
(2) 編譯安裝apr-util
]# tar xf apr-util-1.5.3.tar.bz2
]# cd apr-util-1.5.3
]# ./configure –prefix=/usr/local/apr-util –with-apr=/usr/local/apr/
]# echo $? #:看狀態返回值是否為0
]# make && make install
(3) httpd-2.4.9編譯過程也要依賴于pcre-devel軟件包,需要事先安裝。此軟件包系統光盤自帶,因此,找到并安裝即可。已配置yum源也可yum安裝。
]# yum -y install pcre-devel
2、編譯安裝httpd-2.4.9
]# tar xf httpd-2.4.9.tar.bz2
]# cd httpd-2.4.9
]# ./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-modules=most –enable-mpms-shared=all –with-mpm=event
配置項的含義:安裝位置 配置文件目錄 允許dso機制支持模塊化 啟用ssl功能 啟用cgi支持 支持url重寫 支持傳輸壓縮 支持pcre的正則表達式 指明apr位置 指明apr-util位置 指明啟用大多數模塊 編譯所有支持的mpm 默認使用event
安裝的時候報錯 checking whether to enable mod_ssl… configure: error: mod_ssl has been requested but can not be built due to prerequisite failures
]# yum install openssl-devel
重新configure
]# echo $? #:看狀態返回值是否為0
]# make && make install
3、修改httpd的主配置文件,設置其Pid文件的路徑
編輯/etc/httpd/httpd.conf,添加如下行即可:
PidFile "/var/run/httpd.pid
4、提供SysV服務腳本/etc/rc.d/init.d/httpd,內容如下:
#!/bin/bash
#
# httpd Startup script for the Apache HTTP Server
#
# chkconfig: – 85 15
# description: Apache is a World Wide Web server. It is used to serve \
# HTML files and CGI.
# processname: httpd
# config: /etc/httpd/conf/httpd.conf
# config: /etc/sysconfig/httpd
# pidfile: /var/run/httpd.pid
# Source function library.
. /etc/rc.d/init.d/functions
if [ -f /etc/sysconfig/httpd ]; then
. /etc/sysconfig/httpd
fi
# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}
# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""
# Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
# with the thread-based "worker" MPM; BE WARNED that some modules may not
# work correctly with a thread-based MPM; notably PHP will refuse to start.
# Path to the apachectl script, server binary, and short-form for messages.
apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
prog=httpd
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
start() {
echo -n $"Starting $prog: "
LANG=$HTTPD_LANG daemon –pidfile=${pidfile} $httpd $OPTIONS
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}
stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} -d 10 $httpd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}
reload() {
echo -n $"Reloading $prog: "
if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
RETVAL=$?
echo $"not reloading due to configuration syntax error"
failure $"not reloading $httpd due to configuration syntax error"
else
killproc -p ${pidfile} $httpd -HUP
RETVAL=$?
fi
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart)
if [ -f ${pidfile} ] ; then
stop
start
fi
;;
reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl $@
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
exit 1
esac
exit $RETVAL
而后為此腳本賦予執行權限:
# chmod +x /etc/rc.d/init.d/httpd
加入服務列表:
# chkconfig –add httpd
5.講/usr/local/apache/bin/加入環境變量
vim /etc/profile.d/httpd.sh
export PATH=/usr/local/apache/bin:$PATH
source /etc/profile.d/httpd.sh
6.測試httpd
]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: AH00557: httpd: apr_sockaddr_info_get() failed for lamp
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
[ OK ]
第一個錯誤由于域名解析,本機主機名是lamp
vim /etc/hosts
添加127.0.0.1 lamp
第二個錯誤編輯
vim httpd.conf
添加ServerName localhost:80
重啟服務
]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
三、安裝mysql-5.5.33
備注:可以創建一個文件/mydata/data用來存放mysql的數據,創建一個lvm掛載直此目錄,數據過多時再拉伸此邏輯卷
1.創建目錄以及用戶
]# mkdir -pv /mydata/data
]# groupadd mysql
]# useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql
-r: 創建系統用戶 -s SHELL: 指明用戶的默認shell程序,可用列表在/etc/shells文件中(在此不允許其登陸系統)
-M:不要自動建立用戶的登入目錄 -d 以指定的路徑為家目錄
]# chown -R mysql:mysql /mydata/data
2.安裝并初始化mysql-5.5.33
首先下載平臺對應的mysql版本至本地,這里是64位平臺,因此,選擇的為mysql-5.5.33-linux2.6-x86_64.tar.gz
]# tar xf mysql-5.5.33-linux2.6-x86_64.tar.gz -C /usr/local/
]# cd /usr/local/
]# ln -sv mysql-5.5.33-linux2.6-x86_64/ mysql
-s : 進行軟鏈結(symbolic link)
-v : 在連結之前顯示其檔名
]# chown -R root:mysql mysql-5.5.33-linux2.6-x86_64/
]# cd /usr/local/mysql
]# scripts/mysql_install_db –user=mysql –datadir=/mydata/data
將默認的data目錄設置為/mydata/data
3.為mysql提供主配置文件
配置文件查找次序: /etc/my.cnf –> /etc/mysql/my.cnf –> –default-extra
file=/PATH/TO/CONF_FILE –> ~/.my.cnf
]# mkdir /etc/mysql
]# cp /usr/local/mysql/support-files/my-large.cnf /etc/mysql/my.cnf
]# vim /etc/mysql/my.cnf
修改此文件中thread_concurrency的值為你的CPU個數乘以2,比如這里使用如下行:
thread_concurrency = 2
另外還需要添加如下行指定mysql數據文件的存放位置:
datadir = /mydata/data 數據存放目錄
innodb_file_per_table = on 每張表使用單個表空間文件
skip_name_resolve = on 跳過地址反解
4.為mysql提供啟動腳本
]# cd /usr/local/mysql
]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
]# chmod +x /etc/rc.d/init.d/mysqld
添加至服務列表:
]# chkconfig –add mysqld
]# chkconfig mysqld on
啟動mysql
]# service mysqld start
Starting MySQL [ OK ]
5.修改PATH環境變量,讓系統可以直接使用mysql的相關命令。
]# vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
]# source /etc/profile.d/mysql.sh
mysql客戶端登陸
]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
為了使用mysql的安裝符合系統使用規范,并將其開發組件導出給系統使用,這里還需要進行如下步驟:
6.輸出mysql的man手冊至man命令的查找路徑:
]#vim /etc/man.config,添加如下行即可:
MANPATH /usr/local/mysql/man
7.輸出mysql的頭文件至系統頭文件路徑/usr/include:
這可以通過簡單的創建鏈接實現:
# ln -sv /usr/local/mysql/include /usr/include/mysql
8.輸出mysql的庫文件給系統庫查找路徑:
# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
而后讓系統重新載入系統庫:
# ldconfig
9.安全初始化
mysql_secure_installation:修改root密碼、清楚匿名用戶、控制root遠程登陸
三、編譯安裝php-5.4.26
1、解決依賴關系:
請配置好yum源(系統安裝源及epel源)后執行如下命令:
]# yum -y groupinstall "Desktop Platform Development"
]# yum -y install bzip2-devel libmcrypt-devel libxml2-devel
2、編譯安裝php-5.4.26
]# tar xf php-5.4.26.tar.bz2
]# cd php-5.4.26
]# ./configure –prefix=/usr/local/php –with-mysql=/usr/local/mysql –with-openssl –with-mysqli=/usr/local/mysql/bin/mysql_config –enable-mbstring –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-libxml-dir=/usr –enable-xml –enable-sockets –with-apxs2=/usr/local/apache/bin/apxs –with-mcrypt –with-config-file-path=/etc –with-config-file-scan-dir=/etc/php.d –with-bz2 –enable-maintainer-zts
默認安裝路徑 mysql路徑 與mysql交互的接口 多字節字符串支持 支持freetype字體格式 把php編譯成apache的模塊
php配置文件存放路徑 用到event時需要編譯
說明:
1、這里為了支持apache的worker或event這兩個MPM,編譯時使用了–enable-maintainer-zts選項。
2、如果使用PHP5.3以上版本,為了鏈接MySQL數據庫,可以指定mysqlnd,這樣在本機就不需要先安裝MySQL或MySQL開發包了。mysqlnd從php 5.3開始可用,可以編譯時綁定到它(而不用和具體的MySQL客戶端庫綁定形成依賴),但從PHP 5.4開始它就是默認設置了。
# ./configure –with-mysql=mysqlnd –with-pdo-mysql=mysqlnd –with-mysqli=mysqlnd
]# make
]# make test
]# make intall
為php提供配置文件:
# cp php.ini-production /etc/php.ini
3、 編輯apache配置文件httpd.conf,以apache支持php
]# vim /etc/httpd/httpd.conf
添加如下二行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
定位至DirectoryIndex index.html修改為:
DirectoryIndex index.php index.html
而后重新啟動httpd,或讓其重新載入配置文件即可測試php是否已經可以正常使用。
vim /usr/local/apache/htdocs/index.php
測試頁面index.php示例如下:
<?php
$link = mysql_connect('127.0.0.1','root','123456');
if ($link)
echo "Success…";
else
echo "Failure…";
mysql_close();
phpinfo();
?>
默認編譯安裝測試頁 /usr/local/apache/htdocs/index.html
瀏覽器訪問192.168.8.93
四、安裝php-MyAdmin
]# tar xf phpMyAdmin-3.2.5-all-languages.tar.gz
]# mv phpMyAdmin-3.2.5-all-languages /usr/local/apache/htdocs/pma
]# cd /usr/local/apache/htdocs/pma
]# cp config.sample.inc.php config.inc.php
]# vim config.inc.php
在$cfg['blowfish_secret'] = '任意多個字符';
瀏覽器訪問 192.168.8.93/pma
輸入root 123456(本機設置的mysql用戶和密碼)
ab壓力測試
]$ ab -c 10 -n 100 http://192.168.8.93/pma/index.php
Total transferred: 961538 bytes
HTML transferred: 862500 bytes
Requests per second: 14.72 [#/sec] (mean)
Time per request: 679.228 [ms] (mean)
Time per request: 67.923 [ms] (mean, across all concurrent requests)
Transfer rate: 138.25 [Kbytes/sec] received
]$ ab -c 20 -n 100 http://192.168.8.93/pma/index.php
Total transferred: 961560 bytes
HTML transferred: 862500 bytes
Requests per second: 38.27 [#/sec] (mean)
Time per request: 522.639 [ms] (mean)
Time per request: 26.132 [ms] (mean, across all concurrent requests)
Transfer rate: 359.34 [Kbytes/sec] received
]$ ab -c 20 -n 1000 http://192.168.8.93/pma/index.php
Total transferred: 9615594 bytes
HTML transferred: 8625000 bytes
Requests per second: 37.36 [#/sec] (mean)
Time per request: 535.367 [ms] (mean)
Time per request: 26.768 [ms] (mean, across all concurrent requests)
Transfer rate: 350.80 [Kbytes/sec] received
五、安裝xcache,為php加速:
1、安裝
]# tar xf xcache-3.0.3.tar.gz
]# cd xcache-3.0.3
]# /usr/local/php/bin/phpize
]# ./configure –enable-xcache –with-php-config=/usr/local/php/bin/php-config
]# make && make install
安裝結束時,會出現類似如下行:
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20100525/
2、編輯php.ini,整合php和xcache:
首先將xcache提供的樣例配置導入php.ini
]# mkdir /etc/php.d
]# cp xcache.ini /etc/php.d
說明:xcache.ini文件在xcache的源碼目錄中。
接下來編輯/etc/php.d/xcache.ini,找到extension開頭的行,修改為如下行:
extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so
注意:如果php.ini文件中有多條zend_extension指令行,要確保此新增的行排在第一位。
]$ ab -c 20 -n 100 http://192.168.8.93/pma/index.php
Total transferred: 961570 bytes
HTML transferred: 862500 bytes
Requests per second: 130.63 [#/sec] (mean)
Time per request: 153.107 [ms] (mean)
Time per request: 7.655 [ms] (mean, across all concurrent requests)
Transfer rate: 1226.63 [Kbytes/sec] received
]$ ab -c 20 -n 1000 http://192.168.8.93/pma/index.php
Total transferred: 9615528 bytes
HTML transferred: 8625000 bytes
Requests per second: 107.48 [#/sec] (mean)
Time per request: 186.081 [ms] (mean)
Time per request: 9.304 [ms] (mean, across all concurrent requests)
Transfer rate: 1009.26 [Kbytes/sec] received
六、啟用服務器狀態
mod_status模塊可以讓管理員查看服務器的執行狀態,它通過一個HTML頁面展示了當前服務器的統計數據。這些數據通常包括但不限于:
(1) 處于工作狀態的worker進程數;
(2) 空閑狀態的worker進程數;
(3) 每個worker的狀態,包括此worker已經響應的請求數,及由此worker發送的內容的字節數;
(4) 當前服務器總共發送的字節數;
(5) 服務器自上次啟動或重啟以來至當前的時長;
(6) 平均每秒鐘響應的請求數、平均每秒鐘發送的字節數、平均每個請求所請求內容的字節數;
vim /etc/httpd/httpd.conf
啟用狀態頁面的方法很簡單,只需要在主配置文件中添加如下內容即可:
<Location /server-status>
SetHandler server-status
Require all granted
</Location>
需要提醒的是,這里的狀態信息不應該被所有人隨意訪問,因此,應該限制僅允許某些特定地址的客戶端查看。比如使用Require ip 172.16.0.0/16來限制僅允許指定網段的主機查看此頁面。
ab測試示例:未啟用xcache和啟用xcache后,對phpMyAdmin的主而面進行請求測試的結果如下所示:
ab -c 10 -n 100 http://172.16.100.6/pma/index.php
第二部分:配置apache-2.4.9以fpm方式的php-5.4.26(fcgi模式)
一、需要準備的軟件:將之前的解壓包刪掉,重新編譯安裝
]# cd /usr/local/src/
]# rm -rf php-5.4.26
]# rm -rf xcache-3.0.3
]# tar xf php-5.4.26.tar.bz2
]# tar xf xcache-3.0.3.tar.bz2
二、編譯安裝php-5.4.26
]# cd php-5.4.26
]# ./configure –prefix=/usr/local/php5 –with-mysql=/usr/local/mysql –with-openssl –with-mysqli=/usr/local/mysql/bin/mysql_config –enable-mbstring –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-libxml-dir=/usr –enable-xml –enable-sockets –enable-fpm –with-mcrypt –with-config-file-path=/etc/php5/ –with-config-file-scan-dir=/etc/php5.d –with-bz2
]# make -j 4 && make install
-j 4 :4線程同時編譯,加快速度
創建php配置的存放目錄,與編譯時選項保持一致,并存放配置文件
]# mkdir /etc/php5{,.d}
]# cp php.ini-production /etc/php5/php.ini
為php-fpm創建啟動腳本
]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
]# chmod +x /etc/rc.d/init.d/php-fpm
為php-fpm提供配置文件:
# cp /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf
]# vim /usr/local/php5/etc/php-fpm.conf
修改下列參數
listen = 0.0.0.0:9000
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
pid = /usr/local/php5/var/run/php-fpm.pid
]# service php-fpm start
]# ps -ef |grep fpm
root 12305 1 0 00:00 ? 00:00:00 php-fpm: master process (/usr/local/php5/etc/php-fpm.conf)
nobody 12306 12305 0 00:00 ? 00:00:00 php-fpm: pool www
nobody 12307 12305 0 00:00 ? 00:00:00 php-fpm: pool www
nobody 12308 12305 0 00:00 ? 00:00:00 php-fpm: pool www
nobody 12309 12305 0 00:00 ? 00:00:00 php-fpm: pool www
nobody 12310 12305 0 00:00 ? 00:00:00 php-fpm: pool www
root 12343 7710 0 00:01 pts/0 00:00:00 grep fpm
三、配置httpd-2.4.9
1、啟用httpd的相關模塊
]# cd /etc/httpd/
]# mv httpd.conf httpd.conf_phpmod
]# mv httpd.conf.bak httpd.conf
在Apache httpd 2.4以后已經專門有一個模塊針對FastCGI的實現,此模塊為mod_proxy_fcgi.so,它其實是作為mod_proxy.so模塊的擴充,因此,這兩個模塊都要加載
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
# vim /etc/httpd/httpd.conf
1.添加如下二行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
2.修改索引目錄
DirectoryIndex index.php index.html
2、配置虛擬主機支持使用fcgi
在相應的虛擬主機中添加類似如下兩行。
ProxyRequests Off :關閉正向代理
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/PATH/TO/DOCUMENT_ROOT/$1 對php的私有請求轉發給fpm服務器
127.0.0.1:放php服務器的地址 /usr/local/apache/htdocs/$1
http://www.magedu.com/admin/index.php
本機添加的是
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/$1
ServerName 127.0.0.1
檢查語法(由于上次實驗已經導出了環境變量)
]# httpd -t
Syntax OK
查看加載模塊(有如下兩項)
]# httpd -M
proxy_module (shared)
proxy_fcgi_module (shared)
啟動httpd-2.4.9
]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
3、安裝xcache做壓力測試
1、安裝
# cd /usr/local/src/xcache-3.0.3
# /usr/local/php5/bin/phpize
# ./configure –enable-xcache –with-php-config=/usr/local/php5/bin/php-config
# make && make install
安裝結束時,會出現類似如下行:
Installing shared extensions: /usr/local/php5/lib/php/extensions/no-debug-zts-20100525/
2、編輯php.ini,整合php和xcache:
首先將xcache提供的樣例配置導入php.ini
# cp xcache.ini /etc/php5.d/
說明:xcache.ini文件在xcache的源碼目錄中。
接下來編輯/etc/php5.d/xcache.ini,找到extension開頭的行,修改為如下行:
extension = /usr/local/php5/lib/php/extensions/no-debug-zts-20100525/xcache.so
注意:如果php.ini文件中有多條zend_extension指令行,要確保此新增的行排在第一位。
4、ab壓力測試同上
原創文章,作者:809889031@qq.com,如若轉載,請注明出處:http://www.www58058.com/18596