Nginx反向代理 實現Web負載均衡


  實現負載均衡的方式有很多種,DNS、反向代理、LVS負載均衡器(軟件實現)、F5(負載均衡器,硬件,非常昂貴)這里我們只提到基於DNS,以及反向代理的方式來實現負載均衡Web服務

      DNS服務器實現負載均衡的原理是基於輪詢的方式實現的,直接由DNS解析系統進行隨機分配。除了性能分配不均之外,還有更可怕的就是如果一台服務器down了,DNS服務器因為是基於輪詢方式的負載均的,所以它並不能夠探測到或者沒有對付這種情況的動作,所以會繼續向這台服務器分配流量,導致訪問超時,給用戶帶來不便。並且基於DNS服務實現負載均衡web集群,需要3個公網IP,代價可想而知。現在IPv4的資源及其緊張,鋪張浪費不是一個好的運維工程師,你懂得.

      如果說使用nginx實現負載均衡的話

如圖所示:內網有三台web server(兩台Apache,一台Nginx),管理員可以根據服務器的性能,設置權重,來分配服務器的使用幾率。如果某台服務器反回超時或者掛掉了,Nginx反向代理會基於max_fails這樣的機制,使其不再把用戶分配到那台服務器,這樣比DNS服務器直接進行隨機分配好的多。可靠性和利用率大大提高。你懂得.

     並且基於nginx反向代理:只要有一個IP地址來綁定就可以實現nginx負載均衡,大大節省購買IP地址時的代價。當用戶訪問公司域名時,請求直接被發送到Nginx Server上,Nginx Server根據weight值和fail_timeout來進行合理分配服務器資源。注釋:

更加人性化。反向代理還經常被稱為網關服務器,因為它能夠防止了公司內網Web server直接暴露在外網的環境下,在一定程度上保證了web server的安全。

    NFS 全稱:Network file system NFS由SUN公司開發,目前已經成為文件服務的一種標准。我們在這里,通過NFS實現存儲.

    NFS 跟PHP-FPM服務器為同一台都為(172.16.251.215)

一、配置Nginx反向代理

1、創建用戶和組

2、編譯安裝Nginx

# groupadd nginx
# useradd -g nginx -s /bin/false -M nginx
./configure \
  --prefix=/usr \
  --sbin-path=/usr/sbin/nginx \
  --conf-path=/etc/nginx/nginx.conf \
  --error-log-path=/var/log/nginx/error.log \
  --http-log-path=/var/log/nginx/access.log \
  --pid-path=/var/run/nginx/nginx.pid  \
  --lock-path=/var/lock/nginx.lock \
  --user=nginx \
  --group=nginx \
  --with-http_ssl_module \
  --with-http_flv_module \
  --with-http_stub_status_module \
  --with-http_gzip_static_module \
  --http-client-body-temp-path=/var/tmp/nginx/client/ \
  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \
  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \
  --with-pcre
make && make install

