nginx编译安装(CentOS)

Update: 2013-01-22

设置主机名

h="TMXLlvs2" \
&& hostname $h \
&& sed -i.bak "s/\(HOSTNAME=\).*$/\1$h/g" /etc/sysconfig/network \
&& grep "127.0.0.1.*$h" /etc/hosts || sed -i "s/\(127.0.0.1.*$\)/\1 $h/g" /etc/hosts \
&& ping -c 4 $h

编译环境

yum -y install gcc gcc-c++ autoconf automake make
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel libxml2 libxml2-devel

下载

mkdir -p ~/work/ && cd ~/work/ \
&& wget http://nginx.org/download/nginx-1.2.5.tar.gz \
&& wget https://nginx-upstream-jvm-route.googlecode.com/files/nginx-upstream-jvm-route-0.1.tar.gz

用户及工作目录

groupadd -r nginx && useradd -r -g nginx nginx -s /sbin/nologin -d /dev/null
install -m 755 -o nginx -g nginx -d /var/cache/nginx
install -m 755 -o nginx -g nginx -d /var/log/nginx/

编译安装

V=1.2.5 \
&& cd ~/work/ \
&& tar zxvf nginx-$V.tar.gz \
&& tar zxvf nginx-upstream-jvm-route-0.1.tar.gz \
&& cd nginx-$V \
&& sed -i.bak 's/^CFLAGS="$CFLAGS -g"/#&/' auto/cc/gcc \
&& patch -p0 < ../nginx_upstream_jvm_route/jvm_route.patch \
&& ./configure --user=nginx --group=nginx \
--prefix=/usr/local/nginx \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--pid-path=/var/run/nginx.pid \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_ssl_module \
--add-module=../nginx_upstream_jvm_route \
--with-cc-opt='-O2 -g' \
&& make && make install

启动脚本

[ -f /usr/sbin/nginx ] && mv /usr/sbin/nginx{,.backup}
ln -sfT /usr/local/nginx/sbin/nginx /usr/sbin/nginx

#install -m 755 -o root -g root "CentOS_init.nginx2" /etc/init.d/nginx
#!/bin/sh
#
# nginx        Startup script for nginx
#
# chkconfig: - 85 15
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# description: nginx is a HTTP and reverse proxy server
#
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop nginx
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/nginx ]; then
    . /etc/sysconfig/nginx
fi

prog=nginx
#nginx=${NGINX-/usr/sbin/nginx}
#conffile=${CONFFILE-/etc/nginx/nginx.conf}
nginx=${NGINX-/usr/local/nginx/sbin/nginx}
conffile=${CONFFILE-/usr/local/nginx/conf/nginx.conf}
lockfile=${LOCKFILE-/var/lock/subsys/nginx}
pidfile=${PIDFILE-/var/run/nginx.pid}
SLEEPMSEC=100000
RETVAL=0

start() {
    echo -n $"Starting $prog: "

    daemon --pidfile=${pidfile} ${nginx} -c ${conffile}
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch ${lockfile}
    return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    killproc -p ${pidfile} ${prog}
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}

reload() {
    echo -n $"Reloading $prog: "
    killproc -p ${pidfile} ${prog} -HUP
    RETVAL=$?
    echo
}

upgrade() {
    oldbinpidfile=${pidfile}.oldbin

    configtest -q || return 6
    echo -n $"Staring new master $prog: "
    killproc -p ${pidfile} ${prog} -USR2
    RETVAL=$?
    echo
    /bin/usleep $SLEEPMSEC
    if [ -f ${oldbinpidfile} -a -f ${pidfile} ]; then
        echo -n $"Graceful shutdown of old $prog: "
        killproc -p ${oldbinpidfile} ${prog} -QUIT
        RETVAL=$?
        echo 
    else
        echo $"Upgrade failed!"
        return 1
    fi
}

configtest() {
    if [ "$#" -ne 0 ] ; then
        case "$1" in
            -q)
                FLAG=$1
                ;;
            *)
                ;;
        esac
        shift
    fi
    ${nginx} -t -c ${conffile} $FLAG
    RETVAL=$?
    return $RETVAL
}

rh_status() {
    status -p ${pidfile} ${nginx}
}

# See how we were called.
case "$1" in
    start)
        rh_status >/dev/null 2>&1 && exit 0
        start
        ;;
    stop)
        stop
        ;;
    status)
        rh_status
        RETVAL=$?
        ;;
    restart)
        configtest -q || exit $RETVAL
        stop
        start
        ;;
    upgrade)
        upgrade
        ;;
    condrestart|try-restart)
        if rh_status >/dev/null 2>&1; then
            stop
            start
        fi
        ;;
    force-reload|reload)
        reload
        ;;
    configtest)
        configtest
        ;;
    *)
        echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|upgrade|reload|status|help|configtest}"
        RETVAL=2
esac

exit $RETVAL

Nginx+google-perftools

仅做记录,还没有实验过。

