nginx启动文件报grep错和 useradd错的问题处理

场景

最近某客户有个要求,所有部署的不准运行在root下面,安装在/home/somebody下面(用户家目录),当对nginx进行普通用户运行的时候,会遇到各种各样的问题,比如文件路径不存在呀,权限不够呀等。

本文就是在修改nginx运行在家目录的时候遇到的最后一个问题,提示 useradd没有权限,以及运行的时候出现的异常现象。

$ ./nginx restart
ngx_http_fastdfs_set pid=5550
nginx: the configuration file /home/123/opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /home/123/opt/nginx/conf/nginx.conf test is successful
Stopping nginx:                                            [  OK  ]
grep: unrecognized option '--prefix=/home/123/opt/nginx'
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.
./nginx: line 34: /usr/sbin/useradd: Permission denied
Starting nginx: ngx_http_fastdfs_set pid=5582
                                                           [  OK  ]
touch: cannot touch `/home/123/opt/var/lock/subsys/nginx': No such file or directory

调查

  • 定位代码到如下函数
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
}

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
}
可以看出,make_dirs会完成两个作用,一个是判断编译的时候的--user,如果系统中没有这个账户,那么就新建这个账户;另外一个是从options中提取temp-path变量,并创建该目录。对于我们当前的需求是不需要的,账户已经创建好了,temp-path没有,所以可以在start函数中,直接注释掉对make_dirs的调用。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值