3、提供Nginx服務腳本

  1 #!/bin/sh
  2 #
  3 # nginx - this script starts and stops the nginx daemon
  4 #
  5 # chkconfig:   - 85 15 
  6 # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
  7 #               proxy and IMAP/POP3 proxy server
  8 # processname: nginx
  9 # config:      /etc/nginx/nginx.conf
 10 # config:      /etc/sysconfig/nginx
 11 # pidfile:     /var/run/nginx.pid
 12  
 13 # Source function library.
 14 . /etc/rc.d/init.d/functions
 15  
 16 # Source networking configuration.
 17 . /etc/sysconfig/network
 18  
 19 # Check that networking is up.
 20 [ "$NETWORKING" = "no" ] && exit 0
 21  
 22 nginx="/usr/sbin/nginx"
 23 prog=$(basename $nginx)
 24  
 25 NGINX_CONF_FILE="/etc/nginx/nginx.conf"
 26  
 27 [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 28  
 29 lockfile=/var/lock/subsys/nginx
 30  
 31 make_dirs() {
 32    # make required directories
 33    user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
 34    options=`$nginx -V 2>&1 | grep 'configure arguments:'`
 35    for opt in $options; do
 36        if [ `echo $opt | grep '.*-temp-path'` ]; then
 37            value=`echo $opt | cut -d "=" -f 2`
 38            if [ ! -d "$value" ]; then
 39                # echo "creating" $value
 40                mkdir -p $value && chown -R $user $value
 41            fi
 42        fi
 43    done
 44 }
 45  
 46 start() {
 47     [ -x $nginx ] || exit 5
 48     [ -f $NGINX_CONF_FILE ] || exit 6
 49     make_dirs
 50     echo -n $"Starting $prog: "
 51     daemon $nginx -c $NGINX_CONF_FILE
 52     retval=$?
 53     echo
 54     [ $retval -eq 0 ] && touch $lockfile
 55     return $retval
 56 }
 57  
 58 stop() {
 59     echo -n $"Stopping $prog: "
 60     killproc $prog -QUIT
 61     retval=$?
 62     echo
 63     [ $retval -eq 0 ] && rm -f $lockfile
 64     return $retval
 65 }
 66  
 67 restart() {
 68     configtest || return $?
 69     stop
 70     sleep 1
 71     start
 72 }
 73  
 74 reload() {
 75     configtest || return $?
 76     echo -n $"Reloading $prog: "
 77     killproc $nginx -HUP
 78     RETVAL=$?
 79     echo
 80 }
 81  
 82 force_reload() {
 83     restart
 84 }
 85  
 86 configtest() {
 87   $nginx -t -c $NGINX_CONF_FILE
 88 }
 89  
 90 rh_status() {
 91     status $prog
 92 }
 93  
 94 rh_status_q() {
 95     rh_status >/dev/null 2>&1
 96 }
 97  
 98 case "$1" in
 99     start)
100         rh_status_q && exit 0
101         $1
102         ;;
103     stop)
104         rh_status_q || exit 0
105         $1
106         ;;
107     restart|configtest)
108         $1
109         ;;
110     reload)
111         rh_status_q || exit 7
112         $1
113         ;;
114     force-reload)
115         force_reload
116         ;;
117     status)
118         rh_status
119         ;;
120     condrestart|try-restart)
121         rh_status_q || exit 0
122             ;;
123     *)
124         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
125         exit 2
126 esac

4、配置Nginx實現反向代理

#使用的用戶和組
#user  nobody;
#指定工作衍生進程數(一般等於CPU的總核數或者總和數)
worker_processes  1;

#指定錯誤日志存放的路徑,錯誤日志記錄級別可選項[debug|info|notice|warn|error|crit]
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#指定pid存放的路徑
#pid        logs/nginx.pid;

events {
#默認使用的網絡I/O模型,Linux系統推薦采用epoll模型,FreeBSD推薦采用kqueue模型
#use epoll;
    worker_connections  1024;  #允許的連接數

}


