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

 

具体实现方案:    

        

双网负载均衡 双wan负载均衡_双网负载均衡

  上图中的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. # 
    34. # nginx - this script starts and stops the nginx daemon 
    35. # 
    36. # chkconfig:   - 85 15  
    37. # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \ 
    38. #               proxy and IMAP/POP3 proxy server 
    39. # processname: nginx 
    40. # config:      /etc/nginx/nginx.conf 
    41. # config:      /etc/sysconfig/nginx 
    42. # pidfile:     /var/run/nginx.pid 
    43.   
    44. # Source function library. 
    45. . /etc/rc.d/init.d/functions 
    46.   
    47. # Source networking configuration. 
    48. . /etc/sysconfig/network 
    49.   
    50. # Check that networking is up. 
    51. [ "$NETWORKING" = "no" ] && exit 0 
    52.   
    53. nginx="/usr/sbin/nginx"
    54. prog=$(basename $nginx) 
    55.   
    56. NGINX_CONF_FILE="/etc/nginx/nginx.conf"
    57.   
    58. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 
    59.   
    60. lockfile=/var/lock/subsys/nginx 
    61.   
    62. make_dirs() { 
    63.    # make required directories 
    64. user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` 
    65. options=`$nginx -V 2>&1 | grep 'configure arguments:'` 
    66.    for opt in $options; do 
    67.        if [ `echo $opt | grep '.*-temp-path'` ]; then 
    68. value=`echo $opt | cut -d "=" -f 2` 
    69.            if [ ! -d "$value" ]; then 
    70.                # echo "creating" $value 
    71.                mkdir -p $value && chown -R $user $value 
    72.            fi 
    73.        fi 
    74.    done 
    75. } 
    76.   
    77. start() { 
    78.     [ -x $nginx ] || exit 5 
    79.     [ -f $NGINX_CONF_FILE ] || exit 6 
    80.     make_dirs 
    81.     echo -n $"Starting $prog: " 
    82.     daemon $nginx -c $NGINX_CONF_FILE 
    83. retval=$? 
    84.     echo 
    85.     [ $retval -eq 0 ] && touch $lockfile 
    86.     return $retval 
    87. } 
    88.   
    89. stop() { 
    90.     echo -n $"Stopping $prog: " 
    91.     killproc $prog -QUIT 
    92. retval=$? 
    93.     echo 
    94.     [ $retval -eq 0 ] && rm -f $lockfile 
    95.     return $retval 
    96. } 
    97.   
    98. restart() { 
    99.     configtest || return $? 
    100.     stop 
    101.     sleep 1 
    102.     start 
    103. } 
    104.   
    105. reload() { 
    106.     configtest || return $? 
    107.     echo -n $"Reloading $prog: " 
    108.     killproc $nginx -HUP 
    109. RETVAL=$? 
    110.     echo 
    111. } 
    112.   
    113. force_reload() { 
    114.     restart 
    115. } 
    116.   
    117. configtest() { 
    118.   $nginx -t -c $NGINX_CONF_FILE 
    119. } 
    120.   
    121. rh_status() { 
    122.     status $prog 
    123. } 
    124.   
    125. rh_status_q() { 
    126. >/dev/null 2>&1 
    127. } 
    128.   
    129. case "$1" in 
    130.     start) 
    131.         rh_status_q && exit 0 
    132.         $1 
    133.         ;; 
    134.     stop) 
    135.         rh_status_q || exit 0 
    136.         $1 
    137.         ;; 
    138.     restart|configtest) 
    139.         $1 
    140.         ;; 
    141.     reload) 
    142.         rh_status_q || exit 7 
    143.         $1 
    144.         ;; 
    145.     force-reload) 
    146.         force_reload 
    147.         ;; 
    148.     status) 
    149.         rh_status 
    150.         ;; 
    151.     condrestart|try-restart) 
    152.         rh_status_q || exit 0 
    153.             ;; 
    154.     *) 
    155.         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
    156.         exit 2 
    157. esac 
    158. 添加到服务列表并使开机启动 
    159. # chkconfig --add nginx 
    160. # chkconfig nginx on 
      启动nginx并测试一下是否正常工作。
     
      修改nginx的配置文件
     
       
      
    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.110: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
    25.                root    html; 
    26.           }  
    27.      } 
    28.     
    29. } 
      编辑/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; }; 
    18. } 
      添加两条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
    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
      为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' >
    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
    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

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


    转载于:https://blog.51cto.com/gdbonnie/934133