LAMP平臺(tái)相關(guān)組件功能簡(jiǎn)述
編譯安裝LAMP平臺(tái)
編譯安裝LAMP之以FastCGI方式與php整合
使用ab命令進(jìn)行壓力測(cè)試
####################################### LAMP平臺(tái)相關(guān)組件功能簡(jiǎn)述
一、httpd
作為一款web服務(wù)器本身它僅能處理靜態(tài)請(qǐng)求和內(nèi)容,例如:html、css、圖片等……
二、php
PHP是腳本編程語言,php解析器,其內(nèi)部的 Zend引擎將執(zhí)行分為兩段:
第一段:詞法分析、語法分析
用戶第一次訪問PHP頁面時(shí)候動(dòng)態(tài)編譯為opcode
第二段:執(zhí)行opcode。
三、xcache
每個(gè)用戶請(qǐng)求php都會(huì)生成一個(gè)進(jìn)程用于處理和回應(yīng),由于進(jìn)程之間無法共享opcode,opcode在內(nèi)存空間中,所以當(dāng)進(jìn)程消亡時(shí)opcode會(huì)隨之丟失 ,在實(shí)際生成環(huán)境中有大量用戶請(qǐng)求目標(biāo)是完全相同的,如果每個(gè)請(qǐng)求都需要?jiǎng)討B(tài)生成opcode這不僅是一種巨大的資源浪費(fèi)也會(huì)延長(zhǎng)對(duì)用戶請(qǐng)求的響應(yīng)時(shí)間,因此 為了使進(jìn)程之間能共享opcode,引用緩存器例如:APC、eAccelerator、XCache。
四、php如何與mysql進(jìn)行交互
php解釋器(模塊)本身僅具備解釋php腳本的功能,真正跟mysql交互的是PHP程序,由此程序驅(qū)動(dòng)訪問mysql(php53-mysql)。
五、httpd與php的交互方式
CGI,這種方式把php直接編譯成httpd的模塊,每個(gè)httpd請(qǐng)求進(jìn)程對(duì)應(yīng)一個(gè)php(cgi)進(jìn)程,httpd動(dòng)態(tài)創(chuàng)建進(jìn)程需要時(shí)間,并且額外的進(jìn)程會(huì)占用資源開銷,進(jìn)程間通信的結(jié)果返回httpd進(jìn)程,httpd負(fù)責(zé)銷毀進(jìn)程。由httpd負(fù)責(zé)子進(jìn)程的創(chuàng)建和回收。
PHP_MOD,這種方式php作為http進(jìn)程內(nèi)部模塊運(yùn)行,需要時(shí)候動(dòng)態(tài)加載模塊。
fastcgi(httpd作為客戶端想php服務(wù)器發(fā)起請(qǐng)求,php服務(wù)器內(nèi)部管理和銷毀進(jìn)程)fpm,監(jiān)聽在一個(gè)套接字上,Daemon進(jìn)程。
編譯安裝LAMP平臺(tái)
一、準(zhǔn)備環(huán)境、所需軟件包版本、安裝順序。
平臺(tái)基于redhat5.8
httpd所需軟件包:
apr-1.4.6.tar.bz2
apr-util-1.4.1.tar.bz2
httpd-2.4.3.tar.bz2
mysql所需軟件包:
mysql-5.5.28-linux2.6-i686.tar.gz(通用二進(jìn)制格式)
php所需軟件包:
libmcrypt-2.5.7-5.el5.i386.rpm
libmcrypt-devel-2.5.7-5.el5.i386.rpm
mhash-0.9.2-6.el5.i386.rpm
mhash-devel-0.9.2-6.el5.i386.rpm
php-5.4.13.tar.bz2
xcache所需軟件包:
xcache-2.0.0.tar.bz2
phpMyAdmin所需軟件包:
phpMyAdmin-3.5.1-all-languages.tar.bz2
安裝順序 httpd --> mysql --> php
二、編譯安裝httpd-2.4.4
1、解決依賴關(guān)系
##########################安裝apr########################## tar xf apr-1.4.6.tar.bz2 ./configure --prefix=/usr/local/apr make && make install ##########################安裝apr-util##################### tar xf apr-util-1.4.1.tar.bz2 ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr make && make install ##########################安裝pcre-devel################### yum install pcre-devel
2、編譯安裝httpd-2.4.4
tar xf httpd-2.4.4.tar.bz2 ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so --enable-rewirte --enable-ssl --enable-cgi --enable-cgid --enable-modules=most --enable-mods-shared=most --enable-mpms-shared=all --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util make && make install
3、修改Pid文件的路徑以迎合啟動(dòng)腳本
vim /etc/httpd/httpd.conf PidFile "/var/run/httpd.pid" //添加如下行
4、提供SysV服務(wù)腳本 /etc/rc.d/init.d/httpd,內(nèi)容如下:
#!/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
5、為腳本添加執(zhí)行權(quán)限,開機(jī)自動(dòng)啟動(dòng)
chmod +x /etc/rc.d/init.d/httpd chkconfig --add httpd chkconfig --level 35 httpd on chkconfig --list httpd
6、為命令添加搜索路徑
vim /etc/profile.d/httpd.sh export PATH=$PATH:/usr/local/apache/bin //加入本行內(nèi)容
7、 修改mpm_module為prefork、檢查語法并重啟服務(wù)
vim /etc/httpd/httpd.conf #LoadModule mpm_event_module modules/mod_mpm_event.so 注釋 LoadModule mpm_prefork_module modules/mod_mpm_prefork.so 添加 httpd -t 檢查語法 service httpd restart 重啟服務(wù)
至此httpd安裝完成:
三、安裝mysql-5.5.28
1、創(chuàng)建mysql用戶和mysql組
groupadd -r -g 306 mysql useradd -g 306 -r -u 306 mysql
2、創(chuàng)建鏈接文件
tar xf mysql-5.5.28-linux2.6-i686.tar.gz -C /usr/local cd /usr/local ln -sv mysql-5.5.28-linux2.6-i686 mysql chown -R mysql.mysql /usr/local/mysql/*3
3、為數(shù)據(jù)目錄創(chuàng)建lvm
fdisk /dev/sdb 略過步驟 partprobe /dev/sdb 檢測(cè) pvcreate /dev/sdb1 創(chuàng)建物理塊 vgcreate myvg /dev/sdb1 創(chuàng)建卷組 lvcreate -n mydata -L 5G myvg 創(chuàng)建邏輯卷 lvs mke2fs -j /dev/myvg/mydata 格式化 mkdir /mydata vim /etc/fstab 添加開機(jī)自啟動(dòng) /dev/myvg/mydata /mydata ext3 defaults 0 0 mount -a 掛載 mount mkdir /mydata/data 創(chuàng)建數(shù)據(jù)目錄 chown -R mysql.mysql /mydata/data/ 修改數(shù)據(jù)目錄屬主與屬組 chmod o-rx /mydata/data/ 修改其他人對(duì)此目錄權(quán)限
4、初始化mysql
scripts/mysql_install_db --user=mysql --datadir=/mydata/data/ chown -R root /usr/local/mysql/*
5、添加啟動(dòng)腳本、主配置文件、命令路徑、庫文件、頭文件
#########################添加啟動(dòng)腳本######################## cp support-files/mysql.server /etc/init.d/mysqld chkconfig --add mysqld ########################添加主配置文件####################### cp support-files/my-large.cnf /etc/my.cnf vim /etc/my.cnf datadir = /mydata/data 定義數(shù)據(jù)目錄位置 #########################添加命令############################ vim /etc/profile.d/mysql.sh export PATH=$PATH:/usr/local/mysql/bin //添加本行內(nèi)容 ########################添加庫文件########################### echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf ########################添加頭文件########################### ln -sv /usr/local/mysql/include /usr/include/mysql
至此mysql安裝完成:
三、安裝php-5.4.13(php作為httpd模塊運(yùn)行)
1、解決依賴
rpm -ivh libmcrypt-2.5.7-5.el5.i386.rpm libmcrypt-devel-2.5.7-5.el5.i386.rpm mhash-0.9.2-6.el5.i386.rpm mhash-devel-0.9.2-6.el5.i386.rpm
2、編譯安裝php-5.4.13
tar xf php-5.4.13 ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-opensll --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 #--enable-maintainer-zts(event worker基于線程工作類型的時(shí)候需要此選項(xiàng)) make && make install cp php.ini-production /etc/php.ini 創(chuàng)建配置文件
3、結(jié)合php和httpd
vim /etc/httpd/httpd.conf DirectoryIndex index.php index.html //添加index.php AddType application/x-httpd-php .php //添加本行內(nèi)容 AddType application/x-httpd-php-source .phps //添加本行內(nèi)容
4、測(cè)試php是否正常工作
vim /usr/local/apache/htdocs/index.php <?php phpinfo(); ?>
5、測(cè)試php是否能正常訪問mysql
<?php $conn=mysql_connect('localhost','root',''); if($conn) echo "Success...."; else echo "Failure...."; ?>
四、編譯安裝xcache-2.0.0
1、編譯安裝xcache
tar xf xcache-2.0.0.bz2 cd xcacge /usr/local/php/bin/phpize 準(zhǔn)備好php擴(kuò)展編譯以備編譯,讓擴(kuò)展本身識(shí)別php ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config make && make install
2 、 整合php和xcache
mkdir /etc/php.d cp xcache.ini /etc/php.d vim /etc/php.d/xcache.ini zend_extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so 復(fù)制安裝完成的內(nèi)容后加xcache.so
重啟httpd,至此xcache安裝完成:
五、httpd2.4.4虛擬主機(jī)配置
1 、中心主機(jī)和虛擬主機(jī)不能共存
注釋中心主機(jī) #DocumentRoot "/usr/local/apache/htdocs" 開啟虛擬主機(jī)選項(xiàng) # Virtual hosts Include /etc/httpd/extra/httpd-vhosts.conf
2 、創(chuàng)建虛擬主機(jī) vim /etc/httpd/extra/httpd-vhosts.conf
<VirtualHost *:80> ServerName www.soulboy.com DocumentRoot "/www/soulboy.com" <Directory "/www/soulboy.com"> 如不聲明目錄權(quán)限則默認(rèn)拒絕 Options none AllowOverride none Require all granted </Directory> ErrorLog "/var/log/httpd/soulboy.com-error_log" CustomLog "/var/log/httpd/soulgirl.com-error_log" combined </VirtualHost> <VirtualHost *:80> ServerName www.soulgirl.com DocumentRoot "/www/soulgirl.com" <Directory "/www/soulgirl.com"> Options none AllowOverride none Require all granted </Directory> ErrorLog "/var/log/httpd/soulgirl.com-error_log" CustomLog "/var/log/httpd/soulgirl.com-error_log" common </VirtualHost>
3 、測(cè)試虛擬主機(jī)是否生效
五、部署phpMyAdmin-3.5.1
1 、加壓到虛擬主機(jī)目錄并重新修改目錄名,并創(chuàng)建和修改配置文件
tar xf phpMyAdmin-3.5.1-all-languages.tar.bz2/ -C /www/soulgirl.com cd /www/soulgirl.com mv phpMyAdmin-3.5.1-all-languages / pma 修改目錄名方便訪問 cd pma cp config.sample.inc.php config.inc.php 創(chuàng)建配置文件 penssl rand -base64 10 生成10位的隨機(jī)數(shù) vim config.inc.php 把生成的隨機(jī)數(shù)復(fù)制到配置文件中 $cfg['blowfish_secret'] = 'QkMTw3ZlpJlBKA'
2 、修改mysql用戶密碼
mysqladmin -uroot password 'redhat'
3 、測(cè)試phpMyAdmin部署是否正常
至此說明php和httpd工作正常:
至此說明mysql工作正常:
編譯安裝LAMP之以FastCGI方式與php整合
一、編譯安裝httpd-2.4.4
安裝步驟同上
二、mysql-5.6.10
安裝步驟同上
三、 編譯安裝php-5.4.13
1 、解決依賴關(guān)系
yum -y groupinstall "X Software Development" rpm -ivh libmcrypt-2.5.7-5.el5.i386.rpm libmcrypt-devel-2.5.7-5.el5.i386.rpm mhash-0.9.9-1.el5.centos.i386.rpm mhash-devel-0.9.9-1.el5.centos.i386.rpm
2 、編譯安裝php-5.4.13
tar xf php-5.4.13.tar.bz2 cd php-5.4.13 ./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 --enable-fpm --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 make make intall cp php.ini-production /etc/php.ini 為php提供配置文件:
3 、配置php-fpm
###################為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 chkconfig --add php-fpm chkconfig php-fpm on ##################為php-fpm提供配置文件#################### cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf ######################編輯php-fpm的配置文件################ # vim /usr/local/php/etc/php-fpm.conf 配置fpm的相關(guān)選項(xiàng)為你所需要的值,并啟用pid文件(如下最后一行): pm.max_children = 50 最大進(jìn)程數(shù) pm.start_servers = 5 啟動(dòng)后創(chuàng)建空閑進(jìn)程數(shù) pm.min_spare_servers = 5 最小空閑進(jìn)程數(shù) pm.max_spare_servers = 8 最大空閑進(jìn)程數(shù) pid = /usr/local/php/var/run/php-fpm.pid PidFile路徑 #################啟動(dòng)php-fpm############################## service php-fpm start ps aux | grep php-fpm netstat -tnlp | grep php-fpm 默認(rèn)情況下,fpm監(jiān)聽在127.0.0.1的9000端口,也可以使用如下命令驗(yàn)正其是否已經(jīng)監(jiān)聽在相應(yīng)的套接字。
四、 配置httpd-2.4.4
1 、配置php-fpm
LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so 在Apache httpd 2.4以后已經(jīng)專門有一個(gè)模塊針對(duì)FastCGI的實(shí)現(xiàn),此模塊為mod_proxy_fcgi.so,它其實(shí)是作為mod_proxy.so模塊的擴(kuò)充,因此,這兩個(gè)模塊都要加載2
2 、配置httpd識(shí)別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 3、注釋中心主機(jī) #DocumentRoot "/usr/local/apache/htdocs" 4、刪除注釋,開啟支持虛擬主機(jī)功能 # Virtual hosts Include /etc/httpd/extra/httpd-vhosts.conf
3 、配置虛擬主機(jī)支持使用fcgi
ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/PATH/TO/DOCUMENT_ROOT/$1
4 、配置虛擬主機(jī)支持fastcgi
<VirtualHost *:80> DocumentRoot "/www/soulboy" ServerName www.soulboy.com ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/www/soulboy/$1 <Directory "/www/soulboy"> Options none AllowOverride none Require all granted </Directory> ErrorLog "logs/dummy-host.example.com-error_log" CustomLog "logs/dummy-host.example.com-access_log" common </VirtualHost>
5 、測(cè)試以上配置是否正常
<h1>www.soulboy.com</h1> <?php $conn=mysql_connect('localhost','root','redhat'); if($conn) echo "Success...."; else echo "Failure...."; phpinfo(); ?>
至此說明一切正常:
五、編譯安裝 xcache-3.0.1
tar xf xcache-3.0.1.tar.bz2 cd xcache-3.0.1 /usr/local/php/bin/phpize ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config make && make install mkdir /etc/php.d/ cp xcache.ini /etc/php.d/ service php-fpm restart
使用ab命令進(jìn)行壓力測(cè)試
一、安裝xcache之前
ab -c 100 -n 1000 http://www.soulboy.com/pma/index.php
二、安裝xcache之后(以同樣的并發(fā)量測(cè)試)
ab -c 100 -n 1000 http://www.soulboy.com/pma/index.php