天天看點

Nginx架構的企業級應用

Nginx架構的企業級應用

====================================================

實作HA高可用叢集

實作LB負載均衡叢集

Nginx實作反向代理

Nginx實作動靜分離

==================================================

<a href="http://blog.51cto.com/attachment/201309/205744159.png"></a>

需求:

用戶端通路靜态的請求,由nginx反向代理給後端的Apache伺服器;

用戶端通路動态的請求,由nginx反向代理給後端的php-fpm(fastCGI)伺服器,而且做負載均衡,如果需要通路資料庫,則由php-fpm連接配接mysql;

如果nginx主伺服器當機之後,nginx備伺服器馬上頂替主伺服器,提供服務;

伺服器IP規劃和所需軟體安裝:

IP位址

軟體

nginx主

172.16.22.1 (VIP 172.16.22.10)

nginx+heartbeat

nginx備

172.16.22.2 (VIP 172.16.22.10)

Apache

172.16.22.3

httpd

php-fpm1

172.16.22.4

php(提供fastCGI伺服器)

php-fpm2

172.16.22.5

mysql

172.16.22.6

heartbeat軟體包,已經以附件的形式上傳了nginx、php、mysql的軟體包在網上都很好下載下傳

需解決的問題:

1)、怎麼實作HA高可用叢集

思路:安裝heartbeat軟體,把nginx主伺服器和nginx備伺服器這兩個節點都加入到heartbeat中,用heartbeat的crm管理資源,定義高可用叢集

2)、怎麼實作LB負載均衡叢集

思路:利用nginx的upstream子產品,配置實作應用層的負載均衡

3)、nginx怎麼把客戶的靜态請求送出給後端的Apache伺服器聯系

思路:利用nginx的反向代理給後端的Apache伺服器

4)、nginx怎麼把客戶的動态請求送出給後端的php-fpm伺服器聯系

思路:首先nginx支援fastCGI,然後利用nginx的反向代理給php-fpm伺服器

5)、php-fpm伺服器怎麼和mysql伺服器聯系

思路:mysql授權能讓php-fpm伺服器連接配接資料庫

一、先安裝每個伺服器所需的軟體

nginx主伺服器的配置:

1)、編譯安裝nginx

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

<code>[root@jie1 ~]</code><code># ifconfig | grep "inet addr" | grep -v "127.0.0.1" | awk -F: '{print $2}' | cut -d' ' -f1   檢視ip位址</code>

<code>172.16.22.1</code>

<code>[root@jie1 ~]</code><code>#tar xf nginx-1.4.2.tar.gz</code>

<code>[root@jie1 ~]</code><code># yum -y groupinstall "Development tools" "Server Platform Development"   安裝開發包</code>

<code>[root@jie1 ~]</code><code>#yum -y install pcre-devel  安裝依賴性包</code>

<code>[root@jie1 ~]</code><code># cd nginx-1.4.2</code>

<code>[root@jie1 nginx-1.4.2]</code><code># groupadd nginx</code>

<code>[root@jie1 nginx-1.4.2]</code><code># useradd -r -g nginx nginx</code>

<code>[root@jie1 nginx-1.4.2]</code><code>#./configure \</code>

<code>  </code><code>--prefix=</code><code>/usr</code> <code>\</code>

<code>  </code><code>--sbin-path=</code><code>/usr/sbin/nginx</code> <code>\</code>

<code>  </code><code>--conf-path=</code><code>/etc/nginx/nginx</code><code>.conf \</code>

<code>  </code><code>--error-log-path=</code><code>/var/log/nginx/error</code><code>.log \</code>

<code>  </code><code>--http-log-path=</code><code>/var/log/nginx/access</code><code>.log \</code>

<code>  </code><code>--pid-path=</code><code>/var/run/nginx/nginx</code><code>.pid  \</code>

<code>  </code><code>--lock-path=</code><code>/var/lock/nginx</code><code>.lock \</code>

<code>  </code><code>--user=nginx \</code>

<code>  </code><code>--group=nginx \</code>

<code>  </code><code>--with-http_ssl_module \</code>

<code>  </code><code>--with-http_flv_module \</code>

<code>  </code><code>--with-http_stub_status_module \</code>

<code>  </code><code>--with-http_gzip_static_module \</code>

<code>  </code><code>--http-client-body-temp-path=</code><code>/var/tmp/nginx/client/</code> <code>\</code>

<code>  </code><code>--http-proxy-temp-path=</code><code>/var/tmp/nginx/proxy/</code> <code>\</code>

<code>  </code><code>--http-fastcgi-temp-path=</code><code>/var/tmp/nginx/fcgi/</code> <code>\</code>

<code>  </code><code>--http-uwsgi-temp-path=</code><code>/var/tmp/nginx/uwsgi</code> <code>\</code>

<code>  </code><code>--http-scgi-temp-path=</code><code>/var/tmp/nginx/scgi</code> <code>\</code>

<code>  </code><code>--with-pcre</code>

<code>[root@jie1 nginx-1.4.2]</code><code># make &amp;&amp; make install</code>

2)、提供System V腳本

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

<code>[root@jie1 nginx-1.4.2]</code><code># vim /etc/rc.d/init.d/nginx</code>

<code> </code><code>#!/bin/sh</code>

<code>#</code>

<code># nginx - this script starts and stops the nginx daemon</code>

<code># chkconfig:   - 85 15</code>

<code># description:  Nginx is an HTTP(S) server, HTTP(S) reverse \</code>

<code>#               proxy and IMAP/POP3 proxy server</code>

<code># processname: nginx</code>

<code># config:      /etc/nginx/nginx.conf</code>