http {
    include       mime.types;
    default_type  application/octet-stream;
    #設置使用的字符集,如果一個網站多種字符集,請不要隨便設置。
     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
#通過$remote_addr:可以獲取Nginx反向代理服務器的IP地址
#X-Forwarded-For:用以記錄原有的客戶端ip和原來客戶端請求的服務器地址.
#$remote_user:用於記錄遠程客戶端用戶名稱
#$time_local:用於記錄訪問時間與時區
#$request:用於記錄請求URL與http協議
#$status:用於記錄請求狀態,列如成功狀態為200,,頁面找不到時狀態404
#$body_bytes_sent:用於記錄發送客戶端的文件主體內容的大小;
#$http_referer:用於記錄是從哪個頁面鏈接訪問過來的;
#$http_usr_agent:用於記錄客戶端瀏覽器的相關信息.
#采用日志格式combined記錄的日志的格式是非常廣泛和流行的
access_log path [format [buffer=size | off]]
    access_log  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;                                                                            
    #keepalive_timeout  0;
    keepalive_timeout  65;
    #gzip  on;
    #proxy_cache_path /nginx/cache levels=1:2 keys_zone=mycache:16m
#           inactive=24h max_size=1g;
    upstream backend
    server 172.16.251.253  weight=1 max_fails=2  fail_timeout=30s;
    server 172.16.251.244  weight=1 max_fails=2  fail_timeout=30s;
    server 172.16.251.208  weight=1 max_fails=2  fail_timeout=30s;
}
#upstream:該指令用於設置可以再proxy_pass和fastcgi_pass指令中使用的代理服務器
#weight:設置服務器的權重,權重數值越高,被分配到的客戶端請求數越多.默認為1
#max_fails:指定的時間內對后端服務器請求失敗的次數,如果檢測到后端服務器無法連接及發生服務器錯誤(404錯誤除外),則標記為失敗.默認為1,設為數值0將關閉這項檢測
#fail_timeout:在經歷參數max_fails設置的失敗次數后,暫停的時間.
#down:標記服務器為永久離線狀態,用於ip_hash指令
#backup:僅僅非在backup服務器全部繁忙的時候才會啟用.
    server {
        listen       80;
    server_name www.nginx.com  220.2.2.2;
#server_name 綁定域名,以及IP地址
    location /  {
    proxy_pass http://backend;
    proxy_set_header host www.nginx.com;
    #proxy_cache mycache;
    #proxy_cache_valid 200 301 1d;
    #proxy_cache_valid 404 1m;
}
#proxy_pass:指定后端被代理的服務器
#proxy_connect_timeout:跟后端服務器連接的超時時間_發起握手等候響應超時時間
#proxy_read_timeout:連接成功后_等待后端服務器響應時間_其實已經進入后端的排隊之中等候處理。
#proxy_send_timeout:后端服務器數據回傳時間_就是規定時間內后端服務器必須傳完所有的數據
#proxy_buffer_size:代理請求緩存_這個緩存區間會保存用戶的頭部信息以供Nginx進行規則處理_一般只能保存下頭信息即可
#proxy_buffers:同上 告訴nginx保存單個用的幾個buffer最大可用空間
#proxy_busy_buffers_size:如果系統很忙的時候可以申請更大的Proxy_buffers
#proxy_temp_file_write_size:緩存臨時文件的大小;
#        location / {
#            root   html;
#            index   index.php index.html index.htm;
#        }
        error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   172.16.251.215:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;
    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;
    #    ssl_session_timeout  5m;
    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;
    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
}    

二、安裝web服務,編譯安裝Httpd     Web Server:172.16.251.208                   

 Web Server: 172.16.251.244

1、編譯安裝http2.4.9,依賴於更高版本的apr和apr-util。apr全稱為apache portable runtime 

#tar xf apr-1.5.0.tar.bz2
#cd apr-1.5.0
#./configure --prefix=/usr/local/apr
#make && make install

2、編譯安裝apr-util-1.5.2 

#tar xf apr-util-1.5.2.tar.bz2
#cd apr-util-1.5.2
#./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr/
#make && make install

3、編譯安裝http2.4.9

1 # tar xf httpd-2.4.9.tar.bz2
2 # cd httpd-2.4.9
3 # ./configure --prefix=/usr/local/apache --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-modules=most --enable-mpms-shared=all --with-mpm=event
4 # make && make install

4、后續的配置:

1 #ln -sv /usr/local/apache/include/usr/include/httpd
2 #vim /etc/profile.d/httpd.sh
3 exportPATH=/usr/local/apache/bin:$PATH
4 #vim /etc/man.conf
5 #MANPATH  /usr/local/apache/man

5、修改httpd的主配置文件,設置其Pid文件的路徑 vim /etc/httpd24/httpd.conf

PidFile  "/var/run/httpd.pid"

