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、输出mysql的man手册至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"

编译安装libcrypt和mhash,两个软件编译安装方法没有特别要注意的地方,正常编译安装就可以了。
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、这里为了支持apache的worker或event这两个MPM,编译时使用了--enable-maintainer-zts选项。
    2、如果使用PHP5.3以上版本,为了链接MySQL数据库,可以指定mysqlnd,这样在本机就不需要先安装MySQL或MySQL开发包了。mysqlnd从php 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、安装
12345 # 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

安装结束时,会出现类似如下行:
1 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的使用。


五、  对网站进行压力测试
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服务器性能做出简单的评估。

------------------------------------分割线------------------------------------

CentOS 6.5系统安装配置LAMP(Apache+PHP5+MySQL)服务器环境

Ubuntu 14.04 配置 LAMP+phpMyAdmin PHP(5.5.9)开发环境 

Windows 7下硬盘安装Ubuntu 14.10图文教程

U盘安装Ubuntu 14.10

Ubuntu 14.10 正式发布下载

Ubuntu 14.04 LTS 如何升级到 Ubuntu 14.10 

Ubuntu 14.10 下安装 LAMP 服务图文详解 

------------------------------------分割线------------------------------------

本文永久更新链接地址

相关内容