安装过程中所需要的软件:
apr-1.5.1.tar.gz
apr-util-1.5.4.tar.bz2
httpd-2.4.10.tar.bz2
mysql-5.5.33-linux2.6-x86_64.tar.gz
php-5.3.29.tar.bz2
一. 编译安装apache
1. 编译安装apr
# tar zxvf apr-1.5.1.tar.gz# cd apr-1.5.1# ./configure --prefix=/usr/local/apr# make && make install
2. 编译安装apr-util
# tar jxvf 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. 安装依赖性软件
# yum -y install gcc pcre-devel openssl-devel
4. 编译安装httpd
# tar jxvf httpd-2.4.10.tar.bz2# cd httpd-2.4.10# ./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# make && make install
5. 编写httpd启动脚本/etc/init.d/httpd
# chmod +x httpd # chkconfig --add httpd# chkconfig httpd on# service httpd start
# cat /etc/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/functionsif [ -f /etc/sysconfig/httpd ]; then . /etc/sysconfig/httpdfi# 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/apachectlhttpd=${HTTPD-/usr/local/apache/bin/httpd}prog=httpdpidfile=${PIDFILE-/var/run/httpd/httpd.pid}lockfile=${LOCKFILE-/var/lock/subsys/httpd}RETVAL=0start() { 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 1esacexit $RETVAL
二. 编译安装mysql
1. 新建mysql用户
# groupadd -r mysql# useradd -g mysql -r -s /sbin/nologin -M /mysqldata/data mysql# chown -R mysql:mysql /mysqldata/data
2. 安装初始化mysql
# tar zxvf mysql-5.5.33-linux2.6-x86_64.tar.gz# mv mysql-5.5.33-linux2.6-x86_64 /usr/local# ln -sv mysql-5.5.33-linux2.6-x86_64 mysql# cd mysql# scripts/mysql_install_db --user=mysql --datadir=/mysqldata/data# chown -R root .
3. 配置mysql配置文件my.cnf
# cd /usr/local/mysql# cp support-files/my-large.cnf /etc/my.cnf# vim /etc/my.cnf增加一行:datadir = /mysqldata/data
4. 配置mysqld启动脚本
# cd /usr/local/mysql# cp support-files/mysql.server /etc/init.d/mysqld# chmod +x mysqld# chkconfig --add mysqld# chkconfig mysqld on# service mysqld start
5. 配置mysql库文件查找路径
# vim /etc/ld.so.conf.d/mysql.conf增加/usr/local/mysql/lib# ldconfig
6. 配置PATH变量
# vim /etc/profile.d/mysql.sh增加PATH=/usr/local/mysql/bin:$PATH# . /etc/profile.d/mysql.sh
三. 编译安装php
1. 安装依赖软件
# yum -y groupinstall "Desktop Platform Development"# yum -y install bzip2-devel libmcrypt-devel# yum -y install libtool libtool-devel libtool-ltdl-devel
2. 编译安装php
编译安装php作为apache的module,php安装成功后会在httpd.conf中增加如下行:
LoadModule php5_module modules/libphp5.so
--with-mysql=/usr/local/mysql
--with-mysqli=/usr/local/mysql/bin/mysql_config表示使用本机Mysql作为后台数据库
# tar jxvf php-5.3.29.tar.bz2# cd php-5.3.29# ./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 install
3. 配置apache支持php
# vim httpd.conf增加 AddType application/x-httpd-php .phpAddType application/x-httpd-php-source .phps修改DirectoryIndex index.html 修改前DirectoryIndex index.php index.html 修改后
4. 编写测试文件index.php,测试Php是否能正常使用
5. 性能测试
ab -c 1000 -n 10000 http://www.tech.com/index.phpThis is ApacheBench, Version 2.3 <$Revision: 1604373 $>Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking www.tech.com (be patient)Completed 1000 requestsCompleted 2000 requestsCompleted 3000 requestsCompleted 4000 requestsCompleted 5000 requestsCompleted 6000 requestsCompleted 7000 requestsCompleted 8000 requestsCompleted 9000 requestsCompleted 10000 requestsFinished 10000 requestsServer Software: Apache/2.4.10Server Hostname: www.tech.comServer Port: 80Document Path: /index.phpDocument Length: 11 bytesConcurrency Level: 1000Time taken for tests: 13.315 secondsComplete requests: 10000Failed requests: 0Total transferred: 1980000 bytesHTML transferred: 110000 bytesRequests per second: 751.04 [#/sec] (mean)Time per request: 1331.480 [ms] (mean)Time per request: 1.331 [ms] (mean, across all concurrent requests)Transfer rate: 145.22 [Kbytes/sec] receivedConnection Times (ms) min mean[+/-sd] median maxConnect: 2 97 224.8 44 1085Processing: 11 282 995.7 55 13269Waiting: 10 273 996.8 47 13269Total: 31 379 1014.0 106 13305Percentage of the requests served within a certain time (ms) 50% 106 66% 123 75% 130 80% 139 90% 1114 95% 1619 98% 3096 99% 5748 100% 13305 (longest request)
6. 安装php加速器xcache
# tar zxvf xcache-3.0.4.tar.gz # cd xcache-3.0.4# /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-20090626/# mkdir /etc/php.d# cp xcache.ini /etc/php.d# vim /etc/php.d/xcache.ini修改extension为extension = /usr/local/php/lib/php/extensions/no-debug-zts-20090626/xcache.so
7. 安装xcache之后的性能测试:
ab -c 1000 -n 10000 http://www.tech.com/index.phpThis is ApacheBench, Version 2.3 <$Revision: 1604373 $>Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking www.tech.com (be patient)Completed 1000 requestsCompleted 2000 requestsCompleted 3000 requestsCompleted 4000 requestsCompleted 5000 requestsCompleted 6000 requestsCompleted 7000 requestsCompleted 8000 requestsCompleted 9000 requestsCompleted 10000 requestsFinished 10000 requestsServer Software: Apache/2.4.10Server Hostname: www.tech.comServer Port: 80Document Path: /index.phpDocument Length: 11 bytesConcurrency Level: 1000Time taken for tests: 6.630 secondsComplete requests: 10000Failed requests: 0Total transferred: 1980000 bytesHTML transferred: 110000 bytesRequests per second: 1508.24 [#/sec] (mean)Time per request: 663.026 [ms] (mean)Time per request: 0.663 [ms] (mean, across all concurrent requests)Transfer rate: 291.63 [Kbytes/sec] receivedConnection Times (ms) min mean[+/-sd] median maxConnect: 0 122 283.1 35 1068Processing: 5 188 544.2 50 5652Waiting: 4 181 544.7 44 5652Total: 17 310 660.4 89 5657Percentage of the requests served within a certain time (ms) 50% 89 66% 115 75% 125 80% 153 90% 1093 95% 1337 98% 2374 99% 3587 100% 5657 (longest request)
二. 编译安装php为独立的服务进程php-fpm
1. 安装依赖软件
# yum -y install bzip2-devel libmcrypt-devel libxml2-config-devel# yum -y install libtool libtool-devel libtool-ltdl-devel
2. 编译安装php
# tar jxvf php-5.3.29.tar.bz2 # cd php-5.3.29# ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --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 install//--with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd用于配置php通过socket连接非本机mysql数据库
3. 配置php
# cd php-5.3.29# cp php.ini-production /etc/php.ini # cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm# chmod +x /etc/init.d/php-fpm# chkconfig --add php-fpm# chkconfig php-fpm on# cd /usr/local/php/etc# cp php-fpm.conf.default php-fpm.conf# vim php-fpm.conf修改pm.max_children = 50 //设置php-fpm进程最大数为50pm.start_servers = 5 //设置php-fpm初始进程数为5pm.min_spare_servers = 2 //设置最小空闲进程数为2pm.max_spare_servers = 8 //设置最大空闲进程数为8pid = /usr/local/php/var/run/php-fpm.pid
4. 启动php-fpm进程
# service php-fpm start# ss -tuln | grep 9000 //验证php-fpmtcp LISTEN 0 128 127.0.0.1:9000 *:*
5. 启动httpd的相关模块
//在httpd.conf中配置对于fastcgi的支持LoadModule proxy_module modules/mod_proxy.soLoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
6. 配置虚拟主机支持fcgi
ServerAdmin root@localhost DocumentRoot "/usr/local/apache/htdocs/www.dev.com" ServerName www.dev.com ServerAlias dev.com ProxyRequests off //关闭正向代理 ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/ //把以.php结尾的文件请求发送到php-fpm进程 Options none AllowOverride none Require all granted ErrorLog "logs/www.dev.com_error" CustomLog "logs/www.dev.com_access" combined
7. 配置httpd.conf,让apache能识别.php文件
# vim httpd.conf增加两行AddType application/x-httpd-php .phpAddType application/x-httpd-php-source .phps修改DirectoryIndex index.php index.html