6、補充:
(1)構建MPM為靜態模塊
在全部平台中,MPM都可以構建為靜態模塊。在構建時選擇一種MPM,鏈接到服務器中。如果要改變MPM,必須重新構建。為了使用指定的MPM,請在執行configure腳本 時,使用參數 --with-mpm=NAME。NAME是指定的MPM名稱。編譯完成后,可以使用 ./httpd -l 來確定選擇的MPM。 此命令會列出編譯到服務器程序中的所有模塊,包括 MPM。
(2)構建 MPM 為動態模塊
在Unix或類似平台中,MPM可以構建為動態模塊,與其它動態模塊一樣在運行時加載。 構建 MPM 為動態模塊允許通過修改LoadModule指令內容來改變MPM,而不用重新構建服務器程序。在執行configure腳本時,使用--enable-mpms-shared選項即可啟用此特性。當給出的參數為all時,所有此平台支持的MPM模塊都會被安裝。還可以在參數中給出模塊列表。默認MPM,可以自動選擇或者在執行configure腳本時通過--with-mpm選項來指定,然后出現在生成的服務器配置文件中。編輯LoadModule指令內容可以選擇不同的MPM。

7、提供SysV服務腳本/etc/rc.d/init.d/httpd,內容如下:

 1 #!/bin/bash
 2 #
 3 # httpd        Startup script for the Apache HTTP Server
 4 #
 5 # chkconfig: - 85 15
 6 # description: Apache is a World Wide Web server.  It is used to serve \
 7 #        HTML files and CGI.
 8 # processname: httpd
 9 # config: /etc/httpd/conf/httpd.conf
10 # config: /etc/sysconfig/httpd
11 # pidfile: /var/run/httpd.pid
12 # Source function library.
13 . /etc/rc.d/init.d/functions
14 if [ -f /etc/sysconfig/httpd ]; then
15         . /etc/sysconfig/httpd
16 fi
17 # Start httpd in the C locale by default.
18 HTTPD_LANG=${HTTPD_LANG-"C"}
19 # This will prevent initlog from swallowing up a pass-phrase prompt if
20 # mod_ssl needs a pass-phrase from the user.
21 INITLOG_ARGS=""
22 # Set HTTPD=/usr/sbin/httpd.worker in /etc/sysconfig/httpd to use a server
23 # with the thread-based "worker" MPM; BE WARNED that some modules may not
24 # work correctly with a thread-based MPM; notably PHP will refuse to start.
25 # Path to the apachectl script, server binary, and short-form for messages.
26 apachectl=/usr/local/apache/bin/apachectl
27 httpd=${HTTPD-/usr/local/apache/bin/httpd}
28 prog=httpd
29 pidfile=${PIDFILE-/var/run/httpd.pid}
30 lockfile=${LOCKFILE-/var/lock/subsys/httpd}
31 RETVAL=0
32 start() {
33         echo -n $"Starting $prog: "
34         LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
35         RETVAL=$?
36         echo
37         [ $RETVAL = 0 ] && touch ${lockfile}
38         return $RETVAL
39 }
40 stop() {
41   echo -n $"Stopping $prog: "
42   killproc -p ${pidfile} -d 10 $httpd
43   RETVAL=$?
44   echo
45   [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
46 }
47 reload() {
48     echo -n $"Reloading $prog: "
49     if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
50         RETVAL=$?
51         echo $"not reloading due to configuration syntax error"
52         failure $"not reloading $httpd due to configuration syntax error"
53     else
54         killproc -p ${pidfile} $httpd -HUP
55         RETVAL=$?
56     fi
57     echo
58 }
59 # See how we were called.
60 case "$1" in
61   start)
62   start
63   ;;
64   stop)
65   stop
66   ;;
67   status)
68         status -p ${pidfile} $httpd
69   RETVAL=$?
70   ;;
71   restart)
72   stop
73   start
74   ;;
75   condrestart)
76   if [ -f ${pidfile} ] ; then
77     stop
78     start
79   fi
80   ;;
81   reload)
82         reload
83   ;;
84   graceful|help|configtest|fullstatus)
85   $apachectl $@
86   RETVAL=$?
87   ;;
88   *)
89   echo $"Usage: $prog {start|stop|restart|condrestart|reload|status|fullstatus|graceful|help|configtest}"
90   exit 1
91 esac
92 exit $RETVAL

8、而后為此腳本賦予執行權限:並且加入服務列表:

