V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
colordog
V2EX  ›  NGINX

高手来瞅瞅, centos 下, nginx 安装后,修改日志路径,造成无法启动

  •  
  •   colordog · 2017-03-23 23:29:03 +08:00 · 10608 次点击
    这是一个创建于 2581 天前的主题,其中的信息可能已经有所发展或是发生改变。

    高手们给看看啊,这个是报错内容

    3 月 23 11:23:59 localhost.localdomain systemd[1]: Starting nginx - high performance web server...
    3 月 23 11:23:59 localhost.localdomain nginx[30550]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    3 月 23 11:23:59 localhost.localdomain nginx[30550]: nginx: [emerg] open() "/home/www/log/error.log" failed (13: Permission denied)
    3 月 23 11:23:59 localhost.localdomain nginx[30550]: nginx: configuration file /etc/nginx/nginx.conf test failed
    3 月 23 11:23:59 localhost.localdomain systemd[1]: nginx.service: control process exited, code=exited status=1
    3 月 23 11:23:59 localhost.localdomain systemd[1]: Failed to start nginx - high performance web server.
    3 月 23 11:23:59 localhost.localdomain systemd[1]: Unit nginx.service entered failed state.
    3 月 23 11:23:59 localhost.localdomain systemd[1]: nginx.service failed.

    下面是 nginx.conf 文件内容

    ================== 配置文件==================== user apache;
    worker_processes 1;

    error_log /home/www/log/error.log warn;
    pid /var/run/nginx.pid;

    events {
    worker_connections 1024;
    }

    http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    
    access_log  /home/www/log/access.log  main;
    
    sendfile        on;
    #tcp_nopush     on;
    
    keepalive_timeout  65;
    
    gzip  on;
    
    include /etc/nginx/conf.d/*.conf;
    

    }

    ======================配置文件=========================

    已知 /home/www/log 存在,并且属于 apche 用户名,用户组,为了测试权限,并且尝试给了 777 ,仍旧有问题

    这个问题感觉装系统有时候会发生,有时候又没事,很奇怪, apache 也是,其他软件没遇到,感觉好奇怪。

    23 条回复    2017-03-24 00:10:28 +08:00
    kmahyyg
        1
    kmahyyg  
       2017-03-23 23:32:22 +08:00 via Android
    换到 /var/log 下试试
    colordog
        2
    colordog  
    OP
       2017-03-23 23:34:00 +08:00
    @kmahyyg 那个默认目录没有问题,在阿里云 down 后,修改路径基本没问题,自己找个虚拟机,或者 pc 装的系统,十有八九都会出这个问题,而且安装方法都一样
    zsz
        3
    zsz  
       2017-03-23 23:34:20 +08:00
    一定是权限问题, 当前启动 nginx 的用户没有权限打开这个日志文件写入
    colordog
        4
    colordog  
    OP
       2017-03-23 23:36:23 +08:00
    @zsz 看报错应该是,这个 nginx 里面设置了 use apache 了,然后新建的 log 都改成这个用户和用户组了,而且专门设置了 777 属性,真不知道再怎么弄了
    kmahyyg
        5
    kmahyyg  
       2017-03-23 23:36:34 +08:00 via Android
    检查 nginx 到底在用哪一个用户,我的到目前为 5 台 vps ,从未遇到这个问题。改目录到一个大家都可以写的目录并 chown www:www 可以解决
    kmahyyg
        6
    kmahyyg  
       2017-03-23 23:37:40 +08:00 via Android
    另外,检查下你的 /etc/nginx/conf.d 下的配置有没有相关读写的东西
    colordog
        7
    colordog  
    OP
       2017-03-23 23:38:00 +08:00
    @zsz <br />
    [root@localhost nginx]# ps aux |grep nginx<br />
    root 30595 0.0 0.0 112664 972 pts/0 R+ 11:37 0:00 grep --color=auto nginx
    colordog
        8
    colordog  
    OP
       2017-03-23 23:39:30 +08:00
    @kmahyyg 看下上面的结果,我在阿里云的 8 台服务器也没这个问题, vultr 的 2 台也没有,只是从官方下载的,然后自己安装的,室友八九都有这个问题
    colordog
        9
    colordog  
    OP
       2017-03-23 23:43:48 +08:00
    -- Unit nginx.service has begun starting up.
    3 月 23 11:40:36 localhost.localdomain nginx[30609]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    3 月 23 11:40:36 localhost.localdomain nginx[30609]: nginx: [emerg] open() "/home/www/log/error.log" failed (13: Permission denied)
    3 月 23 11:40:36 localhost.localdomain nginx[30609]: nginx: configuration file /etc/nginx/nginx.conf test failed
    3 月 23 11:40:36 localhost.localdomain systemd[1]: nginx.service: control process exited, code=exited status=1
    3 月 23 11:40:36 localhost.localdomain systemd[1]: Failed to start nginx - high performance web server.
    -- Subject: Unit nginx.service has failed
    -- Defined-By: systemd
    -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
    --
    -- Unit nginx.service has failed.
    --
    -- The result is failed.
    3 月 23 11:40:36 localhost.localdomain systemd[1]: Unit nginx.service entered failed state.
    3 月 23 11:40:36 localhost.localdomain systemd[1]: nginx.service failed.
    3 月 23 11:40:36 localhost.localdomain polkitd[3159]: Unregistered Authentication Agent for unix-process:30603:203289 (system bus name :1.32, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale zh_CN.UTF-8) (disconnected from bus)
    zsz
        10
    zsz  
       2017-03-23 23:50:26 +08:00
    @colordog 尝试 sudo -u apache touch /home/www/log/test.log 看看能不能创建,如果不能,你这个 apache 用户对这个目录都不一定有权限,修改成正确的权限
    kmahyyg
        11
    kmahyyg  
       2017-03-23 23:51:18 +08:00 via Android
    @colordog 前面的这个只能说明你的 nginx 没有运行,具体的建议你除非是 nginx 作为 apache 代理前端,否则将 log 目录与网站根目录的权限设为同一 owner(or group),先 777 试试。
    colordog
        12
    colordog  
    OP
       2017-03-23 23:51:53 +08:00
    @zsz 可以创建,没有任何问题
    colordog
        13
    colordog  
    OP
       2017-03-23 23:52:23 +08:00
    @kmahyyg 777 已测试,无效
    Phant0m
        14
    Phant0m  
       2017-03-23 23:54:43 +08:00
    open() "/home/www/log/error.log" failed (13: Permission denied) 很明显是权限或者 selinux 的问题好吧
    关闭 selinux ( setenforce 0 )
    看看 apache 启动的用户是不是 apache
    zsz
        15
    zsz  
       2017-03-23 23:56:48 +08:00
    @colordog nginx 配置贴出来,你确认 nginx 使用的 apache 这个用户? 确认下 root 用户对这个目录有权限没
    kmahyyg
        16
    kmahyyg  
       2017-03-23 23:59:14 +08:00 via Android
    @colordog

    检查下你的系统有哪些用户 cat /etc/passwd ,检查 acl 配置

    同时,将 nginx.conf 用这个写下试试:
    (以下仅为示例)

    root@supply:/usr/local/nginx/conf# cat ./nginx.conf

    # user 你的用户组 你的用户名;
    user www www;
    worker_processes 1;

    error_log /data/wwwlogs/error_nginx.log crit;
    pid /var/run/nginx.pid;
    worker_rlimit_nofile 51200;


    ls -alh 列下 log 目录下的其他的 log 文件:

    drwxr-xr-x 4 root root 4.0K Dec 15 21:45 ..
    -rw-r--r-- 1 www root 0 Mar 23 06:25 access_nginx.log
    colordog
        17
    colordog  
    OP
       2017-03-24 00:00:57 +08:00
    @Phant0m 已经解决 关闭 selinux ( setenforce 0 ) 这个管用,能给讲讲不
    colordog
        18
    colordog  
    OP
       2017-03-24 00:01:40 +08:00
    @zsz 已经解决,不是配置文件问题, 14 楼的方法完美解决了,感谢
    colordog
        19
    colordog  
    OP
       2017-03-24 00:01:55 +08:00
    @kmahyyg 已经解决,不是配置文件问题, 14 楼的方法完美解决了,感谢
    kmahyyg
        20
    kmahyyg  
       2017-03-24 00:04:02 +08:00 via Android
    很明显 selinux 和 acl 策略出问题了,没必要的话不建议关闭 selinux.

    google 下 selinux 策略去找原因吧
    Phant0m
        21
    Phant0m  
       2017-03-24 00:07:47 +08:00
    @colordog setenforce 0 只是临时关闭,如果不会配置 selinux 那就在配置文件里 disable ,省的下次重启的时候又出问题
    colordog
        22
    colordog  
    OP
       2017-03-24 00:09:01 +08:00
    @kmahyyg
    @Phant0m
    @zsz 再次对大家表示感谢。
    yunshansimon
        23
    yunshansimon  
       2017-03-24 00:10:28 +08:00
    如果权限是 777 都不行,就肯定是 apparmor 的锅了。 apparmor 配置了相关服务程序的访问权限。具体配置文件在 /etc/apparmor.d 里面,具体如何配置,可以 man 来查,只要打开相应目录相应文件的写许可就好了。 apparmor 主要是防止非法程序冒充系统服务获取 root 权限后作恶的。主要的系统服务都有配置,里面具体写清楚了可读写的文件和端口。让客户可以放心让系统服务获取 root 权限。就算恶意程序冒充服务启动了,也最多访问一些 log 文件和服务端口。如果大家对别人的程序不放心,也可以为这个程序写 apparmor 配置,保证它不能访问你不许可的资源。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2765 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 15:39 · PVG 23:39 · LAX 08:39 · JFK 11:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.