基于NFS系统搭建discuz实现负载均衡

来源:互联网 发布:中国透析患者数据 编辑:程序博客网 时间:2024/06/08 08:09

一、NFS基本使用
# 安装基于RPC(Remote Procedure Call)的实现包portmap
shell> yum install portmap
# 安装nfs服务器端nfs-utils
shell> yum install nfs-utils
# 启动portmap服务(nfs是基于portmap进行远程通信的),监听在tcp和udp的111端口上.
shell> service portmap start
shell> chkconfig portmap on
shell> netstat -tunpl | grep portmap
# 启动nfs服务
shell> service nfs start
shell> chkconfig nfs on
# 查看所有rpc进程监听的端口信息
shell> rpcinfo -p localhost
# nfs启动的各个进程介绍
  nfsd(nfs服务;端口:2049/tcp, 2049/udp), mountd(挂载服务;端口:随机), quotad(配额服务;端口:随机)
shell> vim /etc/exports
 添加如下内容:
    /shared         192.168.246.0/24(ro)
 备注:在/etc/exports中定义"导出"的各文件系统,格式如下:
   /path/to/somedir CLIENT_LIST(ro,async)
   每个客户端后面必须跟一个小括号,里面定义了此客户访问特性,如访问权限等
# 导出在/etc/exports文件中新增的文件系统
shell> exportfs -rav
# exportfs命令:
    -a:跟-r或-u选项同时使用,表示重新挂载所有文件系统或取消导出所有文件系统;
    -r: 重新导出
    -u: 取消导出
    -v: 显示详细信息
# 查看系统系导出的文件系统
shell> showmount -e 192.168.246.129
# showmount命令:
    -e NFS_SERVER: 查看NFS服务器“导出”的各文件系统
    -a NFS_SERVER: 查看NFS服务器所有被挂载的文件系统及其挂载的客户端对应关系列表
    -d NFS_SERVER: 显示NFS服务器所有导出的文件系统中被客户端挂载了文件系统列表
# 客户端使用mount命令挂载(需启动例外一台虚拟机作为客户端进行测试, 客户端必须安装启动portmap服务)
  # 格式:mount -t nfs NFS_SERVER:/PATH/TO/SOME_EXPORT  /PATH/TO/SOMEWHRERE
shell> mount -t nfs 192.168.246.129:/shared  /mnt/nfs
# 使用开机自动挂载nfs
shell> vim /etc/fstab
 增加内容:_rnetdev意思为如果挂载失败,会忽略此挂载选项
   192.168.246.129:/shared  /mnt/nfs   nfs      defaults,_rnetdev 0 0
# 重新挂载测试
shell> umount /dev/nfs
shell> mount -a
# 文件系统导出属性(在/etc/exports文件中定义的)
  ro: 只读;
  rw: 读写;
  sync: 同步;
  async: 异步;
  root_squash: 将root用户映射为来宾账号;
  no_root_squash: 将root用户映射为root账号(保留管理员自身的权限);
  all_squash: 将所有的用户映射为来宾账号
  anonuid, anongid: 指定映射的来宾账号的UID和GID.
# 例子:将所有的用都映射为来宾账号nfs
# 服务器端执行:
shell> groupadd -g 510 nfs
shell> useradd -u 510 -g 510 nfs
shell> id nfs
shell> touch /shared/nfstest
shell> chown nfs:nfs /shared/nfstest
shell> ll /shared/nfstest
shell> vim /etc/exports
  修改内容为:
   /shared   192.168.246.0/24(ro,all_squash,anonuid=510,anongid=510)
shell> exportfs -rav
shell> showmount -e 192.168.246.129
# 客户端执行:
shell> mount -t nfs 192.168.246.129:/shared  /mnt/nfs
shell> cd /mnt/nfs
shell> ls -l

二、搭建DNS服务器
# 删除系统安装的bind
shell> yum erase bind-libs bind-utils
# 安装bind97
shell> yum install bind97 bind97-utils bind97-libs
# 修改主配置文件
shell> vim /etc/named.conf
  将options选项只保留directory选项,其它不变.
  options {
      directory       "/var/named";
  };
# 增加域名
shell> vim /etc/named.rfc1912.conf  
  在文件底部增加如下内容:
  zone "spook.com" IN {
        type master;
        file "spook.com.zone";
        allow-update { none; };

  };

  zone "246.168.192.in-addr.arpa" IN {
          type master;
          file "192.168.246.zone";
          allow-update { none; };
  };
# 添加正向解析文件
shell> vim /var/named/spook.com.zone
  在文件中增加如下内容:
  $TTL 1D
  @               IN      SOA     ns1.spook.com.  admin.spook.com.(
                                  2014092501
                                  1H
                                  5M
                                  2D
                                  1W)
                  IN      NS      ns1
                  IN      MX  10  mail
  ns1             IN      A       192.168.246.105
  mail            IN      A       192.168.246.110
  ftp.spook.com.  IN      A       192.168.246.115
  www.spook.com.  IN      A       192.168.246.120
  www.spook.com.  IN      A       192.168.246.130

# 添加反向解析文件
shell> vim /var/named/192.168.246.zone
  在文件中增加如下内容:
  $TTL 1D
  @               IN      SOA     ns1.spook.com.  admin.spook.com.(
                                  2014092501
                                  1H
                                  5M
                                  2D
                                  1W)
                  IN      NS      ns1.spook.com.
  105             IN      PTR     ns1.spook.com.
  110             IN      PTR     mail.spook.com.
  115             IN      PTR     ftp.spook.com.
  120             IN      PTR     www.spook.com.
  130             IN      PTR     www.spook.com.
# 修改属主为named
shell> chgrp named spook.com.zone 192.168.246.zone
# 修改权行为744
shell> chmod 744 spook.com.zone 192.168.246.zone
# 检查配置文件
shell> named-checkconf /etc/named.conf
shell> named-checkconf /etc/named.rfc1912.zones
shell> named-checkzone spook.com.zone /var/named/spook.com.zone
shell> named-checkzone 192.168.246.zone /var/named/192.168.246.zone
#修改DNS服务器
shell> vim /etc/resolv.conf
 修改为如下内容:
  search localdomain
  nameserver 192.168.246.105
# 测试
shell> dig -t A www.spook.com
shell> dig -t ns spook.com
shell> dig -x 192.168.246.120

三、搭建LAMP平台一 (使用moduel方式)
 1、编译安装apache
 httpd-2.4.4需要较新版本的apr和apr-util,因此需要事先对其进行升级。
 升级方式有两种,一种是通过源代码编译安装,一种是直接升级rpm包.

 1.1、解决依赖关系。
 (1)编译安装apr
  shell> tar -xf apr-1.5.1.tar.bz2
  shell> cd apr-1.5.1
  shell> ./configure --prefix=/usr/local/apr
  shell> make && make install
 (2) 编译安装apr-util
  shell> tar -xf apr-util-1.5.3.tar.bz2
  shell> cd apr-util-1.5.3
  shell> ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
  shell> make && make install
 (3)编译安装pcre软件包
  shell> unzip pcre-8.10.zip
  shell> cd pcre-8.10
  shell> ./configure --prefix=/usr/local/pcre
  shell> make && make install

 1.2、编译安装httpd-2.4.10
  shell> tar xf httpd-2.4.10.tar.bz2
  shell> cd httpd-2.4.10
  shell> ./configure --prefix=/usr/local/apache  \
            --sysconfdir=/etc/httpd  \
            --enable-so  \
            --enable-ssl  \
            --enable-cgi  \
            --enable-rewrite  \
            --with-zlib  \
            --with-pcre=/usr/local/pcre  \
            --with-apr=/usr/local/apr  \
            --with-apr-util=/usr/local/apr-util  \
            --enable-modules=most  \
            --enable-mpms-shared=all  \
            --with-mpm=event
  shell> make && make install

 补充:
 (1) 构建MPM为静态模块
   在全部平台中,MPM都可以构建为静态模块。在构建时选择一种MPM,链接到服务器中。
   如果要改变MPM,必须重新构建。为了使用指定的MPM,请在执行configure脚本时,
   使用参数 --with-mpm=NAME。NAME是指定的MPM名称。编译完成后,可以使用 ./httpd -l
   来确定选择的MPM。 此命令会列出编译到服务器程序中的所有模块,包括 MPM。
 (2) 构建 MPM 为动态模块
   在Unix或类似平台中,MPM可以构建为动态模块,与其它动态模块一样在运行时加载。
   构建 MPM 为动态模块允许通过修改LoadModule指令内容来改变MPM,而不用重新构建服务器程序。
   在执行configure脚本时,使用--enable-mpms-shared选项即可启用此特性。当给出的参数为all时,
   所有此平台支持的MPM模块都会被安装。还可以在参数中给出模块列表。默认MPM,
   可以自动选择或者在执行configure脚本时通过--with-mpm选项来指定,然后出现在生成的服务器配置文件中。
   编辑LoadModule指令内容可以选择不同的MPM。

 1.3、修改httpd的主配置文件,设置其Pid文件的路径
  shell> vim /etc/httpd/httpd.conf
   添加如下内容:
    PidFile  "/var/run/httpd.pid"
 
 1.4、提供SysV服务脚本
  shell> 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
  STOP_TIMEOUT=${STOP_TIMEOUT-10}

  # check for 1.3 configuration
  check13 () {
    CONFFILE=/etc/httpd/httpd.conf
    GONE="(ServerType|BindAddress|Port|AddModule|ClearModuleList|"
    GONE="${GONE}AgentLog|RefererLog|RefererIgnore|FancyIndexing|"
    GONE="${GONE}AccessConfig|ResourceConfig)"
    if LANG=C grep -Eiq "^[[:space:]]*($GONE)" $CONFFILE; then
      echo
      echo 1>&2 " Apache 1.3 configuration directives found"
      echo 1>&2 " please read /usr/share/doc/httpd-2.2.3/migration.html"
      failure "Apache 1.3 config directives test"
      echo
      exit 1
    fi
  }

  # The semantics of these two functions differ from the way apachectl does
  # things -- attempting to start while running is a failure, and shutdown
  # when not running is also a failure.  So we just do it the way init scripts
  # are expected to behave here.
  start() {
          echo -n $"Starting $prog: "
          check13 || exit 1
          LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
          RETVAL=$?
          echo
          [ $RETVAL = 0 ] && touch ${lockfile}
          return $RETVAL
  }

  # When stopping httpd a delay (of default 10 second) is required
  # before SIGKILLing the httpd parent; this gives enough time for the
  # httpd parent to SIGKILL any errant children.
  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=$?
          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
  #END
 
  1.5、环境变量设置
   shell> vim /etc/profile.d/apache.sh
    增加如下内容:
    export PATH=$PATH:/usr/local/apache/bin/
   shell> vim /etc/profile.d/apache.sh
    增加如下内容:
    usr/local/apache/lib/
   shell> ldconfig -v

  1.6、启动服务进行测试
   shell> chmod +x /etc/rc.d/init.d/httpd
   shell> chkconfig --add httpd
   shell> chkconfig httpd on
   shell> service httpd start
  1.7、测试
   输入网址:http://192.168.246.120/

2、安装mysql(此处的版本为mysql-5.5.39,下载网站http://www.mysql.com/)
  # 增加mysql用户和mysql组
  shell> groupadd -g 3306 mysql
  shell> useradd -u 3306 -g 3306 -M -s /sbin/nologin mysql
  # 解压mysql安装包并创建链接文件
  shell> tar -xf mysql-5.5.39-linux2.6-i686.tar.gz -C /usr/local/
  shell> ln -s /usr/local/mysql-5.5.39-linux2.6-i686 /usr/local/mysql
  # 修改mysql目录中所有的文件的宿主和属组为mysql
  shell> cd /usr/local/mysql
  shell> chown -R mysql:mysql .
  # 初始化mysql数据库
  shell> scripts/mysql_install_db --user=mysql
  # 修改mysql目录中所有的文件的宿主为root
  shell> chown -R root .
  # 修改mysql目录中data文件的宿主为mysql
  shell> chown -R mysql data
  # 复制mysql的主配置文件
  shell> cp support-files/my-medium.cnf /etc/my.cnf
  # 复制mysql的服务脚本
  shell> cp support-files/mysql.server /etc/init.d/mysqld
  # 将mysql的服务脚本加到服务列表中并在默认级别开启服务
  shell> chkconfig --add mysqld
  shell> chkconfig mysqld on
  # 将mysql的二进制文件增加到系统环境变量中
  shell> vim /etc/profile.d/mysql.sh
    增加 export PATH=$PATH:/usr/local/mysql/bin
  # 将mysql的库文件增加到系统库中
  shell> vim /etc/ld.so.conf.d/mysq.conf
    增加 /usr/local/mysql/lib
  shell> idconfig -v
  # 将mysql的头文件增加到系统库中
  shell> ln -s /usr/local/mysql/include/ /usr/include/mysql
  # 启动mysql服务
  shell> service mysqld start

3、编译安装php-5.4.32
  3.1、解决依赖关系
  shell> rpm -ivh libmcrypt-2.5.7-5.el5.i386.rpm
  shell> rpm -ivh libmcrypt-devel-2.5.7-5.el5.i386.rpm
  shell> rpm -ivh mhash-0.9.2-6.el5.i386.rpm
  shell> rpm -ivh mhash-devel-0.9.2-6.el5.i386.rpm

  3.2、编译安装php-5.4.32
  shell> tar -xf php-5.4.32.tar.bz2
  shell> cd php-5.4.32
  shell> ./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
    说明:
      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
      3、--with-apxs2=/usr/local/apache/bin/apxs 此选项定义以模块方式与httpd结合的
   shell> make
   shell> make install

  3.3、为php提供配置文件:
   shell> cp php.ini-production /etc/php.ini

  3.4、编辑apache配置文件httpd.conf,以apache支持php
   shell> 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
 
  3.5、测试
   shell> vim /usr/local/apache/htdocs/index.php
    添加如下内容:
      <?php
        $con = mysql_connect("localhost","root","");
        if (!$con){
          die('Could not connect: ' . mysql_error());
        }
        echo "Connection mysql sucess~";

        phpinfo();
      ?>
    
4、安装xcache,为php加速
  4.1 安装
   shell> tar xf xcache-3.0.1.tar.gz
   shell> cd xcache-3.0.1
   shell> /usr/local/php/bin/phpize
   shell> ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
   shell> make && make install
   安装结束时,会出现类似如下行:
   Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-zts-20100525/
  4.2 编辑php.ini,整合php和xcache
   #首先将xcache提供的样例配置导入php.ini
   shell> mkdir /etc/php.d/
   shell> cp xcache.ini /etc/php.d
   #编辑/etc/php.d/xcache.ini文件:
   shell> vim /etc/php.d/xcache.ini
    找到zend_extension开头的行,修改为如下行
    zend_extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so
    注意:xcache3.0.4时不需zend_extension参数了,在默认的配置文件中开启extension = xcache.so就可以了

5、安装Discuz论坛
  #注销中心主机
   shell> vim /etc/httpd/httpd.conf
   将如下内容注释:
    #DocumentRoot "/usr/local/apache/htdocs"
   将如下内容取消注释:
    Include /etc/httpd/extra/httpd-vhosts.conf
  #增加虚拟主机
   shell> vim /etc/httpd/extra/httpd-vhosts.conf
   修改为如下内容:
    <VirtualHost *:80>
      DocumentRoot "/www/discuz"
      ServerName www.spook.com
      ErrorLog "/var/log/discuz/error_log"
      CustomLog "/var/log/discuz/access_log" common

      <Directory "/www/discuz">
          Options none
          AllowOverride none
          Require all granted
      </directory>
   </VirtualHost>

  #创建资源目录
   shell> mkdir -pv /www/discuz
   shell> mkdir -pv /www/discuz/logs/
  #测试语法
   shell> httpd -t
  #安装discuz
   shell> unzip php-5.4.32.tar.bz2
   shell> cp -rf upload/* /www/discuz
   在浏览器输入地址www.spook.com根据提示操作即可.
 

四、搭建LAMP平台二 (使用fpm方式)
 1、apache、MySQL的安装与前一部分相同;请根据其进行安装;(我们这里不需要安装MYSQL)
   
 2、编译安装php-5.4.32
   2.1、解决依赖关系
   shell> rpm -ivh libmcrypt-2.5.7-5.el5.i386.rpm
   shell> rpm -ivh libmcrypt-devel-2.5.7-5.el5.i386.rpm
   shell> rpm -ivh mhash-0.9.2-6.el5.i386.rpm
   shell> rpm -ivh mhash-devel-0.9.2-6.el5.i386.rpm

   2.2、编译安装php-5.4.32
   shell> tar -xf php-5.4.32.tar.bz2
   shell> cd php-5.4.32
   shell> ./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
   说明:  1、如果使用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
           
            shell> ./configure --prefix=/usr/local/php  \
              --with-mysql=mysqlnd  \
              --with-openssl   \
              --with-mysqli=mysqlnd  \
              --with-pdo-mysql=mysqlnd  \
              --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

          2、--with-apxs2=/usr/local/apache/bin/apxs和-enable-maintainer-zts 是编译成以httpd模块结合PHP使用的配置选项
             --enable-fpm 是编译成以fastcgi结合PHP使用的配置选项
   shell> make
   shell> make intall

  2.3、为php提供配置文件:
   shell> cp php.ini-production /etc/php.ini

  2.4、配置php-fpm
   #为php-fpm提供Sysv init脚本,并将其添加至服务列表:
    shell> cp sapi/fpm/init.d.php-fpm  /etc/rc.d/init.d/php-fpm
    shell> chmod +x /etc/rc.d/init.d/php-fpm
    shell> chkconfig --add php-fpm
    shell> chkconfig php-fpm on
   #为php-fpm提供配置文件:
    shell> cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
   #编辑php-fpm的配置文件:
    shell> vim /usr/local/php/etc/php-fpm.conf
      配置fpm的相关选项为你所需要的值,并启用pid文件(如下最后一行):
      pm.max_children = 50
      pm.start_servers = 5
      pm.min_spare_servers = 2
      pm.max_spare_servers = 8
      pid = /usr/local/php/var/run/php-fpm.pid
      error_log = /var/log/php-fpm.log
   #启动php-fpm
    shell> service php-fpm start
   #使用如下命令来验正(如果此命令输出有中几个php-fpm进程就说明启动成功了):
    shell> ps aux | grep php-fpm
   #默认情况下,fpm监听在127.0.0.1的9000端口,也可以使用如下命令验正其是否已经监听在相应的套接字。
    shell> netstat -tnlp | grep php-fpm
    tcp    0     0 127.0.0.1:9000    0.0.0.0:*     LISTEN     689/php-fpm
 3、配置httpd-2.4.10
   3.1、启用httpd的相关模块
    #在Apache httpd 2.4以后已经专门有一个模块针对FastCGI的实现,此模块为mod_proxy_fcgi.so,
    #它其实是作为mod_proxy.so模块的扩充,因此,这两个模块都要加载
    shell> vim /etc/httpd/httpd.conf
     开启以下两行:
     LoadModule proxy_module modules/mod_proxy.so
     LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
   3.2 编辑apache配置文件httpd.conf,让apache能识别php格式的页面,并支持php格式的主页
     shell> 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

   3.3 配置虚拟主机支持使用fcgi
     #注销中心主机
      shell> vim /etc/httpd/httpd.conf
      将如下内容注释:
      #DocumentRoot "/usr/local/apache/htdocs"
      将如下内容取消注释:
      Include /etc/httpd/extra/httpd-vhosts.conf

     #配置虚拟主机
      shell> vim /etc/httpd/extra/httpd-vhosts.conf
      
      在相应的虚拟主机中添加类似如下两行。
       ProxyRequests Off
       ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/PATH/TO/DOCUMENT_ROOT/$1
      例如:
      <VirtualHost *:80>
          DocumentRoot "/www/discuz"
          ServerName www.spook.com
          ErrorLog "/var/log/discuz/error_log"
          CustomLog "/var/log/discuz/access_log" common
          ProxyRequests Off
          ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/www/discuz/$1
          <Directory "/www/discuz">
              Options none
              AllowOverride none
              Require all granted
          </Directory>
      </VirtualHost>

      ProxyRequests Off:关闭正向代理
      ProxyPassMatch:把以.php结尾的文件请求发送到php-fpm进程,php-fpm至少需要知道运行的目录和URI,
                    所以这里直接在fcgi://127.0.0.1:9000后指明了这两个参数,其它的参数的传递已经被
                    mod_proxy_fcgi.so进行了封装,不需要手动指定。
     #创建需要的目录
      shell> mkdir -pv /www/discuz
     #检查语法/
      shell> usr/local/apache/bin/httpd -t


   测试: 在一台此域内的主机在IE中输入www.spook.com,分别停止两台LAMP平台的httpd服务,它会显示不同的网页,简单的负载均衡已经实现.
     接下来我们实现分别停止两台LAMP平台的httpd服务,它会显示相同的网页(discuz论坛).


五、实现discuz论坛的负载均衡


 Ⅰ、实现discuz论坛全部代码的共享
  5.1 LAMP1(192.168.246.120)实现nfs功能
   shell> yum install portmap
   shell> yum nfs-utils nfs-utils-lib
   shell> service portmap status
   shell> service nfs start
   #增加nfs系统
   shell> vim /etc/exports
    添加如下内容:
     /www/discuz     192.168.246.0/24(rw)
   #导出新增的nfs系统
   shell> exportfs -rav

  5.2 LAMP2(192.168.246.130)使用nfs功能
   shell> yum install portmap
   shell> service portmap status
   # 查看可以挂载的nfs系统
   shell> showmount -e 192.168.246.120
  
# 挂载的nfs系统

   shell> mount -t nfs 192.168.246.120:/www/discuz  /www/discuz

   # 开机自动挂载

   shell> vim /etc/fstab
    增加内容:_rnetdev意思为如果挂载失败,会忽略此挂载选项

    192.168.246.129:/www/discuz  /www/discuz   nfs   defaults,_rnetdev 0 0


 Ⅱ、实现discuz论坛数据库数据的共享

  5.3 LAMP1(192.168.246.120)新增数据库用户并修改discuz的数据库连接文件
   mysql> create user 'discuz'@'%' identified by 'discuz';
   mysql> grant all privileges on ultrax.* to 'discuz'@'%';
   mysql> show grants for 'discuz'@'%';
   # 登录测试
   shell> mysql -udiscuz -pdiscuz -h192.168.246.120
   # 修改discuz的数据库连接文件
   shell> vim /www/discuz/config/config_global.php
     修改如下数据库连接信息内容:
     // ----------------------------  CONFIG DB  ----------------------------- //
      $_config['db']['1']['dbhost'] = '192.168.246.120';
      $_config['db']['1']['dbuser'] = 'discuz';
      $_config['db']['1']['dbpw'] = 'discuz';
   
   shell> vim /www/discuz/config/config_ucenter.php
     修改如下数据库连接信息内容:
    define('UC_DBHOST', '192.168.246.120');
    define('UC_DBUSER', 'discuz');
    define('UC_DBPW', 'discuz');


  5.4 LAMP2(192.168.246.130)不需要做任何的更新,因为使用的是nfs.

 Ⅲ、测试
  分别停止两台LAMP平台的httpd服务,如果它会显示相同的网页(discuz论坛),都可以互相评论文章,证明成功.

0 0
原创粉丝点击