LAMP平臺的的構建、加速及壓力測試

準備工作

環境:

  • RedHat 5.8 (我這裏用的是redhat5.8的版本,可以根據自己的情況靈活進行選擇其它的發行版,但是編譯環境和依賴的庫一定要具備)

軟件:

  • apr-1.4.6.

  • apr-util-1.5.4

  • httpd-2.4.12

  • libmcrypt-2.5.8

  • mcrypt-2.6.8

  • mhash-0.9.9.9

  • pcre-8.33

  • php-5.4.38

  • xcache-3.0.1

  • mysql-5.5.41-linux2.6


一、編譯安裝apache

1、解決依賴關係

(1) 編譯安裝apr

# tar xf apr-1.4.6.tar.bz2
# cd apr-1.4.6
# ./configure --prefix=/usr/local/apr
# make && make install

(2) 編譯安裝apr-util

# tar xf apr-util-1.5.4.tar.bz2
# cd apr-util-1.5.4
# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
# make && make install

(3) 編譯安裝pcre

# tar xf pcre-8.33.tar.gz
# cd pcre-8.33
# ./configure --prefix=/usr/local/pcre
# make && make install


2、編譯安裝httpd-2.4.12

(1) 首先下載httpd-2.4.12到本地,然後進行如下操作

# tar xf httpd-2.4.12tar.bz2
# cd httpd-2.4.12
#./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd --enable-so--enable-ssl --enable-cgi --enable-rewrite --with-zlib--with-pcre=/usr/local/pcre --with-apr=/usr/local/apr--with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpms-shared=most--with-mpm=event
#make && make install

(2)修改httpd的主配置文件,設置其Pid文件的路徑

編輯/etc/httpd/httpd.conf,添加如下行即可:

PidFile  "/var/run/httpd.pid"

注意:如果缺少這一步,則後面的SysV風格的腳本無法正常工作!

(3) 提供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


 這個腳本的主要功能就是可以實現像

#service httpd {start|restart|stop|status|reload}

之類的功能

 

而後爲此腳本賦予執行權限:

# chmod+x /etc/rc.d/init.d/httpd

注意:必須給這個腳本執行權限,否則服務無法正常執行!

加入服務列表:

#chkconfig --add httpd
#chkconfig --level 35 httpd on

接下來就可以啓動服務進行測試了。


二、安裝mysql-5.5.41

這裏我們使用通用二進制的格式來安裝,具體的安裝方法與編譯安裝差不多,只是省去了編譯的環節。

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、安裝並初始化mysql-5.5.41

下載對應平臺的MySQL到本地,我這裏下載的是32位平臺的,可以到官網下載所需要的版本。

# tar xf mysql-5.5.41-linux2.6-i686.tar.gz -C /usr/local
# cd /usr/local/
# ln -sv mysql-5.5.41-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

另外還需要添加如下行指定mysql數據文件的存放位置:

datadir = /mydata/data

5、mysql提供sysv服務腳本

添加至服務列表:

# chkconfig --add mysqld
# chkconfig mysqld on

而後就可以啓動服務測試使用了。

 

爲了使用mysql的安裝符合系統使用規範,並將其開發組件導出給系統使用,這裏還需要進行如下步驟:(以下步驟是可省的,如果爲了節省時間,可以不裝)

6、輸出mysqlman手冊至man命令的查找路徑:

編輯/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 -v

9、修改PATH環境變量,讓系統可以直接使用mysql的相關命令。

/etc/profile.d/新建一個文件,可以任意命名,我這裏就叫mysql,在該文件中添加:

export PATH=$PATH:/usr/local/mysql/bin

重新登錄系統即可生效。


三、  編譯安裝php-5.4.38

1、解決依賴關係

請配置好yum源(可以是本地系統光盤)後執行如下命令:

   # yum -y groupinstall "XSoftware Development"

編譯安裝libcryptmhash,兩個軟件編譯安裝方法沒有特別要注意的地方,正常編譯安裝就可以了。

2、編譯安裝php-5.4.38

# tar xf php-5.4.38.tar.bz2
# cd php-5.4.38
# ./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
# make
# make test
# make intall

    說明:

    1、這裏爲了支持apacheworkerevent這兩個MPM,編譯時使用了--enable-maintainer-zts選項。

    2、如果使用PHP5.3以上版本,爲了鏈接MySQL數據庫,可以指定mysqlnd,這樣在本機就不需要先安裝MySQLMySQL開發包了。mysqlndphp 5.3開始可用,可以編譯時綁定到它(而不用和具體的MySQL客戶端庫綁定形成依賴),但從PHP5.4開始它就是默認設置了。

 # ./configure --with-mysql=mysqlnd--with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd

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是否已經可以正常使用。

 

四、  安裝xcache,爲php加速

1、安裝

# tar xf xcache-3.0.1.tar.gz
# 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

安裝結束時,會出現類似如下行:

Installing shared extensions:    /usr/local/php/lib/php/extensions/no-debug-zts-20100525/

2、編輯php.ini,整合phpxcache

首先將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的使用。


五、  對網站進行壓力測試

1、ab使用方法

 

使用ab命令測試apache服務器性能:

 

-c concurrency:一次性發起的請求個數,默認爲1

-i:測試時使用HEAD方法,默認爲GET

-k:啓用HTTP長連接請求方式;

-n requests:發起的模擬請求個數;默認爲1個;請求數要大於等於併發連接數;

-q:靜默模式,在請求數大於150個時不輸出請求完成百分比;

 

輸出結果:

Time taken for tests:從第一個請求連接建立到收到最後一個請求的響應報文結束所經歷的時長;

Complete requests:成功的請求數;

簡單測試:

ab -c 100 -n 1000 http://www.a.com/index.php

通過這種方法可以對web服務器性能做出簡單的評估。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章