ubuntu下编译安装nginx

本文介绍如何从零开始配置Nginx以支持SSL加密连接、静态文件压缩、FLV流媒体播放及真实IP获取等功能。通过详细步骤说明安装所需依赖库并进行配置的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值