<code># config:      /etc/sysconfig/nginx</code>

<code># pidfile:     /var/run/nginx.pid</code>

<code>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   </code> 

<code># Source function library.</code>

<code>. </code><code>/etc/rc</code><code>.d</code><code>/init</code><code>.d</code><code>/functions</code>

<code># Source networking configuration.</code>

<code>. </code><code>/etc/sysconfig/network</code>

<code># Check that networking is up.</code>

<code>[ </code><code>"$NETWORKING"</code> <code>= </code><code>"no"</code> <code>] &amp;&amp; </code><code>exit</code> <code>0</code>

<code>nginx=</code><code>"/usr/sbin/nginx"</code>

<code>prog=$(</code><code>basename</code> <code>$nginx)</code>

<code>NGINX_CONF_FILE=</code><code>"/etc/nginx/nginx.conf"</code>

<code>[ -f </code><code>/etc/sysconfig/nginx</code> <code>] &amp;&amp; . </code><code>/etc/sysconfig/nginx</code>

<code>lockfile=</code><code>/var/lock/subsys/nginx</code>

<code>make_dirs() {</code>

<code>   </code><code># make required directories</code>

<code>   </code><code>user=`nginx -V 2&gt;&amp;1 | </code><code>grep</code> <code>"configure arguments:"</code> <code>| </code><code>sed</code> <code>'s/[^*]*--user=\([^ ]*\).*/\1/g'</code> <code>-`</code>

<code>   </code><code>options=`$nginx -V 2&gt;&amp;1 | </code><code>grep</code> <code>'configure arguments:'</code><code>`</code>

<code>   </code><code>for</code> <code>opt </code><code>in</code> <code>$options; </code><code>do</code>

<code>       </code><code>if</code> <code>[ `</code><code>echo</code> <code>$opt | </code><code>grep</code> <code>'.*-temp-path'</code><code>` ]; </code><code>then</code>

<code>           </code><code>value=`</code><code>echo</code> <code>$opt | </code><code>cut</code> <code>-d </code><code>"="</code> <code>-f 2`</code>

<code>           </code><code>if</code> <code>[ ! -d </code><code>"$value"</code> <code>]; </code><code>then</code>

<code>               </code><code># echo "creating" $value</code>

<code>               </code><code>mkdir</code> <code>-p $value &amp;&amp; </code><code>chown</code> <code>-R $user $value</code>

<code>           </code><code>fi</code>

<code>       </code><code>fi</code>

<code>   </code><code>done</code>

<code>}</code>

<code>start() {</code>

<code>    </code><code>[ -x $nginx ] || </code><code>exit</code> <code>5</code>

<code>    </code><code>[ -f $NGINX_CONF_FILE ] || </code><code>exit</code> <code>6</code>

<code>    </code><code>make_dirs</code>

<code>    </code><code>echo</code> <code>-n $</code><code>"Starting $prog: "</code>

<code>    </code><code>daemon $nginx -c $NGINX_CONF_FILE</code>

<code>    </code><code>retval=$?</code>

<code>    </code><code>echo</code>

<code>    </code><code>[ $retval -</code><code>eq</code> <code>0 ] &amp;&amp; </code><code>touch</code> <code>$lockfile</code>

<code>    </code><code>return</code> <code>$retval</code>

<code>stop() {</code>

<code>    </code><code>echo</code> <code>-n $</code><code>"Stopping $prog: "</code>

<code>    </code><code>killproc $prog -QUIT</code>

<code>    </code><code>[ $retval -</code><code>eq</code> <code>0 ] &amp;&amp; </code><code>rm</code> <code>-f $lockfile</code>

<code>restart() {</code>

<code>    </code><code>configtest || </code><code>return</code> <code>$?</code>

<code>    </code><code>stop</code>

<code>    </code><code>sleep</code> <code>1</code>

<code>    </code><code>start</code>

<code>reload() {</code>

<code>    </code><code>echo</code> <code>-n $</code><code>"Reloading $prog: "</code>

<code>    </code><code>killproc $nginx -HUP</code>

<code>    </code><code>RETVAL=$?</code>

<code>force_reload() {</code>

<code>    </code><code>restart</code>

<code>configtest() {</code>

<code>  </code><code>$nginx -t -c $NGINX_CONF_FILE</code>

<code>rh_status() {</code>

<code>    </code><code>status $prog</code>

<code>rh_status_q() {</code>

<code>    </code><code>rh_status &gt;</code><code>/dev/null</code> <code>2&gt;&amp;1</code>

<code>case</code> <code>"$1"</code> <code>in</code>

<code>    </code><code>start)</code>

<code>        </code><code>rh_status_q &amp;&amp; </code><code>exit</code> <code>0</code>

<code>        </code><code>$1</code>

<code>        </code><code>;;</code>

<code>    </code><code>stop)</code>

<code>        </code><code>rh_status_q || </code><code>exit</code> <code>0</code>

<code>    </code><code>restart|configtest)</code>

<code>    </code><code>reload)</code>

<code>        </code><code>rh_status_q || </code><code>exit</code> <code>7</code>

<code>    </code><code>force-reload)</code>

<code>        </code><code>force_reload</code>

<code>    </code><code>status)</code>

<code>        </code><code>rh_status</code>

<code>    </code><code>condrestart|try-restart)</code>

<code>            </code><code>;;</code>

<code>    </code><code>*)</code>

<code>        </code><code>echo</code> <code>$</code><code>"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"</code>

<code>        </code><code>exit</code> <code>2</code>

<code>esac</code>

<code>[root@jie1 nginx-1.4.2]</code><code># chmod +x /etc/rc.d/init.d/nginx</code>