1 # chmod +x /etc/rc.d/init.d/httpd
2 # chkconfig --add httpd

9、配置Apache能夠調用后端的PHP-FPM服務器(fastCIG服務器),因為默認編譯安裝的Httpd有許多模塊都是被注釋掉的,要啟用之,去掉注釋即可:並且在這里Apache是作為反向代理服務,代理PHP-FPM(fastCGI)服務器工作之.所以在這里需要啟動代理模塊

1 LoadModule proxy_module modules/mod_proxy.so
2 #LoadModule proxy_connect_module modules/mod_proxy_connect.so
3 #LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
4 #LoadModule proxy_http_module modules/mod_proxy_http.so
5 LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

10、根據標准配置我們還應該啟用虛擬主機,在Httpd2.4.9中配置文件被拆分了多個子配置文件,在/etc/httpd24/extra/此目錄下眾多的配置文件都是httpd的,前面說到要想啟用虛擬主機也要去掉注釋,表示將虛擬主機的配置文件包含之,實現的方法都是如此

 1 460 # User home directories
 2 461 #Include /etc/httpd24/extra/httpd-userdir.conf
 3 462
 4 463 # Real-time info on requests and configuration
 5 464 #Include /etc/httpd24/extra/httpd-info.conf
 6 465
 7 466 # Virtual hosts
 8 467 Include /etc/httpd24/extra/httpd-vhosts.conf
 9 468
10 469 # Local access to the Apache HTTP Server Manual
11 470 #Include /etc/httpd24/extra/httpd-manual.conf
12 471
13 472 # Distributed authoring and versioning (WebDAV)
14 473 #Include /etc/httpd24/extra/httpd-dav.conf
15 474
16 475 # Various default settings
17 476 #Include /etc/httpd24/extra/httpd-default.conf
18 477

11、配置虛擬主機,在httpd2.4.9中虛擬主機需要配置控制才能夠訪問,默認是拒絕訪問的.DocumentRoot:定義虛擬主機網站的根目錄(這里的根目錄是NFS通過網絡共享存儲)我們將其掛載到本地,以及后端PHP Server的ip地址,Apache現在的角色是反向代理PHP-FPM應用成服務器

# vim /etc/httpd24/extra/httpd-vhosts.conf
 1 ​# The first VirtualHost section is used for all requests that do not
 2 # match a ServerName or ServerAlias in any <VirtualHost> block.
 3 #
 4 <VirtualHost *:80>
 5     ServerAdmin Admin@aaa.com
 6     DocumentRoot "/usr/html/"
 7     ServerName www.aaa.com
 8     Errorlog "logs/access.log"
 9     CustomLog "logs/error.log" combined
10   <Directory  "/usr/html/">
11     Options None
12     Require all granted
13 </Directory>
14     ProxyRequests Off
15    ProxyPassMatch ^/(.*\.php)$  fcgi://172.16.251.215:9000/usr/html/$1
16 </VirtualHost>

這里我們已經將其掛載之(NFS共享存儲的文件系統),使用mount命令可查看之;

1 #mount -t nfs ServerIP:/ShareDirectory  /LocalDirectory
2 #mount -t nfs 172.16.251.215/usr/html   /usr/html
3 #mount
4 #172.16.251.215:/usr/html on /usr/html type nfs (rw,vers=4,addr=172.16.251.215

另外一台web服務器配置如上

 

三、安裝mysql DB  MysqlDB Server 172.16.251.243

1、准備數據存放的文件系統,新建一個邏 輯卷,並將其掛載至特定目錄即可。這里不再給出過程。(實際生產環境下都是如此)這里假設其邏輯卷的掛載目錄為/mydata

2、新建用戶以安全方式運行進程:

# groupadd -r mysql
# useradd -g mysql -r -s /sbin/nologin -M -d /mydata/data mysql
# chown -R mysql:mysql /mydata/data

3、安裝mysql DB-5.5.33

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

4、為mysql提供主配置文件:

# cd /usr/local/mysql
# cp support-files/my-large.cnf  /etc/my.cnf

