手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

NFS实现LAMP分离多服务器同步更新服务

[日期:2014-03-27] 来源:Linux社区  作者:chenpipi [字体:大 中 小]

上一篇Linux下编译安装LAMP并分离为多台服务器写的是LAMP分离为多台主机;但是由于是各自独立;如需要更新或用户更新数据时;需要每台机器单独更新;且如httpd服务器等都是需要多台的话就会出现数据不同步的现象;现在就上一篇进行补全;实现多台httpd主机利用NFS文件系统基于RPC协议来同步更新数据。

Ubuntu 13.04 安装 LAMP\Vsftpd\Webmin\phpMyAdmin 服务及设置 http://www.linuxidc.com/Linux/2013-06/86250.htm

CentOS 5.9下编译安装LAMP(Apache 2.2.44+MySQL 5.6.10+PHP 5.4.12) http://www.linuxidc.com/Linux/2013-03/80333p3.htm

RedHat 5.4下Web服务器架构之源码构建LAMP环境及应用PHPWind http://www.linuxidc.com/Linux/2012-10/72484p2.htm

LAMP源码环境搭建WEB服务器Linux+Apache+MySQL+PHP http://www.linuxidc.com/Linux/2013-05/84882.htm

大致规划:

网段:172.16.251.

1台DNS服务器(172.16.251.84)

2台httpd服务器(172.16.251.85/86)

1台php服务器(172.16.251.87)

1台mysql服务器(172.16.251.88)

做了四个域名解析到2台httpd服务器上:

域名:www.myadmin.cn www.soul.org www.linuxidc.com www.88181.com

NFS实现LAMP分离多服务器同步更新服务


一、NFS和RPC定义

网络文件系统(Network File System,NFS),一种使用于分散式文件系统的协议,由SUN公司开发,于1984年向外公布。功能是通过网络让不同的机器、不同的操作系统能够彼此分享个别的数据,让应用程序在客户端通过网络访问位于服务器磁盘中的数据,是在Unix系统间实现磁盘文件共享的一种方法。NFS的基本原则是“容许不同的客户端及服务端通过一组RPC分享相同的文件系统”,它是独立于操作系统,容许不同硬件及操作系统的系统共同进行文件的分享。目前最新版本为NFSv4。

远程过程调用(Remote Procedure Call,RPC)是一个计算机通信协议。该协议允许运行于一台计算机的程序调用另一台计算机的子程序,而程序员无需额外地为这个交互作用编程。

 


二、安装配置NFS服务

NFS是由nfs-utils软件包提供的;可以先查看系统上是否安装;如未安装可以先使用yum来安装

[Linux87]#rpm -qa nfs*

nfs-utils-lib-1.1.5-6.el6.x86_64

nfs-utils-1.2.3-39.el6.x86_64

nfs4-acl-tools-0.3.3-6.el6.x86_64

[Linux87]#rpm -ql nfs-utils  查看该包安装生成了哪些文件

/etc/nfsmount.conf

/etc/rc.d/init.d/nfs

/etc/rc.d/init.d/nfslock

/etc/rc.d/init.d/rpcgssd

/etc/rc.d/init.d/rpcidmapd

/etc/rc.d/init.d/rpcsvcgssd

/etc/request-key.d/id_resolver.conf 

安装成功后既可启动:

[Linux87]#service nfs start

Starting NFS services:                                    [  OK  ]

Starting NFS quotas:                                      [  OK  ]

Starting NFS mountd:                                      [  OK  ]

Starting NFS daemon:                                      [  OK  ]

Starting RPC idmapd:                                      [  OK  ]

#默认就启动上述这么多服务

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

[Linux87]#rpcinfo  可以查看详细信息

  program version netid    address                service    owner

    100000    4    tcp      0.0.0.0.0.111          portmapper superuser

    100000    3    tcp      0.0.0.0.0.111          portmapper superuser

    100000    2    tcp      0.0.0.0.0.111          portmapper superuser

    100000    4    udp      0.0.0.0.0.111          portmapper superuser

    100000    3    udp      0.0.0.0.0.111          portmapper superuser

    100000    2    udp      0.0.0.0.0.111          portmapper superuser

    100000    4    local    /var/run/rpcbind.sock  portmapper superuser

    100000    3    local    /var/run/rpcbind.sock  portmapper superuser

    100024    1    udp      0.0.0.0.230.98        status    29

    100024    1    tcp      0.0.0.0.144.225        status    29

    100011    1    udp      0.0.0.0.3.107          rquotad    superuser

    100011    2    udp      0.0.0.0.3.107          rquotad    superuser

    100011    1    tcp      0.0.0.0.3.107          rquotad    superuser

    100011    2    tcp      0.0.0.0.3.107          rquotad    superuser

2、配置NFS共享文件系统

