天天看点

DNS轮询实现Web Server共享静态资源(多图)

规划如图:

<a href="http://s3.51cto.com/wyfs02/M00/23/30/wKioL1M0JVzBUa1eAAJiz-S8BLE366.jpg" target="_blank"></a>

分析如下:

<a href="http://s3.51cto.com/wyfs02/M02/23/2F/wKiom1M0JZLw282oAAQ_KCHymaA559.jpg" target="_blank"></a>

实验环境准备:

<a href="http://s3.51cto.com/wyfs02/M01/23/30/wKioL1M0JX6xHcHmAARIBQI3SMk521.jpg" target="_blank"></a>

操作步骤:

Step1 DNS

a) 安装bind(dns)服务器

1

<code>yum -y </code><code>install</code> <code>bind</code>

b) 配置cache only的dns服务器

   i) 在 vim /etc/named/named.conf 中输入以下内容

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<code>options {</code>

<code>    </code><code>directory       </code><code>"/var/named"</code><code>;</code>

<code>    </code><code>dump-</code><code>file</code>       <code>"/var/named/data/cache_dump.db"</code><code>;</code>

<code>    </code><code>statistics-</code><code>file</code> <code>"/var/named/data/named_stats.txt"</code><code>;\</code>

<code>    </code><code>allow-query     { any; };  </code><code># 允许任意客户端查询</code>

<code>    </code><code>recursion </code><code>yes</code><code>;</code>

<code>    </code><code>rrset order { random; };   </code><code># dns轮询解析</code>

<code>}</code>

<code>logging {</code>

<code>    </code><code>channel default_debug {</code>

<code>            </code><code>file</code> <code>"data/named.run"</code><code>;</code>

<code>            </code><code>severity dynamic;</code>

<code>    </code><code>};</code>

<code>};</code>

<code>zone </code><code>"."</code> <code>IN {</code>

<code>        </code><code>type</code> <code>hint;</code>

<code>        </code><code>file</code> <code>"named.ca"</code><code>;</code>

<code>include </code><code>"/etc/named.rfc1912.zones"</code><code>;</code>

   ii) 在 vim /etc/named.rfc1912.zones 中输入以下内容

<code>zone </code><code>"king.com"</code> <code>IN {</code>

<code>        </code><code>type</code> <code>master;</code>

<code>        </code><code>file</code> <code>"king.com.zone"</code><code>;   </code><code>#正向解析配置文件</code>

<code>zone </code><code>"43.16.172.in-addr.arpa"</code> <code>IN {</code>

<code>        </code><code>file</code> <code>"172.16.43.zone"</code><code>;  </code><code>#反向解析配置文件</code>

   iii) 在 vim /var/named/king.com.zone 中输入以下内容

<code>$TTL 600</code>

<code>@       IN      SOA     dns.king.com.   admin.king.com. (</code>

<code>                        </code><code>2014032401</code>

<code>                        </code><code>1H</code>

<code>                        </code><code>5M</code>

<code>                        </code><code>3D</code>

<code>                        </code><code>12H )</code>

<code>        </code><code>IN      NS      dns</code>

<code>dns     IN      A       172.16.43.1</code>

<code>www     IN      A       172.16.43.2</code>

<code>www     IN      A       172.16.43.3</code>

   iv) 在 vim /var/named/172.16.43.zone 中输入以下内容

<code>        </code><code>IN      NS      dns.king.com.</code>

<code>1       IN      PTR     dns.king.com.</code>

<code>2       IN      PTR     www.king.com.</code>

<code>3       IN      PTR     www.king.com.</code>

   v) 更改iii,iv步的权限及属主属组    

<code>chown</code> <code>root:named </code><code>/var/named/</code><code>*.zone</code>

<code>chmod</code> <code>640 </code><code>/var/named/</code><code>*.zone</code>

<code>service named start</code>

   vi) 测试:

<code># vim /etc/resolv.conf 更改 DNS1=172.16.43.1</code>

<code>dig</code> <code>-t A www.king.com</code>

<a href="http://s3.51cto.com/wyfs02/M00/23/2F/wKiom1M0JbLChkw_AATXWmupGVg043.jpg" target="_blank"></a>

   客户端查询,更改物理机DNS

<a href="http://s3.51cto.com/wyfs02/M02/23/30/wKioL1M0JY_BlpuqAAGctoSZSUo500.jpg" target="_blank"></a>

   测试如下:

<a href="http://s3.51cto.com/wyfs02/M02/23/30/wKioL1M0JZzAhg1QAAUYqqvv8Zw377.jpg" target="_blank"></a>

   反解查询:

<code>dig</code> <code>-t PTR -x 172.16.43.2</code>

<a href="http://s3.51cto.com/wyfs02/M01/23/2F/wKiom1M0Jc2TmVLgAAUCO4D2OQ0670.jpg" target="_blank"></a>