並修改此文件中thread_concurrency的值為你的CPU個數乘以2,比如這里使用如下行,另外還需要添加如下行指定mysql數據文件的存放位置:

1 #thread_concurrency = 2
2 #datadir = /mydata

5、為mysql提供sysv服務腳本:

# cd /usr/local/mysql
# cp support-files/mysql.server  /etc/rc.d/init.d/mysqld
# chmod +x /etc/rc.d/init.d/mysqld

6、添加至服務列表: 

# chkconfig --add mysqld
# chkconfig mysqld on

為了使用mysql的安裝符合系統使用規范,並將其開發組件導出給系統使用,這里還需要進行如下步驟:
7、輸出mysql的man手冊至man命令的查找路徑: 編輯/etc/man.config,添加如下行即可:

1 MANPATH  /usr/local/mysql/man

8、輸出mysql的頭文件至系統頭文件路徑/usr/include:這可以通過簡單的創建鏈接實現

1 # ln -sv /usr/local/mysql/include  /usr/include/mysql

9、修改PATH環境變量,讓系統可以直接使用mysql的相關命令。

1 # vim /etc/profile.d/mysql.sh
2 # export PATH=/usr/local/mysql/bin:$PATH
3 # .  /etc/profile.d/mysql.sh
4 # echo $PATH

(PHP-FPM)Application Server IP:172.16.251.215  NFS Server IP:172.16.251.215

四、編譯安裝php-5.4.26

1 # yum -y groupinstall "Desktop Platform Development"
2 # yum -y install bzip2-devel libmcrypt-devel

如果想讓編譯的php支持mcrypt、mhash擴展和libevent

另外,也可以根據需要安裝libevent,系統一般會自帶libevent,但版本有些低。因此可以編譯安裝之

說明:libevent是一個異步事件通知庫文件,其API提供了在某文件描述上發生某事件時或其超時時執行回調函數的機制,它主要用來替換事件驅動的網絡服務器上的event loop機制。目前來說, libevent支持/dev/poll、kqueue、select、poll、epoll及Solaris的event ports。

1、libevent

1 # tar zxvf libevent-1.4.14b-stable.tar.gz
2 # cd libevent-1.4.14b-stable
3 # ./configure
4 # make && make install
5 # make verify

2、libiconv

1 # tar zxvf libiconv-1.13.1.tar.gz
2 # cd libiconv-1.13.1
3 # ./configure
4 # make && make install

3、libmcrypt

1 # tar zxvf libmcrypt-2.5.8.tar.gz
2 # cd libmcrypt-2.5.8
3 # ./configure
4 # make && make install
5 # ldconfig -v
6 # cd libltdl
7 # ./configure --with-gmetad --enable-gexec
8 # make && make install

4、mhash

1 # tar jxvf mhash-0.9.9.9.tar.bz2
2 # cd mhash-0.9.9.9
3 # ./configure
4 # make && make install
5 # ln -sv /usr/local/lib/libmcrypt* /usr/lib/
6 # ln -sv /usr/local/lib/libmhash.* /usr/lib/

5、編譯安裝php5.4.26;

 

# tar xf php-5.4.4.tar.bz2
# cd php-5.4.4
#  ./configure --prefix=/usr/local/php  --with-openssl --enable-fpm --enable-sockets --enable-sysvshm  --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd   --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-libxml-dir=/usr --enable-xml  --with-mhash --with-mcrypt  --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl
# make
# make test
# make intall

 

 

 

說明:如果前面第1步解決依賴關系時安裝mcrypt相關的兩個rpm包,此./configure命令還可以帶上--with-mcrypt選項以讓php支持mycrpt擴展。--with-snmp選項則用於實現php的SNMP擴展,但此功能要求提前安裝net-snmp相關軟件包

為php提供配置文件:

1  cp php.ini-production /etc/php.ini

為php-fpm提供Sysv init腳本,並將其添加至服務列表:

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

編輯php-fpm的配置文件:

1 # vim /usr/local/php/etc/php-fpm.conf

