12

I'm embarking on watching my NGINX error.log files at level: warn... probably a silly idea and will cause me to crash my server as I work out any bugs happening, but hey, we're nerds and this is why we're here.

I'm noticing a [warn] and an [emerg] pop up every time I restart my server, which shows:

[warn] 8041#0: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1
[emerg] 8041#0: open() "/run/nginx.pid" failed (13: Permission denied)

The top of my nginx.conf file reads:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

Which to me, shows me a few things.

  1. I'm running NGINX with the user: www-data.
  2. The number of worker processes that are allowed is automatically adjusted.
  3. my PID file/information is being stored in /run/nginx.pid.

The error tells me that NGINX doesn't have permission to access /run/nginx.pid, which led me to see the user permissions for said file.

sudo ls -la /run/nginx.pid

reveals:

-rw-r--r-- 1 root root 5 Jun 18 05:34 /run/nginx.pid

Then trying:

ps -ef | grep nginx

produces:

root      5914     1  0 05:34 ?        00:00:00 nginx: master process /u
www-data  5917  5914  0 05:34 ?        00:00:00 nginx: worker process

scratches head

Now, can somebody out there tell me why, or how the hell NGINX has managed to create the master process with root ownership, and now the worker processes are owned by www-data?

Or more to the point, anybody have some suggestions on what to do about this [emerg] error I'm getting?

My first thought is to just try and change the ownership of the /run/nginx.pid file and see how NGINX likes it, but I kind of feel that even if I do that manually this time, when I restart the server, I'll run into the same problem.

My second thought is maybe there is somewhere else that I define my worker process initiation within NGINX..

Thanks.

EDIT

The contents of the /etc/systemd/system/multi-user.target.wants/nginx.service file are:

[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=/usr/sbin/nginx -s quit

[Install]
WantedBy=multi-user.target
8
  • 1
    That is the correct behaviour of the user parameter (see this). The error also stems from from the fact that nginx is designed to run the master process as root. How are you starting nginx? systemd? init-v? Is the binary setuid by any chance?
    – grochmal
    Commented Jun 18, 2016 at 15:08
  • OK. So NGINX is supposed to run the master process as root. So everything is as it should be with regards to permissions as shown above. I still am befuddled why my [emerg] error pops up. My NGINX starts with the AWS server, that hardly ever down. However, if I do restart using sudo service nginx restart I see the above mentioned error. Can you show me how to find exactly how it's starting so I can answer your question @grochmal?
    – ntk4
    Commented Jun 19, 2016 at 2:26
  • Well, different OSes currently have different start procedures, although systemd is by far the most common. If you have the systemctl command you're using systemd, and if you have it the file that will explain to us how nginx starts shall be under /etc/systemd/system/multi-user.target.wants/nginx.service.
    – grochmal
    Commented Jun 20, 2016 at 1:28
  • Please see the EDIT I've added to the question above. I think this means that it's systemd, @grochmal, ? I also see that the nginx.pid declarations are matching in both my nginx.conf file and my /etc/systemd/system/multi-user.target.wants/nginx.service file, and that it's forking. So nothing alerts me yet, perhaps I'm not seeing it?
    – ntk4
    Commented Jun 21, 2016 at 2:32
  • That explains why the master process runs as root right? (systemd has no directive in ExecStart= to run as another user). Now, your worker process is running as ww-data therefore nginx is not ignoring user www-data;. Therefore the errors are baffling. In which log do you see these errors? Inside error_log as configured in nginx.conf?
    – grochmal
    Commented Jun 22, 2016 at 0:55

3 Answers 3

11

I got the same error on my Centos 7 server today.

nginx.pid" failed (13: Permission denied)

For me, it turned out to be a problem with SELinux. I did the following to make it work again:

systemctl stop nginx
touch /var/run/nginx.pid
chcon -u system_u -t httpd_var_run_t /var/run/nginx.pid
systemctl start nginx

running

ls -Z nginx.pid

should output

-rw-r--r--. root root system_u:object_r:httpd_var_run_t:s0 nginx.pid

4
  • 2
    what directory are you running that from? your third line references nginx.pid as relative, but apparently there's a /run/nginx.pid and a /var/run/nginx.pid. Commented Dec 16, 2018 at 12:23
  • +1 for this, it solved my issue on CentOS 7. The issue happened due a previous installation that was in a much lower version that I needed, which was installed 1.12 and I needed 1.17. Running the touch command without being in the folder the nginx.pid was not being created though, I had to go directly in the folder for it to take propper effect.
    – Banns
    Commented Jun 17, 2019 at 11:26
  • This worked on centos 8 with nginx 1.19 after running third command from directory /var/run/
    – Stephen
    Commented Oct 21, 2020 at 7:09
  • Worked for me too! Edited to add the path in the chcon. Commented Dec 3, 2020 at 21:16
1

In my case I got a

    "/usr/local/var/run/nginx.pid" failed (13: Permission denied)

    bind() to 0.0.0.0:80 failed (48: Address already in use)

and the working solution was made up of these steps:

  1. stop root process

    sudo nginx -s stop
    
  2. check if process stopped

    ps aux | grep nginx
    
  3. restart process

    sudo nginx -s reload
    

gave me the error

    nginx: [error] open() “/usr/local/var/run/nginx.pid” failed (2: No such file or directory)

probabil .pid was started with the wrong root user as I uncommented the line with path to .pid in /usr/local/etc/nginx/nginx.conf and then I commented it back again

  1. to start nginx as a user and not root

    brew services start nginx
    
  2. result at running command

    ps aux | grep nginx
    
    youruser 89212 0.0 0.0 4268280 644 s002  S+ 2:46PM 0:00.00 grep nginx
    youruser 89179 0.0 0.0 4302204 1776 ?? S 2:45PM 0:00.00 nginx: worker process  
    youruser 89178 0.0 0.0  4275372 4368 ?? S 2:45PM 0:00.01 nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;
    

And as it can be seen, the nginx process started with the expected user and not as root and the conflict between processes was gone and I could access the PHP application local domain.

1
For Ubuntu 20.04+

I got the exact same error while I was using sudo systemctl reload nginx

Using sudo service nginx restart instead throws no error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.