<code>[root@jie1 nginx-1.4.2]</code><code># service nginx start</code>

<code>Starting nginx:                                            [  OK  ]</code>

<code>[root@jie1 nginx-1.4.2]</code><code>#</code>

3)、編譯安裝src格式的heartbeat的源碼包

<code>[root@jie1 ~]</code><code>#  useradd mockbuild  建立此使用者用于編譯src的源碼包</code>

<code>[root@jie1 ~]</code><code># rpm -ivh heartbeat-2.1.4-12.el6.src.rpm</code>

<code>   </code><code>1:heartbeat              </code><code>################################### [100%]</code>

<code>[root@jie1 ~]</code><code># yum -y install rpm-build</code>

<code>[root@jie1 ~]</code><code>#cd rpmbuild/</code>

<code>[root@jie1 rpmbuild]</code><code># cd SPECS/</code>

<code>[root@jie1 rpmbuild]</code><code># yum -y install glib2-devel libnet-devel libtool-ltdl-devel net-snmp-devel openhpi-libs gnutls-devel python-devel</code>

<code>[root@jie1 rpmbuild]</code><code># rpmbuild -ba heartbeat.spec</code>

<code>[root@jie1 x86_64</code><code># pwd</code>

<code>/root/rpmbuild/RPMS/x86_64</code>

<code>[root@jie1 x86_64</code><code>#ls      生成的所有軟體包</code>

<code>heartbeat-2.1.4-12.el6.x86_64.rpm            heartbeat-ldirectord-2.1.4-12.el6.x86_64.rpm</code>

<code>heartbeat-debuginfo-2.1.4-12.el6.x86_64.rpm  heartbeat-pils-2.1.4-12.el6.x86_64.rpm</code>

<code>heartbeat-devel-2.1.4-12.el6.x86_64.rpm      heartbeat-stonith-2.1.4-12.el6.x86_64.rpm</code>

<code>heartbeat-gui-2.1.4-12.el6.x86_64.rpm</code>

<code>[root@jie1 x86_64]</code><code>#mv  heartbeat-debuginfo-2.1.4-12.el6.x86_64.rpm heartbeat-ldirectord-2.1.4-12.el6.x86_64.rpm heartbeat-devel-2.1.4-12.el6.x86_64.rpm  /root   有些軟體包不必安裝,是以移動到别的目錄下</code>

<code>[root@jie1 x86_64]</code><code>#ls</code>

<code>heartbeat-2.1.4-12.el6.x86_64.rpm      heartbeat-pils-2.1.4-12.el6.x86_64.rpm</code>

<code>heartbeat-gui-2.1.4-12.el6.x86_64.rpm  heartbeat-stonith-2.1.4-12.el6.x86_64.rpm</code>

<code>[root@jie1 x86_64]</code><code>#yum -y install PyXML   安裝依賴性包</code>

<code>[root@jie1 x86_64]</code><code># rpm -ivh *.rpm   直接安裝此目錄下的所有rpm包</code>

<code>Preparing...                </code><code>################################# [100%]</code>

<code>   </code><code>1:heartbeat-pils         </code><code>################################# [ 25%]</code>

<code>   </code><code>2:heartbeat-stonith      </code><code>################################# [ 50%]</code>

<code>   </code><code>3:heartbeat              </code><code>################################# [ 75%]</code>

<code>   </code><code>4:heartbeat-gui          </code><code>################################# [100%]</code>

<code>[root@jie1 x86_64]</code><code>#</code>

4)、建立heartbeat的配置檔案和認證檔案,以及修改hosts檔案,使HA的節點能用主機名進行通信

<code>[root@jie1 ~]</code><code># cd /usr/share/doc/heartbeat-2.1.4/</code>

<code>[root@jie1 heartbeat-2.1.4]</code><code># cp authkeys ha.cf /etc/ha.d/</code>

<code>[root@jie1 heartbeat-2.1.4]</code><code># vim /etc/hosts</code>

<code>172.16.22.1 jie1.com jie1</code>

<code>172.16.22.2 jie2.com jie2</code>

5)、修改heartbeat的配置檔案和認證檔案

<code>[root@jie1 heartbeat-2.1.4]</code><code># cd /etc/ha.d/</code>

<code>[root@jie1 ha.d]</code><code># openssl rand -hex 8 #生成随機數</code>

<code>29c59aeaf3109993</code>

<code>[root@jie1 ha.d]</code><code># sed -e '/^#/d' authkeys</code>

<code>auth 3</code>

<code>3 md5 29c59aeaf3109993   </code><code>#把生成的随機數</code>

<code>[root@jie1 ha.d]</code><code># chmod 600 authkeys</code>

<code>[root@jie1 ha.d]</code><code># grep -v "^#" ha.cf | grep -v "^$"</code>

<code>logfile </code><code>/var/log/ha-log</code>        <code>#日志存放位置</code>

<code>keepalive 2                    </code><code>#心跳的時間間隔,預設時間機關為秒</code>

<code>deadtime 3                    </code><code># 超出該時間間隔未收到對方節點的心跳,則認    為對方已經死亡</code>

<code>warntime 10                   </code><code>#超出該時間間隔未收到對方節點的心跳,則發出警告并記錄到日志中,但此時不會切換</code>

<code>initdead 60     </code><code>#在某些系統上,系統啟動或重新開機之後需要經過一段時間網絡才能正常工作,該選項用于解決這種情況産生的時間間隔。</code>

<code>udpport 694                </code><code>#設定廣播通信使用的端口,694為預設使用的端口号</code>