step2 apache

   a) 为安装准备环境    

<code>rpm -e httpd --nodeps</code>

<code>rm</code> <code>-rf </code><code>/etc/httpd</code>

<code>rm</code> <code>-rf </code><code>/usr/lib64/httpd</code>

<code>yum -y </code><code>install</code> <code>pcre-devel</code>

   b) 其余手动安装配置

   i) 配置安装apr-1.5.0

<code>tar</code> <code>xf apr-1.5.0.</code><code>tar</code><code>.bz2</code>

<code>cd</code> <code>apr-1.5.0</code>

<code>.</code><code>/configure</code> <code>--prefix=</code><code>/usr/local/apr</code>

<code>make</code> <code>&amp;&amp; </code><code>make</code> <code>install</code>

   ii) 配置安装apr-util-1.5.3

<code>tar</code> <code>xf apr-util-1.5.3.</code><code>tar</code><code>.bz2</code>

<code>cd</code> <code>apr-util-1.5.3</code>

<code>.</code><code>/configure</code> <code>--prefix=</code><code>/usr/local/apr-util</code> <code>--with-apr=</code><code>/usr/local/apr</code>

   iii) 配置安装httpd-2.4.9    

<code>tar</code> <code>xf httpd-2.4.9.</code><code>tar</code><code>.bz2</code>

<code>cd</code> <code>httpd-2.4.9</code>

<code>.</code><code>/configure</code> <code>--prefix=</code><code>/usr/local/apache</code> <code>--sysconfdir=</code><code>/etc/httpd</code> <code>--</code><code>enable</code><code>-so --</code>

<code>enable</code><code>-ssl --</code><code>enable</code><code>-cgi --</code><code>enable</code><code>-rewrite --with-zlib --with-pcre --with-</code>

<code>apr=</code><code>/usr/local/apr</code> <code>--with-apr-util=</code><code>/usr/local/apr-util</code> <code>--</code><code>enable</code><code>-modules=most --</code>

<code>enable</code><code>-mpms-shared=all --with-mpm=event</code>

   c) 配置开机服务及环境变量

   在 vim /etc/rc.d/init.d/httpd 编辑此项

   请参考 http://apprentice.blog.51cto.com/2214645/1380490

   在 vim /etc/httpd/httpd.conf 添加此项    

<code>PidFile </code><code>"/var/run/httpd.pid"</code>

   # 导出环境变量

<code>echo</code> <code>"PATH=/usr/local/apache/bin:$PATH"</code> <code>&gt; </code><code>/etc/profile</code><code>.d</code><code>/httpd</code><code>.sh</code>

   # 重载环境变量

<code>. </code><code>/etc/profile</code><code>.d</code><code>/httpd</code><code>.sh</code>

   # 启动/添加开机服务

<code>chkconfig --add httpd</code>

<code>chkconfig httpd on</code>

<code>service httpd start</code>

   d) 测试前3台机器是否工作正常

<a href="http://s3.51cto.com/wyfs02/M01/23/2F/wKiom1M0K52RjUf7AAP0STbEAQo023.jpg" target="_blank"></a>

step3 nfs,php-fpm

   a) 安装nfs工具

   # 如果机器内核没有编译nfs则需要安装

<code>yum -y </code><code>install</code> <code>nfs-utils</code>

<code>mkdir</code> <code>/nfsshared</code>

   在 vim /etc/exports 编辑此项

   # 为172.16.43.0/24此段开放加载nfs网络文件功能

<code>/nfsshared</code>   <code>172.16.43.0</code><code>/24</code><code>(rw,async,no_root_squash)</code>

   # 添加客户端准备操作的账户,防止nobody属组文件出现

<code>useradd</code> <code>-r -s </code><code>/sbin/nologin</code> <code>daemon</code>

   # 导出所有挂载分区

<code>exportfs -a</code>

<code>service nfs restart</code>

   b) 客户端测试挂载

<code>showmount -e 172.16.43.4</code>

<code>mkdir</code> <code>/share</code>

   # 挂载网络文件系统

<code>mount</code> <code>-t nfs 172.16.43.4:</code><code>/nfsshared</code> <code>/share</code>

   # 开机挂载可编辑 /etc/fstab

<code>172.16.43.4:</code><code>/nfsshared</code> <code>/share</code>  <code>ext4</code>

   # 测试nfs, 在/nfsshared中touch文件,观察2个客户端(web1,web2)

<a href="http://s3.51cto.com/wyfs02/M01/23/30/wKioL1M0JbqwNomrAANfqYVa0Ec718.jpg" target="_blank"></a>

c) 安装php-fpm

   i) 解决php安装依赖

<code>yum -y groupinstall </code><code>"Desktop Platform Development"</code>

<code>yum -y </code><code>install</code> <code>libmcrypt-devel</code>

