title: nginx.conf and nginx.pid users and permissions
body: <p>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. </p>
  
  <p>I'm noticing a [warn] and an [emerg] pop up every time I restart my server, which shows: </p>
  
  <pre><code>[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)
  </code></pre>
  
  <p>The top of my nginx.conf file reads: </p>
  
  <pre><code>user www-data;
  worker_processes auto;
  pid /run/nginx.pid;
  </code></pre>
  
  <p>Which to me, shows me a few things. </p>
  
  <ol>
  <li>I'm running NGINX with the user: www-data. </li>
  <li>The number of worker processes that are allowed is automatically adjusted. </li>
  <li>my PID file/information is being stored in /run/nginx.pid. </li>
  </ol>
  
  <p>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. </p>
  
  <pre><code>sudo ls -la /run/nginx.pid
  </code></pre>
  
  <p>reveals:</p>
  
  <pre><code>-rw-r--r-- 1 root root 5 Jun 18 05:34 /run/nginx.pid
  </code></pre>
  
  <p>Then trying: </p>
  
  <pre><code>ps -ef | grep nginx
  </code></pre>
  
  <p>produces:</p>
  
  <pre><code>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
  </code></pre>
  
  <p><em>scratches head</em></p>
  
  <p>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? </p>
  
  <p>Or more to the point, anybody have some suggestions on what to do about this [emerg] error I'm getting? </p>
  
  <p>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. </p>
  
  <p>My second thought is maybe there is somewhere else that I define my worker process initiation within NGINX.. </p>
  
  <p>Thanks. </p>
  
  <p><strong><em>EDIT</em></strong></p>
  
  <p>The contents of the <code>/etc/systemd/system/multi-user.target.wants/nginx.service</code> file are: </p>
  
  <pre><code>[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
  </code></pre>