<code>mcast eth0 225.23.32.1 694 1 0  </code><code>#多點傳播位址</code>

<code>auto_failback on   </code><code>#用于定義當主節點恢複後,是否将服務自動切回</code>

<code>node jie1.com     </code><code>#必須寫hostname顯示的主機名,節點一的主機名</code>

<code>node jie2.com</code>

<code>ping</code> <code>172.16.0.1   </code><code>#用ping網關,來驗證節點是否當機</code>

<code>crm on</code>

<code>[root@jie1 ha.d]</code><code>#</code>

6)、把nginx的服務腳本加入到heartbeat的資源目錄下,讓heartbeat的crm(資源管理層)來管理nginx服務。

<code>[root@jie1 ha.d]</code><code># cd resource.d/</code>

<code>[root@jie1 resource.d]</code><code># cp /etc/rc.d/init.d/nginx ./</code>

<code>[root@jie1 resource.d]</code><code># service nginx stop 關閉nginx服務,讓heartbeat來管理</code>

<code>Stopping nginx:                                            [  OK  ]</code>

<code>[root@jie1 resource.d]</code><code>#passwd hacluster  為hacluster使用者建立密碼</code>

nginx備伺服器的配置:

<code>[root@jie2 ~]</code><code># ifconfig | grep "inet addr" | grep -v "127.0.0.1" | awk -F: '{print $2}' | cut -d' ' -f1   檢視ip位址</code>

<code>172.16.22.2</code>

<code>[root@jie2 ~]</code><code>#tar xf nginx-1.4.2.tar.gz</code>

<code>[root@jie2 ~]</code><code># yum -y groupinstall "Development tools" "Server Platform Development"   安裝開發包</code>

<code>[root@jie2 ~]</code><code>#yum -y install pcre-devel  安裝依賴性包</code>

<code>[root@jie2 ~]</code><code># cd nginx-1.4.2</code>

<code>[root@jie2 nginx-1.4.2]</code><code># groupadd nginx</code>

<code>[root@jie2 nginx-1.4.2]</code><code># useradd -r -g nginx nginx</code>

<code>[root@jie2 nginx-1.4.2]</code><code>#./configure \</code>

<code>--prefix=</code><code>/usr</code><code>\</code>

<code>--sbin-path=</code><code>/usr/sbin/nginx</code><code>\</code>

<code>--conf-path=</code><code>/etc/nginx/nginx</code><code>.conf \</code>

<code>--error-log-path=</code><code>/var/log/nginx/error</code><code>.log \</code>

<code>--http-log-path=</code><code>/var/log/nginx/access</code><code>.log \</code>

<code>--pid-path=</code><code>/var/run/nginx/nginx</code><code>.pid  \</code>

<code>--lock-path=</code><code>/var/lock/nginx</code><code>.lock \</code>

<code>--user=nginx \</code>

<code>--group=nginx \</code>

<code>--with-http_ssl_module \</code>

<code>--with-http_flv_module \</code>

<code>--with-http_stub_status_module \</code>

<code>--with-http_gzip_static_module \</code>

<code>--http-client-body-temp-path=</code><code>/var/tmp/nginx/client/</code><code>\</code>

<code>--http-proxy-temp-path=</code><code>/var/tmp/nginx/proxy/</code><code>\</code>

<code>--http-fastcgi-temp-path=</code><code>/var/tmp/nginx/fcgi/</code><code>\</code>

<code>--http-uwsgi-temp-path=</code><code>/var/tmp/nginx/uwsgi</code><code>\</code>

<code>--http-scgi-temp-path=</code><code>/var/tmp/nginx/scgi</code><code>\</code>

<code>--with-pcre</code>

<code>[root@jie2 nginx-1.4.2]</code><code># make &amp;&amp; make install</code>

2)、複制nginx主伺服器的System V腳本檔案和heartbeat所需的軟體包

<code>[root@jie2 ~]</code><code># scp 172.16.22.1:/etc/rc.d/init.d/nginx  /etc/rc.d/init.d/</code>

<code>[root@jie2 ~]</code><code>#scp 172.16.22.1:/root/rpmbuild/RPMS/x86_64/*  /root</code>

<code>[root@jie2 ~]</code><code># ls</code>

<code>anaconda-ks.cfg                            </code><code>install</code><code>.log</code>

<code>heartbeat-2.1.4-12.el6.x86_64.rpm          </code><code>install</code><code>.log.syslog</code>

<code>heartbeat-pils-2.1.4-12.el6.x86_64.rpm</code>

<code>heartbeat-stonith-2.1.4-12.el6.x86_64.rpm</code>

<code>[root@jie2 ~]</code><code>#</code>

3)、安裝從nginx主伺服器copy過來的heartbeat軟體

<code>[root@jie2 ~]</code><code># yum -y install PyXML libnet-devel net-snmp-libs</code>

<code>[root@jie2 ~]</code><code># rpm -ivh *.rpm</code>

<code>Preparing...                </code><code>################################### [100%]</code>

<code>   </code><code>1:heartbeat-pils         </code><code>################################### [ 25%]</code>

<code>   </code><code>2:heartbeat-stonith      </code><code>################################### [ 50%]</code>

<code>   </code><code>3:heartbeat              </code><code>################################### [ 75%]</code>

<code>   </code><code>4:heartbeat-gui          </code><code>################################### [100%]</code>

4)、由于是HA叢集,HA叢集必須保證節點的配置檔案完全一樣,在這裡我們直接把nginx主伺服器的heartbeat的配置檔案copy過來。

<code>[root@jie2 ~] </code><code>scp</code>  <code>172.16.22.1:</code><code>/etc/ha</code><code>.d/{ha.cf,authkeys}  </code><code>/etc/ha</code><code>.d/</code>

