利用lamp架构搭建Discuz论坛,并实现对数据库的高可用

lamp架构=LAMP指的Linux(操作系统)、Apache(HTTP 服务器),MySQL(数据库软件) 和PHP(有时也是指Perl或Python) 的第一个字母,一般用来建立web 服务器。

创新互联公司长期为成百上千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为山西企业提供专业的成都做网站、成都网站设计,山西网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。

利用lamp架构搭建Discuz论坛,并实现对数据库的高可用


#############源码安装php###########

tar jxf php-5.6.20.tar.bz2

cd php-5.6.20

解决依赖性:

yum install freetype-devel libmcrypt-2.5.8-9.el6.x86_64.rpmnet-snmp-devel libmcrypt-devel-2.5.8-9.el6.x86_64.rpm libpng-devel gmp-devel libjpeg-turbo-devel libcurl-devel libxml2-devel-y

检测依赖性:

[root@server1 php-5.6.20]# ./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local/lnmp/php/etc --with-openssl --with-snmp --with-gd --with-zlib --with-curl --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir --without-pear --with-gettext --with-gmp --enable-inline-optimization --enable-soap --enable-ftp --enable-sockets --enable-mbstring --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mcrypt --with-mhash --with-mysql --with-mysqli --with-pdo-mysql --enable-mysqlnd

没有报错之后就可以执行以下操作:

[root@server1 php-5.6.20]# make && make install

[root@server1 php-5.6.20]# cp php.ini-production /usr/local/lnmp/php/etc/php.ini

[root@server1 php-5.6.20]# cd /usr/local/lnmp/php/etc/

[root@server1 etc]# cp php-fpm.conf.default php-fpm.conf

cd /mnt/php-5.6.20/sapi/fpm/

[root@server1 fpm]# cp init.d.php-fpm /etc/init.d/php-fpm

[root@server1 fpm]# chmod +x /etc/init.d/php-fpm

[root@server1 fpm]# cd /usr/local/lnmp/php/etc/

[root@server1 etc]# vim php-fpm.conf#取消25行的注释

 25 pid = run/php-fpm.pid

 

[root@server1 etc]# vim php.ini##修改时区

925 date.timezone = 'Asia/Shanghai'

[root@server1 etc]# /etc/init.d/php-fpm start

Starting php-fpm  done

[root@server1 etc]# cd /usr/local/

[root@server1 local]# cd nginx/sbin/

[root@server1 sbin]# ls

nginx

[root@server1 sbin]# pwd

/usr/local/nginx/sbin

[root@server1 sbin]# vim ~/.bash_profile

PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin

 

[root@server1 sbin]# source ~/.bash_profile

[root@server1 sbin]# nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

 

 cd/usr/local/nginx/conf

