返回列表 发帖

Ubuntu 9.10安装Nginx 0.8.34和upstream fair

首先,为了编译 Nginx,应在新装好的 Ubuntuserver 环境下安装如下软件包:
9 v  K9 k! k+ K
* m# F/ B8 n$ P3 @" t sudo apt-get install build-essential libpcre3-dev libssl-dev libxslt-dev libgd2-xpm-dev libgeoip-dev 9 \! L/ G, d  d% C
/ M/ H# g/ S, P. j  a) L( L
然后下载 0.8.34 版本的 Nginx:
/ @2 a  W1 Z  F% M2 Z& {9 R" M7 `2 z
9 W) M, ?) w9 Q$ ?
* w  A; |# ~  Q" Z# U# V! ?( Q
! [* Y& B2 ?7 D$ ~( ]0 ~: Cwget http://www.linuxidc.com/system/s ... nginx-0.8.34.tar.gz3 d9 ^9 j# H& b- y
; d! ^4 M& ^) L( Z7 h5 a- j7 ~
解压:( k7 y/ s( U9 v& I6 u

* s3 [, p9 j6 i, H4 h, i; Y tar xvzf nginx-0.8.34.tar.gz
" L3 Y- P( T  ?* y6 a6 }+ d$ t! h# j8 L7 l  Z
下载 upstream fair 模块。upstream fair 是比内建的负载均衡更加智能的负载均衡模块。它采用的不是内建负载均衡使用的轮换的均衡算法,而是可以根据页面大小、加载时间长短智能的进行负载均衡。- u: A% Q4 H% R: i
$ v5 I; |9 E5 ^1 u" j8 r% M
wget http://www.linuxidc.com/system/s ... fair-2131c73.tar.gz5 A" y; Z1 F: K% y2 j' o
& K5 i; @2 j) V) ^; @
解压:
( R( @) R" V6 ?* U4 a$ d( Q/ ~, r
tar xvzf gnosek-nginx-upstream-fair-2131c73.tar.gz
* u& S1 x, ^  ~+ |; g. w: t, @4 r: ^
然后进入 nginx 源码目录执行 configure 配置编译选项。下面是我所使用的配置:% {6 v' t8 z. J8 [0 P. p- S

  D$ R( w: m& K8 c6 x* e9 l/ j ./configure --conf-path=/etc/nginx/nginx.conf \--error-log-path=/var/log/nginx/error.log \
4 b& @4 `4 f1 h: W7 r5 R--pid-path=/var/run/nginx.pid \) U" _* Z2 q1 @2 n& e
--lock-path=/var/lock/nginx.lock \
; H$ ~# w2 g/ q! S) ?- v--http-log-path=/var/log/nginx/access.log \1 O" \' J! P' ]; q" ]+ ?. N8 d( P
--http-client-body-temp-path=/var/lib/nginx/body \, N% i: f. L8 d& Z# D) k4 `. Q8 _
--http-proxy-temp-path=/var/lib/nginx/proxy \% [' W5 `# \* Z; d
--http-fastcgi-temp-path=/var/lib/nginx/fastcgi \
% s; J* }5 J8 h" r% f--with-debug \
, M8 W+ u8 H* I$ J! T--with-http_stub_status_module \
2 _- H  b2 w2 m2 `--with-http_flv_module \
) |8 z  c# h, L& u- n--with-http_ssl_module \) g: {0 F, E2 ]4 C9 d1 s
--with-http_dav_module \+ a6 |! t! M3 S+ F) S1 f
--with-http_gzip_static_module \" T. v% B3 r5 _% f( Y! n; U8 u
--with-mail \# G: p3 J; l7 V0 S9 v6 o
--with-mail_ssl_module \, h+ @" S: w2 {3 a* ]! x& Y
--with-ipv6 \
! Q/ \" \8 D* [' E; Y  B+ A' c$ L--with-http_realip_module \
6 ~' Q5 \" w6 a8 ~--with-http_geoip_module \
) b& ]' G8 d4 j+ l--with-http_xslt_module \3 m$ Y) Y! J9 V- v$ k* K4 G
--with-http_image_filter_module \' R2 [, c5 V1 H6 r* b
--with-sha1=/usr/include/openssl \
8 m; A( {! L/ V  W  v4 }( e--with-md5=/usr/include/openssl \
) X2 r8 G  p% `3 h; u--add-module=/home/mikespook/gnosek-nginx-upstream-fair-2131c73
5 C9 ]+ Z; ]9 \1 }7 ~$ k0 W- {4 }$ G
, p  k9 C( E$ Z; Z! k& S这个配置来自于 Jeff Waugh 的 PPA 中的 nginx 0.8.34 编译选项。配置、lock、pid 等文件的位置都是按照 Ubuntu 系统惯例设置的。需要注意的是 –add-module 指向的是 upstream fair 的解压缩目录的绝对路径。这样就可以将 upstream fair 编译进 nginx0 w( ]# D9 L" d. \7 c( m

# Y' n0 U( r0 y然后编译并安装:. p% n7 ]3 r' _* f- w  |# S
, [4 N; r8 [& X$ g9 n
make && make install 6 h( S1 j" I6 i/ U, r" M1 n+ {
/ [7 X* t; C) T) w8 v* m
nginx 就安装成功了。目录 /var/lib/nginx 需要手工建立,否则启动 nginx 会报错:8 J+ @# V& l+ n, |" t

: S& {2 f0 ^4 }0 x: l3 v% f2 C
1 N% g) r# @  G* d* \8 c [emerg]: mkdir() "/var/lib/nginx/body" failed (2: No such file or directory)  4 |4 U5 J9 t+ d
& e0 i. r/ p0 A% r# {- a* A
5 ]% ?/ k' j( @& [9 j* m
建立shell 脚本 /etc/init.d/nginx(nginx):
# I" @& c/ M2 _' Z% V. c
9 q4 V" ^: J* p2 o9 X( b& [ #! /bin/sh0 u9 ^. ~! p8 n& L% `9 r) U' }

7 n) q) R: _  x3 ^### BEGIN INIT INFO
2 z: ~3 S! Z; F2 C* F! H; g" W% ^# Provides:          nginx% ~* x" T- E6 ]. [* H% ^6 J3 b! u$ f
# Required-Start:    $all! L+ a: W' ~" L0 w  U( U9 `+ R
# Required-Stop:     $all
: w  U0 m- u; U8 N( F# Default-Start:     2 3 4 5
# O* t$ N. K! R" I# Default-Stop:      0 1 61 l# n1 |2 K6 s$ f% ]
# Short-Description: starts the nginx web server4 t' R4 P$ g$ I# K# i  e" Q
# Description:       starts nginx using start-stop-daemon! l3 U5 ?; B3 X4 y8 Q
### END INIT INFO
+ u6 C( G) F! F  r: [
8 ^$ }' p1 x( xPATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin* ^+ W( v# V; n6 Q0 f5 s
DAEMON=/usr/local/nginx/sbin/nginx! k9 H- h% w$ i4 k' s% ?% G% G
NAME=nginx& J1 B$ P* J) N0 c- Y. U
DESC=nginx
. Z9 V" C6 S" W" v1 S , i( M/ D2 F: _0 W5 Y& @5 m: u
test -x $DAEMON || exit 0
* z# f5 ?2 Y0 S- N7 I4 ?% H4 q6 Q , P4 C6 L6 }9 q% u8 \  d
# Include nginx defaults if available+ k9 C* F4 \3 F* ?0 N
if [ -f /etc/default/nginx ] ; then
; x" l7 [4 l) c        . /etc/default/nginx
" D9 m& w) |0 q: X3 tfi7 a/ U, J' F& h; M8 h: w
1 y6 q: h! b, P# m
set -e
1 s9 ~: U, j. q& K; D/ [! L
7 x' m1 t5 E) @/ Lcase "$1" in
; b$ |6 \1 V0 n, [+ F1 ~  start)
. U" k+ b) ?- ]/ M7 b        echo -n "Starting $DESC: "
! C  l* {( M% D+ I  M" u  @        start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \% A: i3 z2 x' R" g  A
                --exec $DAEMON -- $DAEMON_OPTS- _( ?, c. m+ j' d9 U( l
        echo "$NAME."+ p0 C3 b+ o9 W2 s3 r1 Y& |
        ;;. a% ]. y% H% S) c6 \5 I2 m6 [
  stop)$ P" B0 y2 {6 F3 P
        echo -n "Stopping $DESC: "' B0 r: L: y1 O$ |2 s
        start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
4 R7 e0 B6 [. @: B9 W                --exec $DAEMON$ q# l+ z( J1 u/ q" B  x
        echo "$NAME."2 r% A# z  x  O
        ;;
6 ]& g3 H: H( O! Y  restart|force-reload)4 D; o6 P0 {6 P, o8 x: B
        echo -n "Restarting $DESC: "
0 p, ~8 s# K, o/ c3 Z        start-stop-daemon --stop --quiet --pidfile \
& J3 R9 g  G; W8 ?! u                /var/run/$NAME.pid --exec $DAEMON- N& q3 h" k) l
        sleep 1
! F! _7 G6 e  |- W& F; o        start-stop-daemon --start --quiet --pidfile \& W2 h1 h, N" o
                /var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS  h/ _$ q: \  w& q, F! l: b! N8 X/ }( p
        echo "$NAME."/ {* v) k# c; T: K
        ;;0 l0 _) n$ R* a3 H$ |/ l% U* P, Q$ p! w
  reload); C- E+ M- I9 p  l
      echo -n "Reloading $DESC configuration: "
3 h6 f; v1 }8 _/ e) u0 U. ?      start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \- a2 V* h1 u3 T" o- N8 @
          --exec $DAEMON) H' V! Q; I, D
      echo "$NAME."1 R+ F8 K1 V# V* `  F' K3 |
      ;;/ d1 H9 K+ g5 @0 \8 c6 V, N0 d
  *)( [; [2 a  T- E7 ~* h0 `& A* p! S
        N=/etc/init.d/$NAME/ T) N  k* ~" W: ~- o9 N3 `
        echo "Usage: $N {start|stop|restart|force-reload}" >&2, g$ k- I  |. y5 ~
        exit 18 R) v' C! I" ]
        ;;
8 u  I* x, {1 c  H  @esac+ A8 [$ M/ c% p- K0 O8 v

/ J/ R6 x: M/ c& ^: O" Xexit 0
/ [: {0 g) ]4 s, [2 h5 c. T+ J7 S* p  X( W
并执行命令:
1 b5 Y& c2 I+ T2 p
" W$ Q, E& A3 G/ a
4 n& p: l2 U& a sudo update-rc.d -f nginx defaults + Z4 t- h7 |) i3 f) w
/ O8 O0 V  B- v, i* d
更新 rc 后,即可使用:) z; j3 l1 ~6 |# ~8 H0 C

! d- \9 x$ i$ P, [9 ] sudo /etc/init.d/nginx start  l' H6 Y. D) u5 b" B
sudo /etc/init.d/nginx stop% S1 A$ a8 O5 X9 G9 `
sudo /etc/init.d/nginx restart 6 E# `1 c4 x2 G

: `% I- B! l1 T+ L控制 nginx 启动。
3 c/ _" e1 ~/ N5 a其他配置不再累述,张宴的 blog 里写得非常清晰。只补充一下 upstream fair 的使用,只要在 nginx 配置文件的 upstream 段加入 fair 开关即可:  i9 ]; r# ]* d3 D+ z# ?
/ Y& p, p# d; t& `: d2 z
upstream backend {
0 z& C& P- I  q! B. ^    server server1;
$ J0 t7 }, E$ M+ d' R. g; }; H4 j$ J    server server2;
  ^$ O3 ]' K+ U9 h4 l8 r( U9 i    fair;9 F) Z1 Y: J2 O  X
} * h' C: E$ \4 u# s, b3 P

- G& |: g0 s. O" `1 s1 I当然,Ubuntu 还可以用上面提到的 Jeff 的 ppa 源进行安装:  z3 h) ]# E8 ~' e: v7 W/ r
9 P4 u7 _# y% ~0 f1 S0 q
echo "deb http://ppa.launchpad.net/jdub/devel/ubuntu hardy main" >> /etc/apt/sources.list
& Y" h8 a9 Q" [2 K. }: S$ ]8 J8 \apt-key adv --keyserver keyserver.Ubuntu.com --recv-keys E9EEF4A10 k0 d/ S% E  p+ T" v8 q; o
apt-get update- n+ M; H! l0 t: H
apt-get install nginx - \2 ?2 A" G/ a- ]

$ K* h1 d. r7 T4 H+ d简单快捷。

返回列表