<code>[email protected]'s password:</code>

<code>ha.cf                             100%   10KB  10.3KB</code><code>/s</code>   <code>00:00</code>

<code>authkeys                          100%  653     0.6KB</code><code>/s</code>   <code>00:00</code>

<code>[root@jie2 ~] </code><code>scp</code>  <code>172.16.22.1:</code><code>/etc/hosts</code> <code>/etc/</code>

<code>hosts                             100%  250     0.2KB</code><code>/s</code>   <code>00:00</code>

5)、把nginx的服務腳本加入到heartbeat的資源目錄下,讓heartbeat的crm(資源管理層)來管理nginx服務。

<code>[root@jie2 ~]</code><code># cd /etc/ha.d/</code>

<code>[root@jie2 ha.d]</code><code># cd resource.d/</code>

<code>[root@jie2 resource.d]</code><code># cp /etc/rc.d/init.d/nginx ./</code>

<code>[root@jie2 resource.d]</code><code># service nginx stop 關閉nginx服務,讓heartbeat來管理</code>

<code>[root@jie2 resource.d]</code><code>#passwd hacluster  為hacluster使用者建立密碼</code>

Apache伺服器的配置:

apache部落客采用rpm包安裝,各位博友可以采用源碼包編譯安裝

<code>[root@jie3 ~]</code><code># ifconfig | grep "inet addr" | grep -v "127.0.0.1" | awk -F: '{print $2}' | cut -d' ' -f1   檢視ip位址</code>

<code>172.16.22.3</code>

<code>[root@jie3 ~]</code><code># yum -y install httpd</code>

<code>[root@jie3 ~]</code><code># service httpd start</code>

php-fpm1伺服器的配置:

1)、安裝php,編譯支援fpm

<code>[root@jie4 ~]</code><code># ifconfig | grep "inet addr" | grep -v "127.0.0.1" | awk -F: '{print $2}' | cut -d' ' -f1   檢視ip位址</code>

<code>172.16.22.4</code>

<code>[root@jie4 ~]</code><code># tar xf php-5.4.19.tar.bz2</code>

<code>[root@jie4 ~]</code><code># yum -y groupinstall "Development tools" "Server Platform Development"  安裝開發包組</code>

<code>[root@jie4 ~]</code><code># yum -y install libmcrypt-devel mhash-devel bzip2-devel  libxml2-devel  安裝依賴性包</code>

<code>[root@jie4 ~]</code><code># cd php-5.4.19</code>

<code>[root@jie4 php-5.4.19]</code><code># ./configure --prefix=/usr/local/php  --enable-fpm --with-openssl --enable-mbstring \</code>

<code>--with-freetype-</code><code>dir</code> <code>--with-jpeg-</code><code>dir</code> <code>--with-png-</code><code>dir</code> <code>--with-zlib --with-libxml-</code><code>dir</code><code>=</code><code>/usr</code> <code>--</code><code>enable</code><code>-xml  \</code>

<code>--</code><code>enable</code><code>-sockets  --with-mcrypt  --with-bz2 --with-config-</code><code>file</code><code>-path=</code><code>/etc</code> <code>--with-config-</code><code>file</code><code>-scan-</code><code>dir</code><code>=</code><code>/etc/php</code><code>.d  \</code>

<code>--with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd</code>

<code>[root@jie4 php-5.4.19]</code><code># make &amp;&amp; make install</code>

2)、提供php的配置檔案,php-fpm的System V腳本和php-fpm的配置檔案,啟動php-fpm服務

<code>[root@jie4 php-5.4.19]</code><code># cp php.ini-production /etc/php.ini</code>

<code>[root@jie4 php-5.4.19]</code><code># cp sapi/fpm/init.d.php-fpm  /etc/rc.d/init.d/php-fpm</code>

<code>[root@jie4 php-5.4.19]</code><code># chmod +x /etc/rc.d/init.d/php-fpm</code>

<code>[root@jie4 php-5.4.19]</code><code># chkconfig --add php-fpm</code>

<code>[root@jie4 php-5.4.19]</code><code># chkconfig php-fpm on</code>

<code>[root@jie4 php-5.4.19]</code><code># cd /usr/local/php/etc/</code>

<code>[root@jie4 etc]</code><code># cp php-fpm.conf.default php-fpm.conf</code>

<code>[root@jie4 etc]</code><code># vim php-fpm.conf</code>

<code>listen = 172.16.22.4:9000   </code><code>#把監聽的127.0.0.1改成本機網卡的IP</code>

<code>[root@jie4 etc]</code><code># service php-fpm start</code>

php-fpm2伺服器的配置(和php-fpm1伺服器的安裝配置一樣):

<code>[root@jie5 ~]</code><code># ifconfig | grep "inet addr" | grep -v "127.0.0.1" | awk -F: '{print $2}' | cut -d' ' -f1   檢視ip位址</code>

<code>172.16.22.5</code>

<code>[root@jie5 ~]</code><code># tar xf php-5.4.19.tar.bz2</code>

<code>[root@jie5 ~]</code><code># yum -y groupinstall "Development tools" "Server Platform Development"  安裝開發包組</code>

<code>[root@jie5 ~]</code><code># yum -y install libmcrypt-devel mhash-devel bzip2-devel  libxml2-devel  安裝依賴性包</code>

<code>[root@jie5 ~]</code><code># cd php-5.4.19</code>

<code>[root@jie5 php-5.4.19]</code><code># ./configure --prefix=/usr/local/php  --enable-fpm --with-openssl --enable-mbstring \</code>

