一、概述:
上篇博客《Linux网络服务-LAMP之Php基于Apache的模块实现》介绍了一个由基于Centos6.5_x86-64+Httpd+Php(module)+Mysql构建的一个简单的LAMP环境,其中中我使用Php基于httpd服务的一部分、然后再结合mysql一同工作;也归纳了httpd与Php结合的常见三种方式,以及和mysql它们三者之间是如何通信的,最后实现了一个博客系统的部署。
现在假设就上篇博文中的拓扑图来讲,我的httpd服务器不足以满足需求,需要多增加一台,以达到平均分摊工作量的目的,于是我就加了一台和之前一样的httpd服务器,可是现在又遇到一个问题他们之间的网页数据存放该如何解决,又如何能达到两台httpd服务器存的数据能一模一样,于是引入了NFS这个概念,首先需要了解,什么是NFS,它是如何工作的?
NFS,是Network File System的简写,即网络文件系统。NFS会经常用到,用于在网络上共享存储。这样讲,你对NFS可能不太了解,我不妨举一个例子来说明一下NFS是用来做什么的。假如有三台机器A、B、C,它们需要访问同一个目录,目录中都是图片,传统的做法是把这些图片分别放到A、B、C。但是使用NFS只需要放到A上,然后A共享给B和C即可。访问的时候,B和C是通过网络的方式去访问A上的那个目录的。它的配置我将在下面的实验中展开。先看一下图吧:
2.两台Web Server通过在Fastcgi上面的NFS服务将其共享出来的文件系统挂载至本地存放网页数据文件;
3.如果客户端请求的是Php脚本文件,httpd将基于Fastcgi协议与后端的Fastcgi Server进行通信,并将Php脚本文件在Fastcgi内部执行后返回到其中的一台Web Server,最后在通过http/https协议将结果响应给客户端;
4.若客户提交的请求包括了Php脚本文件并且还需要用到数据库,此时Fastcgi则会基于mysql协议与Mysql Server进行通信,工作;
二、实践操作
==================================安装DNS服务==================================
[root@ns ~]# yum intall -y bind
首先要到达DNS轮询的方式我们需要在主配置文件中填以下代码: options { rrset-order { class IN type A name "www.maoqiu.com" order cyclic; }; }; rrset-order 支持三个参数:fixed, random, cyclic. fixed 会将多个A记录按配置文件的顺序固定给出 random 会随机给出 cyclic 会循环给出 #注销以下信息 [root@ns ~]# vim /etc/named.conf options { // listen-on port 53 { 127.0.0.1; }; // listen-on-v6 port 53 { ::1; }; directory "/var/named"; memstatistics-file "/var/named/data/named_mem_stats.txt"; allow-query { any; }; recursion yes; // dnssec-enable yes; // dnssec-validation yes; // dnssec-lookaside auto; /* Path to ISC DLV key */ bindkeys-file "/etc/named.iscdlv.key"; rrset-order { class IN type A name "www.maoqiu.com" order cyclic; }; #→新添加的内容 managed-keys-directory "/var/named/dynamic"; }; logging { channel default_debug { file "data/named.run"; severity dynamic; }; }; zone "." IN { type hint; file "named.ca"; }; include "/etc/named.rfc1912.zones"; //include "/etc/named.root.key"; #继续编辑/etc/named.rfc1912.zons配置文件为其创建区域信息 [root@ns ~]# vim /etc/named.rfc1912.zones #在文件最后添加以下信息 zone "maoqiu.com" IN { type master; file "maoqiu.com.zone" }; zone "41.16.172.in-addr.arpa" IN { type master; file "172.16.41.zone" }; #检查主配置文件语法 [root@ns ~]# named-checkconf#→如果执行结果没有任何输出则表示语法上面对了,但是不能保证我们填写的信息是对的,等下看看,有问题再修改 [root@ns ~]#
[root@ns ~]# cd /var/named/ [root@ns named]# vim maoqiu.com.zone #→创建正向区域配置文件 $TTL 6400 @ IN SOA ns.maoqiu.com. admin.maoqiu.com ( 2014032701 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum IN NS ns ns IN A 172.16.41.3 www IN A 172.16.41.1 www IN A 172.16.41.2 [root@ns named]# vim 172.16.41.zone #→创建反向区域配置文件 $TTL 6400 @ IN SOA ns.maoqiu.com. admin.maoqiu.com ( 2014032701 ; serial 1D ; refresh 1H ; retry 1W ; expire 3H ) ; minimum IN NS ns.maoqiu.com. 3 IN PTR ns.maoqiu.com. 1 IN PTR www.maoqiu.com. 2 IN PTR www.maoqiu.com. [root@ns named]# named-checkzone maoqiu.com /var/named/maoqiu.com.zone #→检查正向区域配置文件语法 zone maoqiu.com/IN: loaded serial 2014032701 OK [root@ns named]# named-checkzone 41.16.172.in-addr.arpa /var/named/172.16.41.zone #→检查反向区域配置文件语法 zone 41.16.172.in-addr.arpa/IN: loaded serial 2014032701 OK [root@ns named]#4.修改区域文件属性
[root@ns named]# chmod 640 maoqiu.com.zone 172.16.41.zone [root@ns named]# chown root:named maoqiu.com.zone 172.16.41.zone
[root@ns named]# vim /etc/resolv.conf # Generated by NetworkManager search maoqiu.com nameserver 172.16.41.3
[root@ns named]# service named start Starting named: [ OK ] [root@ns named]# ss -tunl | grep :53 udp UNCONN 0 0 172.16.41.3:53 *:* udp UNCONN 0 0 127.0.0.1:53 *:* tcp LISTEN 0 3 172.16.41.3:53 *:* tcp LISTEN 0 3 127.0.0.1:53 *:* [root@ns named]#
[root@www ~]# echo "<h1>web1</h1>" >/usr/local/apache2/htdocs/index.html [root@www ~]# service httpd2 start Starting httpd: [root@www ~]# [root@www ~]# echo "<h1>web2</h1>" >/usr/local/apache2/htdocs/index.html [root@www ~]# service httpd2 start Starting httpd: [root@www ~]#
ok.看来DNS正常能够做到轮询了
==================================安装NFS服务==================================
[root@fast ~]# mke2fs -t ext4 /dev/sda3 #创建共享目录,并挂载此分区 [root@fast ~]# mkdir /usr/local/apache2/htdocs [root@fast ~]# mount /dev/sda3 /webshared/ [root@fast ~]# vim /etc/fstab#→实现开机自动挂载 /dev/sda3 /usr/local/apache2/htdocs ext4 defaults,acl 0 0
#查看是否安装了服务器端程序 [root@fast ~]# rpm -qa | grep 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 [root@fast ~]# vim /etc/exports /usr/local/apache2/htdocs 172.16.41.1(rw,async,no_root_squash) 172.16.41.2(rw,async,no_root_squash)
[root@fast ~]# service nfs start Starting NFS services: [ OK ] Starting NFS quotas: [ OK ] Starting NFS mountd: [ OK ] Starting NFS daemon: [ OK ] Starting RPC idmapd: [ OK ] [root@fast ~]# showmount -e localhost #→本地测试没有问题 Export list for localhost: /webshared 172.16.41.2,172.16.41.1 [root@www ~]# showmount -e 172.16.41.4 #→在nfs客户端(web1)执行,探查fastcgi主机所导出的nfs文件系统 Export list for 172.16.41.4: /webshared 172.16.41.2,172.16.41.1 [root@www ~]# [root@www ~]# showmount -e 172.16.41.4 #→在nfs客户端(web2)执行,探查fastcgi主机所导出的nfs文件系统 Export list for 172.16.41.4: /webshared 172.16.41.2,172.16.41.1 [root@www ~]#
[root@www ~]# mount -t nfs 172.16.41.4:/usr/local/apache2/htdocs /usr/local/apache2/htdocs #→在web1上挂载 [root@www ~]# mount -t nfs 172.16.41.4:/usr/local/apache2/htdocs /usr/local/apache2/htdocs #→在web2上挂载
[root@www ~]# vim /etc/fstab 172.16.41.4:/usr/local/apache2/htdocs /usr/local/apache2/htdocs nfs defaults 0 0 [root@www ~]# vim /etc/fstab 172.16.41.4:/usr/local/apache2/htdocs /usr/local/apache2/htdocs nfs defaults 0 0 #注意我是在两台web服务器上做的操作哦,不要认为我做了两遍.
==================================编译安装Php==================================
[root@fast ~]# yum -y groupinstall "Desktop Platform Development" [root@fast ~]# yum -yinstall bzip2-devel libmcrypt-devel
[root@fast ~]# tar xf php-5.4.26.tar.bz2 [root@fast ~]# cd php-5.4.26 [root@fast ~]## ./configure --prefix=/usr/local/php --with-openssl --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 --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd [root@fast ~]# make [root@fast ~]# make test [root@fast ~]# make install
[root@fast ~]# cp php.ini-production /etc/php.ini
#为php-fpm提供Sysv init脚本,并将其添加至服务列表: [root@fast ~]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm [root@fast ~]# chmod +x /etc/rc.d/init.d/php-fpm [root@fast ~]# chkconfig --add php-fpm [root@fast ~]# chkconfig php-fpm on
[root@fast ~]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@fast ~]# 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 = run/php-fpm.pid #→将此行取消注释7.接下来就可以启动php-fpm了:
[root@fast ~]# service php-fpm start Starting php-fpm done [root@fast ~]# #使用如下命令来验正(如果此命令输出有中几个php-fpm进程就说明启动成功了): [root@fast ~]# ps -aux | grep php #默认情况下,fpm监听在127.0.0.1的9000端口,也可以使用如下命令验正其是否已经监听在相应的套接字。 # netstat -tnlp | grep php-fpm tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 689/php-fpm #php-fpm是为外面的httpd服务的,故我们需要将它的监听端口改为172.16.41.4 [root@fast ~]# vim /usr/local/php/etc/php-fpm.conf listen= 172.16.41.4:9000
=============================配置httpd-2.4.9=============================
[root@www ~]# vim /etc/httpd2/httpd.conf LoadModule proxy_module modules/mod_proxy.so #→启用 LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so #→启用
[root@www ~]# vim /etc/httpd2/httpd.conf #DocumentRoot "/usr/local/apache2/htdocs" 将此行注释掉 [root@www ~]# vim /etc/httpd2/extra/httpd-vhosts.conf <VirtualHost *:80> DocumentRoot "/usr/local/apache2/htdocs" ServerName www.maoqiu.com ErrorLog "logs/discuz-error_log" CustomLog "logs/discuz-access_log" common </Directory "/usr/local/apache/htdocs"> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory> ProxyRequests Off ProxyPassMatch ^/(.*\.php)$ fcgi://172.16.41.4:9000/usr/local/apache2/htdocs/$1 <IfModule dir_module> DirectoryIndex index.php index.html </IfModule> </VirtualHost>说明:
[root@www ~]# vim /etc/httpd2/httpd.conf #添加如下二行 AddType application/x-httpd-php .php AddType application/x-httpd-php-source .phps #定位至DirectoryIndex index.html 修改为: DirectoryIndex index.php index.html
==========================测试NFS、php、httpd==========================
[root@fast ~]# vim /usr/local/apache2/htdocs/index.php <?php phpinfo(); ?>
[root@fast ~]# vim /usr/local/apache2/htdocs/index.php <?php $link = mysql_connect(‘172.16.41.5‘,‘guomaoqiu‘,‘guo.150019‘); if ($link) echo "Connect to Mysql server success!"; else echo "Connect to Mysql server failure!"; mysql_close(); phpinfo(); ?>
[root@mysql ~]# mysql -uroot -pguo.150019 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.5.33-log MySQL Community Server (GPL) Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> GRANT ALL PRIVILEGES ON *.* TO ‘guomaoqiu‘@‘%‘ IDENTIFIED BY ‘guo.150019‘; Query OK, 0 rows affected (0.03 sec) mysql> select host,user,password from user; +------------------+-----------+-------------------------------------------+ | host | user | password | +------------------+-----------+-------------------------------------------+ | localhost | root | *736C3B5499F163A84CCB2C0F176F3ABC93410FB5 | | mysql.maoqiu.com | root | *736C3B5499F163A84CCB2C0F176F3ABC93410FB5 | | 127.0.0.1 | root | *736C3B5499F163A84CCB2C0F176F3ABC93410FB5 | | % | guomaoqiu | *736C3B5499F163A84CCB2C0F176F3ABC93410FB5 | | % | worduser | *736C3B5499F163A84CCB2C0F176F3ABC93410FB5 | +------------------+-----------+-------------------------------------------+ 5 rows in set (0.00 sec) mysql> FLUSH PRIVILEGES ; Query OK, 0 rows affected (0.07 sec) mysql> quit
在客户端输入http://www.maoqiu.com
ok,链接数据库成功,最后一步就是搭建一个论坛看一下是否能同步数据:
==================================安装phpwind==================================
1.解压下载到的phpwind_v9.0_utf8.zip论坛软件包
[root@fast ~]# unzip phpwind_v9.0_utf8.zip [root@fast ~]# cd phpwind_v9.0_utf8 [root@fast phpwind_v9.0_utf8]# cd upload/ #将这个包里面的文件全部移动到nfs服务器的/usr/local/apache2/htdocs目录中 [root@fast upload]# mv * /usr/local/apache2/htdocs/ mv: overwrite `/usr/local/apache2/htdocs/index.php‘? y #因为这个软件包里面有index.php这个文件,所以我们直接覆盖之前的那个测试文件 [root@fast upload]#
2.在任意一台web服务器上面进行安装(我这里使用http://172.16.41.1去装)
[root@fast htdocs]# chmod -R a+rw attachment conf data html src themes windid
权限已经改过来了,现在继续...
[root@mysql ~]# mysql -uroot -pguo.150019 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.5.33-log MySQL Community Server (GPL) Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement. mysql> CREATE DATABASE winddb; Query OK, 1 row affected (0.05 sec) mysql> GRANT ALL PRIVILEGES ON winddb.* TO ‘winduser‘@‘%‘ IDENTIFIED by ‘guo.150019‘; Query OK, 0 rows affected (0.03 sec) mysql> FLUSH PRIVILEGES ; Query OK, 0 rows affected (0.07 sec) mysql>
======================测试两台服务器是否能同步数据======================
1.在172.16.41.1这台web服务器上发帖
ok,看来我的两台服务器现在已经在用NFS网络共享文件系统了,那我再用域名登录一下呢
ok,看来我的两台服务器现在已经在用NFS网络共享文件系统了,那我再用域名登录一下呢
本文出自 “一叶知秋” 博客,请务必保留此出处http://maoqiu.blog.51cto.com/8570467/1386643