本次实验目的:实现httpd与php通过fpm结合。
服务器相关信息:
IP:172.16.113.16 2.6.32-504.el6.x86_64 [[email protected]~]# cat redhat-re [[email protected]~]# cat /etc/redhat-release CentOSrelease 6.6 (Final)
编译安装httpd2.2
需要的源码包:
httpd-2.4.16.tar.bz2
apr-1.5.0.tar.bz2
apr-util-1.5.3.tar.bz2
准备开发环境:
[[email protected]]# yum groupinstall -y "Development tools" "ServerPlatform Development"
解决依赖关系:安装pcre-devel包
[[email protected]]# yum install -y pcre-devel //支持perl的正则表达式
展开并安装apr
[[email protected]~]# tar xf apr-1.5.0.tar.bz2
[[email protected]~]# cd apr-1.5.0
[[email protected]]# ./configure --prefix=/usr/local/apr
[[email protected]]# make &&make install
[[email protected]]# cd .. //切换回上一级目录
展开并安装apr-util
[[email protected]~]# tar xf apr-util-1.5.3.tar.bz2
[[email protected]~]# cd apr-util-1.5.3
[[email protected]]# ./configure --prefix=/usr/local/apr-util--with-apr=/usr/local/apr/
[[email protected]]# make && make install &&cd ..
展开并安装httpd
[[email protected]~]# tar xf httpd-2.4.16.tar.bz2
[[email protected]]# cd httpd-2.4.16
[[email protected]]# ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24--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
#--prefix=/usr/local/apache //指明安装目录
#--sysconfdir=/etc/httpd24 //指定配置文件的安装路径,此处为了避免与rpm包所装的httpd冲突,将路径名取名为httpd24
#--enable-so //支持httpd的模块化机制
#--enable-ssl //启用支持ssl功能,实现https
#--enable-cgi //支持cgi协议
#--enable-rewrite //支持url重写
#--with-zlib //支持传输压缩
#--with-pcre //支持perl语言的正则表达式
#--with-apr=/usr/local/apr //指明编译升级的apr的路径
#--with-apr-util=/usr/local/apr-util//指明编译升级的apr-util路径
#--enable-modules=most //指明启动大多数的模块
#--enable-mpms-shared=all //将所有mpm模块都编译进httpd(httpd2.4版本的新特性)
#--with-mpm=event //默认使用event的MPM工作模型
[[email protected]]# make && make install
[[email protected]]# mkdir /var/run/httpd24 //创建httpd的pid创建目录
[[email protected]]# vim /etc/httpd24/httpd.conf //编辑配置文件
PidFile"/var/run/httpd24/httpd.pid" //添加运行时pid文件的位置
为httpd24提供服务管理脚本:放置于/etc/rc.d/init.d/httpd24,并赋予可执行权限
[[email protected]]# vim /etc/rc.d/init.d/httpd24
#!/bin/bash
#chkconfig: - 85 15
#description: The Apache HTTP Server Management script.
./etc/rc.d/init.d/functions
apachectl=/usr/local/apache/bin/apachectl
httpd=/usr/local/apache/bin/httpd
prog=httpd
pidfile=/var/run/httpd24/httpd.pid
lockfile=/var/lock/subsys/httpd24
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}
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 ${STOP_TIMEOUT} $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=6
echo $"not reloading due to configuration syntaxerror"
failure $"not reloading $httpd due to configurationsyntax error"
else
# Force LSB behaviour from killproc
LSB=1 killproc -p ${pidfile} $httpd -HUP
RETVAL=$?
if [ $RETVAL -eq 7 ]; then
failure $"httpd shutdown"
fi
fi
echo
}
case"$1" in
start)
start
;;
stop)
stop
;;
status)
status -p ${pidfile} $httpd
RETVAL=$?
;;
restart)
stop
start
;;
condrestart|try-restart)
if status -p ${pidfile} $httpd >&/dev/null; then
stop
start
fi
;;
force-reload|reload)
reload
;;
graceful|help|configtest|fullstatus)
$apachectl [email protected]
RETVAL=$?
;;
*)
echo $"Usage: $prog{start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"
RETVAL=2
esac
exit$RETVAL
[[email protected]]# chmod +x /etc/rc.d/init.d/httpd24
[[email protected]]# chkconfig --add httpd24 //将httpd24添加至开机自动启动项
[[email protected]]# chkconfig httpd24 on //开启开机自启动httpd24
[[email protected]]# vim /etc/profile.d/httpd24.sh //创建此文件并添加如下信息,以为将编译的httpd24添加至环境变量
exportPATH=/usr/local/apache/bin:$PATH
编译安装mariadb
需要的源码包:
mariadb-5.5.43-linux-x86_64.tar.gz (二进制包)
安装mariadb,可通过展开包中的“INSTALL-BINARY”查看安装步骤:
[[email protected]~]# tar -xf mariadb-5.5.43-linux-x86_64.tar.gz
[[email protected]~]# mv mariadb-5.5.43-linux-x86_64 /usr/local/mysql //将展开目录移动至/usr/local下,并重命名为"mysql"
[[email protected]~]# cd /usr/local/mysql
[[email protected]]# groupadd -r -g 306 mysql //创建系统用户组:mysql
[[email protected]]# useradd -r -g 306 -u 306 mysql //创建系统用户:mysql
[[email protected]]# chown -R mysql.mysql . //将mariadb的展开目录改为属主属组都是mysql
[[email protected]]# mkdir -pv /mydata/data //创建数据库存放目录,此目录最好为xfs文件系统
mkdir:created directory `/mydata‘
mkdir:created directory `/mydata/data‘
[[email protected]]# scripts/mysql_install_db --user=mysql--datadir=/mydata/data //运行程序自带脚本安装mariadb
[[email protected]]# chown -R root . //将mariadb的展开目录及其子目录属主改为root
将mariadb自带的配置文件模板拷贝至/etc下命名为my.cnf作为mariadb的配置文件
[[email protected]]# scp support-files/my-large.cnf /etc/my.cnf
[[email protected]]# vim /etc/my.cnf //配置下面几项参数,入没有需手动添加
thread_concurrency= 8 //此处改为服务器cpu核心数的2倍
//在[mysqld]配置池中添加下面三项参数:
datadir =/mydata/data
innodb_file_per_table= yes
skip_name_resolve= yes
为mariadb提供服务管理脚本,mariadb自带有服务管理脚本,拷贝至/etc/rc.d/init.d/下即可
[[email protected]]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
启动mariadb,并运行mariadb的安全初始化脚本,完成初始化配置,为root添加密码等操作:
[[email protected]]# service mysqld start
[[email protected]]# bin/mysql_secure_installation
将mariadb添加至开机自动启动
[[email protected]]# chkconfig --add mysqld
[[email protected]]# chkconfig mysqld on
将mariadb目录添加至环境变量
[[email protected]]# vim /etc/profile.d/mariadb.sh
exportPATH=/usr/local/mysql/bin:$PATH
编译安装php
需要的源码包:php-5.6.13.tar.bz2
1、解决依赖关系:(事先配置好yum源)
[[email protected]]# yum install -y libmcrypt-devel mhash libxml2-devel bzip2-devel
2、安装php
[[email protected]~]# tar xf php-5.6.13.tar.bz2
[[email protected]_mariadb ~]# cd php-5.6.13
[[email protected]]# ./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
// --enable-fpm 此项参数最为关键。是表明让php运行为单独的服务
[[email protected]]# make -j 4 && make install //开启多线程编译安装
3、提供php的配置文件:php源码包中自带配置文件模板,由于刚才编译安装时指定了php启动扫描配置文件的路径为/etc/php.d
因此:
[[email protected]]# cp php.ini-production /etc/php.d/php.ini
4、位php提供服务管理脚本:php源码包中自带
[[email protected]]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[[email protected]]# chmod +x /etc/rc.d/init.d/php-fpm //为其赋予可执行权限
[[email protected]]# chkconfig --add php-fpm //添加至开机自动启动项
[[email protected]]# chkconfig php-fpm on //开机自动启动
5、为fpm提供配置文件
[[email protected]]# cp /usr/local/php/etc/php-fpm.conf.default/usr/local/php/etc/php-fpm.conf
[[email protected]]# vim /usr/local/php/etc/php-fpm.conf //编辑配置文件,编辑如下几项
pid =run/php-fpm.pid
pm.max_children= 50 //服务启动的最大进程数
pm.start_servers= 10 //开启服务是启动的进程数
pm.min_spare_servers= 5 //最小空闲进程数
pm.max_spare_servers= 10 //最大空闲进程数
6、修改httpd配置文件
启用如下两个模块:
LoadModuleproxy_module modules/mod_proxy.so
LoadModuleproxy_fcgi_module modules/mod_proxy_fcgi.so
//在Apache httpd 2.4以后已经专门有一个模块针对FastCGI的实现,此模块为mod_proxy_fcgi.so,它其实是作为mod_proxy.so模块的扩充,因此,这两个模块都要加载
添加如下两项参数,在Directory中添加;若想使此项对虚拟主机生效,需添加至虚拟主机内部
ProxyRequestsOff
ProxyPassMatch^/(.*\.php)$ fcgi://127.0.0.1:9000/PATH/TO/DOCUMENT_ROOT/$1
修改 DirectoryIndex index.php index.html
添加:
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
7、重启httpd24,编辑php测试
[[email protected]]# vim /usr/local/apache/htdocs/index.php
<?php
$conn=mysql_connect(‘127.0.0.1‘,‘root‘,‘root‘);
if ($conn)
echo "OK";
else
"Failure";
phpinfo();
?>
8、在浏览器输入url进行测试:
至此,php的fpm工作机制已搭建完成