<code>--with-freetype-</code><code>dir</code><code>--with-jpeg-</code><code>dir</code><code>--with-png-</code><code>dir</code><code>--with-zlib --with-libxml-</code><code>dir</code><code>=</code><code>/usr--enable-xml</code>  <code>\</code>

<code>--</code><code>enable</code><code>-sockets  --with-mcrypt  --with-bz2 --with-config-</code><code>file</code><code>-path=</code><code>/etc--with-config-file-scan-dir</code><code>=</code><code>/etc/php</code><code>.d  \</code>

<code>[root@jie5 php-5.4.19]</code><code># make &amp;&amp; make install</code>

<code>[root@jie5 php-5.4.19]</code><code># cp php.ini-production /etc/php.ini</code>

<code>[root@jie5 php-5.4.19]</code><code># cp sapi/fpm/init.d.php-fpm  /etc/rc.d/init.d/php-fpm</code>

<code>[root@jie5 php-5.4.19]</code><code># chmod +x /etc/rc.d/init.d/php-fpm</code>

<code>[root@jie5 php-5.4.19]</code><code># chkconfig --add php-fpm</code>

<code>[root@jie5 php-5.4.19]</code><code># chkconfig php-fpm on</code>

<code>[root@jie5 php-5.4.19]</code><code># cd /usr/local/php/etc/</code>

<code>[root@jie5 etc]</code><code># cp php-fpm.conf.default php-fpm.conf</code>

<code>[root@jie5 etc]</code><code># vim php-fpm.conf</code>

<code>listen = 172.16.22.5:9000   </code><code>#把監聽的127.0.0.1改成本機網卡的IP</code>

<code>[root@jie5 etc]</code><code># service php-fpm start</code>

mysql伺服器的配置:

1)、編譯安裝mysql的源碼包

<code>[root@jie6 ~]</code><code># ifconfig | grep "inet addr" | grep -v "127.0.0.1" | awk -F: '{print $2}' | cut -d' ' -f1   檢視ip位址</code>

<code>172.16.22.6</code>

<code>[root@jie6 ~]</code><code># tar xf mysql-5.5.33.tar.gz</code>

<code>[root@jie6 ~]</code><code># yum -y groupinstall "Development tools" "Server Platform Development"</code>

<code>[root@jie6 ~]</code><code># cd mysql-5.5.33</code>

<code>[root@jie6 mysql-5.5.33]</code><code># yum -y install cmake</code>

<code>[root@jie6 mysql-5.5.33]</code><code># cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \</code>

<code>-DMYSQL_DATADIR=</code><code>/mydata/data</code>  <code>-DSYSCONFDIR=</code><code>/etc</code> <code>\</code>

<code>-DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 \</code>

<code>-DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DWITH_SSL=system \</code>

<code>-DWITH_ZLIB=system -DWITH_LIBWRAP=0 -DMYSQL_UNIX_ADDR=</code><code>/tmp/mysql</code><code>.sock \</code>

<code>-DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci</code>

<code>[root@jie6 mysql-5.5.33]</code><code># make &amp;&amp; make install</code>

2)、提供mysql的配置檔案和system V腳本,初始化資料庫

<code>[root@jie6 mysql-5.5.33]</code><code># cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf</code>

<code>[root@jie6 mysql-5.5.33]</code><code># cp /usr/local/mysql/support-files/mysql.server  /etc/rc.d/init.d/mysqld</code>

<code>[root@jie6 mysql-5.5.33]</code><code># cd /usr/local/mysql/</code>

<code>[root@jie6 mysql]</code><code># useradd -r mysql</code>

<code>[root@jie6 mysql]</code><code># chown -R root:mysql ./*</code>

<code>[root@jie6 mysql]</code><code># mkdir -pv /mydata/data  建立存放資料庫的路徑,企業一般放在做raid磁盤陣列的LVM上</code>

<code>mkdir</code><code>: created directory `</code><code>/mydata</code><code>'</code>

<code>mkdir</code><code>: created directory `</code><code>/mydata/data</code><code>'</code>

<code>[root@jie6 mysql]</code><code># chown -R mysql:mysql /mydata/data/</code>

<code>[root@jie6 mysql]</code><code># vim /etc/my.cnf</code>

<code>vim </code><code>/etc/my</code><code>.cnf</code>

<code>    </code><code>thread_concurrency = 4</code>

<code>    </code><code>datadir = </code><code>/mydata/data</code>    <code>修改資料庫存放的路徑</code>

<code>[root@jie6 mysql]</code><code># /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/mydata/data/  --basedir=/usr/local/mysql   初始化資料庫,datadir是指定資料庫的存放路徑,basedir是指定資料庫安裝的路徑</code>

<code>[root@jie6 mysql]</code><code># service mysqld start</code>

<code>Starting MySQL........                                     [  OK  ]</code>

3)、把源碼包安裝mysql的PATH變量、庫檔案、頭檔案,關聯到系統識别的路徑下

<code>[root@jie6 mysql]</code><code>#echo "PATH=/usr/local/mysql/bin:$PATH" &gt;/etc/profile.d/mysqld.sh</code>

<code>[root@jie6 mysql]</code><code>#source /etc/profile.d/mysqld.sh</code>

<code>[root@jie6 mysql]</code><code>#echo "/usr/local/mysql/lib" &gt;/etc/ld.so.conf.d/mysqld.conf</code>

<code>[root@jie6 mysql]</code><code>#ldconfig -v | grep mysql</code>

<code>[root@jie6 mysql]</code><code>#ln -sv /usr/local/mysql/include/ /usr/local/mysqld</code>

自此所有伺服器的軟體已經安裝完成,且能成功啟動