配置fpm的相關選項為你所需要的值,並啟用pid文件(如下最后一行):注釋:因為php-fpm默認只監聽127.10.0.1:9000上;

1 pm.max_children = 150
2 pm.start_servers = 8
3 pm.min_spare_servers = 5
4 pm.max_spare_servers = 10
5 pid = /usr/local/php/var/run/php-fpm.pid
6 listen = 0.0.0.0:9000

接下來就可以啟動php-fpm了:

# service php-fpm start

使用如下命令來驗正(如果此命令輸出有中幾個php-fpm進程就說明啟動成功了):

1 # ps aux | grep php-fpm

默認情況下,fpm監聽在127.0.0.1的9000端口,也可以使用如下命令驗正其是否已經監聽在相應的套接字。

1 # netstat -tnlp | grep php-fpm
2 tcp        0      0  0.0.0.0:9000              0.0.0.0:*                   LISTEN      689/php-fpm

五、NFS服務在Centos或者redhat系列發行版linux中默認已安裝過啦!這里我們只要修改配置文件啟用之就行啦!

1 # vim /etc/exports
2 #/usr/html          172.16.0.0/16(rw,no_root_squash)

 

 

六 、整合nginx和php5 ,因為在公司內部還有一台Nginx的webSercer

1、編輯/etc/nginx/nginx.conf,啟用如下選項:

location ~ \.php$ {
root           html;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
include        fastcgi_params;
}

2、編輯/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;

並在所支持的主頁面格式中添加php格式的主頁,類似如下:

1 location / {
2 root   html;
3 index  index.php index.html index.htm;
4 }

而后重新載入nginx的配置文件:

1 # service nginx reload

4、在/usr/html新建index.php的測試頁面,測試php是否能正常工作:

1 # cat > /usr/html/index.php << EOF
2 <?php
3 phpinfo();
4 ?>

 

七、安裝xcache,為php加速:

1、安裝

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

安裝結束時,會出現類似如下行:

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-zts-20100525/

2、編輯php.ini,整合php和xcache:

首先將xcache提供的樣例配置導入php.ini

1 # mkdir /etc/php.d
2 # cp xcache.ini /etc/php.d

注意:如果php.ini文件中有多條zend_extension指令行,要確保此新增的行排在第一位。

3、重新啟動php-fpm

1 # service php-fpm restart

 

 

八、搭建論壇

1、安裝wordpress
#Unzip  wordpress-3.0.5-zh_CN.zip
# mv wordpress /www/htdoc2/mybbs
# cp wp-config-sample.php wp-config.php
#chmod  a+x wp-config.php
# vim wp-config.php
# mysql -uroot -pzhangxiaocen –hlocalhost
mysql> create database wpdb
18 define('DB_NAME', 'wpdb');
                                                                                                                                                                                                                                                                                                                                                                                             
21 define('DB_USER', 'root');
                                                                                                                                                                                                                                                                                                                                                                                             
24 define('DB_PASSWORD', 'zhangxiaocen');
27 define('DB_HOST', 'localhost');
                                                                                                                                                                                                                                                                                                                                                                                              
30 define('DB_CHARSET', 'utf8');
2、 安裝Discuz_X2.5_SC_GBK
# unzip Discuz_X2.5_SC_GBK.zip
# mv upload    /www/htdoc3/bbs
# chown -R daemon:daemon  ./*
#mysql>create database discuz;
#grant all on discuz.* to discuz@'172.16.251.243' identified by 'zhangxiaocen';

通過網頁登上論壇發表帖子,並通過其他web服務器登錄,查看帖子!這里就不再演示。負載均衡的話,寫上三個不同的網頁頁面測試之.

 

 

9、基於DNS實現負載均衡

 

1 # yum -y install bind
$TTL  664
@       IN SOA   DNS.nginx.com.         admin.nginx.com. (
                                        0       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
                 IN          NS              DNS.nginx.com.
DNS           IN          A               172.16.251.198
www         IN          A               172.16.251.253
www         IN          A               172.16.251.244
www         IN          A               172.16.251.208

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM