当前位置: > CentOS > CentOS服务器 > 负载均衡 >

CentOS 6.5实现LAMP+NFS双web服务负载均衡

时间:2015-07-15 23:22来源:linux.it.net.cn 作者:IT网

环境说明:

CentOS 6.5最小化安装,安装相应的组件:

[root@http ~]# yum groupinstall -y"Development tools"

[root@mysql ~]# yum groupinstall -y"Server Platform Development"

 

准备三台主机:

httpa: 192.168.1.217  搭建web服务器 安装php

httpdb:192.168.1.218  搭建web服务器 安装php

mysql:192.168.1.1.219   搭建MysqlNFS服务器

 

主机C搭建NFS服务器共享/shared目录,主机AB安装webhttpd)服务并挂载主机C共享的NFS共享目录作为其DocumentRoot,实现通过在浏览器中输入主机A和主机BIP地址能够访问到相同的页面。

 

一、在192.168.1.219主机上搭建NFS服务器

 

1.安装NFS服务:

[root@mysql ~]# yum -y install nfs-utils rpcbind

nfs-utils :包括基本的NFS命令与监控程序

portmap redhat6中用这个rpcbind

 

2.建立共享目录,用户并授予权限:

[root@mysql ~]# mkdir /shared

[root@mysql ~]# ll -d /shared/

drwxr-xr-x. 2 root root 4096 Jul 13 22:19/shared/

[root@mysql ~]#

 

[root@mysql ~]# groupadd nfsuser

[root@mysql ~]# useradd -g nfsuser nfsuser

[root@mysql ~]# id nfsuser

uid=500(nfsuser) gid=500(nfsuser)groups=500(nfsuser)

[root@mysql ~]#

 

[root@mysql ~]# vim /etc/exports

/shared        192.168.1.0/24(rw,all_squash,anonuid=500,anongid=500)

"/etc/exports" 1L, 70Cwritten                                                          

[root@mysql ~]#

说明:

all_squash:将远程访问的所有普通用户及所属组都映射为匿名用户或用户组(nfsnobody);

anonuid=xxx:将远程访问的所有用户都映射为匿名用户,并指定该用户为本地用户(UID=xxx);

anongid=xxx:将远程访问的所有用户组都映射为匿名用户组账户,并指定该匿名用户组账户为本地用户组账户(GID=xxx);

 

[root@mysql ~]# setfacl -m u:nfsuser:rwx /shared/

[root@mysql ~]#

 

3.启动NFS服务:

[root@mysql ~]# chkconfig rpcbind on

[root@mysql ~]# chkconfig nfs on

[root@mysql ~]# service rpcbind start

Starting rpcbind:                                         [  OK  ]

[root@mysql ~]# service nfs start

Starting NFS services:                                     [  OK  ]

Starting NFS mountd:                                       [  OK  ]

Starting NFS daemon:                                       [  OK  ]

Starting RPC idmapd:                                       [  OK  ]

[root@mysql ~]#

[root@mysql ~]# showmount -e localhost

Export list for localhost:

/shared 192.168.1.0/24

[root@mysql ~]#

二、在192.168.1.219主机上搭建MySQL服务器

 

1、创建mysql用户和mysql

要想初始化mysql,需要有mysql用户和组。

[root@mysql ~]# groupadd -r -g 306 mysql

[root@mysql ~]# useradd -g 306 -r -u 306 mysql

[root@mysql ~]# id mysql

uid=306(mysql) gid=306(mysql)groups=306(mysql)

[root@mysql ~]#

 

2、创建mysql数据存放目录

这里练习就在根目录下创建/mydata/data目录:

[root@mysql ~]# mkdir -pv /mydata/data

mkdir: created directory `/mydata'

mkdir: created directory `/mydata/data'

[root@mysql ~]# ll -d /mydata/data/

drwxr-xr-x. 2 root root 4096 Jul 13 22:40/mydata/data/

[root@mysql ~]#

 

mysql数据目录,需要把属主属组更改为mysql,权限更改为0-rwx

[root@mysql ~]# cd /mydata/

[root@mysql mydata]# ll

total 4

drwxr-xr-x. 2 root root 4096 Jul 13 22:40data

[root@mysql mydata]# chown -R mysql:mysql data/

[root@mysql mydata]# chmod o-rwx data/

[root@mysql mydata]# ll

total 4

drwxr-x---. 2 mysql mysql 4096 Jul 13 22:40data

[root@mysql mydata]#

 

3、解压并安装MySQL 5.6.24,使用的通用二进制格式的MySQL

 

(1).通用二进制包是编译好的,直接解压就可以用,注意:mysql要求解压的包需在/usr/local目录下,并且目录名字必须是mysql

[root@mysql bao]# ll

total 304736

-rw-r--r--. 1 root root 312043744 Jul 1322:48 mysql-5.6.24-linux-glibc2.5-x86_64.tar.gz

[root@mysql bao]# tar -xfmysql-5.6.24-linux-glibc2.5-x86_64.tar.gz -C /usr/local/

[root@mysql bao]# cd /usr/local/

[root@mysql local]# ln -sv mysql-5.6.24-linux-glibc2.5-x86_64/mysql

`mysql' ->`mysql-5.6.24-linux-glibc2.5-x86_64/'

[root@mysql local]# ll mysql

lrwxrwxrwx. 1 root root 35 Jul 13 22:50mysql -> mysql-5.6.24-linux-glibc2.5-x86_64/

[root@mysql local]#

 

(2). 更改mysql目录中的文件的属主、属组为mysql用户和mysql组:

[root@mysql mysql]# pwd

/usr/local/mysql

[root@mysql mysql]# chown -R mysql:mysql *

[root@mysql mysql]# ll

total 172

drwxr-xr-x. 2 mysql mysql   4096 Jul 13 22:49bin

-rw-r--r--. 1 mysql mysql  17987 Mar 26 00:34COPYING

drwxr-xr-x. 3 mysql mysql   4096 Jul 13 22:49data

drwxr-xr-x. 2 mysql mysql   4096 Jul 13 22:49docs

drwxr-xr-x. 3 mysql mysql   4096 Jul 13 22:49include

-rw-r--r--. 1 mysql mysql 102986 Mar 26 00:35 INSTALL-BINARY

drwxr-xr-x. 3 mysql mysql   4096 Jul 13 22:49lib

drwxr-xr-x. 4 mysql mysql   4096 Jul 13 22:49man

drwxr-xr-x. 10 mysql mysql   4096 Jul 13 22:49 mysql-test

-rw-r--r--. 1 mysql mysql   2496 Mar 26 00:34README

drwxr-xr-x. 2 mysql mysql   4096 Jul 13 22:49scripts

drwxr-xr-x. 28 mysql mysql   4096 Jul 13 22:49 share

drwxr-xr-x. 4 mysql mysql   4096 Jul 13 22:49sql-bench

drwxr-xr-x. 2 mysql mysql   4096 Jul 13 22:49support-files

[root@mysql mysql]#

 

4、初始化mysql数据库:

执行scripts目录下的脚本,该文件作用:mysql需要初始化,这个步骤就是完成初始化的,我们需要手动完成。

[root@mysql mysql]# scripts/mysql_install_db --user=mysql --datadir=/mydata/data/

Installing MySQL systemtables..../bin/mysqld: error while loading shared libraries: libaio.so.1:cannot open shared object file: No such file or directory

[root@mysql mysql]#

[root@mysql mysql]# yum install -y libaio

[root@mysql mysql]# scripts/mysql_install_db --user=mysql--datadir=/mydata/data/

为了安全,需要把mysql目录下文件属主更改回root用户:

[root@mysql mysql]# chown -R root /usr/local/mysql/*

 

5、修改及配置MySQL服务脚本及配置文件

 

(1) 拷贝MySQL服务脚本:

[root@mysql ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

[root@mysql ~]# chmod +x /etc/init.d/mysqld

[root@mysql ~]# chkconfig --add mysqld

[root@mysql ~]# chkconfig mysqld on

[root@mysql ~]#

 

(2)修改mysql配置文件datadir数据目录位置:

[root@mysql ~]# vim /etc/my.cnf

datadir=/mydata/data

"/etc/my.cnf" 10L, 249Cwritten                                                         

[root@mysql ~]#

 

(3)启动mysql服务:

[root@mysql ~]# service mysqld start

Starting MySQL. SUCCESS!

[root@mysql ~]#

 

(4)mysql启动之后,会生成socket位置mysql.sock文件,修改其位置生成位置:

[root@mysql ~]# ll -d /var/lib/mysql/mysql.sock

srwxrwxrwx. 1 mysql mysql 0 Jul 13 22:59/var/lib/mysql/mysql.sock

[root@mysql ~]# vim /etc/my.cnf

socket=/tmp/mysql.sock

"/etc/my.cnf" 10L, 239Cwritten                                                         

[root@mysql ~]#

 

6、为mysql添加PATH环境变量

[root@make mysql]# vi /etc/profile.d/mysqld.sh

export PATH=$PATH:/usr/local/mysql/bin

"/etc/profile.d/mysqld.sh" [New]1L, 39C written

[root@make mysql]# . /etc/profile.d/mysqld.sh

[root@make mysql]# echo $PATH

/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/apache/bin:/usr/local/mysql/bin

[root@make mysql]#

 

7、为mysql添加man帮助及输出头文件和库文件

[root@make mysql]# vi /etc/man.config

49 MANPATH /usr/local/mysql/man

"/etc/man.config" 154L, 4999Cwritten

[root@make mysql]#

 

[root@make mysql]# ln -sv /usr/local/mysql/include//usr/include/mysql

`/usr/include/mysql' ->`/usr/local/mysql/include/'

[root@make mysql]#

[root@make mysql]# vi /etc/ld.so.conf.d/mysql.conf

/usr/local/mysql/lib

"/etc/ld.so.conf.d/mysql.conf"[New] 1L, 21C written

[root@make mysql]# ldconfig -v

 

8、为mysql添加密码:

[root@make ~]# mysqladmin -u root password '123456'

Warning: Using a password on the commandline interface can be insecure.

[root@make ~]#

说明:

mysql-5.6会提示命令行使用密码不安全的。

 

至此,mysql安装完毕

三、在192.168.1.217192.168.1.218主机上安装httpdPHP

[root@httpa bao]# ll

total 20452

-rw-r--r--. 1 root root   826885 Jul 13 23:06 apr-1.5.2.tar.bz2

-rw-r--r--. 1 root root   694427 Jul 13 23:07 apr-util-1.5.4.tar.bz2

-rw-r--r--. 1 root root  5054838 Jul 13 23:07 httpd-2.4.12.tar.bz2

-rw-r--r--. 1 root root   634589 Jul 13 23:07 libmcrypt-2.5.6.tar.gz

-rw-r--r--. 1 root root 13724681 Jul 1323:07 php-5.6.8.tar.bz2

[root@httpa bao]#

 

1、在安装httpd之前需要先安装aprapr-util以及pcre-devel等相关包。在编译安装httpd之前需要Development Tools开发工具集。具体步骤如下:

 

(1)安装apr

[root@httpa bao]# tar xf apr-1.5.2.tar.bz2

[root@httpa bao]# cd apr-1.5.2

[root@httpa apr-1.5.2]# ./configure--prefix=/usr/local/apr

[root@httpa apr-1.5.2]# make

[root@httpa apr-1.5.2]# make install

 

(2)安装apr-util

[root@httpa bao]# tar xf apr-util-1.5.4.tar.bz2

[root@httpa bao]# cd apr-util-1.5.4

[root@httpa apr-util-1.5.4]# ./configure--prefix=/usr/local/apr-util --with-apr=/usr/local/apr

[root@httpa apr-util-1.5.4]# make

[root@httpa apr-util-1.5.4]# make install

 

(3)安装pcre-devel

[root@httpa ~]# yum install -y pcre-devel

 

(4)安装httpd

[root@httpa bao]# mv apr-1.5.2 apr

[root@httpa bao]# mv apr-util-1.5.4 apr-util

[root@httpa bao]# tar xf httpd-2.4.12.tar.bz2

[root@httpa bao]# cp -r apr apr-util/root/bao/httpd-2.4.12/srclib/

[root@httpa httpd-2.4.12]# ./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 --with-included-apr

[root@httpa httpd-2.4.12]# make

[root@httpa httpd-2.4.12]# make install

说明:

--prefix=/usr/local/apache :安装指定路径。

--sysconfdir=/etc/httpd :指定配置文件安装路径。

--enable-so :支持动态共享模块,如果没有此功能,php无法与apache一起工作,必须安。

--enable-ssl :启用ssl功能,不安装无法启用https

--enable-rewrite :支持url重写。

--enable-cgi :支持cgi

 

2、启动服务进行简单测试

[root@httpa ~]# /usr/local/apache/bin/apachectl start

[root@httpa ~]# setenforce 0

[root@httpa ~]# service iptables stop

iptables: Setting chains to policy ACCEPT:filter          [  OK  ]

iptables: Flushing firewall rules:                         [  OK  ]

iptables: Unloading modules:                               [ OK  ]

[root@httpa ~]#

[root@httpa ~]# netstat -lntp

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address               Foreign Address             State      

tcp       0      0 :::80            :::*         LISTEN      51267/httpd        

[root@httpa ~]#

网页192.168.217显示It works!

 

3、为了更好的管理和使用httpd服务,可为其提供PATH环境变量和SysV风格的脚本。

 

(1) 提供PATH环境变量:

[root@httpa ~]# vim /etc/profile.d/httpd.sh

export PATH=$PATH:/usr/local/apache/bin

"/etc/profile.d/httpd.sh" [New]1L, 40C written                                         

[root@httpa ~]# . /etc/profile.d/httpd.sh

[root@httpa ~]#

 

(2) 提供SysV风格的脚本:

[root@httpa ~]# vim /etc/init.d/httpd

#!/bin/bash

#

# chkconfig: - 85 15

# description: Apache is a World Wide Webserver. It is used to serve \

. /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 swallowingup a pass-phrase prompt if

# mod_ssl needs a pass-phrase from theuser.

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 mays not

# work correctly with a thread-based MPM;notably PHP will refuse to start.

# Path to the apachectl script, serverbinary, 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 sysntax 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|htlp|configtest|fullstatus)

       $apachectl $@

       RETVAL=$?

       ;;

  *)

       echo "Usage: $prog {start|stop|restart|reload|status|help}"

       exit 1

       ;;

