首页 > 代码库 > web集群综合项目keepalived+lvs+lamp+nfs+mysql

web集群综合项目keepalived+lvs+lamp+nfs+mysql

web集群综合项目

实验描述:

  在此项目中,为了使公司内部的web站点提供更稳定的服务,搭建负载均衡群集以及故障转移群集,具体拓扑环境如下;

                       技术分享    

实验环境:

主机

Ip地址

软件

主调度器

192.168.100.150

kernel-devel openssl-devel popt-devel  ipvsadm keepalived-1.2.13.tar.gz

从调度器

192.168.100.151

kernel-devel openssl-devel popt-devel  ipvsadm keepalived-1.2.13.tar.gz

Web站点1

192.168.100.152

Lamp平台

Web站点2

192.168.100.153

Lamp平台

Web站点3

192.168.100.154

Lamp平台

Nfs服务器1

192.168.100.155

nfs-utils rpcbind kernel-devel  openssl-devel popt-devel  ipvsadm keepalived-1.2.13.tar.gz

Nfs服务器2

192.168.100.156

nfs-utils rpcbind kernel-devel  openssl-devel popt-devel  ipvsadm keepalived-1.2.13.tar.gz

amoeba代理服务器

192.168.100.157

amoeba-mysql-binary-2.2.0.tar.gz jdk-6u14-linux-x64.bin

数据库master节点

192.168.100.158

Lamp平台

数据库slave1节点

192.168.100.159

Lamp平台

数据库slave2节点

192.168.100.160

Lamp平台

实验思路:

  此项目中,前端搭建了lvs网站负载群集,通过lvs来完成网站的节点健康检查和负载,为了保证lvs调度器的稳定,搭建keepalived完成调度器的双机热备,web站点的存储连接着后端的nfs服务器,使用aotufs网络挂载的方式,在nfs共享存储服务器中同样也搭建了keepalived双机热备,同时也保证了nfs存储的稳定,另一端是web站点连接的mysql数据库,数据库采用主从复制、读写分离的方案,另外通过代理服务器amoeba提供到web站点进行服务,这里的web服务器部署了lamp平台,动静页面及于一身的server

实验步骤:

部署后端数据库

搭建时间服务器:

主节点上安装ntp时间服务:192.168.100.158

yum -y install ntp

sed -i ‘/^server/s/^/#/g‘ /etc/ntp.conf

cat <<END >>/etc/ntp.conf

server 127.127.1.0

fudge 127.127.1.0 stratum 8

END

/etc/init.d/ntpd restart

chkconfig ntpd on

netstat -utpln |grep ntp

从节点同步时间:192.168.100.159-160

yum -y install ntpdate

/usr/sbin/ntpdate 192.168.100.158

 

安装mysql192.168.100.158-160

wget ftp://ftp.linuxfan.cn/tools/lamp_install_publis-app-2015-07-16.tar.xz

tar Jxvflamp_install_publis-app-2015-07-16.tar.xz

cd bin/

./ mysql_install.sh

./mysql_config.sh

 

配置MySQL主从复制:

1)主服务器配置:192.168.100.158

sed -i‘s/^log-bin=.*/log-bin=master-bin\nlog-slave-updates=ture/g‘ /etc/my.cnf  ##更改配置文件中的某个部分

sed -i ‘/^server-id/s/1/11/g‘/etc/my.cnf    ##更改server id 11 ,此为服务器的id号,在数据库中是唯一的。

/etc/init.d/mysqld restart    ##重新启动mysql服务

 

yum -y install mysql

mysql -uroot -p123123     ##进入数据库

mysql> grant replication slave on *.* to‘myslave‘@‘192.168.100.%‘ identified by ‘123123‘;     ##进入数据库后进行授权访问

mysql> flush privileges;   ##刷新授权的权限

mysql> show master status;  ##记住File的及Position的值,此处为master-bin.000001337  ,需要在从服务器上用到

mysql> create database db_test;  ##创建文件测试数据库

mysql> quit

 

配置从服务器1192.168.100.159

sed -i ‘/^server-id/s/1/22/g‘/etc/my.cnf    ##更改server  id22