二、配置HA高可用叢集

heartbeat的配置檔案必須存放在兩邊的節點上,且完全保持一緻

利用圖形化界面的crm配置heartbeat的資源

[root@jie1 resource.d]#hb_gui &amp; 運作圖形化界面

<a href="http://blog.51cto.com/attachment/201309/105846256.png"></a>

<a href="http://blog.51cto.com/attachment/201309/105903633.png"></a>

<a href="http://blog.51cto.com/attachment/201309/105921122.png"></a>

<a href="http://blog.51cto.com/attachment/201309/105937207.png"></a>

<a href="http://blog.51cto.com/attachment/201309/105951360.png"></a>

<a href="http://blog.51cto.com/attachment/201309/110018164.png"></a>

<a href="http://blog.51cto.com/attachment/201309/110035365.png"></a>

<a href="http://blog.51cto.com/attachment/201309/110050177.png"></a>

<a href="http://blog.51cto.com/attachment/201309/110105569.png"></a>

<a href="http://blog.51cto.com/attachment/201309/110134852.png"></a>

<a href="http://blog.51cto.com/attachment/201309/110153714.png"></a>

<a href="http://blog.51cto.com/attachment/201309/110211159.png"></a>

自此heartbeat實作了nginx的高可用

三、配置LB負載均衡叢集

四、配置反向代理

五、配置動靜分離

由于三四五都隻需要在nginx的配置檔案中實作,部落客在此直接全部配置好

1)、讓nginx支援fastCGI,修改fastcgi_param檔案為以下内

<code>[root@jie1 /]</code><code># cd /etc/nginx/</code>

<code>[root@jie1 nginx]</code><code># vim fastcgi_params</code>

<code>fastcgi_param  GATEWAY_INTERFACE  CGI</code><code>/1</code><code>.1;</code>

<code>fastcgi_param  SERVER_SOFTWARE    nginx;</code>

<code>fastcgi_param  QUERY_STRING       $query_string;</code>

<code>fastcgi_param  REQUEST_METHOD     $request_method;</code>

<code>fastcgi_param  CONTENT_TYPE       $content_type;</code>

<code>fastcgi_param  CONTENT_LENGTH     $content_length;</code>

<code>fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;</code>

<code>fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;</code>

<code>fastcgi_param  REQUEST_URI        $request_uri;</code>

<code>fastcgi_param  DOCUMENT_URI       $document_uri;</code>

<code>fastcgi_param  DOCUMENT_ROOT      $document_root;</code>

<code>fastcgi_param  SERVER_PROTOCOL    $server_protocol;</code>

<code>fastcgi_param  REMOTE_ADDR        $remote_addr;</code>

<code>fastcgi_param  REMOTE_PORT        $remote_port;</code>

<code>fastcgi_param  SERVER_ADDR        $server_addr;</code>

<code>fastcgi_param  SERVER_PORT        $server_port;</code>

<code>fastcgi_param  SERVER_NAME        $server_name;</code>

2)、修改nginx的配置檔案

<code>[root@jie1 ~]</code><code># cd /etc/nginx/</code>

<code>[root@jie1 nginx]</code><code># grep -v "#" nginx.conf| grep -v "^$"</code>

<code>worker_processes  1;</code>