<code>yum -y </code><code>install</code> <code>bzip2</code><code>-devel</code>

   ii) 安装php with fpm

<code>tar</code> <code>xf php-5.4.19.</code><code>tar</code><code>.bz2</code>

<code>cd</code> <code>php-5.4.19</code>

<code>.</code><code>/configure</code> <code>--prefix=</code><code>/usr/local/php</code> <code>--with-mysql=mysqlnd --with-pdo-</code>

<code>mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --</code><code>enable</code><code>-mbstring --with-</code>

<code>freetype-</code><code>dir</code> <code>--with-jpeg-</code><code>dir</code> <code>--with-png-</code><code>dir</code> <code>--with-zlib --with-libxml-</code><code>dir</code><code>=</code><code>/usr</code> <code>--</code>

<code>enable</code><code>-xml --</code><code>enable</code><code>-sockets --</code><code>enable</code><code>-fpm --with-mcrypt --with-config-</code><code>file</code><code>-</code>

<code>path=</code><code>/etc</code> <code>--with-config-</code><code>file</code><code>-scan-</code><code>dir</code><code>=</code><code>/etc/php</code><code>.d --with-bz2</code>

   iii) 为php提供配置文件:

<code>cp</code> <code>php.ini-production </code><code>/etc/php</code><code>.ini</code>

   iv) 配置php-fpm

   为php-fpm提供SysV init脚本,并将其添加至服务列表:

<code>cp</code> <code>sapi</code><code>/fpm/init</code><code>.d.php-fpm  </code><code>/etc/rc</code><code>.d</code><code>/init</code><code>.d</code><code>/php-fpm</code>

<code>chmod</code> <code>+x </code><code>/etc/rc</code><code>.d</code><code>/init</code><code>.d</code><code>/php-fpm</code>

<code>chkconfig --add php-fpm</code>

<code>chkconfig php-fpm on</code>

   为php-fpm提供配置文件:

<code>cp</code> <code>/usr/local/php/etc/php-fpm</code><code>.conf.default </code><code>/usr/local/php/etc/php-fpm</code><code>.conf</code>

   编辑php-fpm的配置文件:vim /usr/local/php/etc/php-fpm.conf

   配置fpm的相关选项为你所需要的值,并启用pid文件(如下最后一行):

<code>pid = </code><code>/usr/local/php/var/run/php-fpm</code><code>.pid</code>

<code>listen = 172.16.43.4:9000</code>

   vi) 编辑apache为运行php后端程序做准备

   # 分别编辑 172.16.43.2/3中的 vim /etc/httpd/httpd.conf

<code>DocumentRoot </code><code>"/share"</code>

<code>&lt;Directory </code><code>"/share"</code><code>&gt;</code>

   # 开启fcgi转发

<code>LoadModule proxy_module modules</code><code>/mod_proxy</code><code>.so</code>

<code>LoadModule proxy_fcgi_module modules</code><code>/mod_proxy_fcgi</code><code>.so</code>

   # 禁止apache正向代理,转而实现反向代理后端的php-fpm

<code>ProxyRequests Off</code>

<code>ProxyPassMatch ^/(.*\.php)$ fcgi:</code><code>//172</code><code>.16.43.4:9000</code><code>/nfsshared</code>

   # 添加apache识别php文件

<code>AddType application</code><code>/x-httpd-php</code>  <code>.php</code>

<code>AddType application</code><code>/x-httpd-php-source</code>  <code>.phps</code>

   # 定位至DirectoryIndex

<code>DirectoryIndex  index.php  index.html</code>

   d) 安装配置Discuz在 /nfsshared

<code>cd</code> <code>/nfsshared</code>

   # 下载Discuz最新版本到此目录

<code>unzip Discuz_X3.1_SC_UTF8.zip</code>

<code>rm</code> <code>-rf Discuz_X3.1_SC_UTF8.zip readme/ utility/</code>

<code>mv</code> <code>.</code><code>/upload/</code><code>* ./</code>

<code>rm</code> <code>-rf upload/</code>

   e) 测试 php页面解析

   安装discuz需要将 config , data , uc_client , uc_server 的权限设置为777

<a href="http://s3.51cto.com/wyfs02/M02/23/30/wKioL1M0JdShwrovAAiKKTTVwNw057.jpg" target="_blank"></a>

step4 mysql

   i) 创建mysql的数据目录

<code>mkdir</code> <code>/data</code>

<code>groupadd -r mysql</code>

<code>useradd</code> <code>-g mysql -r -s </code><code>/sbin/nologin</code> <code>-M -d </code><code>/data</code> <code>mysql</code>

<code>chown</code> <code>-R mysql:mysql </code><code>/data</code>

   ii) 安装二进制mysql

