nginx更换启动脚本

之前使用的nginx启动脚本交互提示信息不全,使用起来不太方便。浏览nginx wiwk时看到一个启动脚本,想尝试一下。
脚本地址:http://wiki.nginx.org/RedHatNginxInitScript

配置好参数和启动脚本权限后启动nginx使用时报如下错误。

/etc/init.d/nginx start
grep:无法识别的选项“--prefix=/usr/local/nginx.base”
用法: grep [选项]... PATTERN [FILE]...
试用‘grep --help’来获得更多信息。
useradd:无法识别的选项“--prefix=/usr/local/nginx.base”

细看启动脚本,找到这段内容。

make_dirs() {
   # make required directories
   user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
   if [ -z "`grep $user /etc/passwd`" ]; then
       useradd -M -s /bin/nologin $user
   fi
   options=`$nginx -V 2>&1 | grep 'configure arguments:'`
   for opt in $options; do
       if [ `echo $opt | grep '.*-temp-path'` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}

脚本每次都会调用nginx -V,取得编译时用--user指定的用户名。如果指定的用户不存在,则会创建该用户。还会根据编译的参数创建临时目录,并且修改权限。没有考虑未使用--user指定用户的场景,我编译nginx时没有加--user参数,就报错了。

重新编译了nginx,添加--user参数,指定用户以后正常了。这个启动脚本考虑的情况较多,交互提示较之前的更为人性化,效果不错。如果应用场景不允许重新编译nginx,可以直接跳过 make_dirs() 这个函数,在 # make required directories 上面加入一行 return 1 即可。

附本人nginx编译参数,系统是CentOS 6.3。

./configure --prefix=/usr/local/nginx.base \
--user=www \
--group=www \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-mail --with-mail_ssl_module \
--http-client-body-temp-path=/usr/local/nginx.base/tmp/client \
--http-proxy-temp-path=/usr/local/nginx.base/tmp/proxy \
--http-fastcgi-temp-path=/usr/local/nginx.base/tmp/fastcgi \
--http-uwsgi-temp-path=/usr/local/nginx.base/tmp/uwsgi \
--http-scgi-temp-path=/usr/local/nginx.base/tmp/scgi