esac

exit $RETVAL

"/etc/rc.d/init.d/httpd" [New]85L, 2084C written                                       

[root@httpa ~]# chmod +x /etc/init.d/httpd

[root@httpa ~]# chkconfig --add httpd

[root@httpa ~]# chkconfig httpd on

[root@httpa ~]#

 

4、更换pid进程文件存放目录:

每个进程启动后都会有个pid文件,编译安装httpdpid文件在logs目录里面,系统进程一般pid文件都放在/var/run目录下,如果想更换pid进程文件存放目录,需要在主配置文件中加入PidFile  "/var/run/httpd.pid"项,注意:编译安装默认没有该项,需要手工加入:

[root@httpa ~]# vim /etc/httpd/httpd.conf

34 pidfile "/var/run/httpd.pid"

[root@httpa ~]# netstat -lntp             

Active Internet connections (only servers)

Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name  

tcp       0      0 :::80          :::*           LISTEN      51563/httpd        

[root@httpa ~]# kill 51563

[root@httpa ~]# service httpd start

[root@httpa ~]# ll -d /var/run/httpd.pid

-rw-r--r--. 1 root root 6 Jul 13 23:43/var/run/httpd.pid

[root@httpa ~]#

 

5、为httpd添加man帮助及输出头文件:

 

(1) httpd添加man帮助:

[root@http ~]# yum install -y man pages   (最小化安装系统如果没有man手册需要安装)

[root@http ~]# vim /etc/man.config

48 MANPATH /usr/local/apache/man

[root@http ~]# man httpd

 

(2) 输出头文件:

[root@http ~]# ln -sv /usr/local/apache/include//usr/include/httpd

`/usr/include/httpd' ->`/usr/local/apache/include/'

[root@http ~]#

 

至此,httpd服务安装成功。

四、安装PHP

 

1、安装依赖包,解压并编译安装php-5.6.8

[root@httpa ~]# yum install libxml2-devel -y

[root@httpa ~]# yum install bzip2-devel -y

 

[root@httpa bao]# tar xf libmcrypt-2.5.6.tar.gz

[root@httpa bao]# cd libmcrypt-2.5.6

[root@httpa libmcrypt-2.5.6]# ./configure

[root@httpa libmcrypt-2.5.6]# make &&make install

 

[root@httpa bao]# tar xf php-5.6.8.tar.bz2

[root@httpa bao]# cd php-5.6.8

[root@httpa php-5.6.8]# ./configure --prefix=/usr/local/php--with-mysql=mysqlnd --with-openssl --with-mysqli=mysqlnd --enable-mbstring--with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib--with-libxml-dir=/usr --enable-xml --enable-sockets --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d--with-bz2 --enable-maintainer-zts --with-apxs2=/usr/local/apache/bin/apxs-with-pdo-mysql=mysqlnd -with-freetype-dir

