Question

I installed nginx as root, now I can't get permissions right to run as another user

Posted August 10, 2016 63.1k views
NginxUbuntu

As the title says, I have a server where nginx was originally installed by the root user. I now want to run nginx as a non-root user, a new user that I added to both the sudo the www-data groups.

When I try to run nginx from this new user, I get the following message:

nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)
2016/08/09 22:07:52 [warn] 17609#0: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1
2016/08/09 22:07:52 [emerg] 17609#0: open() "/var/log/nginx/access.log" failed (13: Permission denied)

I’m confused why this is an issue—I changed the owner and group of /var/log/nginx/ to www-data. The nginx master process user is root, which I believe is correct, and the sub-processes are running (or trying to run) as www-data. The file permissions for error.log and ‘access.log` are 755. What am I missing?

2 comments
  • Hi! Can you post the output of the following command?

    sudo ls -lah /var/log/nginx
    
  • Thanks for the suggestion. Here’s what I see after running that command:

    drwxrw-rw- 2 www-data www-data 4.0K Aug 8 12:15 .
    drwxrwxr-x 11 root syslog 4.0K Aug 10 06:31 ..
    -rwxrw-rw- 1 www-data www-data 7.6K Aug 10 12:55 access.log
    -rwxrw-rw- 1 www-data www-data 3.9K Aug 9 22:01 error.log

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
8 answers

I received some advice that solved my problem and seems to be sound counsel. I’m now starting the server from the non-root account using sudo and all is well. One little trick!

  • What did you do to solve this issue? I have the same problem.
    marcelo@ubuntu-s-1vcpu-1gb-nyc3-01:~$ nginx -t
    nginx: [alert] could not open error log file: open() “/var/log/nginx/error.log” failed (13: Permission denied)
    2018/01/27 17:58:44 [warn] 2078#2078: the “user” directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:2

I ran into this exact problem, which had me stumped for a while. The problem is that nginx needs to execute as root and let the user directive in the /etc/nginx/nginx.conf handle running everything as the correct user. For me, I had the sytemd job defining the user to run nginx as.

  • After wasting two days for solving this problem on my test server.. Your answer helped me to resolve it finally. Just after reading your answer; I remembered I had changed user on nginx.conf file from root to nginx after installation. Before finding your answer chganging file permissions to 777 and also completely disabling selinux could not solve problem and I was getting crazy!!

Try setting the permissions for /var/log/nginx/ to 766.

sudo chmod -R 766 /var/log/nginx/

Thanks for the suggestion! I tried it out with no luck. I can’t even cd into the /var/log/nginx/ directory without sudo, an issue I’ve never had before. Permissions are definitely set to 766, though:

drwxrw-rw- 2 www-data www-data 4096 Aug 8 12:15 nginx

I got it fixed by running this command:
sudo /etc/init.d/nginx start

:)

Your Nginx web-user isnt permitted to access the logs. Modify the permissions with:

sudo chown -R www-data:www-data /var/log/nginx;
sudo chmod -R 755 /var/log/nginx;
Submit an Answer