一、安装顺序httpd-->Mysql-->PHP-->XCache。首先Apache HTTPD编译安装。
1、所需源码包。
apr-1.5.1.tar.gz #Apache Portable Runtime不同操作系统兼容程序
apr-util-1.5.3.tar.gz
httpd-2.4.9.tar.gz
2、开始编译安装。
[root@localhost ~]# yum -y groupinstall "Development Tools" #安装开发工具[root@localhost ~]# yum -y groupinstall "Additional Development"[root@localhost ~]# tar -zxvf apr-1.5.1.tar.gz[root@localhost ~]# cd apr-1.5.1[root@localhost apr-1.5.1]# ./configure --prefix=/usr/local/apr[root@localhost apr-1.5.1]# make[root@localhost apr-1.5.1]# make install[root@localhost ~]# tar -zxvf apr-util-1.5.3.tar.gz[root@localhost ~]# cd apr-util-1.5.3[root@localhost apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util \--with-apr=/usr/local/apr[root@localhost apr-util-1.5.3]# make[root@localhost apr-util-1.5.3]# make install[root@localhost ~]# tar -zxvf httpd-2.4.9.tar.gz[root@localhost ~]# cd httpd-2.4.9[root@localhost httpd-2.4.9]# ./configure --prefix=/usr/local/apache \--sysconfdir=/etc/httpd --enable-so --enable-rewrite --enable-ssl --enable-cgi \--enable-cgid --enable-mods-shared=most --enable-mpms-shared=all --with-apr=/usr/local/apr\--with-apr-util=/usr/local/apr-utilconfigure: error: pcre-config for libpcre not found. PCRE is required and available from [root@localhost httpd-2.4.9]# yum -y install pcre-devel #安装缺失依赖包[root@localhost httpd-2.4.9]# make[root@localhost httpd-2.4.9]# make install[root@localhost ~]# vim /etc/httpd/httpd.conf PidFile "/var/run/httpd.pid" #修改PidFile路径ServerName 127.0.0.1 #若无DNS即填IP[root@localhost ~]# vim /etc/init.d/httpd #创建服务启动文件,可从rpm包复制该文件。#!/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.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[root@localhost ~]# chmod +x /etc/init.d/httpd [root@localhost ~]# chkconfig --add httpd[root@localhost ~]# chkconfig --level 35 httpd on[root@localhost ~]# vim /etc/profile.d/httpd.sh #添加PATH,必须以sh结尾export PATH=$PATH:/usr/local/apache/bin[root@localhost ~]# echo $PATH #重新登录即可生效/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/apache/bin:/root/bin[root@localhost ~]# setenforce 0[root@localhost ~]# service iptables stop[root@localhost ~]# service httpd start
二、编译安装Mysql。
1、源码包。
mysql-5.5.30-linux2.6-x86_64.tar.gz
2、开始编译安装。
[root@localhost ~]# tar -zxvf mysql-5.5.30-linux2.6-x86_64.tar.gz -C /usr/local #注意:需要解压到/usr/local目录下且命名为mysql[root@localhost ~]# ln -sv /usr/local/mysql-5.5.30-linux2.6-x86_64/ /usr/local/mysql[root@localhost ~]# groupadd -r -g 306 mysql[root@localhost ~]# useradd -g 306 -r -u 306 mysql #创建不允许登录的用户mysql[root@localhost ~]# chown -R mysql.mysql /usr/local/mysql/*[root@localhost ~]# mkdir -p /mydata/data #创建mysql数据存放目录,建议独立逻辑卷挂载[root@localhost ~]# chown -R mysql.mysql /mydata/data[root@localhost ~]# chmod o-rx /mydata/data[root@localhost mysql]# scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/mydata/data/ #运行安装脚本[root@localhost mysql]# chown -R root /usr/local/mysql/* #安全考虑属主该为root,但数据目录必须为mysql[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld #添加启动脚本[root@localhost mysql]# chmod +x /etc/init.d/mysqld [root@localhost mysql]# chkconfig --add mysqld[root@localhost mysql]# chkconfig --level 35 mysqld on[root@localhost mysql]# cp support-files/my-large.cnf /etc/my.cnf #添加配置文件cp: overwrite `/etc/my.cnf'? y[root@localhost mysql]# vim /etc/my.cnf[mysqld]thread_concurrency = 4 #线程并发量datadir = /mydata/data #数据目录[root@localhost mysql]# vim /etc/man.config #Manual文件链接MANPATH /usr/local/mysql/man[root@localhost mysql]# vim /etc/profile.d/mysql.sh #Mysql执行程序链接export PATH=$PATH:/usr/local/mysql/bin[root@localhost mysql]# vim /etc/ld.so.conf.d/mysql.conf #库文件链接/usr/local/mysql/lib[root@localhost ~]# ldconfig -v #重新读取库文件[root@localhost ~]# ln -sv /usr/local/mysql/include /usr/include/mysql #头文件链接[root@localhost ~]# service mysqld start
三、编译安装PHP。
1、源码包。
php-5.4.31.tar.gz
libmcrypt-2.5.7-5.el5.i386.rpm #这四个包为--with-mcrypt选项的依赖包
libmcrypt-devel-2.5.7-5.el5.i386.rpm mhash-0.9.9-1.el5.centos.i386.rpm mhash-devel-0.9.9-1.el5.centos.i386.rpm2、编译安装以模块化运行的php以及xcache扩展。
[root@localhost ~]# tar xf php-5.4.31.tar.gz[root@localhost ~]# cd php-5.4.31[root@localhost php-5.4.31]#./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[root@localhost php-5.4.31]# make[root@localhost php-5.4.31]# make install[root@localhost php-5.4.31]# cp php.ini-production /etc/php.ini #定义配置文件[root@localhost ~]# vim /etc/httpd/httpd.conf #配置Apache识别php程序AddType application/x-httpd-php .phpAddType application/x-httpd-php-source .phpsDirectoryIndex index.html index.php[root@localhost ~]# cd /usr/local/apache/htdocs/[root@localhost htdocs]# mv index.html index.php[root@localhost htdocs]# vim index.phpIt works!
[root@localhost ~]# tar -zxvf xcache-2.0.1.tar.gz[root@localhost ~]# cd xcache-2.0.1[root@localhost xcache-2.0.1]# /usr/local/php/bin/phpize #php扩展编译准备[root@localhost xcache-2.0.1]# ./configure --enable-xcache \--with-php-config=/usr/local/php/bin/php-config [root@localhost xcache-2.0.1]# make installInstalling shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20100525/[root@localhost xcache-2.0.1]# mkdir /etc/php.d[root@localhost xcache-2.0.1]# cp xcache.ini /etc/php.d/[root@localhost xcache-2.0.1]# vim /etc/php.d/xcache.inizend_extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so;zend_extension_ts = c:/php/extensions/php_xcache.dll[root@localhost xcache-2.0.1]# service httpd restart #重启httpd,主页有xcache模块即成功
3、配置apache以fpm方式运行的php
[root@localhost ~]# tar xf php-5.4.31.tar.gz[root@localhost ~]# cd php-5.4.31[root@localhost ~]# ./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[root@localhost php-5.4.31]# make[root@localhost php-5.4.31]# make install为php提供配置文件:# cp php.ini-production /etc/php.ini3、配置php-fpm 为php-fpm提供Sysv init脚本,并将其添加至服务列表:# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm# chmod +x /etc/rc.d/init.d/php-fpm# chkconfig --add php-fpm# chkconfig php-fpm on为php-fpm提供配置文件:# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf 编辑php-fpm的配置文件:# vim /usr/local/php/etc/php-fpm.conf配置fpm的相关选项为你所需要的值,并启用pid文件(如下最后一行):pm.max_children = 50pm.start_servers = 5pm.min_spare_servers = 2pm.max_spare_servers = 8pid = /usr/local/php/var/run/php-fpm.pid 接下来就可以启动php-fpm了:# service php-fpm start使用如下命令来验正(如果此命令输出有中几个php-fpm进程就说明启动成功了):# ps aux | grep php-fpm默认情况下,fpm监听在127.0.0.1的9000端口,也可以使用如下命令验正其是否已经监听在相应的套接字。# netstat -tnlp | grep php-fpmtcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 689/php-fpm启用httpd的相关模块在Apache httpd 2.4以后已经专门有一个模块针对FastCGI的实现,此模块为mod_proxy_fcgi.so,它其实是作为mod_proxy.so模块的扩充,因此,这两个模块都要加载LoadModule proxy_module modules/mod_proxy.soLoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so2、配置虚拟主机支持使用fcgi在相应的虚拟主机中添加类似如下两行。 ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/PATH/TO/DOCUMENT_ROOT/$1 例如:DocumentRoot "/www/magedu.com" ServerName magedu.com ServerAlias www.magedu.com ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/www/magedu.com/$1 Options none AllowOverride none Require all granted ProxyRequests Off:关闭正向代理ProxyPassMatch:把以.php结尾的文件请求发送到php-fpm进程,php-fpm至少需要知道运行的目录和URI,所以这里直接在fcgi://127.0.0.1:9000后指明了这两个参数,其它的参数的传递已经被mod_proxy_fcgi.so进行了封装,不需要手动指定。3、编辑apache配置文件httpd.conf,让apache能识别php格式的页面,并支持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补充:Apache httpd 2.4以前的版本中,要么把PHP作为Apache的模块运行,要么添加一个第三方模块支持PHP-FPM实现。
四、配置DNS,虚拟主机。
[root@localhost ~]# yum -y install bind[root@localhost ~]# vim /etc/named.conf options { listen-on port 53 { any; }; allow-query { any; }; forwarders { 114.114.114.114; }; dnssec-validation no;zone "a.net" IN { type master; file "/var/named/a.net";};zone "1.16.172.in-addr.arpa" IN { type master; file "named-1.16.172";};[root@localhost ~]# vim /var/named/a.net $TTL 86400@ IN SOA a.net. root.a.net. ( 2014071011 8H 15M 1W 1D );@ IN NS a.net.;@ IN A 172.16.1.1;www IN A 172.16.1.1;[root@localhost ~]# vim /var/named/named-1.16.172 $TTL 86400@ IN SOA a.net. root.a.net. ( 2014071011 8H 15M 1W 1D );@ IN NS a.net.;1 IN PTR a.net.; 1 IN PTR www.a.net.;[root@localhost ~]# vim /etc/httpd/httpd.conf#DocumentRoot "/usr/local/apache/htdocs" #注释该行Include /etc/httpd/extra/httpd-vhosts.conf #此行取消注释[root@localhost ~]# vim /etc/httpd/extra/httpd-vhosts.confDocumentRoot "/web/a.net" ServerName www.a.net Options none AllowOverride none Require all granted ErrorLog "/var/log/httpd/a.net-error_log" CustomLog "/var/log/httpd/a.net-access_log" combined