Dave Perrett

Compiling Nginx With Flv Streaming Support on Ubuntu

flash, flex, java, linux, nginx, programming, streaming, ubuntu, web

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

1
2
3
> 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) :

1
2
3
4
5
6
> ./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.

1
2
3
4
./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.

1
2
3
4
5
6
7
> 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.

1
2
3
4
./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 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
> 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 :

1
2
> make
> sudo make install

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

1
> 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 :

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
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
#! /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) :

1
2
> /etc/init.d/nginx stop
> /etc/init.d/nginx start

Finally, we want to make sure nginx launches at startup :

1
> 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 :

1
2
3
4
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!