天天看點

雙web伺服器負載均衡配置

  本次實驗是為了解決單台的web伺服器通路壓力過大而進行的,這裡用雙web伺服器加單獨的Mysql伺服器來實作;平台是RedHat Linux 5.8系統。

具體實作方案:    

雙web伺服器負載均衡配置

  上圖中的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. 建立SysV腳本,使之支援服務啟動指令 
  29. # vim /etc/rc.d/init.d/nginx   編輯如下内容 
  30. #!/bin/sh 
  31. # nginx - this script starts and stops the nginx daemon 
  32. # chkconfig:   - 85 15  
  33. # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \ 
  34. #               proxy and IMAP/POP3 proxy server 
  35. # processname: nginx 
  36. # config:      /etc/nginx/nginx.conf 
  37. # config:      /etc/sysconfig/nginx 
  38. # pidfile:     /var/run/nginx.pid 
  39. # Source function library. 
  40. . /etc/rc.d/init.d/functions 
  41. # Source networking configuration. 
  42. . /etc/sysconfig/network 
  43. # Check that networking is up. 
  44. [ "$NETWORKING" = "no" ] && exit 0 
  45. nginx="/usr/sbin/nginx" 
  46. prog=$(basename $nginx) 
  47. NGINX_CONF_FILE="/etc/nginx/nginx.conf" 
  48. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 
  49. lockfile=/var/lock/subsys/nginx 
  50. make_dirs() { 
  51.    # make required directories 
  52.    user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` 
  53.    options=`$nginx -V 2>&1 | grep 'configure arguments:'` 
  54.    for opt in $options; do 
  55.        if [ `echo $opt | grep '.*-temp-path'` ]; then 
  56.            value=`echo $opt | cut -d "=" -f 2` 
  57.            if [ ! -d "$value" ]; then 
  58.                # echo "creating" $value 
  59.                mkdir -p $value && chown -R $user $value 
  60.            fi 
  61.        fi 
  62.    done 
  63. start() { 
  64.     [ -x $nginx ] || exit 5 
  65.     [ -f $NGINX_CONF_FILE ] || exit 6 
  66.     make_dirs 
  67.     echo -n $"Starting $prog: " 
  68.     daemon $nginx -c $NGINX_CONF_FILE 
  69.     retval=$? 
  70.     echo 
  71.     [ $retval -eq 0 ] && touch $lockfile 
  72.     return $retval 
  73. stop() { 
  74.     echo -n $"Stopping $prog: " 
  75.     killproc $prog -QUIT 
  76.     retval=$? 
  77.     echo 
  78.     [ $retval -eq 0 ] && rm -f $lockfile 
  79.     return $retval 
  80. restart() { 
  81.     configtest || return $? 
  82.     stop 
  83.     sleep 1 
  84.     start 
  85. reload() { 
  86.     configtest || return $? 
  87.     echo -n $"Reloading $prog: " 
  88.     killproc $nginx -HUP 
  89.     RETVAL=$? 
  90.     echo 
  91. force_reload() { 
  92.     restart 
  93. configtest() { 
  94.   $nginx -t -c $NGINX_CONF_FILE 
  95. rh_status() { 
  96.     status $prog 
  97. rh_status_q() { 
  98.     rh_status >/dev/null 2>&1 
  99. case "$1" in 
  100.     start) 
  101.         rh_status_q && exit 0 
  102.         $1 
  103.         ;; 
  104.     stop) 
  105.         rh_status_q || exit 0 
  106.         $1 
  107.         ;; 
  108.     restart|configtest) 
  109.         $1 
  110.         ;; 
  111.     reload) 
  112.         rh_status_q || exit 7 
  113.         $1 
  114.         ;; 
  115.     force-reload) 
  116.         force_reload 
  117.         ;; 
  118.     status) 
  119.         rh_status 
  120.         ;; 
  121.     condrestart|try-restart) 
  122.         rh_status_q || exit 0 
  123.             ;; 
  124.     *) 
  125.         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
  126.         exit 2 
  127. esac 
  128. 添加到服務清單并使開機啟動 
  129. # chkconfig --add nginx 
  130. # 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.      } 

  編輯/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. zone "web2.com" IN { 
  8.            type master; 
  9.            file "web2.com.zone"; 
  10.            allow-update{ none; }; 
  11. }; 
  12. zone "mp.com" IN { 
  13.            type master; 
  14.            file "mp.com.zone"; 
  15.            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. }  

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. # vim /usr/local/php/etc/php-fpm.conf 
  4.  pm.max_children = 50 
  5.  pm.start_servers = 5 
  6.  pm.min_spare_servers = 2 
  7.  pm.max_spare_servers = 8 
  8.  pid = /usr/local/php/var/run/php-fpm.pid  
  9. 啟動并驗證 
  10. # service php-fpm start 
  11. # 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 

繼續閱讀