ubuntu下编译安装nginx

First, we need to get the latest version of nginx :

> wget http://nginx.org/download/nginx-0.8.34.tar.gz
> tar xvzf nginx-0.8.34.tar.gz
> cd nginx-0.8.34
Next, configure it to support ssl, gzip, flv streaming and real-ip. I generally compile it to /opt/nginx-YYMMDD (change the —prefix setting if you want to put it somewhere else) :

> ./configure \
        --prefix=/opt/nginx-20100311 \
        --with-http_ssl_module \
        --with-http_gzip_static_module \
        --with-http_flv_module \
        --with-http_realip_module
At this point I got an error about missing PCRE – this Ubuntu installation is basically fresh so there’s a lot of stuff missing.

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
I had libpcre3 installed, but i needed libpcre3-dev as well.

> sudo aptitude install libpcre3-dev 
> ./configure \
        --prefix=/opt/nginx-20100311 \
        --with-http_ssl_module \
        --with-http_gzip_static_module \
        --with-http_flv_module \
        --with-http_realip_module
We get a bit further this time, but still no luck.

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.
This time it’s libssl-dev that’s missing :

> sudo aptitude install libssl-dev
> ./configure \
        --prefix=/opt/nginx-20100311 \
        --with-http_ssl_module \
        --with-http_gzip_static_module \
        --with-http_flv_module \
        --with-http_realip_module
.....
.....
.....

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + md5: using OpenSSL library
  + sha1 library is not used
  + using system zlib library
Now that we’ve successfully configured it, we can compile and install :

> make 
> sudo make install
Now, make a symlink so you can switch to newer versions easily :

> sudo ln -s /opt/nginx-20100311 /opt/nginx
Finally, we want to set up a startup script. Create the file /etc/init.d/nginx and paste the following in :

#! /bin/sh

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

PATH=/opt/nginx/sbin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/opt/nginx/sbin/nginx
NAME=nginx
DESC=nginx

test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
	. /etc/default/nginx
fi

set -e

case "$1" in
  start)
	echo -n "Starting $DESC: "
	start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
		--exec $DAEMON -- $DAEMON_OPTS
	echo "$NAME."
	;;
  stop)
	echo -n "Stopping $DESC: "
	start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
		--exec $DAEMON
	echo "$NAME."
	;;
  restart|force-reload)
	echo -n "Restarting $DESC: "
	start-stop-daemon --stop --quiet --pidfile \
		/var/run/$NAME.pid --exec $DAEMON
	sleep 1
	start-stop-daemon --start --quiet --pidfile \
		/var/run/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS
	echo "$NAME."
	;;
  reload)
      echo -n "Reloading $DESC configuration: "
      start-stop-daemon --stop --signal HUP --quiet --pidfile /var/run/$NAME.pid \
          --exec $DAEMON
      echo "$NAME."
      ;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	exit 1
	;;
esac

exit 0
This allows you to start/stop the server easily (although I find stop a bit flakey) :

> /etc/init.d/nginx stop
> /etc/init.d/nginx start
Finally, we want to make sure nginx launches at startup :

> sudo update-rc.d nginx defaults
Now you should be able to load http://yourserver.com/ and see nginx.

The last thing to do is enable the flv streaming module. You’ll need to add a rule to your nginx.conf something like the following :

location ~ \.flv$ {
	flv;
	root /path/to/your/flv/files;
}
This will catch any request with a .flv extension, and stream it from the folder /path/to/your/flv/files .

Done!

猜你喜欢

转载自kevin1.iteye.com/blog/902769