<code>tar</code> <code>xf mysql-5.5.33-linux2.6-x86_64.</code><code>tar</code><code>.gz -C </code><code>/usr/local</code>

<code>cd</code> <code>/usr/local</code>

<code>ln</code> <code>-sv mysql-5.5.33-linux2.6-x86_64 mysql</code>

<code>cd</code> <code>mysql</code>

<code>chown</code> <code>-R mysql:mysql  .</code>

<code>mysql</code><code>/scripts/mysql_install_db</code> <code>--user=mysql --datadir=</code><code>/data</code>

<code>chown</code> <code>-R root  .</code>

   # 提供mysql的配置文件

<code>cp</code> <code>support-files</code><code>/my-large</code><code>.cnf  </code><code>/etc/my</code><code>.cnf</code>

   # 需要添加如下行指定mysql数据文件的存放位置:

<code>datadir = </code><code>/mydata/data</code>

   iii) 为mysql提供sysv服务脚本:

<code>cd</code> <code>/usr/local/mysql</code>

<code>cp</code> <code>support-files</code><code>/mysql</code><code>.server  </code><code>/etc/rc</code><code>.d</code><code>/init</code><code>.d</code><code>/mysqld</code>

<code>chmod</code> <code>+x </code><code>/etc/rc</code><code>.d</code><code>/init</code><code>.d</code><code>/mysqld</code>

   添加至服务列表:

<code>chkconfig --add mysqld</code>

<code>chkconfig mysqld on</code>

<code>echo</code> <code>"export PATH=/usr/local/mysql/bin:$PATH"</code> <code>&gt; </code><code>/etc/profile</code><code>.d</code><code>/mysql</code><code>.sh</code>

<code>. </code><code>/etc/profile</code><code>.d</code><code>/mysql</code><code>.sh</code>

   iv) 启动服务并授权php服务器账号访问

<code>service mysqld restart</code>

<code>mysql</code>

<code>grant all on *.* to </code><code>'root'</code><code>@</code><code>'172.16.43.4'</code> <code>identified by </code><code>'123456'</code><code>;</code>

<code>flush privileges;</code>

<a href="http://s3.51cto.com/wyfs02/M01/23/2F/wKiom1M0JgTzISq0AAWStIN1Hzg786.jpg" target="_blank"></a>

   v) 继续Step3  e)的安装过程,安装完成后

<a href="http://s3.51cto.com/wyfs02/M00/23/30/wKioL1M0JeDRsIs5AATEDiVyeTw663.jpg" target="_blank"></a>

<a href="http://s3.51cto.com/wyfs02/M00/23/30/wKioL1M0JeqRHnG-AAQVDLaxPB4183.jpg" target="_blank"></a>

   vi) 继续测试由172.16.43.2上传静态文件测试172.16.43.3是否能共享此静态文件

<a href="http://s3.51cto.com/wyfs02/M02/23/2F/wKiom1M0JkTAyXRqAAXeWKM1aMo556.jpg" target="_blank"></a>

<a href="http://s3.51cto.com/wyfs02/M02/23/30/wKioL1M0JoaTumpQAAZAQqsnsYQ962.jpg" target="_blank"></a>

   vii) 查看文件情况

<a href="http://s3.51cto.com/wyfs02/M01/23/2F/wKiom1M0Jr-QAxgzAAR3Lez4zsE802.jpg" target="_blank"></a>

Step5 xache

   i) 安装xcache

<code>tar</code> <code>xf xcache-3.0.3.</code><code>tar</code><code>.bz2</code>

<code>cd</code> <code>xcache-3.0.3</code>

<code>/usr/local/php/bin/phpize</code>

<code>.</code><code>/configure</code> <code>--</code><code>enable</code><code>-xcache --with-php-config=</code><code>/usr/local/php/bin/php-config</code>

   # 安装结束时,会出现类似如下行, 将后半句复制

<code>Installing shared extensions:     </code><code>/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/</code>

   ii) 编辑php.ini,整合php和xcache:

   # 首先将xcache提供的样例配置导入php.ini

<code>mkdir</code> <code>/etc/php</code><code>.d</code>

   # xcache.ini文件在xcache的源码目录中。

<code>cp</code> <code>xcache.ini </code><code>/etc/php</code><code>.d</code>

   # 接下来编辑/etc/php.d/xcache.ini 修改为如下:

<code>extension = </code><code>/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/xcache</code><code>.so</code>

   # 启动php-fpm服务

<code>service php-fpm restart</code>

   iii)  在 /nfsshared 中建立 testxcache.php 文件测试xcache的启用情况

<a href="http://s3.51cto.com/wyfs02/M00/23/30/wKioL1M0JqTj_IuTAAThrE3L4sU217.jpg" target="_blank"></a>

     本文转自My_King1 51CTO博客,原文链接:http://blog.51cto.com/apprentice/1385584,如需转载请自行联系原作者