lamp整合三連發(3)
LAMP整合方式2
要求:(1) 三者分離於兩臺或三臺主機;
(2) 一個虛擬主機用於提供phpMyAdmin;另一個虛擬主機用於提供wordpress;
(3) xcache
(4) 嘗試mpm為非prefork機制;
現有伺服器:172.18.29.141CentOS 6.6為httpd(包含php模組)伺服器
172.18.29.142CentOS 6.6作為mysql伺服器
一、編譯安裝httpd
一、編譯安裝apache
1、解決依賴關係
httpd-2.4.9需要較新版本的apr和apr-util,因此需要事先對其進行升級。升級方式有兩種,一種是通過原始碼編譯安裝,一種是直接升級rpm包。這裡選擇使用編譯原始碼的方式進行。
(1) 編譯安裝apr
# tar xf apr-1.5.0.tar.bz2
# cd apr-1.5.0
# ./configure --prefix=/usr/local/apr
# 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
# make && make install
(3) httpd-2.4.9編譯過程也要依賴於pcre-devel軟體包,需要事先安裝。
2.編譯安裝httpd2.4,下載httpd2.4原始碼安裝包
# tar xf httpd-2.4.9.tar.bz2
# cd httpd-2.4.9
# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --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
# make && make install
3、修改httpd的主配置檔案,設定其Pid檔案的路徑。
4、提供SysV服務指令碼/etc/rc.d/init.d/httpd,內容如下:
注意:修改其中的httpd 變數,為適合自己的變數內容
apachectl=/usr/local/apache/bin/apachectl
httpd=${HTTPD-/usr/local/apache/bin/httpd}
pidfile=${PIDFILE-/var/run/httpd.pid}
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
#!/bin/bash # #httpdStartupscriptfortheApacheHTTPServer # #chkconfig:-8515 #description:ApacheisaWorldWideWebserver.Itisusedtoserve\ #HTMLfilesandCGI. #processname:httpd #config:/etc/httpd/conf/httpd.conf #config:/etc/sysconfig/httpd #pidfile:/var/run/httpd.pid #Sourcefunctionlibrary. ./etc/rc.d/init.d/functions if[-f/etc/sysconfig/httpd];then ./etc/sysconfig/httpd fi #StarthttpdintheClocalebydefault. HTTPD_LANG=${HTTPD_LANG-"C"} #Thiswillpreventinitlogfromswallowingupapass-phrasepromptif #mod_sslneedsapass-phrasefromtheuser. INITLOG_ARGS="" #SetHTTPD=/usr/sbin/httpd.workerin/etc/sysconfig/httpdtouseaserver #withthethread-based"worker"MPM;BEWARNEDthatsomemodulesmaynot #workcorrectlywithathread-basedMPM;notablyPHPwillrefusetostart. #Pathtotheapachectlscript,serverbinary,andshort-formformessages. 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_LANGdaemon--pidfile=${pidfile}$httpd$OPTIONS RETVAL=$? echo [$RETVAL=0]&&touch${lockfile} return$RETVAL } stop(){ echo-n$"Stopping$prog:" killproc-p${pidfile}-d10$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$"notreloadingduetoconfigurationsyntaxerror" failure$"notreloading$httpdduetoconfigurationsyntaxerror" else killproc-p${pidfile}$httpd-HUP RETVAL=$? fi echo } #Seehowwewerecalled. 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[email protected] RETVAL=$? ;; *) echo$"Usage:$prog{start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}" exit1 esac exit$RETVAL
而後為此指令碼賦予執行許可權:
# chmod +x /etc/rc.d/init.d/httpd
加入服務列表:
# chkconfig --add httpd
接下來就可以啟動服務進行測試了。
二、安裝mariadb
1、準備資料存放的檔案系統
新建一個邏輯卷,並將其掛載至特定目錄即可。這裡不再給出過程。
這裡假設其邏輯卷的掛載目錄為/mydata,而後需要建立/mydata/data目錄做為mysql資料的存放目錄。
2、新建使用者以安全方式執行程序:
# groupadd -r mysql
# useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql
# chown -R mysql:mysql /mydata/data
3、安裝並初始化mariadb
首先下載mariadb二進位制安裝包至本地。
# tar xf mysql-5.5.33-linux2.6-i686.tar.gz -C /usr/local
# cd /usr/local/
# ln -sv mysql-5.5.33-linux2.6-i686 mysql
# cd mysql
# chown -R mysql:mysql .
# scripts/mysql_install_db --user=mysql --datadir=/mydata/data
# chown -R root .
4、為mysql提供主配置檔案:
# cd /usr/local/mysql
# cp support-files/my-large.cnf /etc/my.cnf
並修改此檔案中thread_concurrency的值為你的CPU個數乘以2,比如這裡使用如下行:
thread_concurrency = 2
另外還需要新增如下行指定mysql資料檔案的存放位置:
datadir = /mydata/data
5、為mysql提供sysv服務指令碼:
# cd /usr/local/mysql
# cp support-files/mysql.server /etc/rc.d/init.d/mariadbd
# chmod +x /etc/rc.d/init.d/mariadbd
新增至服務列表:
# chkconfig --add mariadbd
# chkconfig mariadbd on
而後就可以啟動服務測試使用了。
為了使用mysql的安裝符合系統使用規範,並將其開發元件匯出給系統使用,這裡還需要進行如下步驟:
6、輸出mysql的man手冊至man命令的查詢路徑:
編輯/etc/man.config,新增如下行即可:
MANPATH /usr/local/mysql/man
7、輸出mysql的標頭檔案至系統標頭檔案路徑/usr/include:
這可以通過簡單的建立連結實現:
# ln -sv /usr/local/mysql/include /usr/include/mariadb
8、輸出mysql的庫檔案給系統庫查詢路徑:
# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mariadb.conf
而後讓系統重新載入系統庫:
# ldconfig
9、修改PATH環境變數,讓系統可以直接使用mysql的相關命令。具體實現過程這裡不再給出。
# PATH=/usr/local/mysql/bin:$PATH
三、編譯安裝php5
1、解決依賴關係:
請配置好yum源(系統安裝源及epel源)後執行如下命令:
# yum -y groupinstall "Desktop Platform Development"
# yum -y install bzip2-devel libmcrypt-devel libxml2-devel
2、編譯安裝php-5.4.26
首先下載原始碼包至本地目錄,下載位置ftp://172.16.0.1/pub/Sources/new_lamp。
# 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
說明:
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
這裡用:
# ./configure --prefix=/usr/local/php --enable-maintainer-zts --with-mysql=mysqlnd --with-openssl --with-mysqli=mysqlnd --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/apache24/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2
# make
# make test
# make intall
為php提供配置檔案:
# cp php.ini-production /etc/php.ini
3、 編輯apache配置檔案httpd.conf,以apache支援php
# vim /etc/httpd/httpd.conf
1、新增如下二行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
2、定位至DirectoryIndex index.html
修改為:
DirectoryIndex index.php index.html
而後重新啟動httpd,或讓其重新載入配置檔案即可測試php是否已經可以正常使用。
測試頁面index.php示例如下:
<?php
$link = mysql_connect('127.0.0.1','root','mageedu');
if ($link)
echo "Success...";
else
echo "Failure...";
mysql_close();
?>
四、安裝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-20131226/
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,找到zend_extension開頭的行,修改為如下行:
zend_extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so
注意:如果php.ini檔案中有多條zend_extension指令行,要確保此新增的行排在第一位。
五、啟用伺服器狀態
mod_status模組可以讓管理員檢視伺服器的執行狀態,它通過一個HTML頁面展示了當前伺服器的統計資料。這些資料通常包括但不限於:
(1) 處於工作狀態的worker程序數;
(2) 空閒狀態的worker程序數;
(3) 每個worker的狀態,包括此worker已經響應的請求數,及由此worker傳送的內容的位元組數;
(4) 當前伺服器總共傳送的位元組數;
(5) 伺服器自上次啟動或重啟以來至當前的時長;
(6) 平均每秒鐘響應的請求數、平均每秒鐘傳送的位元組數、平均每個請求所請求內容的位元組數;
啟用狀態頁面的方法很簡單,只需要在主配置檔案中新增如下內容即可:
<Location /server-status>
SetHandler server-status
Require all granted
</Location>
需要提醒的是,這裡的狀態資訊不應該被所有人隨意訪問,因此,應該限制僅允許某些特定地址的客戶端檢視。比如使用Require ip 172.16.0.0/16來限制僅允許指定網段的主機檢視此頁面。
六、ab測試示例:未啟用xcache和啟用xcache後,對phpMyAdmin的主而面進行請求測試的結果如下所示:
#ab-n1000-c100http://172.18.29.141/wordpress/index.php ThisisApacheBench,Version2.3<$Revision:655654$> Copyright1996AdamTwiss,ZeusTechnologyLtd,http://www.zeustech.net/ LicensedtoTheApacheSoftwareFoundation,http://www.apache.org/ Benchmarking172.18.29.141(bepatient) Completed100requests Completed200requests Completed300requests Completed400requests Completed500requests Completed600requests Completed700requests Completed800requests Completed900requests Completed1000requests Finished1000requests ServerSoftware:Apache/2.4.6 ServerHostname:172.18.29.141 ServerPort:80 DocumentPath:/wordpress/index.php DocumentLength:217bytes ConcurrencyLevel:100 Timetakenfortests:0.146seconds Completerequests:1000 Failedrequests:0 Writeerrors:0 Non-2xxresponses:1003 Totaltransferred:405212bytes HTMLtransferred:217651bytes Requestspersecond:6864.97[#/sec](mean) Timeperrequest:14.567[ms](mean) Timeperrequest:0.146[ms](mean,acrossallconcurrentrequests) Transferrate:2716.57[Kbytes/sec]received ConnectionTimes(ms) minmean[+/-sd]medianmax Connect:010.713 Processing:3132.61320 Waiting:0132.61320 Total:4142.11420 Percentageoftherequestsservedwithinacertaintime(ms) 50%14 66%15 75%15 80%15 90%15 95%18 98%18 99%18 100%20(longestrequest)
#ab-n1000-c100http://172.18.29.141/wordpress/index.php ThisisApacheBench,Version2.3<$Revision:655654$> Copyright1996AdamTwiss,ZeusTechnologyLtd,http://www.zeustech.net/ LicensedtoTheApacheSoftwareFoundation,http://www.apache.org/ Benchmarking172.18.29.141(bepatient) Completed100requests Completed200requests Completed300requests Completed400requests Completed500requests Completed600requests Completed700requests Completed800requests Completed900requests Completed1000requests Finished1000requests ServerSoftware:Apache/2.4.6 ServerHostname:172.18.29.141 ServerPort:80 DocumentPath:/wordpress/index.php DocumentLength:217bytes ConcurrencyLevel:100 Timetakenfortests:0.153seconds Completerequests:1000 Failedrequests:0 Writeerrors:0 Non-2xxresponses:1002 Totaltransferred:404808bytes HTMLtransferred:217434bytes Requestspersecond:6536.33[#/sec](mean) Timeperrequest:15.299[ms](mean) Timeperrequest:0.153[ms](mean,acrossallconcurrentrequests) Transferrate:2583.94[Kbytes/sec]received ConnectionTimes(ms) minmean[+/-sd]medianmax Connect:010.814 Processing:4142.21419 Waiting:1132.21418 Total:4152.11520 Percentageoftherequestsservedwithinacertaintime(ms) 50%15 66%15 75%16 80%16 90%16 95%18 98%19 99%20 100%20(longestrequest)
發現提升並沒有很大。
轉載於:https://blog.51cto.com/kingslanding/1767994