sed -i‘/^server-id/arelay-log=relay-log-bin\nrelay-log-index=slave-relay-bin.index‘/etc/my.cnf    ##替换mysql配置文件,

/etc/init.d/mysqld restart   ##重启mysql服务

yum -y install mysql

mysql -uroot -p123123   ##登陆数据库

mysql> change master tomaster_host=‘192.168.100.158‘,master_user=‘myslave‘,master_password=‘123123‘,master_log_file=‘master-bin.000001‘,master_log_pos=339;             ##进行指定mysql的服务器的master-bin,注意在指定master的时候是stop        slave的状态下,不能再开启的状态下指定。

mysql> start slave;     ##启动slave

mysql> show slave status\G;  ##查看无error即可,并且两个runningyes

mysql> show databases;  ##验证数据库是否同步,验证在主服务器上登陆数据库创建的文件是否同步

mysql> quit          ##退出mysql数据库

 

配置从服务器2192.168.100.160

sed -i ‘/^server-id/s/1/33/g‘ /etc/my.cnf

sed -i‘/^server-id/arelay-log=relay-log-bin\nrelay-log-index=slave-relay-bin.index‘/etc/my.cnf

/etc/init.d/mysqld restart

yum -y install mysql

mysql -uroot -p123123

mysql> change master to master_host=‘192.168.100.158‘,master_user=‘myslave‘,master_password=‘123123‘,master_log_file=‘master-bin.000001‘,master_log_pos=339;

mysql> start slave;

mysql> show slave status\G;  ##查看error=0即可

mysql> show databases;  ##验证数据库是否同步

mysql> quit

注:两个从服务器的配置基本相同,指定的server id 不能相同。

 

搭建MySQL读写分离:

1)安装软件:192.168.100.157

lftp ftp.linuxfan.cn

>cd tools/

> get amoeba-mysql-binary-2.2.0.tar.gz jdk-6u14-linux-x64.bin

>bye

yum -y remove java

chmod +x jdk-6u14-linux-x64.bin

./jdk-6u14-linux-x64.bin

mv jdk1.6.0_14/ /usr/local/jdk1.6

vi /etc/profile

export JAVA_HOME=/usr/local/jdk1.6

exportCLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib

exportPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/bin/:$PATH:$HOME/bin

export AMOEBA_HOME=/usr/local/amoeba

export PATH=$PATH:$AMOEBA_HOME

:wq

source /etc/profile

java -version

mkdir /usr/local/amoeba

tar zxvf amoeba-mysql-binary-2.2.0.tar.gz-C /usr/local/amoeba/

chmod -R 755 /usr/local/amoeba/

/usr/local/amoeba/bin/amoeba  ##验证

2)数据授权给amoeba读写权限:

mysql> grant all on *.* to linuxfan@‘192.168.100.%‘identified by ‘123123‘;   ##192.168.100.158完成

mysql> show grants forlinuxfan@‘192.168.100.%‘;   ##192.168.100.159-160同步了权限

3)代理服务器修改配置文件:192.168.100.157

vim /usr/local/amoeba/conf/amoeba.xml

 30                                        <property name="user">amoeba</property>

 31

 32                                        <property name="password">123456</property>

115                 <propertyname="defaultPool">master</property>

116

117                <propertyname="writePool">master</property>   ##注意删除<!--  -->的注释

118                 <propertyname="readPool">slaves</property>

:set nu ##显示行号

:wq

 

vim /usr/local/amoeba/conf/dbServers.xml

 25                         <!-- mysql user-->

 26                         <propertyname="user">linuxfan</property>   ##该用户必须是上一步授权的用户

 27                        

 28                         <!--  mysql password -->   ##修改

 29                         <propertyname="password">123123</property>  ##删除下一行的“-->

 44        <dbServer name="master" parent="abstractServer">  ##修改为master

 45                 <factoryConfig>

 46                         <!-- mysql ip-->

 47                         <propertyname="ipAddress">192.168.100.158</property>  ##指定正确的masterip

 51        <dbServer name="slave1" parent="abstractServer"> ##修改为slave1

 52                 <factoryConfig>

 53                         <!-- mysql ip-->

 54                         <propertyname="ipAddress">192.168.100.159</property>  ##指定slave1ip地址

 55                 </factoryConfig>

 56        </dbServer>

 57        <dbServer name="slave2" parent="abstractServer"> ##添加如下6行,指定slave2ip

 58                 <factoryConfig>

 59                         <!-- mysql ip-->

 60                        <propertyname="ipAddress">192.168.100.160</property>

 61                 </factoryConfig>

 62        </dbServer>

 64        <dbServer name="slaves" virtual="true">  ##修改为slaves

 70                         <propertyname="poolNames">slave1,slave2</property>  ##修改集群的成员名称用逗号隔开

