本次实验是为了解决单台的web服务器访问压力过大而进行的,这里用双web服务器加单独的Mysql服务器来实现;平台是RedHat Linux 5.8系统。

 

具体实现方案:    

        双web服务器负载均衡配置_Linux

  上图中的web1上将安装:Nginx、DNS、NFS;web2只安装Nginx;Mysql上安装Mysql数据库和PHP。

  当某客户端发送访问请求时,DNS会将请求轮询到web1或web2服务器上,DNS上的算法会计算出该将请求发给哪个服务器响应,基本上是平均的,这样就减小了web服务器的访问压力;

  访问请求到达web服务器后,web服务器会检测客户端请求的是什么内容,如果是静态页面文件,就直接响应客户端;如果是动态的网页内容,就会通过接口送至Mysql服务器上的PHP进行解析,PHP再访问数据库的数据,并将结果返回web服务器,web服务器就响应客户端。

 


步骤:  

  首先每台服务器都要先关闭RedHat的SELinux。

  1. 查看SELinux的当前状态 
  2. #getenforce 
  3. 如果是处于'ermissive',就不用再执行下面的命令了 
  4. #setenfoce 0   关闭 

配置web1

  1.编译安装Nginx

  1. # yum -y install pcre-devel  解决依赖关系  
  2. # groupadd -r nginx  
  3. # useradd -r -g nginx -s /bin/false -M nginx 
  4. # tar xf nginx-1.2.2.tar.gz 
  5. # cd nginx-1.2.2 
  6. # ./configure \ 
  7.   --prefix=/usr \ 
  8.   --sbin-path=/usr/sbin/nginx \ 
  9.   --conf-path=/etc/nginx/nginx.conf \ 
  10.   --error-log-path=/var/log/nginx/error.log \ 
  11.   --http-log-path=/var/log/nginx/access.log \ 
  12.   --pid-path=/var/run/nginx/nginx.pid  \ 
  13.   --lock-path=/var/lock/nginx.lock \ 
  14.   --user=nginx \ 
  15.   --group=nginx \ 
  16.   --with-http_ssl_module \ 
  17.   --with-http_flv_module \ 
  18.   --with-http_stub_status_module \ 
  19.   --with-http_gzip_static_module \ 
  20.   --http-client-body-temp-path=/var/tmp/nginx/client/ \ 
  21.   --http-proxy-temp-path=/var/tmp/nginx/proxy/ \ 
  22.   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \ 
  23.   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ 
  24.   --http-scgi-temp-path=/var/tmp/nginx/scgi \ 
  25.   --with-pcre 
  26. # make 
  27. # maek install 
  28.  
  29. 创建SysV脚本,使之支持服务启动命令 
  30. # vim /etc/rc.d/init.d/nginx   编辑如下内容 
  31.  
  32. #!/bin/sh 
  33. # nginx - this script starts and stops the nginx daemon 
  34. # chkconfig:   - 85 15  
  35. # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \ 
  36. #               proxy and IMAP/POP3 proxy server 
  37. # processname: nginx 
  38. # config:      /etc/nginx/nginx.conf 
  39. # config:      /etc/sysconfig/nginx 
  40. # pidfile:     /var/run/nginx.pid 
  41.   
  42. # Source function library. 
  43. . /etc/rc.d/init.d/functions 
  44.   
  45. # Source networking configuration. 
  46. . /etc/sysconfig/network 
  47.   
  48. # Check that networking is up. 
  49. [ "$NETWORKING" = "no" ] && exit 0 
  50.   
  51. nginx="/usr/sbin/nginx" 
  52. prog=$(basename $nginx) 
  53.   
  54. NGINX_CONF_FILE="/etc/nginx/nginx.conf" 
  55.   
  56. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 
  57.   
  58. lockfile=/var/lock/subsys/nginx 
  59.   
  60. make_dirs() { 
  61.    # make required directories 
  62.    user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` 
  63.    options=`$nginx -V 2>&1 | grep 'configure arguments:'` 
  64.    for opt in $options; do 
  65.        if [ `echo $opt | grep '.*-temp-path'` ]; then 
  66.            value=`echo $opt | cut -d "=" -f 2` 
  67.            if [ ! -d "$value" ]; then 
  68.                # echo "creating" $value 
  69.                mkdir -p $value && chown -R $user $value 
  70.            fi 
  71.        fi 
  72.    done 
  73.   
  74. start() { 
  75.     [ -x $nginx ] || exit 5 
  76.     [ -f $NGINX_CONF_FILE ] || exit 6 
  77.     make_dirs 
  78.     echo -n $"Starting $prog: " 
  79.     daemon $nginx -c $NGINX_CONF_FILE 
  80.     retval=$? 
  81.     echo 
  82.     [ $retval -eq 0 ] && touch $lockfile 
  83.     return $retval 
  84.   
  85. stop() { 
  86.     echo -n $"Stopping $prog: " 
  87.     killproc $prog -QUIT 
  88.     retval=$? 
  89.     echo 
  90.     [ $retval -eq 0 ] && rm -f $lockfile 
  91.     return $retval 
  92.   
  93. restart() { 
  94.     configtest || return $? 
  95.     stop 
  96.     sleep 1 
  97.     start 
  98.   
  99. reload() { 
  100.     configtest || return $? 
  101.     echo -n $"Reloading $prog: " 
  102.     killproc $nginx -HUP 
  103.     RETVAL=$? 
  104.     echo 
  105.   
  106. force_reload() { 
  107.     restart 
  108.   
  109. configtest() { 
  110.   $nginx -t -c $NGINX_CONF_FILE 
  111.   
  112. rh_status() { 
  113.     status $prog 
  114.   
  115. rh_status_q() { 
  116.     rh_status >/dev/null 2>&1 
  117.   
  118. case "$1" in 
  119.     start) 
  120.         rh_status_q && exit 0 
  121.         $1 
  122.         ;; 
  123.     stop) 
  124.         rh_status_q || exit 0 
  125.         $1 
  126.         ;; 
  127.     restart|configtest) 
  128.         $1 
  129.         ;; 
  130.     reload) 
  131.         rh_status_q || exit 7 
  132.         $1 
  133.         ;; 
  134.     force-reload) 
  135.         force_reload 
  136.         ;; 
  137.     status) 
  138.         rh_status 
  139.         ;; 
  140.     condrestart|try-restart) 
  141.         rh_status_q || exit 0 
  142.             ;; 
  143.     *) 
  144.         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
  145.         exit 2 
  146. esac 
  147. 添加到服务列表并使开机启动 
  148. # chkconfig --add nginx 
  149. # chkconfig nginx on 

  启动nginx并测试一下是否正常工作。

 

  修改nginx的配置文件

  1. worker_processes  2; 
  2. events { 
  3.     worker_connections   50000; 
  4. http { 
  5.     include       mime.types; 
  6.     default_type  application/octet-stream; 
  7.     sendfile      on; 
  8.     keepalive_timeout   65; 
  9.     server { 
  10.          listen       172.16.3.110:80; 
  11.          server_name  localhost; 
  12.          location / { 
  13.               root    html; 
  14.               index   index.html index.htm index.php; 
  15.          } 
  16.          location ~ \.php$ { 
  17.               root    html; 
  18.               fastcgi_pass   172.16.3.112:9000; 
  19.               fastcgi_index  inedx.php; 
  20.               fastcgi_param  SCRIPT_FILENAME    /scripts$fastcgi_script_name; 
  21.               include        fastcgi_params;       
  22.          error_page    500 502 503 504   /50x.html; 
  23.          location = /50x.html { 
  24.                root    html; 
  25.           }  
  26.      } 
  27.     

  编辑/etc/nginx/fastcgi_params,将内容替换

  1. fastcgi_param  GATEWAY_INTERFACE  CGI/1.1; 
  2. fastcgi_param  SERVER_SOFTWARE    nginx; 
  3. fastcgi_param  QUERY_STRING       $query_string; 
  4. fastcgi_param  REQUEST_METHOD     $request_method; 
  5. fastcgi_param  CONTENT_TYPE       $content_type; 
  6. fastcgi_param  CONTENT_LENGTH     $content_length; 
  7. fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name; 
  8. fastcgi_param  SCRIPT_NAME        $fastcgi_script_name; 
  9. fastcgi_param  REQUEST_URI        $request_uri; 
  10. fastcgi_param  DOCUMENT_URI       $document_uri; 
  11. fastcgi_param  DOCUMENT_ROOT      $document_root; 
  12. fastcgi_param  SERVER_PROTOCOL    $server_protocol; 
  13. fastcgi_param  REMOTE_ADDR        $remote_addr; 
  14. fastcgi_param  REMOTE_PORT        $remote_port; 
  15. fastcgi_param  SERVER_ADDR        $server_addr; 
  16. fastcgi_param  SERVER_PORT        $server_port; 
  17. fastcgi_param  SERVER_NAME        $server_name; 

配置NFS共享

  1. # vim /etc/exports 
  2. /etc/nginx/mnt 172.16.0.0/16 (rw,no_root_squash,) 

  编译安装DNS

  1. # yum -y install bind97 bind97-devel bind97-libs bind97-utils  


  修改主配置文件/etc/named.conf
  1. 修改文件中的zone部分 
  2. zone "web1.com" IN { 
  3.            type master; 
  4.            file "web1.com.zone"; 
  5.            allow-update{ none; }; 
  6. }; 
  7.  
  8. zone "web2.com" IN { 
  9.            type master; 
  10.            file "web2.com.zone"; 
  11.            allow-update{ none; }; 
  12. }; 
  13.  
  14. zone "mp.com" IN { 
  15.            type master; 
  16.            file "mp.com.zone"; 
  17.            allow-update{ none; }; 

  添加两条A记录

  1. $TTL  600 
  2. @      IN  SOA    ns.web1.com.   admin.web1.com.  ( 
  3.                                    0     ; serial 
  4.                                    1D    ; refresh 
  5.                                    1H    ; retry 
  6.                                    1W    ; expire 
  7.                                    3H )  ; minimum  
  8.       IN   NS      ns.web.com 
  9. ns    IN    A      172.16.3.110 
  10. www   IN    A   172.16.3.110 
  11. www   IN    A      172.16.3.111 

2.配置web2

  这里安装Nginx与web1相同,只是蟹盖配置文件时有些不同

  1. worker_processes  2;  
  2. events {  
  3.     worker_connections   50000;  
  4. }  
  5. http {  
  6.     include       mime.types;  
  7.     default_type  application/octet-stream;  
  8.     sendfile      on;  
  9.     keepalive_timeout   65;  
  10.     server {  
  11.          listen       172.16.3.111:80;  
  12.          server_name  localhost;  
  13.          location / {  
  14.               root    html;  
  15.               index   index.html index.htm index.php;  
  16.          }  
  17.          location ~ \.php$ {  
  18.               root    html;  
  19.               fastcgi_pass   172.16.3.112:9000;  
  20.               fastcgi_index  inedx.php;  
  21.               fastcgi_param  SCRIPT_FILENAME    /scripts$fastcgi_script_name;  
  22.               include        fastcgi_params;        
  23.          error_page    500 502 503 504   /50x.html;  
  24.          location = /50x.html {  
  25.                root    html;  
  26.           }   
  27.      }  
  28.      
  29. }  

 

3.配置Mysql服务器

  编译安装Mysql数据库  

  将下载好的Mysql源码包进行编译安装

  1. # tar xf mysql-5.5.24-linux2.6-i686.tar.gz -C /usr/local 
  2. # cd /usr/local/ 
  3. # ln -sv mysql-5.5.24-linux2.6-i686  mysql 
  4. # cd mysql  
  5. # chown -R mysql:mysql  . 
  6. # scripts/mysql_install_db --user=mysql --datadir=/mydata/data 
  7. # chown -R root  . 

修改配置文件

  1. # cd /usr/local/mysql 
  2. # cp support-files/my-large.cnf  /etc/my.cnf   软件提供的样本 
  3. 修改 
  4. thread_concurrency = 2  
  5. datadir = /mydata/data 

  为Mysql提供服务启动脚本

  1. # cd /usr/local/mysql 
  2. # cp support-files/mysql.server  /etc/rc.d/init.d/mysqld 
  3. # chkconfig --add mysqld 
  4. # chkconfig mysqld on 

  这样Mysql就可以启动了,但是因为是编译安装,所以仅仅这样很不符合系统使用规范,我们就把它做的规范些

  1.  输出man手册 
  2. # vim /etc/man.config    添加 
  3. MANPATH  /usr/local/mysql/man   
  4. 输出头文件到指定的位置 
  5. # ln -sv /usr/local/mysql/include  /usr/include/mysql 
  6. 输出库文件的路径 
  7. # echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf 
  8. 设置PATH的环境变量 
  9. # vim /etc/profile 
  10. PATH=/usr/local/mysql/bin:#PATH 

  然后重新载入下:#ldconfig

  编译安装PHP

  1. # tar xf php-5.4.4.tar.bz2 
  2. # cd php-5.4.4 
  3. #  ./configure --prefix=/usr/local/php4nginx  
  4. --with-mysql=/usr/local/mysql --with-openssl --enable-fpm  
  5. --enable-sockets --enable-sysvshm   
  6. --with-mysqli=/usr/local/mysql/bin/mysql_config  
  7. --enable-mbstring --with-freetype-dir --with-jpeg-dir  
  8. --with-png-dir --with-zlib-dir --with-libxml-dir=/usr  
  9. --enable-xml  --with-mhash --with-mcrypt  --with-config-file-path=/etc  
  10. --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl 
  11. # make # make test # make intall 

  为PHP提供配置文件和服务启动脚本

  1. # cp php.ini-production /etc/php.ini 
  2. # cp sapi/fpm/init.d.php-fpm  /etc/rc.d/init.d/php-fpm 
  3. # chmod +x /etc/rc.d/init.d/php-fpm 
  4. # chkconfig --add php-fpm 
  5. # chkconfig php-fpm on 

  设置php-fpm参数

  1. 首先把PHP提供的配置文件样本复制并重命名 
  2. # cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf  
  3.  
  4. # vim /usr/local/php/etc/php-fpm.conf 
  5.  pm.max_children = 50 
  6.  pm.start_servers = 5 
  7.  pm.min_spare_servers = 2 
  8.  pm.max_spare_servers = 8 
  9.  pid = /usr/local/php/var/run/php-fpm.pid  
  10. 启动并验证 
  11. # service php-fpm start 
  12. # ps aux | grep php-fpm 

  PHP算是配置好了,可是为了系统更给力的工作,给PHP安装xcache加速器吧

  1. # tar xf xcache-2.0.0.tar.gz 
  2. # cd xcache-2.0.0 
  3. # /usr/local/php/bin/phpize 
  4. # ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config 
  5. # make && make install 
  6. 安装完成后,会出现一个 
  7. Installing shared extensions: ..............的字符串,把冒号后面的复制下备用 
  8. 编辑php.ini就可以将exache跟php整合到一块了 
  9. 把xcache提供的样例配置导入php.ini 
  10. # mkdir /etc/php.d 
  11. # cp /xcache/xcache.ini /etc/php.d 
  12. 编辑/etc/php.d,找到zend_extension开头行,将复制的字符串粘进去 
  13. 然后重启php-fpm 
  14. # service php-fpm restart 

 

 

(对与写博客,也就是对平常知识的总结,这是需要下大功夫的,并且很有必要;也不是说不会总结,只是,像这种写成文章来,还真是不好搞,写着写着就乱了,也看了很多写的好的文章、博客,跟人家一比较还真不好意思拿出来让大家看了;想想这个也只有多练了。不唠叨了,苦练吧!)