下面假设相关的软件及模块的压缩包都在/home/soft目录下:
1. 解压nginx
cd /home/soft
tar -zxvf nginx-1.2.0.tar.gz
2. 准备nginx_upstream_jvm_route模块的包
cd /nginx-1.2.0
patch -p0 < ../nginx_upstream_jvm_route/jvm_route.patch
3. 解压下载好的pcre包
cd ../
tar -zxvf pcre-8.31.tar.gz
4.下面安装nginx
cd /nginx-1.2.0
./configure --prefix=/usr/local/nginx --with-pcre=/home/soft/pcre-8.31 --with-http_stub_status_module --with-http_ssl_module --add-module=/home/soft/nginx_upstream_jvm_route
make
make install
5. 查看Nginx已安装/编译的模块
/usr/local/nginx/sbin/nginx -V
6. 使用中发现某个模块(例如:http_realip_module)未安装?可以重新编译,然后复制objs/ngxin到nginx的sbin目录,替换原来的文件
重新编译并添加新模块(注意make后就不要make install了,不然就是覆盖安装了)
cd /nginx-1.2.0
./configure --prefix=/usr/local/nginx --with-pcre=/home/soft/pcre-8.31 --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --add-module=/home/soft/nginx_upstream_jvm_route
make
备份并替换原来的sbin/nginx文件
cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
cp ./objs/nginx /app/nginx/sbin/
7. 让nginx作为服务启动
vi /etc/init.d/nginx
然后输入如下内容
#! /bin/sh
# description: Nginx Server
# chkconfig: 2345 99 01
# source function library
. /etc/rc.d/init.d/functions
set -e
ulimit -SHn 51200
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="nginx daemon"
NAME=nginx
DAEMON=/usr/local/nginx/sbin/$NAME
CONFIGFILE=/usr/local/nginx/conf/nginx.conf
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0
d_start() {
$DAEMON -c $CONFIGFILE || echo -n " already running"
}
d_stop() {
kill -QUIT `cat $PIDFILE` || echo -n " not running"
killall nginx >/dev/null 2>1&
}
d_test() {
$DAEMON -t
}
d_reload() {
kill -HUP `cat $PIDFILE` || echo -n " can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
test)
d_test
;;
reload)
echo -n "Reloading $DESC configuration..."
d_reload
echo "reloaded."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
# One second might not be time enough for a daemon to stop,
# if this happens, d_start will fail (and dpkg will break if
# the package is being upgraded). Change the timeout if needed
# be, or change d_stop to have start-stop-daemon use --retry.
# Notice that using --retry slows down the shutdown process somewhat.
sleep 1
d_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 3
;;
esac
exit 0
添加执行权限
chmod +x /etc/init.d/nginx
添加服务
chkconfig --add nginx
chkconfig nginx on
启动/停止服务
service nginx start/stop
8. 启动失败
如下
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
查看端口占用情况
netstat -ntpl
结束占用端口的进程,重新启动nginx