:wq

/usr/local/amoeba/bin/amoebastart&   ##后台启动代理服务

netstat -utpln |grep 8066  ##验证

 

 

搭建存储nfs服务器;192.168.100.155-156

1.安装nfs-utilsrcpbind软件包  192.168.100.155-156

[root@localhost ~]# yum -y installnfs-utils rpcbind

[root@localhost ~]# for i in rpcbind nfs;dochkconfig $i on; done   ##设置为开机启动

2.设置共享目录

[root@localhost ~]# mkdir /opt/wwwroot

[root@localhost ~]# chmod 755/opt/wwwroot  ##设置权限

vi /etc/exports

/opt/wwwroot          192.168.100.0/24(rw,sync,no_root_squash)

3.启动nfs服务程序

[root@localhost ~]# /etc/init.d/rpcbindrestart

[root@localhost ~]# /etc/init.d/nfs restart

                                      chkconfig rpcbind on

                                      chkconfig nfs on

[root@localhost ~]# netstat -utpnl |greprpcbind

[root@localhost ~]# showmount -e ##查看本机共享的目录

 

搭建NFS服务器的keepalived双机热备;

1.安装keepalived(主从上都需要安装)192.168.100.155-156

yum -y install kernel-devel openssl-develpopt-devel  ipvsadm

lftp ftp.linuxfan.cn

lftp ftp.linuxfan.cn:/tools> getkeepalived-1.2.13.tar.gz

tar -zxvf keepalived-1.2.13.tar.gz -C/usr/src/

cd /usr/src/keepalived-1.2.13/

./configure --prefix=/--with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/

make &&make install

chkconfig --add keepalived

chkconfig keepalived on

 

2.配置keepalived;

主配置:(192.168.100.155上操作)

cd /etc/keepalived/

mv keepalived.conf keepalived.conf.bak

vi /etc/keepalived/keepalived.conf

global_defs {

   router_id HA_TEST_R1    ##本服务器的名称,若环境中有多个         keepalived时,此名称不能一致

}

vrrp_instance VI_1 {                  ##定义VRRP热备实例,每一个keep组都不同

   state MASTER          ##MASTER表示主服务器

   interface eth0           ##承载VIP地址的物理接口

   virtual_router_id 1           ##虚拟路由器的ID号,每一个keep组都不同

   priority 100                ##优先级,数值越大优先级越高

   advert_int 1              ##通告间隔秒数(心跳频率)

   authentication {                 ##认证信息

       auth_type PASS                  ##认证类型

       auth_pass 123456   ##密码字串

    }

   virtual_ipaddress {

 192.168.100.95            ##指定漂移地址(VIP

    }

}

 

从配置:(192.168.100.156上操作)

cd /etc/keepalived/

mv keepalived.conf keepalived.conf.bak

vi /etc/keepalived/keepalived.conf

global_defs {

   router_id HA_TEST_R2    ##本服务器的名称

}

vrrp_instance VI_1 {

   state BACKUP                    ##SLAVE表示从服务器

   interface eth0

   virtual_router_id 1

   priority 99                           ##优先级,低于主服务器

   advert_int 1

   authentication {

       auth_type PASS

       auth_pass 123456

    }

   virtual_ipaddress {

       192.168.100.95

    }

}

 

3.启动服务及应用服务:192.168.100.155-156

1)启动keepalived

/etc/init.d/keepalived start

ip addr show dev eth0      ##验证vip是否在主服务器上

 

