两台Web服务器 实现负载均衡 +FastCGI模块+MySQL实现分布式架构
操作系统:RedHat 5.8
实验环境准备 :三台服务器
Server 1 IP :172.16.2.1 安装:apache NFS
Server 2 IP :172.16.2.2 安装:apache DNS
Server 3 IP:172.16..2.3 安装:php mysql
实验图如下 :
工作原理:
1、当客户端请求www.tast.com
2、Server 2 上的DNS会以轮询的方式把请求转给 172.16.2.1 和172.16.2.2
3、如果此次请求是静态页面 web服务就直接把结果返给客户 ,如果是动态页面,就转给server 3 上的php 解析
4、如果需要用到数据库的数据,就通过数据库的接口访问数据库
5、Php解析后把结果返回给前端的web
6、Web把结果返回给客户
步骤
1、配置server 1
安装配置 apache 和 NFS
2、配置 server 2
安装配置apache 和DNS
3、配置 server 3
安装配置 mysql 和 php
4、总结
注:此次试验只是给大家提供一种思路,可以这么配置,企业实际需求中,不同的需求,不同的操作系统,不同的软件版本,配置过程会略有不同,但原理都是想通了,希望大家在做实验的同时不要忘了思考,学而不思则罔,原理理解了,我们就可以根据实际需求搭建出更适合自己需要的服务。
过程:
一、配置server 1
1.1 配置编译安装环境
由于此次试验每个软件都是编译安装,所以每台服务器都要先配置好编译安装环境
确保这两个组安装
- Development Tools
- Development Libraries
- #yum -y groupinstall "Development Libraries"
1.2 安装httpd 2.4.2 版本
- #yum -y install pcre-devel 解决依赖关系
- # tar xf httpd-2.4.2.tar.bz2
- # cd httpd-2.4.2
- # less INSTALL 查看安装说明
- $ ./configure --prefix=PREFIX
- $ make
- $ make install
- $ PREFIX/bin/apachectl start
- # ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd
- --enable-so --enable-rewrite --with-zlib --with-pcre
- --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util
- --enable-modules=most --enable-ssl --enable-modules-shared=most
- #make && make install
1.3 修改httpd的主配置文件,设置其Pid文件的路径
- #vim /etc/httpd/httpd.conf
- idFile "/var/run/httpd.pid" 添加
1.4 提供SysV服务脚本,内容如下:
#vim /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 加入服务列表:
1.5 安装apache fastcgi模块:
- #tar -zxvf mod_fastcgi-current.tar.gz
- #cd mod_fastcgi
- #cp Makefile.AP2 Makefile
- #Vim Makefile 修改top_dir=/usr/local/apache #你的apache安装路径
- #make
- #make install
1.6修改配置文件 让apache支持php-fpm
- 增加: LoadModule fastcgi_module modules/mod_fastcgi.so
- 有的话不用添加。
- ScriptAlias /cgi-bin/ "/usr/local/php/bin/"
- FastCgiExternalServer /usr/local/php/bin/php-fpm -host 172.16.2.3:9000
- AddType application/x-httpd-php .php
- AddHandler php-fastcgi .php
- Action php-fastcgi /cgi-bin/php-fpm
- <Directory "/usr/local/php/bin/">
- Options -Indexes FollowSymLinks +ExecCGI
- Order allow,deny
- Allow from all
- </Directory>