[root@server1 conf]# vim nginx.conf

 49         location / {

 50             root   html;

 51             index index.php index.html index.htm;#调整优先级,先看index.php

 52         }

 

 71         location ~ \.php$ {

 72             root           html;

 73             fastcgi_pass   127.0.0.1:9000;

 74             fastcgi_index  index.php;

 75             fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

 76             include        fastcgi.conf;

[root@server1 conf]# nginx

[root@server1 conf]# nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@server1 conf]# cd ..

[root@server1 nginx]# cd html/

[root@server1 html]# vim nginx.php

phpinfo()

?>

找到当前主机mysql的缓存文件

[root@server1 mysql]# ll /var/lib/mysql/mysql.sock

srwxrwxrwx 1 mysql mysql 0 3月  23 17:16 /var/lib/mysql/mysql.sock

 

cd /usr/local/lnmp/php/etc

[root@server1 etc]# vim php.ini

#默认数据存储在/var/lib/mysql/mysql.sock这个文件中

1001 pdo_mysql.default_socket=/var/lib/mysql/mysql.sock

1150 mysql.default_socket =/var/lib/mysql/mysql.sock

1209 mysqli.default_socket =/var/lib/mysql/mysql.sock

[root@server1 etc]# /etc/init.d/php-fpm reload

Reload service php-fpm  done

 

测试:

利用lamp架构搭建Discuz论坛,并实现对数据库的高可用

Php安装成功!

 

 

 

 

Heartbeat + mysql +drbd

三个部分分别提供了高可用,数据库,以及存储服务。将之后的论坛整合成一个完整的体系

说明:server3,server4提供以上三个服务

1数据库

直接yum install mysql-server -y  #server3和server4都做

mysql_secure_installation#安全初始化,设置root密码,否则数据库不要密码也能登陆

 

2.Heartbeat

[root@server3 heartbeat]# ls

heartbeat-3.0.4-2.el6.x86_64.rpm        heartbeat-libs-3.0.4-2.el6.x86_64.rpm

heartbeat-devel-3.0.4-2.el6.x86_64.rpm  ldirectord-3.9.5-3.1.x86_64.rpm

Server3,server4都要安装

[root@server3 heartbeat]# yum install * -y

[root@server3 heartbeat]# cd /etc/ha.d/

[root@server3 ha.d]# ls

harc  rc.d  README.config  resource.d  shellfuncs

[root@server3 ha.d]# cp /usr/share/doc/heartbeat-3.0.4/{authkeys,ha.cf,haresources} .

[root@server3 ha.d]# vim ha.cf

48 keepalive 2

56 deadtime 30

76 udpport 1111

91 bcast   eth0

157 auto_failback on

211         node    server3.example.com

212         node    server4.example.com

222 ping 172.25.50.250

254 respawn hacluster /usr/lib64/heartbeat/ipfail

262 apiauth ipfail gid=haclient uid=hacluster

 

[root@server3 ha.d]# vim authkeys

 23 auth 1

 24 1 crc

 25 #2 sha1 HI!

 26 #3 md5 Hello!

[root@server3 ha.d]# vim haresources

在文件最后添加:

server3.example.com IPaddr::172.25.50.200/24/eth0 drbddisk::homework Filesystem::/dev/drbd2::/var/lib/mysql::ext4 mysqld

 

##做测试可以用:server3.example.com IPaddr::172.25.50.200/24/eth0 mysqld(上面的是针对drbd所作的)

 

[root@server3 ha.d]# scp ha.cf haresources authkeys 172.25.50.40:/etc/ha.d/

两边都重新启动heartbeat

3.drbd共享存储

给server3和server4都划分一个4G的虚拟磁盘

[root@server3 mnt]# fdisk -l

Disk/dev/vdb: 4294 MB, 4294967296 bytes

16 heads, 63 sectors/track, 8322 cylinders

Units = cylinders of 1008 * 512 = 516096 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x00000000

 

[root@server4 drbd]# fdisk -l

Disk/dev/vda: 4294 MB, 4294967296 bytes

16 heads, 63 sectors/track, 8322 cylinders

Units = cylinders of 1008 * 512 = 516096 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0x00000000

 

安装以下rpm包

drbd-8.4.2-2.el6.x86_64.rpm

drbd-bash-completion-8.4.2-2.el6.x86_64.rpm

drbd-heartbeat-8.4.2-2.el6.x86_64.rpm

drbd-km-2.6.32_431.el6.x86_64-8.4.2-2.el6.x86_64.rpm

drbd-pacemaker-8.4.2-2.el6.x86_64.rpm

drbd-udev-8.4.2-2.el6.x86_64.rpm

drbd-utils-8.4.2-2.el6.x86_64.rpm

drbd-xen-8.4.2-2.el6.x86_64.rpm

[root@server3 drbd]# rpm -ivh *

Server4也需要安装这些rpm包

[root@server3 mnt]# cd /etc/drbd.d/

[root@server3 drbd.d]# ls

global_common.conf

[root@server3 drbd.d]# vim homework.res

[root@server3 drbd.d]# cat homework.res

resource homework {

meta-disk internal;

device /dev/drbd2;#自定义的共享磁盘名称

syncer {verify-alg sha1;

}

on server3.example.com {#节点的hostname

disk /dev/vdb;#自身主将用来设置成共享的磁盘路径

address 172.25.50.30:7789;#节点ip

}

on server4.example.com {

disk /dev/vda;

address 172.25.50.40:7789;

}

}

这样就无需担心发布共享磁盘名称不一样如何解决的问题了

[root@server3 drbd.d]# scp homework.res 172.25.50.40:/etc/drbd.d/

[root@server3 drbd.d]# drbdadm create-md homework

##server3和server4都做

  --==  Thank you for participating in the global usage survey  ==--

The server's response is:

Writing meta data...

initializing activity log

NOT initializing bitmap

New drbd meta data block successfully created.

[root@server3 drbd.d]# /etc/init.d/drbd start#server4同时启动

Starting DRBD resources: [

     create res: homework

   prepare disk: homework

    adjust disk: homework

     adjust net: homework

]

[root@server3 drbd.d]# cat /proc/drbd

version: 8.4.2 (api:1/proto:86-101)

GIT-hash: 7ad5f850d711223713d6dcadc3dd48860321070c build by root@server1.example.com, 2017-02-21 09:59:13

 

 1: cs:Connected ro:Secondary/Secondaryds:Inconsistent/Inconsistent C r-----

    ns:0 nr:0 dw:0 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:4194140

[root@server3 drbd.d]# drbdadm primary homework --force

[root@server3 drbd.d]# mkfs.ext4 /dev/drbd2

[root@server3 mnt]# mount /dev/drbd2 /mnt/mount/

[root@server3 mnt]# cd mount/

[root@server3 mount]# ls

lost+found

[root@server3 mount]# touch 111

[root@server3 mount]# ls

111  lost+found

[root@server3 mount]# cd ..

[root@server3 mnt]# umount /mnt/mount/

[root@server3 mnt]# drbdadm secondary homework

[root@server4 drbd.d]# drbdadm primary homework

[root@server4 drbd.d]# vim homework.res

[root@server4 drbd.d]# mount /dev/drbd2 /mnt/mount/

[root@server4 drbd.d]# cd /mnt/mount/

[root@server4 mount]# ls

111  lost+found

[root@server4 mnt]# umount /mnt/mount/

[root@server4 mnt]# drbdadm secondary homework

 

在挂载的目录下看到在server3上创建的文件,说明分布式共享存储配置成功

 

Heartbeat+mysql

关闭两台虚拟机的 heartbeat 服务

[root@server3 mnt]# /etc/init.d/mysqld stop

[root@server3 mnt]# drbdadm primary homework

[root@server3 mnt]# mount /dev/drbd2 /mnt/mount/

 

[root@server3 mount]# cd /var/lib/mysql/

[root@server3 mysql]# cp -r * /mnt/mount/

[root@server3 ~]# umount /mnt/mount/

[root@server3 ~]# mount /dev/drbd2 /var/lib/mysql/

[root@server3 ~]# chown mysql.mysql /var/lib/mysql/ -R

[root@server3 ~]# /etc/init.d/mysqld start

正在启动 mysqld:                                          [确定]

[root@server3 ~]# cd

[root@server3 ~]# /etc/init.d/mysqld stop

停止 mysqld:                                              [确定]

[root@server3 ~]# umount /var/lib/mysql/

[root@server3 ~]# drbdadm secondary homework

 

把server3,server4的heartbeat服务打开服务会自动开启,当server3的heatbeat服务关闭是。Vip:172.25.50.200会调转到server4上。

 

最后:

整合

 

现在我们来搭建一个论坛

[root@server1 mnt]# unzip Discuz_X3.2_SC_UTF8.zip -d /usr/local/nginx/html/

##将这个论坛压缩包解压在nginx的默认发布目录中

 

[root@server1 mnt]# cd /usr/local/nginx/html/

[root@server1 html]# ls

50x.html  index.html  nginx.php  readme  upload  utility

[root@server1 html]# cd upload/

[root@server1 upload]# ls

admin.php  connect.php      forum.php  member.php  search.php  uc_server

api        cp.php           group.php  misc.php    source      userapp.php

api.php    crossdomain.xml  home.php   plugin.php  static

archiver   data             index.php  portal.php  template

config     favicon.ico      install    robots.txt  uc_client

[root@server1 upload]# chmod 777 config/

[root@server1 upload]# chmod 777 data/ -R

[root@server1 upload]# chmod 777 uc_* -R

 

在serer3上设置root用户可以远程登陆的权限

 grant all privileges on *.* to "root"@"%" identified  by "redhat";

Query OK, 0 rows affected (0.00 sec)

 

测试方法,在serer1上远程登陆172.25.50.200这个ip的数据库

并且在数据库中有解的权限即可。

利用lamp架构搭建Discuz论坛,并实现对数据库的高可用 

 

在浏览器上:172.25.50.10/upload/

利用lamp架构搭建Discuz论坛,并实现对数据库的高可用

利用lamp架构搭建Discuz论坛,并实现对数据库的高可用

利用lamp架构搭建Discuz论坛,并实现对数据库的高可用

 

数据库写入测试:在server3上

利用lamp架构搭建Discuz论坛,并实现对数据库的高可用

 

高可用测试:

关闭server3上的heartbeat服务。

利用lamp架构搭建Discuz论坛,并实现对数据库的高可用

利用lamp架构搭建Discuz论坛,并实现对数据库的高可用

如图,可以看见heartbeat服务停了之后,sererv4自动接管数据库,这时查看数据库内容与在server3上看到的一致!

实验成功!!!!!!!!!!!!!!!!!!!!!!!!


当前文章:利用lamp架构搭建Discuz论坛,并实现对数据库的高可用
网站URL:http://myzitong.com/article/geeeoi.html