<code>events {</code>

<code>    </code><code>worker_connections  1024;</code>

<code>http {</code>

<code>    </code><code>include       mime.types;</code>

<code>    </code><code>default_type  application</code><code>/octet-stream</code><code>;</code>

<code>    </code><code>sendfile        on;</code>

<code>    </code><code>keepalive_timeout  65;</code>

<code>    </code><code>upstream webphpfpm {</code>

<code>        </code><code>server 172.16.22.4:9000;</code>

<code>        </code><code>server 172.16.22.5:9000;</code>

<code>     </code><code>}  </code><code>#upstream子產品定義負載均衡,此定義php-fpm的負載均衡</code>

<code>    </code><code>server {</code>

<code>        </code><code>listen       80;</code>

<code>        </code><code>server_name  localhost;</code>

<code>        </code><code>location / {</code>

<code>            </code><code>root   </code><code>/web</code><code>;</code>

<code>            </code><code>index  index.php  index.html index.htm;</code>

<code>        </code><code>}</code>

<code>        </code><code>error_page   500 502 503 504  </code><code>/50x</code><code>.html;</code>

<code>        </code><code>location = </code><code>/50x</code><code>.html {</code>

<code>            </code><code>root   html;</code>

<code>        </code><code>location ~ \.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {</code>

<code>              </code><code>proxy_pass  http:</code><code>//172</code><code>.16.22.3;</code>

<code>        </code><code>}  </code><code>#proxy_pass定義靜态請求的反向代理</code>

<code>        </code><code>location ~ \.(php|css|jsp)$ {</code>

<code>            </code><code>root      </code><code>/webphp</code><code>;  </code><code>#此處定義後端php-fpm伺服器的網頁存放路</code>

<code>                                 </code><code>徑,後端此伺服器必須有此目錄</code>

<code>            </code><code>fastcgi_pass   webphpfpm;</code>

<code>            </code><code>fastcgi_index  index.php;</code>

<code>            </code><code>fastcgi_param  SCRIPT_FILENAME  </code><code>/scripts</code><code>$fastcgi_script_name;</code>

<code>            </code><code>include        fastcgi_params;</code>

<code>        </code><code>} </code><code>#動态請求送出給後端的php-fpm伺服器,webphpfpm為此前定義負載均衡的名稱</code>

<code>    </code><code>}</code>

3)、複制nginx主伺服器的配置檔案和支援fastcgi的檔案到nginx備伺服器上

<code>[root@jie1 nginx]</code><code># scp nginx.conf  172.16.22.2:/etc/nginx/</code>

<code>[root@jie1 nginx]</code><code># scp fastcgi_params 172.16.22.2:/etc/nginx/</code>

六、測試

測試檔案的準備

Apache伺服器上面建立網頁檔案

<code>[root@jie3 html]</code><code># pwd</code>

<code>/var/www/html</code>

<code>[root@jie3 html]</code><code># ls</code>

<code>1.jpeg  index.html   在網頁根目錄下存放一個測試檔案和一張圖檔用于測試</code>

<code>[root@jie3 html]</code><code># cat index.html</code>

<code>&lt;h1&gt;this is Apache server&lt;</code><code>/h1</code><code>&gt;</code>

<code>[root@jie3 html]</code><code>#</code>

所有的php-fpm伺服器上面建立網頁檔案,在生産環境中必須保持一樣

php-fpm1伺服器的測試頁面

<code>[root@jie4 webphp]</code><code># pwd</code>

<code>/webphp</code>    <code>#此檔案夾是存放網頁檔案的根目錄,是在nginx裡面指定的目錄</code>

<code>[root@jie4 webphp]</code><code># ls</code>

<code>index.php  testdb.php  </code><code>test</code><code>.php</code>

<code>[root@jie4 webphp]</code><code># cat index.php   測試頁面</code>

<code>&lt;h1&gt; this is php-fpm1 server &lt;</code><code>/h1</code><code>&gt;</code>

<code>[root@jie4 webphp]</code><code># cat test.php   測試phpinfo頁面</code>

<code>&lt;h1&gt;php-fpm1&lt;</code><code>/h1</code><code>&gt;</code>

<code>&lt;?php</code>

<code>phpinfo();</code>

<code>?&gt;</code>

<code>[root@jie4 webphp]</code><code># cat testdb.php   測試連接配接資料庫的頁面</code>

<code>$link=mysql_connect(</code><code>'172.16.22.6'</code><code>,</code><code>'root'</code><code>,</code><code>'mypass'</code><code>);</code>

<code>if</code> <code>($link) </code><code>echo</code>  <code>"mysql test success!!"</code><code>;</code>

<code>else</code> <code>echo</code> <code>"mysql test failed!!!"</code><code>;</code>

<code>mysql_close();</code>

<code>[root@jie4 webphp]</code><code>#</code>

php-fpm2伺服器的測試頁面

<code>[root@jie5 webphp]</code><code># pwd</code>

<code>/webphp</code>

<code>[root@jie5 webphp]</code><code># ls</code>

<code>[root@jie5 webphp]</code><code># cat index.php</code>

<code>&lt;h1&gt; this is php-fpm2 server &lt;</code><code>/h1</code><code>&gt;</code>

<code>[root@jie5 webphp]</code><code># cat test.php</code>

<code>&lt;h1&gt;php-fpm2&lt;</code><code>/h1</code><code>&gt;</code>

<code>[root@jie5 webphp]</code><code># cat testdb.php</code>

<code>[root@jie5 webphp]</code><code>#</code>

1)測試動靜分離

通路的是vip的位址,靜态網頁檔案和圖檔都會被nginx代理到Apache伺服器上,

<a href="http://blog.51cto.com/attachment/201309/115155595.png"></a>

<a href="http://blog.51cto.com/attachment/201309/115805770.png"></a>

測試動态的網頁檔案,被nginx代理到php-fpm伺服器上

<a href="http://blog.51cto.com/attachment/201309/115725818.png"></a>

2)測試負載均衡

測試phpinfo檔案,多測試幾次看看是不是負載到不同的php-fpm伺服器上

<a href="http://blog.51cto.com/attachment/201309/120037701.png"></a>

<a href="http://blog.51cto.com/attachment/201309/120057202.png"></a>

3)測試mysql

測試是否可以連接配接mysql的測試檔案

<a href="http://blog.51cto.com/attachment/201309/120249664.png"></a>

<a href="http://blog.51cto.com/attachment/201309/120306382.png"></a>

4)測試高可用

用heartbeat宕到nginx主伺服器,看nginx備伺服器是否繼續提供服務

現在看見資源都運作nginx主伺服器上

<a href="http://blog.51cto.com/attachment/201309/120913982.png"></a>

停掉nginx主伺服器的heartbeat,看資源是否在nginx備伺服器上自動啟動

<code>[root@jie1 nginx]</code><code># service heartbeat status</code>

<code>heartbeat OK [pid 4294 et al] is running on jie1.com [jie1.com]...</code>

<code>[root@jie1 nginx]</code><code># service heartbeat stop</code>

<code>Stopping High-Availability services:</code>

<code>Done.</code>

<code>[root@jie1 nginx]</code><code>#</code>

可以看見nginx主伺服器jie1.com節點當機之後nginx備伺服器自行啟動并搶占資源

<a href="http://blog.51cto.com/attachment/201309/122159369.png"></a>

自此heartbeat+nginx實作HA的高可用和LB負載均衡已經完成

此部落格沒有對nginx的配置檔案參數做詳細說明,也沒有設定nginx的優化參數,有關nginx的優化以及nginx配置檔案詳解,以及nginx實作諸多功能的詳細配置會在nginx相關部落格中寫出。請大家多多關注

<a href="http://down.51cto.com/data/2363504" target="_blank">附件:http://down.51cto.com/data/2363504</a>

本文轉自 jie783213507 51CTO部落格,原文連結:http://blog.51cto.com/litaotao/1297322,如需轉載請自行聯系原作者