#!/bin/bash
# nginx 环境安装脚本,注意环境不同可能导致脚本运行出错,如果环境不同建议手工一条一条执行指令。
# 环境:CentOS release 5.8 Final
 
#创建工作目录
mkdir -p /dist/{dist,src}
cd /dist/dist
 
#下载安装包
wget http://bbs.osapub.com/down/google-perftools-1.8.3.tar.gz &> /dev/null
wget http://bbs.osapub.com/down/libunwind-0.99.tar.gz &> /dev/null
wget http://bbs.osapub.com/down/pcre-8.01.tar.gz &> /dev/null
wget http://bbs.osapub.com/down/nginx-1.0.5.tar.gz &> /dev/null
 
#------------------------------------------------------------------------
# 使用Google的开源TCMalloc库,忧化性能
 
cd /dist/src
tar zxf ../dist/libunwind-0.99.tar.gz
cd libunwind-0.99/
 
## 注意这里不能加其它 CFLAGS加速编译参数
 
CFLAGS=-fPIC ./configure
make clean
make CFLAGS=-fPIC
make CFLAGS=-fPIC install
if [ "$?" == "0" ]; then
    echo "libunwind-0.99安装成功." >> ./install_log.txt
else
    echo "libunwind-0.99安装失败." >> ./install_log.txt
    exit 1
fi
 
##----------------------------------------------------------
## 使用Google的开源TCMalloc库,提高MySQL在高并发情况下的性能
 
cd /dist/src
tar zxf ../dist/google-perftools-1.8.3.tar.gz
cd google-perftools-1.8.3/
 
CHOST="x86_64-pc-linux-gnu" CFLAGS="-march=nocona -O2 -pipe" CXXFLAGS="-march=nocona -O2 -pipe" \
./configure
 
make clean
make && make install
if [ "$?" == "0" ]; then
    echo "google-perftools-1.8.3安装成功." >> ./install_log.txt
else
    echo "google-perftools-1.8.3安装失败." >> ./install_log.txt
    exit 1
fi
 
echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
/sbin/ldconfig
 
 
################ 安装nginx ##########################
#安装Nginx所需的pcre库
 
cd /dist/src
tar zxvf ../dist/pcre-8.01.tar.gz
cd pcre-8.01/
 
CHOST="x86_64-pc-linux-gnu" CFLAGS="-march=nocona -O2 -pipe" CXXFLAGS="-march=nocona -O2 -pipe" \
./configure
make && make install
if [ "$?" == "0" ]; then
    echo "pcre-8.01安装成功." >> ./install_log.txt
else
    echo "pcre-8.01安装失败." >> ./install_log.txt
    exit 1
fi
cd ../
 
 
## 安装Nginx
## 为优化性能,可以安装 google 的 tcmalloc ,之前己经安装过了
## 所以我们编译 Nginx 时,加上参数 --with-google_perftools_module
## 然后在启动nginx前需要设置环境变量 export LD_PRELOAD=/usr/local/lib/libtcmalloc.so
## 加上 -O2 参数也能优化一些性能
##
## 默认的Nginx编译选项里居然是用 debug模式的(-g参数),在 auto/cc/gcc 文件最底下,去掉那个 -g 参数
## 就是将  CFLAGS="$CFLAGS -g"  修改为   CFLAGS="$CFLAGS"   或者直接删除这一行
 
cd /dist/src
rm -rf nginx-1.0.5
tar zxf ../dist/nginx-1.0.5.tar.gz
cd nginx-1.0.5/
 
sed -i 's#CFLAGS="$CFLAGS -g"#CFLAGS="$CFLAGS "#' auto/cc/gcc
 
make clean
 
CHOST="x86_64-pc-linux-gnu" CFLAGS="-march=nocona -O2 -pipe" CXXFLAGS="-march=nocona -O2 -pipe" \
./configure --user=www --group=www \
--prefix=/usr/local/nginx \
--with-http_stub_status_module \
--with-google_perftools_module
 
make && make install
if [ "$?" == "0" ]; then
    echo "nginx-1.0.5安装成功." >> ./install_log.txt
else
    echo "nginx-1.0.5安装失败." >> ./install_log.txt
    exit 1
fi
cd ../
 
#创建Nginx日志目录
mkdir -p /data/logs
chmod +w /data/logs
chown -R www:www /data/logs
 
cd /usr/local/nginx/
mv conf conf_bak
ln -s /data/conf/nginx/ conf
 
echo 'export LD_PRELOAD=/usr/local/lib/libtcmalloc.so' > /root/nginx_start
echo 'ulimit -SHn 51200' >> /root/nginx_start
echo '/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf' >> /root/nginx_start
 
echo '/usr/local/nginx/sbin/nginx -t' > /root/nginx_reload
echo 'kill -HUP `cat /usr/local/nginx/logs/nginx.pid`' >> /root/nginx_reload
 
chmod 700 /root/nginx_*