需要共享的是PHP的服务器;共享给指定的两台httpd主机;所以可以直接指定IP地址;这里单独建立一个分区用于NFS;挂载至web配置文件中指定的DocumentRoot目录;当然也可以先挂载在配置;这里是由于之前已配置好服务;所以直接挂载文件为指定目录:

httpd配置中的路径:

[WEB85]#cat /etc/httpd24/extra/httpd-vhosts.conf

#

<VirtualHost *:80>

    ServerAdmin  admin@linuxidc.com

    DocumentRoot "/usr/local/apache/www/linuxidc" #本地路径

    ServerName www.linuxidc.com

    ServerAlias linuxidc.com

    ErrorLog "/usr/local/apache/logs/linuxidc.error_log"

    CustomLog "/usr/local/apache/logs/linuxidc.access_log" combined

    ProxyRequests Off

    ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.251.87:9000/var/www/linuxidc/$1

                                       

#上面这个路径就是PHP服务器的存放路径;也就是需要共享的NFS路径:/var/www

                                             

    <Directory "/usr/local/apache/www/linuxidc">

        DirectoryIndex index.html index.php

        Options none

        Require all granted

    </Directory>

</VirtualHost>

 

[PHP87]#mount

/dev/mapper/vg0-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)

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

/dev/mapper/vg0-usr on /usr type ext4 (rw)

/dev/mapper/vg0-var on /var type ext4 (rw)

/dev/sda3 on /var/www type ext4 (rw,acl)  #这个目录

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

sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)

nfsd on /proc/fs/nfsd type nfsd (rw)

#****注意:挂载时在fstab中写入需要acl属性****

                                                                                               

#配置NFS主要是修改/etc/exports文件

[PHP87]#vim /etc/exports

#文件格式很简单:

文件系统    客户端1(文件系统导出属性1...)  客户端2()

                                   

/var/www        172.16.251.85(rw)              172.16.251.86(rw)

                                   

#rw:读写

#async:异步

#sync:同步

#root_squash:压缩root用户;基于imapd;将root通过网络访问时转为#nfsnobody用户

#no_root_squash:不压缩root权限

#all_squash:压缩所有用户

#anonuid=[num]:指定匿名用户映射为的UID和GID

#anongid=[num]

修改完成后可以查看属性

 

[PHP87]#exportfs -v

/var/www        172.16.251.85(rw,wdelay,root_squash,no_subtree_check)

/var/www        172.16.251.86(rw,wdelay,root_squash,no_subtree_check)

[PHP87]#其中有很多默认属性的

现在可以去httpd服务器查看该机器的共享NFS

 

[WEB85]#showmount -e 172.16.251.87

Export list for 172.16.251.87:

/var/www 172.16.251.86,172.16.251.85

[WEB85]#

[WEB86]#showmount -e 172.16.251.87

Export list for 172.16.251.87:

/var/www 172.16.251.86,172.16.251.85

[WEB86]#

3、挂载配置读写权限

mount -t type device dir

    -t:指定文件系统类型

[WEB85]#mount -t nfs 172.16.251.87:/var/www/ /usr/local/apache/www/

[WEB85]#mount

/dev/mapper/vg0-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)

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

/dev/mapper/vg0-usr on /usr type ext4 (rw)

/dev/mapper/vg0-var on /var type ext4 (rw)

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

sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)

172.16.251.87:/var/www/ on /usr/local/apache/www type nfs (rw,vers=4,addr=172.16.251.87,clientaddr=172.16.251.85)

#查看以挂载;第二台httpd也需要同样挂载;挂载完成后记得写入到/etc/fstab中

                                                                                       

[WEB85]#vim /etc/fstab

增加下面这行内容

172.16.251.87:/var/www  /usr/local/apache/www  nfs    defaults,_netdev        0 0

                                                                                                                                                                                                                                   

挂载完成后;是无法写入数据的

[WEB85]#cd /usr/local/apache/www/

[WEB85]#mkdir aa

mkdir: cannot create directory `aa': Permission denied

[WEB85]#

                                                                           

需要到php服务器配置NFS的权限;为了安全;新建立一个账户;httpd服务器也建立同样的账户

[PHP87]#useradd -u 600 web

[PHP87]#id web

uid=600(web) gid=600(web) groups=600(web)

[PHP87]#setfacl -R -m u:600:rwx /var/www/  -R是因为其下面有其他的目录

[PHP87]#getfacl /var/www/

getfacl: Removing leading '/' from absolute path names

# file: var/www/

# owner: root

# group: root

user::rwx

user:web:rwx

group::r-x

mask::rwx

other::r-x

                                                                                                                                                                                                                                                       

#httpd两台服务器建立相应的账户

[WEB85]#useradd -u 600 web

[WEB85]#su - web

[web@localhost ~]$ cd /usr/local/apache/www/

[web@localhost www]$ mkdir aaa

[web@localhost www]$ ll

total 52

drwxrwxr-x  2 nobody nobody  4096 Mar 26 16:09 aaa

#测试创建成功

linux
相关资讯       lamp 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款