首页 > 代码库 > Nginx之——安装

Nginx之——安装

软件下载(Nginx官网):http://nginx.org/ 

一、准备环境:

1、安装pcre:pcre(Perl Compatible Regular Expressions :Perl兼容的正则表达式)用来支持rewrite

cd /tol/soft

tar -jxf pcre-8.32.tar.bz2

cd pcre-8.32

./configure --prefix=/usr/local/pcre

make && make install

cp /usr/local/pcre/lib/libpcre.a /usr/local/pcre/libpcre.a

cp /usr/local/pcre/lib/libpcre.la /usr/local/pcre/libpcre.la

cp /usr/local/pcre/include/pcre.h /usr/local/pcre/pcre.h

mkdir /usr/local/pcre/.libs

cp /usr/local/pcre/lib/libpcre.a /usr/local/pcre/.libs/libpcre.a

cp /usr/local/pcre/lib/libpcre.la /usr/local/pcre/.libs/libpcre.la

cp /usr/local/pcre/include/pcre.h /usr/local/pcre/.libs/pcre.h


2、创建用户

#useradd -M -s /sbin/nologin nginx


二、安装

cd /tol/soft

tar -zxf nginx-1.2.4.tar.gz 

tar -zxf nginx-upstream-jvm-route-0.1.tar.gz

cd nginx-1.2.4

patch -p0 < /tol/soft/nginx_upstream_jvm_route/jvm_route.patch


#nginx 编译参数参考如下#如果提示pcre相关的错误,按照以上方法安装pcre后 添加参数 --with-pcre=/usr/local/pcre

#nginx_upstream_jvm_route模块是解决nginx负载均衡中tomcat session 不同步问题,不过他不是共享,也不是同步,而是通过 cookie_session 来判别!

./configure  --user=nginx --group=nginx --prefix=/tol/app/nginx-1.2.4  --with-http_ssl_module  --with-http_stub_status_module --add-module=/tol/soft/nginx_upstream_jvm_route


make && make install

若make的时候报错,则

vi objs/Makefile

大概在1129行 ***./configure --disable-shared


ln -s /tol/app/nginx-1.2.4 /tol/app/nginx

cp /tol/app/nginx/sbin/nginx /bin/


三、配置Nginx为系统服务

管理脚本(修改脚本中的install_dir变量值为实际安装目录即可):

vi /etc/init.d/nginx

#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15 
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse #               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /tol/app/nginx/conf/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid
 
# Source function library.
. /etc/rc.d/init.d/functions
 
# Source networking configuration.
. /etc/sysconfig/network
 
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 
install_dir=/tol/app/nginx 
nginx="$install_dir/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="$install_dir/conf/nginx.conf"
 
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 
lockfile=/var/lock/subsys/nginx
 
make_dirs() {
   # make required directories
   user=`nginx -V 2>&1 | grep "configure arguments:" | sed ‘s/[^*]*--user=\([^ ]*\).*/\1/g‘ -`
   options=`$nginx -V 2>&1 | grep ‘configure arguments:‘`
   for opt in $options; do
       if [ `echo $opt | grep ‘.*-temp-path‘` ]; then
           value=http://www.mamicode.com/`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
 
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
 
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
 
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
 
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
 
force_reload() {
    restart
}
 
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
 
rh_status() {
    status $prog
}
 
rh_status_q() {
    rh_status >/dev/null 2>&1
}
 
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

设置Nginx开机自启动:

chmod +x /etc/init.d/nginx

chkconfig --add nginx

chkconfig nginx on


启动Nginx:

service nginx start