[root@httpa php-5.6.8]# make

[root@httpa php-5.6.8]# make install

 

2、给php提供配置文件:

php安装程序中,有两个文件叫php.ini-developmentphp.ini-production

php.ini-development :开发用的配置文件。

php.ini-production :生产环境用的配置文件。

 

[root@httpa ~]# cp /root/bao/php-5.6.8/php.ini-production/etc/php.ini

[root@httpa ~]# ll -d /etc/php.ini

-rw-r--r--. 1 root root 72369 Jul 14 00:03/etc/php.ini

[root@httpa ~]#

 

3、配置httpd,使其能够支持php

 

(1) 首先能够让apache能够处理php结尾的页面文件,添加php类型:

[root@httpa ~]# vim /etc/httpd/httpd.conf

381    AddType application/x-httpd-php .php

382    AddType application/x-httpd-php-source .phps

 

 (2) 支持php网页:

251 <IfModule dir_module>

252    DirectoryIndex index.php index.html

253 </IfModule>

 

4、重启httpd服务,测试PHP网页:

[root@httpa ~]# vim /usr/local/apache/htdocs/index.php

<?php

phpinfo();

?>

"/usr/local/apache/htdocs/index.php"[New] 4L, 21C written                              

[root@httpa ~]# service httpd restart

Stopping httpd:                                           [  OK  ]

Starting httpd:                                            [  OK  ]

[root@httpa ~]#

浏览器测试是否出现PHP网页!

 

 

192.168.1.218主机上安装apachephp,和192.168.1.217安装一样!

五、配置安装Discuz并验证

 

1.分别在192.168.1.217192.168.1.218两台主机上挂载nfs共享目录:

[root@httpa ~]# mount -t nfs 192.168.1.219:/shared /usr/local/apache/htdocs/

mount: wrong fs type, bad option, badsuperblock on 192.168.1.219:/shared,

      missing codepage or helper program, or other error

      (for several filesystems (e.g. nfs, cifs) you might

      need a /sbin/mount.<type> helper program)

      In some cases useful info is found in syslog - try

      dmesg | tail  or so

[root@httpa ~]#

需要安装nfs-utils

[root@httpa ~]# yum install -y nfs-utils

[root@httpa ~]# mount -t nfs 192.168.1.219:/shared/usr/local/apache/htdocs/

[root@httpa ~]# mount

/dev/mapper/vg_httpa-lv_root on / type ext4(rw)

proc on /proc type proc (rw)

sysfs on /sys type sysfs (rw)

devpts on /dev/pts type devpts(rw,gid=5,mode=620)

tmpfs on /dev/shm type tmpfs(rw,rootcontext="system_u:object_r:tmpfs_t:s0")

/dev/sda1 on /boot type ext4 (rw)

none on /proc/sys/fs/binfmt_misc typebinfmt_misc (rw)

192.168.1.219:/shared on/usr/local/apache/htdocs type nfs(rw,vers=4,addr=192.168.1.219,clientaddr=192.168.1.217)

[root@httpa ~]#

 

2.192.168.1.219主机上登陆mysql,建立Discunz数据库:

mysql> create database duz;

Query OK, 1 row affected (0.00 sec)

mysql>

 

授权两台web服务器可以连接数据库:

mysql> use mysql;

Reading table information for completion oftable and column names

You can turn off this feature to get aquicker startup with -A

Database changed

mysql> grant all privileges on *.* to'root'@'192.168.1.217' identified by '123456';

Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on *.* to'root'@'192.168.1.218' identified by '123456';

Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

mysql>

 

3.192.168.1.219主机上解压Discunznfs共享目录/shared中:

[root@mysql bao]# ll

total 316940

-rw-r--r--. 1 root root  12490656 Jul 14 00:52Discuz_X3.2_SC_UTF8.zip

[root@mysql bao]# unzip Discuz_X3.2_SC_UTF8.zip

[root@mysql bao]# mkdir /shared/bbs

[root@mysql bao]# cp -r upload/* /shared/bbs/

[root@mysql bao]# cd /shared/bbs/

[root@mysql bbs]# chmod -R 777 *

 

4.浏览器192.168.1.217/bbs安装:

数据库服务器:192.168.1.219

 

此时:192.168.1.217/bbs192.168.1.218/bbs网页是一样的!



(责任编辑:IT)
------分隔线----------------------------