4.配置rsync实现两台nfs服务器的目录实时同步;192.168.100.155-156

备份源:192.168.100.156

yum -y install rsync      ##确认安装rsync软件

发起端:192.168.100.155

ssh-keygen -t  rsa                 ##创建以密钥对加密的方式,远程时不用密码

ssh-copy-id root@192.168.100.156    ##将公钥发送给备份源

ssh root@192.168.100.156     ##此时远程将不需要密码

lftp ftp.linuxfan.cn

cd /tools

get inotify-tools-3.14.tar.gz

tar zxvf  inotify-tools-3.14.tar.gz   -C /usr/src/    ##解压inotify

cd /usr/src/ inotify-tools-3.14/

./configure &&make &&amake install

vi /root/bin/rsync.sh

#!/bin/bash

rsync -avzHP /opt/wwwroot/ root@192.168.100.156:/opt/wwwroot/--delete

echo "30 0 * * 6" >/var/spool/cron/root

:wq

chmod +x  /root/bin/rsync.sh

/root/bin/rsync.sh

配置前端调度器keepalived+lvs

安装keepalived(在两台调度服务器上安装,192.168.100.150192.168.100.151

yum -y install kernel-devel openssl-develpopt-devel ipvsadm

lftp ftp.linuxfan.cn

cd /tools/

get keepalived-1.2.13.tar.gz

bye

tar zxvf keepalived-1.2.13.tar.gz -C/usr/src/

cd /usr/src/keepalived-1.2.13/

./configure --prefix=/--with-kernel-dir=/usr/src/kernels/2.6.32-431.el6.x86_64/

make &&make install

cd

[root@localhost ~]# ls /etc/keepalived/

keepalived.conf  samples

chkconfig --add keepalived

chkconfig keepalived on

 

2.配置调度器(主:192.168.100.150,备:192.168.100.151,漂移ip192.168.100.95web服务器池(节点1:192.168.100.152、节点2:192.168.100.153、节点3192.168.100.154

1)、配置主HA调度服务器

cd /etc/keepalived/

mv keepalived.conf keepalived.conf.bak

vi /etc/keepalived/keepalived.conf

global_defs {

   router_id HA_TEST_R3    ##本服务器的名称

}

vrrp_instance VI_2 {                  ##定义VRRP热备实例

   state MASTER          ##MASTER表示主服务器

   interface eth0           ##承载VIP地址的物理接口

   virtual_router_id 2           ##虚拟路由器的ID

   priority 100                ##优先级,数值越大优先级越高

   advert_int 1              ##通告间隔秒数(心跳频率)

   authentication {                 ##认证信息

       auth_type PASS                  ##认证类型

       auth_pass 123456   ##密码字串

    }

   virtual_ipaddress {

 192.168.100.95            ##指定漂移地址(VIP

    }

virtual_server 192.168.100.95 80 {

       delay_loop 15

       lb_algo rr

       lb_kind DR

       protocol TCP

real_server 192.168.100.152 80 {

       weight 1

       TCP_CHECK {

                connect_port 80

                connect_timeout 3

                nb_get_retry 3

                delay_before_retry 4

                }

       }

real_server 192.168.100.153 80 {

       weight 1

       TCP_CHECK {

                connect_port 80

                connect_timeout 3

                nb_get_retry 3

                delay_before_retry 4

                }

       }

real_server 192.168.100.154 80 {

       weight 1

       TCP_CHECK {

                connect_port 80

                connect_timeout 3

                nb_get_retry 3

                delay_before_retry 4

                }

       }

}

:wq

modprobe ip_vs

lsmod |grep ip_vs

echo "modprobe ip_vs">>/etc/rc.local

/etc/init.d/keepalived restart

chkconfig ipvsadm off

ip addr show dev eth0

2)、配置HA从调度服务器

cd /etc/keepalived/

mv keepalived.conf keepalived.conf.bak

vi /etc/keepalived/keepalived.conf

global_defs {

   router_id HA_TEST_R4    ##本服务器的名称

}

vrrp_instance VI_2 {                  ##定义VRRP热备实例

   state BACKUP           ##BACKUP表示主服务器

   interface eth0           ##承载VIP地址的物理接口

   virtual_router_id 2           ##虚拟路由器的ID

   priority 99                  ##优先级,数值越大优先级越高

   advert_int 1              ##通告间隔秒数(心跳频率)

   authentication {                 ##认证信息

       auth_type PASS                  ##认证类型

       auth_pass 123456   ##密码字串

    }

   virtual_ipaddress {

 192.168.100.95            ##指定漂移地址(VIP

    }

virtual_server 192.168.100.95 80 {

       delay_loop 15

       lb_algo rr

       lb_kind DR

       protocol TCP

real_server 192.168.100.152 80 {

       weight 1

       TCP_CHECK {

                connect_port 80

                connect_timeout 3

                nb_get_retry 3

                delay_before_retry 4

                }

       }

real_server 192.168.100.153 80 {

       weight 1

       TCP_CHECK {

                connect_port 80

                connect_timeout 3

                nb_get_retry 3

                delay_before_retry 4

                }

       }

real_server 192.168.100.154 80 {

       weight 1

       TCP_CHECK {

                connect_port 80

                connect_timeout 3

                nb_get_retry 3

                delay_before_retry 4

                }

       }

}

:wq

 

modprobe ip_vs

lsmod |grep ip_vs

echo "modprobe ip_vs">>/etc/rc.local

/etc/init.d/keepalived restart

chkconfig ipvsadm off

ip addr show dev eth0

######设置防火墙和selinux(此处为测试)#########

iptables -F

setenforce 0

 

配置Web节点服务器(lvsDR工作模式的配置,分别在节点服务器上做如下设置,可以使用脚本)

wgetftp://ftp.linuxfan.cn/tools/lamp_install_publis-app-2015-07-16.tar.xz

tar Jxvflamp_install_publis-app-2015-07-16.tar.xz

cd bin/

./apache_install.sh&&mysql_install.sh &&php_install.sh

./php_config.sh &&mysql_config.sh&&lamp_config.sh

/etc/init.d/mysqld start

mysql -uroot -p123123

/etc/init.d/mysqld stop

ln -s /usr/local/httpd/bin/*/usr/local/bin/   ##优化执行命令的路径

cp /usr/local/httpd/bin/apachectl/etc/init.d/httpd

vim /etc/init.d/httpd    ##在开始位置修改bash和添加chkconfigdescription;修改第82行实现执行命令时友好提示

1 #!/bin/bash        ##声明shellbash

2 # chkconfig: 35 85 15     ##35运行级别开机启动,开机启动顺序为85,关机关闭顺序为15

3 # description: A Scripts for apache httpddeamon!  

82 $HTTPD -k $ARGV  &&echo "httpd is $ARGVcomplete."      ##82

:wq

ls -l /etc/init.d/httpd   ##确认文件有执行权限,如果没有使用命令“chmod+x /etc/init.d/httpd”授权

chkconfig --add httpd

chkconfig httpd on

 

 

以下部分可用脚本:

cd /etc/sysconfig/network-scripts/

cp ifcfg-lo ifcfg-lo:0

vi ifcfg-lo:0

DEVICE=lo:0

IPADDR=192.168.100.95

NETMASK=255.255.255.255

ONBOOT=yes

:wq

/etc/init.d/network restart

echo "route add -host 192.168.100.95dev lo:0" >>/etc/rc.local

route add -host 192.168.100.95 dev lo:0

ip addr show dev lo

 

vi /etc/sysctl.conf

net.ipv4.conf.all.arp_ignore = 1

net.ipv4.conf.all.arp_announce = 2

net.ipv4.conf.default.arp_ignore = 1

net.ipv4.conf.default.arp_announce = 2

net.ipv4.conf.lo.arp_ignore = 1

net.ipv4.conf.lo.arp_announce = 2

 

sysctl -p

/usr/local/httpd/bin/apachectl start

iptables -F

setenforce 0

 

以上部分可以用脚本

yum -y install autofs

yum -y install nfs-utils rpcbind

vi /etc/auto.master

/usr/local/httpd/htdocs  /etc/auto.nfs

vi /etc/auto.nfs

linuxfan -rw 192.168.100.195:/opt/wwwroot

/etc/init.d/autofs restart

chkconfig autofs on

cd /usr/local/httpd/htdocs/linuxfan

ls

 

脚本:

#!/bin/bash

##by linuxfan 2016-10-31

#################set lo:0############

IF_LO="/etc/sysconfig/network-scripts/ifcfg-lo:0"

VIP=192.168.100.95

cat <<END >>$IF_LO

DEVICE=lo:0

IPADDR=$VIP        

NETMASK=255.255.255.255

ONBOOT=yes

END

if [ -e $IF_LO ];then

/etc/init.d/network restart

ip addr show dev lo:0

fi

###############change kernel args##########

cat <<END >> /etc/sysctl.conf

net.ipv4.conf.all.arp_ignore = 1

net.ipv4.conf.all.arp_announce = 2

net.ipv4.conf.default.arp_ignore = 1

net.ipv4.conf.default.arp_announce = 2

net.ipv4.conf.lo.arp_ignore = 1

net.ipv4.conf.lo.arp_announce = 2

END

grep arp /etc/sysctl.conf

if [ $? -eq 0 ];then

sysctl -p

fi

##################set route#############

echo "route add -host $VIP devlo:0" >>/etc/rc.local

source /etc/rc.local

route add -host $VIP dev lo:0

ip r |grep $VIP

#################check iptables &selinux ###

/etc/init.d/iptables stop

setenforce 0

部署web项目:

一:发布论坛discuz

1.配置数据库:在master上进行操作192.168.100.158

mysql -uroot -p123123

mysql> create database bbsdb;

mysql> grant all on bbsdb.* to‘runbbs‘@‘localhost‘ identified by ‘123123‘;

mysql> flush privileges;

mysql> quit

2.下载discuz并发布:在NFS服务器中192.168.100.155

lftp ftp.linuxfan.cn

cd /tools

get discuz_7.2_full_sc_utf8.zip

bye

mkdir /opt/wwwroot/bbs

unzip discuz_7.2_full_sc_utf8.zip -ddiscuz  ##解压

cp -rf discuz/upload/* /opt/wwwroot/bbs

cd /opt/wwwroot/bbs/

chown daemon forumdata/ attachments/uc_client/data/cache/ templates/ config.inc.php -R

3.访问安装:

http://www.linuxfan.cn /bbs /install/

技术分享

技术分享

技术分享

技术分享


mysql 数据库中可以看到该项目的写入记录;

技术分享

4.安装完成后的处理:

mv install/ install.lock

chmod 600 install.lock/

 

二:发布wordpress博客系统:

1.配置数据库:

mysql> create database wpdb;

mysql> grant all on wpdb.* to‘wpadm‘@‘localhost‘ identified by ‘123123‘;

mysql> quit

2.下载wordpress,并发布:

wgethttps://cn.wordpress.org/wordpress-4.4.1-zh_CN.zip

unzip wordpress-4.4.1-zh_CN.zip

cp wordpress/usr/local/httpd/htdocs/linuxfan/ -rf

cd /usr/local/httpd/htdocs/linuxfan/wordpress/

cp wp-config-sample.php wp-config.php

##修改配置文件可以使用vimwp-config.php

sed -i ‘s/database_name_here/wpdb/g‘wp-config.php

sed -i ‘s/username_here/wpadm/g‘wp-config.php

sed -i ‘s/password_here/123123/g‘wp-config.php

3.访问:

http://www.linuxfan.cn/wordpress/

补充:

实验中所用到的脚本内容;

cat apache_install.sh

#!/bin/bash

#by linuxfan

rpm -e httpd httpd-manual --nodeps

ls /root/httpd*

if [ $? -eq 0 ];then

tar zxvf /root/httpd-2.2.17.tar.gz -C/usr/src/

cd /usr/src/httpd-2.2.17/

./configure --prefix=/usr/local/httpd--enable-rewrite --enable-so --disable-access 1>/dev/null

make &&make install

fi

 

cat mysql_install.sh

#!/bin/bash

##第一配置yum,安装ncurses依赖包

yum -y install ncurses-*

#解压cmake,安装基础环境

tar zxvf /root/cmake-2.8.6.tar.gz -C /usr/src/

cd /usr/src/cmake-2.8.6

#配置,编译安装cmake

./configure &&gmake &&gmakeinstall

##解压mysql

tar zxvf /root/mysql-5.5.22.tar.gz -C/usr/src/

cd /usr/src/mysql-5.5.22/

#cmake进行配置mysql

cmake-DCMAKE_INSTALL_PREFIX=/usr/local/mysql  #指定安装目录\

-DDEFAULT_CHARSET=utf8   #指定字符集为utf8 \

-DDEFAULT_COLLATION=utf8_general_ci   ##指定字符校验 \

-DWITH_EXTRA_CHARSETS=all   ##支持额外字符集\

-DSYSCONFDIR=/etc/  ##指定配置文件位置

make &&make install   #编译安装

if [ -e /usr/local/mysql ];then

echo "mysql installsuccessfully."

fi

 

cat php_install.sh

#!/bin/bash

##by linuxfan 20150611

#1.卸载已经安装rpm

rpm -qa |grep php

if [ $? -eq 0 ];then

rpm -e php php-mysql --nodeps

fi

#2.安装mcrypt支持,安装的顺序必须libmcrypt-->mhash-->mcrypt,每安装都必须ln链接到系统库中,echo"/usr/local/lib/" >>/etc/ld.conf&&ldconfig

##########install mcrypt###########

tar zxvf /root/libmcrypt-2.5.8.tar.gz -C/usr/src/

cd /usr/src/libmcrypt-2.5.8/

./configure &&make &&makeinstall

ln -s /usr/local/lib/libmcrypt.* /usr/lib

tar zxvf /root/mhash-0.9.9.9.tar.gz -C/usr/src/

cd /usr/src/mhash-0.9.9.9/

./configure &&make &&makeinstall

ln -s /usr/local/lib/libmhash* /usr/lib/

tar zxvf /root/mcrypt-2.6.8.tar.gz -C/usr/src/

cd /usr/src/mcrypt-2.6.8/

./configure &&make &&makeinstall

#3.安装php

##############install php #############

yum -y install libxml2-* zlib-*

PHV=php-5.3.28

tar zxvf /root/$PHV.tar.gz -C /usr/src/

cd /usr/src/$PHV/

./configure --prefix=/usr/local/php5--with-mcrypt --with-apxs2=/usr/local/httpd/bin/apxs--with-mysql=/usr/local/mysql/ \

 --with-config-file-path=/usr/local/php5--enable-mbstring &&make &&make install

if [ -e /usr/local/php5 ]

then

echo "php install success."

fi

 

cat mysql_config.sh

#!/bin/bash

#1.复制配置文件

cp/usr/src/mysql-5.5.22/support-files/my-medium.cnf /etc/my.cnf

#2.添加系统服务

cp /usr/src/mysql-5.5.22/support-files/mysql.server/etc/init.d/mysqld

chmod +x /etc/init.d/mysqld

chkconfig --add mysqld

chkconfig mysqld  on

#3.优化PATH路径,执行命令时方便,单引号双引号都行

grep mysql /etc/profile

if [ $? -eq 0 ];then

echo "PATH is set."

else

echo "export PATH=$PATH:/usr/local/mysql/bin"  >>/etc/profile

source /etc/profile  ##执行文件

fi

#4.初始化mysql,创建用户,赋权

useradd -M -s /sbin/nologin mysql

chown -R mysql:mysql /usr/local/mysql

/usr/local/mysql/scripts/mysql_install_db  \

--basedir=/usr/local/mysql \

--datadir=/usr/local/mysql/data--user=mysql

#5.启动mysql,并设置为开机启动

if [ -e /tmp/mysql.sock ];then

/etc/init.d/mysqld restart

else

/etc/init.d/mysqld start

fi

chkconfig mysqld on

#6.修改密码,并提示密码

mysqladmin -u root password ‘123123‘  &&echo "mysql root password is123123"

 

cat php_config.sh

#!/bin/bash

##by linuxfan

##############config php############

PHV=php-5.3.28

cp /usr/src/$PHV/php.ini-development/usr/local/php5/php.ini

#修改配置项支持php标记<?php?>

sed -i ‘s/short_open_tag =Off/short_open_tag = On/g‘ /usr/local/php5/php.ini

##设置默认字符集utf8

echo "default_charset ="utf8" " >>/usr/local/php5/php.ini

###########add module zend############

ZDV=ZendGuardLoader-php-5.3-linux-glibc23-x86_64

tar zxvf /root/$ZDV.tar.gz -C /root/

cp -rf /root/$ZDV/php-5.3.x/ZendGuardLoader.so/usr/local/php5/lib/php/

cat <<END>>/usr/local/php5/php.ini

zend_extension=/usr/local/php5/lib/php/ZendGuardLoader.so

zend_enable=1

END

 

cat lamp_config.sh

#!/bin/bash

##by scfa 2015-06-30

################# Setting  apache with  php  ################

#1.修改apache的配置文件

APACHE_C=/usr/local/httpd/conf/httpd.conf

##添加ServerName设置FQDN

sed -i ‘/^#ServerName/a ServerNamewww.linuxfan.cn‘ $APACHE_C

##在第310行后一行添加php应用类型的支持

sed -i ‘310a \    AddType application/x-httpd-php .php‘$APACHE_C

##修改默认首页,支持index.php

sed -i ‘s/DirectoryIndexindex.html/DirectoryIndex index.html index.php/g‘ $APACHE_C

netstat -uptln |grep 80 &>/dev/null

if [ $? -eq 0 ]

then

         /usr/local/httpd/bin/apachectl  stop &&/usr/local/httpd/bin/apachectl  start

         netstat-uptln |grep 80

         echo"apache  restart successful"

else

         /usr/local/httpd/bin/apachectlstart

         netstat-utpln |grep 80

fi

#2.mysql的配置

if [ -e /tmp/mysql.sock ];then

echo "mysql is running."

else

/etc/init.d/mysqld start

fi

实验难点:

1.web站点挂载nfs的存储时,容易出现挂载目录全部删除的情况或者是挂载不上,此时停止autofs的服务,umount掉挂载点,检查autofs的配置文件,检查nfs服务器的共享目录,然后再启动autofs服务;

2.在此项目中,web站点将nfs服务器的共享目录挂载到了apache的安装目录下的htdocs网页存放目录中,可能出现的情况是autofs挂载成功,但是httpd/目录下的文件全部没有了,此时无法启动httpd/bin/apachectl服务,解决办法:先将autofs服务停止,umount掉挂载点,启动apache服务,检查80端口号,然后再将apache服务启动;

3.部署后端数据库时,配置数据库之后的初始化过程中,可能会初始化失败,或者是初始化成功改不了数据库的密码,此时,停止mysqld的服务,检查/tmp的权限是否为1777,检查/usr/local/mysqld/及以下目录属主属组全部为mysqld用户,删除/usr/local/mysqld/data/*下全部文件,并且重启主机,然后进行初始化mysql,启动mysqld,修改mysql的密码;

4.注意nfs服务器中的共享目录的权限,需要设置为755,目的是web站点不可以写入,只能读取,并且两台nfs服务器在做rsync的同时,需要指定-P选项保持文件的权限;

5.客户端在访问mysql数据库的原理:在数据库的读写分离中,客户端连接数据库使用的是代理服务器的amoeba用户,8066号端口,然后再通过代理服务器访问数据库,访问数据库时使用的是数据库中授权的用户linuxfan。(也可以在代理amoeba服务器的amoeba.xml配置文件中更改amoeba服务的端口号80663306,这样客户端就不用指定8066端口);

6.nfs服务器中的网页项目有改变的话,web站点可能会出现无法读取挂载的问题,此时停止autofs的服务,umount掉挂载点,重新启动autofs服务;

7.注意前端调度器中配置的keepalived和后端nfs服务器所配置的keepalived,这两个keepalived的配置文件中的服务器名称,热备实例名称,路由器id不能相同;

拓展:


web集群综合项目keepalived+lvs+lamp+nfs+mysql