基于http的php模块模式
一、需要准备的软件:
apr-1.5.0.tar apr-util-1.5.3.tar httpd-2.4.9.tar mysql-5.5.33-linux2.6-x86_64.tar php-5.4.26.tar phpMyAdmin-3.2.5-all-languages.tar xcache-3.0.3.tar
1.关闭防火墙
~]# service iptables stop
~]# setenforce 0
2.配置yum源
~]# cd /etc/yum.repos.d/
]# wget http://mirrors.163.com/.help/CentOS6-Base-163.repo
]# sed -i 's/$releasever/6/g' CentOS6-Base-163.repo
]# yum clean all
]# yum makecache
]# yum -y install epel-release.noarch
3.将准备的软件放到/usr/local/src/下面
]# ll
total 204568
-rw-r–r–. 1 root root 813976 Jun 16 19:20 apr-1.5.0.tar.bz2
-rw-r–r–. 1 root root 695303 Jun 16 19:20 apr-util-1.5.3.tar.bz2
-rw-r–r–. 1 root root 4994460 Jun 16 19:20 httpd-2.4.9.tar.bz2
-rw-r–r–. 1 root root 186839926 Jun 16 19:20 mysql-5.5.33-linux2.6-x86_64.tar.gz
-rw-r–r–. 1 root root 12270535 Jun 16 19:20 php-5.4.26.tar.bz2
-rw-r–r–. 1 root root 3709673 Jun 16 19:20 phpMyAdmin-3.2.5-all-languages.tar.gz
-rw-r–r–. 1 root root 142503 Jun 16 19:20 xcache-3.0.3.tar.bz2
二、编译安装apache
1、解决依赖关系
httpd-2.4.9需要较新版本的apr和apr-util,因此需要事先对其进行升级。升级方式有两种,一种是通过源代码编译安装,一种是直接升级rpm包。这里选择使用编译源代码的方式进行
安装gcc库:]# yum -y install gcc
(1) 编译安装apr
]# tar xf apr-1.5.0.tar.bz2
]# cd apr-1.5.0
]# ./configure –prefix=/usr/local/apr
]# echo $? #:看状态返回值是否为0
]# 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/
]# echo $? #:看状态返回值是否为0
]# make && make install
(3) httpd-2.4.9编译过程也要依赖于pcre-devel软件包,需要事先安装。此软件包系统光盘自带,因此,找到并安装即可。已配置yum源也可yum安装。
]# yum -y install pcre-devel
2、编译安装httpd-2.4.9
]# tar xf httpd-2.4.9.tar.bz2
]# cd httpd-2.4.9
]# ./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
配置项的含义:安装位置 配置文件目录 允许dso机制支持模块化 启用ssl功能 启用cgi支持 支持url重写 支持传输压缩 支持pcre的正则表达式 指明apr位置 指明apr-util位置 指明启用大多数模块 编译所有支持的mpm 默认使用event
安装的时候报错 checking whether to enable mod_ssl… configure: error: mod_ssl has been requested but can not be built due to prerequisite failures
]# yum install openssl-devel
重新configure
]# echo $? #:看状态返回值是否为0
]# make && make install
3、修改httpd的主配置文件,设置其Pid文件的路径
编辑/etc/httpd/httpd.conf,添加如下行即可:
PidFile "/var/run/httpd.pid
4、提供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
而后为此脚本赋予执行权限:
# chmod +x /etc/rc.d/init.d/httpd
加入服务列表:
# chkconfig –add httpd
5.讲/usr/local/apache/bin/加入环境变量
vim /etc/profile.d/httpd.sh
export PATH=/usr/local/apache/bin:$PATH
source /etc/profile.d/httpd.sh
6.测试httpd
]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: AH00557: httpd: apr_sockaddr_info_get() failed for lamp
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message
[ OK ]
第一个错误由于域名解析,本机主机名是lamp
vim /etc/hosts
添加127.0.0.1 lamp
第二个错误编辑
vim httpd.conf
添加ServerName localhost:80
重启服务
]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
三、安装mysql-5.5.33
备注:可以创建一个文件/mydata/data用来存放mysql的数据,创建一个lvm挂载直此目录,数据过多时再拉伸此逻辑卷
1.创建目录以及用户
]# mkdir -pv /mydata/data
]# groupadd mysql
]# useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql
-r: 创建系统用户 -s SHELL: 指明用户的默认shell程序,可用列表在/etc/shells文件中(在此不允许其登陆系统)
-M:不要自动建立用户的登入目录 -d 以指定的路径为家目录
]# chown -R mysql:mysql /mydata/data
2.安装并初始化mysql-5.5.33
首先下载平台对应的mysql版本至本地,这里是64位平台,因此,选择的为mysql-5.5.33-linux2.6-x86_64.tar.gz
]# tar xf mysql-5.5.33-linux2.6-x86_64.tar.gz -C /usr/local/
]# cd /usr/local/
]# ln -sv mysql-5.5.33-linux2.6-x86_64/ mysql
-s : 进行软链结(symbolic link)
-v : 在连结之前显示其档名
]# chown -R root:mysql mysql-5.5.33-linux2.6-x86_64/
]# cd /usr/local/mysql
]# scripts/mysql_install_db –user=mysql –datadir=/mydata/data
将默认的data目录设置为/mydata/data
3.为mysql提供主配置文件
配置文件查找次序: /etc/my.cnf –> /etc/mysql/my.cnf –> –default-extra
file=/PATH/TO/CONF_FILE –> ~/.my.cnf
]# mkdir /etc/mysql
]# cp /usr/local/mysql/support-files/my-large.cnf /etc/mysql/my.cnf
]# vim /etc/mysql/my.cnf
修改此文件中thread_concurrency的值为你的CPU个数乘以2,比如这里使用如下行:
thread_concurrency = 2
另外还需要添加如下行指定mysql数据文件的存放位置:
datadir = /mydata/data 数据存放目录
innodb_file_per_table = on 每张表使用单个表空间文件
skip_name_resolve = on 跳过地址反解
4.为mysql提供启动脚本
]# cd /usr/local/mysql
]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
]# chmod +x /etc/rc.d/init.d/mysqld
添加至服务列表:
]# chkconfig –add mysqld
]# chkconfig mysqld on
启动mysql
]# service mysqld start
Starting MySQL [ OK ]
5.修改PATH环境变量,让系统可以直接使用mysql的相关命令。
]# vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
]# source /etc/profile.d/mysql.sh
mysql客户端登陆
]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
为了使用mysql的安装符合系统使用规范,并将其开发组件导出给系统使用,这里还需要进行如下步骤:
6.输出mysql的man手册至man命令的查找路径:
]#vim /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
9.安全初始化
mysql_secure_installation:修改root密码、清楚匿名用户、控制root远程登陆
三、编译安装php-5.4.26
1、解决依赖关系:
请配置好yum源(系统安装源及epel源)后执行如下命令:
]# yum -y groupinstall "Desktop Platform Development"
]# yum -y install bzip2-devel libmcrypt-devel libxml2-devel
2、编译安装php-5.4.26
]# 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
默认安装路径 mysql路径 与mysql交互的接口 多字节字符串支持 支持freetype字体格式 把php编译成apache的模块
php配置文件存放路径 用到event时需要编译
说明:
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
]# make
]# make test
]# make intall
为php提供配置文件:
# cp php.ini-production /etc/php.ini
3、 编辑apache配置文件httpd.conf,以apache支持php
]# vim /etc/httpd/httpd.conf
添加如下二行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
定位至DirectoryIndex index.html修改为:
DirectoryIndex index.php index.html
而后重新启动httpd,或让其重新载入配置文件即可测试php是否已经可以正常使用。
vim /usr/local/apache/htdocs/index.php
测试页面index.php示例如下:
<?php
$link = mysql_connect('127.0.0.1','root','123456');
if ($link)
echo "Success…";
else
echo "Failure…";
mysql_close();
phpinfo();
?>
默认编译安装测试页 /usr/local/apache/htdocs/index.html
浏览器访问192.168.8.93
四、安装php-MyAdmin
]# tar xf phpMyAdmin-3.2.5-all-languages.tar.gz
]# mv phpMyAdmin-3.2.5-all-languages /usr/local/apache/htdocs/pma
]# cd /usr/local/apache/htdocs/pma
]# cp config.sample.inc.php config.inc.php
]# vim config.inc.php
在$cfg['blowfish_secret'] = '任意多个字符';
浏览器访问 192.168.8.93/pma
输入root 123456(本机设置的mysql用户和密码)
ab压力测试
]$ ab -c 10 -n 100 http://192.168.8.93/pma/index.php
Total transferred: 961538 bytes
HTML transferred: 862500 bytes
Requests per second: 14.72 [#/sec] (mean)
Time per request: 679.228 [ms] (mean)
Time per request: 67.923 [ms] (mean, across all concurrent requests)
Transfer rate: 138.25 [Kbytes/sec] received
]$ ab -c 20 -n 100 http://192.168.8.93/pma/index.php
Total transferred: 961560 bytes
HTML transferred: 862500 bytes
Requests per second: 38.27 [#/sec] (mean)
Time per request: 522.639 [ms] (mean)
Time per request: 26.132 [ms] (mean, across all concurrent requests)
Transfer rate: 359.34 [Kbytes/sec] received
]$ ab -c 20 -n 1000 http://192.168.8.93/pma/index.php
Total transferred: 9615594 bytes
HTML transferred: 8625000 bytes
Requests per second: 37.36 [#/sec] (mean)
Time per request: 535.367 [ms] (mean)
Time per request: 26.768 [ms] (mean, across all concurrent requests)
Transfer rate: 350.80 [Kbytes/sec] received
五、安装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-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 -c 20 -n 100 http://192.168.8.93/pma/index.php
Total transferred: 961570 bytes
HTML transferred: 862500 bytes
Requests per second: 130.63 [#/sec] (mean)
Time per request: 153.107 [ms] (mean)
Time per request: 7.655 [ms] (mean, across all concurrent requests)
Transfer rate: 1226.63 [Kbytes/sec] received
]$ ab -c 20 -n 1000 http://192.168.8.93/pma/index.php
Total transferred: 9615528 bytes
HTML transferred: 8625000 bytes
Requests per second: 107.48 [#/sec] (mean)
Time per request: 186.081 [ms] (mean)
Time per request: 9.304 [ms] (mean, across all concurrent requests)
Transfer rate: 1009.26 [Kbytes/sec] received
六、启用服务器状态
mod_status模块可以让管理员查看服务器的执行状态,它通过一个HTML页面展示了当前服务器的统计数据。这些数据通常包括但不限于:
(1) 处于工作状态的worker进程数;
(2) 空闲状态的worker进程数;
(3) 每个worker的状态,包括此worker已经响应的请求数,及由此worker发送的内容的字节数;
(4) 当前服务器总共发送的字节数;
(5) 服务器自上次启动或重启以来至当前的时长;
(6) 平均每秒钟响应的请求数、平均每秒钟发送的字节数、平均每个请求所请求内容的字节数;
vim /etc/httpd/httpd.conf
启用状态页面的方法很简单,只需要在主配置文件中添加如下内容即可:
<Location /server-status>
SetHandler server-status
Require all granted
</Location>
需要提醒的是,这里的状态信息不应该被所有人随意访问,因此,应该限制仅允许某些特定地址的客户端查看。比如使用Require ip 172.16.0.0/16来限制仅允许指定网段的主机查看此页面。
ab测试示例:未启用xcache和启用xcache后,对phpMyAdmin的主而面进行请求测试的结果如下所示:
ab -c 10 -n 100 http://172.16.100.6/pma/index.php
第二部分:配置apache-2.4.9以fpm方式的php-5.4.26(fcgi模式)
一、需要准备的软件:将之前的解压包删掉,重新编译安装
]# cd /usr/local/src/
]# rm -rf php-5.4.26
]# rm -rf xcache-3.0.3
]# tar xf php-5.4.26.tar.bz2
]# tar xf xcache-3.0.3.tar.bz2
二、编译安装php-5.4.26
]# cd php-5.4.26
]# ./configure –prefix=/usr/local/php5 –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/php5/ –with-config-file-scan-dir=/etc/php5.d –with-bz2
]# make -j 4 && make install
-j 4 :4线程同时编译,加快速度
创建php配置的存放目录,与编译时选项保持一致,并存放配置文件
]# mkdir /etc/php5{,.d}
]# cp php.ini-production /etc/php5/php.ini
为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
为php-fpm提供配置文件:
# cp /usr/local/php5/etc/php-fpm.conf.default /usr/local/php5/etc/php-fpm.conf
]# vim /usr/local/php5/etc/php-fpm.conf
修改下列参数
listen = 0.0.0.0:9000
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
pid = /usr/local/php5/var/run/php-fpm.pid
]# service php-fpm start
]# ps -ef |grep fpm
root 12305 1 0 00:00 ? 00:00:00 php-fpm: master process (/usr/local/php5/etc/php-fpm.conf)
nobody 12306 12305 0 00:00 ? 00:00:00 php-fpm: pool www
nobody 12307 12305 0 00:00 ? 00:00:00 php-fpm: pool www
nobody 12308 12305 0 00:00 ? 00:00:00 php-fpm: pool www
nobody 12309 12305 0 00:00 ? 00:00:00 php-fpm: pool www
nobody 12310 12305 0 00:00 ? 00:00:00 php-fpm: pool www
root 12343 7710 0 00:01 pts/0 00:00:00 grep fpm
三、配置httpd-2.4.9
1、启用httpd的相关模块
]# cd /etc/httpd/
]# mv httpd.conf httpd.conf_phpmod
]# mv httpd.conf.bak httpd.conf
在Apache httpd 2.4以后已经专门有一个模块针对FastCGI的实现,此模块为mod_proxy_fcgi.so,它其实是作为mod_proxy.so模块的扩充,因此,这两个模块都要加载
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
# vim /etc/httpd/httpd.conf
1.添加如下二行
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
2.修改索引目录
DirectoryIndex index.php index.html
2、配置虚拟主机支持使用fcgi
在相应的虚拟主机中添加类似如下两行。
ProxyRequests Off :关闭正向代理
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/PATH/TO/DOCUMENT_ROOT/$1 对php的私有请求转发给fpm服务器
127.0.0.1:放php服务器的地址 /usr/local/apache/htdocs/$1
http://www.magedu.com/admin/index.php
本机添加的是
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/usr/local/apache/htdocs/$1
ServerName 127.0.0.1
检查语法(由于上次实验已经导出了环境变量)
]# httpd -t
Syntax OK
查看加载模块(有如下两项)
]# httpd -M
proxy_module (shared)
proxy_fcgi_module (shared)
启动httpd-2.4.9
]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
3、安装xcache做压力测试
1、安装
# cd /usr/local/src/xcache-3.0.3
# /usr/local/php5/bin/phpize
# ./configure –enable-xcache –with-php-config=/usr/local/php5/bin/php-config
# make && make install
安装结束时,会出现类似如下行:
Installing shared extensions: /usr/local/php5/lib/php/extensions/no-debug-zts-20100525/
2、编辑php.ini,整合php和xcache:
首先将xcache提供的样例配置导入php.ini
# cp xcache.ini /etc/php5.d/
说明:xcache.ini文件在xcache的源码目录中。
接下来编辑/etc/php5.d/xcache.ini,找到extension开头的行,修改为如下行:
extension = /usr/local/php5/lib/php/extensions/no-debug-zts-20100525/xcache.so
注意:如果php.ini文件中有多条zend_extension指令行,要确保此新增的行排在第一位。
4、ab压力测试同上
原创文章,作者:809889031@qq.com,如若转载,请注明出处:http://www.178linux.com/18596