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

两台Web服务器 实现负载均衡 +FastCGI模块+MySQL实现分布式架构

[日期:2012-07-20] 来源:Linux社区  作者:单人旅行 [字体: ]
两台Web服务器 实现负载均衡 +FastCGI模块+MySQL实现分布式架构
 
操作系统RedHat 5.8
 
实验环境准备 :三台服务器 
Server 1  IP :172.16.2.1    安装:apache  NFS
Server 2  IP :172.16.2.2    安装:apache  DNS  
Server 3  IP:172.16..2.3    安装:php  mysql
实验图如下 :
 

工作原理:
1、当客户端请求www.tast.com 
2、Server 2 上的DNS会以轮询的方式把请求转给 172.16.2.1 和172.16.2.2 
3、如果此次请求是静态页面 web服务就直接把结果返给客户 ,如果是动态页面,就转给server 3 上的php 解析
4、如果需要用到数据库的数据,就通过数据库的接口访问数据库 
5、Php解析后把结果返回给前端的web
6、Web把结果返回给客户 
步骤
1、配置server 1
          安装配置 apache 和 NFS  
2、配置 server 2
          安装配置apache 和DNS
3、配置 server 3 
          安装配置 mysql 和 php 
4、总结
注:此次试验只是给大家提供一种思路,可以这么配置,企业实际需求中,不同的需求,不同的操作系统,不同的软件版本,配置过程会略有不同,但原理都是想通了,希望大家在做实验的同时不要忘了思考,学而不思则罔,原理理解了,我们就可以根据实际需求搭建出更适合自己需要的服务。 
过程

一、配置server 1

1.1 配置编译安装环境 

   由于此次试验每个软件都是编译安装,所以每台服务器都要先配置好编译安装环境 
确保这两个组安装 
 
  1. Development Tools 
  2. Development Libraries 
  3. #yum  -y groupinstall "Development Libraries"   

1.2  安装httpd 2.4.2 版本 

      
  1. #yum -y install pcre-devel  解决依赖关系  
  2. # tar xf httpd-2.4.2.tar.bz2 
  3. # cd httpd-2.4.2 
  4. # less INSTALL   查看安装说明  
  5.  $ ./configure --prefix=PREFIX 
  6.      $ make 
  7.      $ make install 
  8.      $ PREFIX/bin/apachectl start 
  9. # ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd 
  10. --enable-so --enable-rewrite --with-zlib --with-pcre 
  11. --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util
  12.  --enable-modules=most  --enable-ssl    --enable-modules-shared=most  
  13. #make && make install  
1.3 修改httpd的主配置文件,设置其Pid文件的路径
  1. #vim  /etc/httpd/httpd.conf 
  2. idFile  "/var/run/httpd.pid" 添加 
  
1.4 提供SysV服务脚本,内容如下:
 
  1. #vim /etc/rc.d/init.d/httpd

  2. #!/bin/bash 
  3. # httpd        Startup script for the Apache HTTP Server 
  4. # chkconfig: - 85 15 
  5. # description: Apache is a World Wide Web server.  It is used to serve \ 
  6. #          HTML files and CGI. 
  7. # processname: httpd 
  8. # config: /etc/httpd/conf/httpd.conf 
  9. # config: /etc/sysconfig/httpd 
  10. # pidfile: /var/run/httpd.pid 
  11. # Source function library. 
  12. . /etc/rc.d/init.d/functions 
  13. if [ -f /etc/sysconfig/httpd ]; then 
  14.         . /etc/sysconfig/httpd 
  15. fi 
  16. # Start httpd in the C locale by default. 
  17. HTTPD_LANG=${HTTPD_LANG-"C"} 
  18. # This will prevent initlog from swallowing up a pass-phrase prompt if 
  19. # mod_ssl needs a pass-phrase from the user. 
  20. INITLOG_ARGS="" 
  21. # Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server 
  22. # with the thread-based "worker" MPM; BE WARNED that some modules may not 
  23. # work correctly with a thread-based MPM; notably PHP will refuse to start. 
  24. # Path to the apachectl script, server binary, and short-form for messages. 
  25. apachectl=/usr/local/apache/bin/apachectl 
  26. httpd=${HTTPD-/usr/local/apache/bin/httpd} 
  27. prog=httpd 
  28. pidfile=${PIDFILE-/var/run/httpd.pid} 
  29. lockfile=${LOCKFILE-/var/lock/subsys/httpd} 
  30. RETVAL=0 
  31. start() { 
  32.         echo -n $"Starting $prog: " 
  33.         LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS 
  34.         RETVAL=$? 
  35.         echo 
  36.         [ $RETVAL = 0 ] && touch ${lockfile} 
  37.         return $RETVAL 
  38. stop() { 
  39. echo -n $"Stopping $prog: " 
  40. killproc -p ${pidfile} -d 10 $httpd 
  41. RETVAL=$? 
  42. echo 
  43. [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile} 
  44. reload() { 
  45.     echo -n $"Reloading $prog: " 
  46.     if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then 
  47.         RETVAL=$? 
  48.         echo $"not reloading due to configuration syntax error" 
  49.         failure $"not reloading $httpd due to configuration syntax error" 
  50.     else 
  51.         killproc -p ${pidfile} $httpd -HUP 
  52.         RETVAL=$? 
  53.     fi 
  54.     echo 
  55. # See how we were called. 
  56. case "$1" in 
  57.   start) 
  58. start 
  59. ;; 
  60.   stop) 
  61. stop 
  62. ;; 
  63.   status) 
  64.         status -p ${pidfile} $httpd 
  65. RETVAL=$? 
  66. ;; 
  67.   restart) 
  68. stop 
  69. start 
  70. ;; 
  71.   condrestart) 
  72. if [ -f ${pidfile} ] ; then 
  73. stop 
  74. start 
  75. fi 
  76. ;; 
  77.   reload) 
  78.         reload 
  79. ;; 
  80.   graceful|help|configtest|fullstatus) 
  81. $apachectl $@ 
  82. RETVAL=$? 
  83. ;; 
  84.   *) 
  85. echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}" 
  86. exit 1 
  87. esac 
  88. exit $RETVAL 
  89. # chmod +x /etc/rc.d/init.d/httpd   为此脚本赋予执行权 
  90. # chkconfig --add httpd  加入服务列表: 
1.5 安装apache fastcgi模块:  
 
  1. #tar -zxvf mod_fastcgi-current.tar.gz    
  2. #cd mod_fastcgi  
  3. #cp Makefile.AP2 Makefile   
  4. #Vim  Makefile 修改top_dir=/usr/local/apache  #你的apache安装路径 
  5. #make   
  6. #make install 
 1.6修改配置文件 让apache支持php-fpm 
 
  1. 增加: LoadModule fastcgi_module modules/mod_fastcgi.so    
  2. 有的话不用添加。   
  3. ScriptAlias /cgi-bin/ "/usr/local/php/bin/"   
  4. FastCgiExternalServer /usr/local/php/bin/php-fpm -host 172.16.2.3:9000   
  5.   AddType application/x-httpd-php .php   
  6.    AddHandler php-fastcgi .php   
  7.     Action php-fastcgi /cgi-bin/php-fpm   
  8.   
  9.     <Directory "/usr/local/php/bin/">  
  10.     Options -Indexes FollowSymLinks +ExecCGI   
  11.     Order allow,deny   
  12.     Allow from all   
  13.     </Directory>  
  14.   
linux
相关资讯       负载均衡  分布式 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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