Keith
Keith

Reputation: 1917

How do I configure nginx to put its error.log file somewhere I have write privileges?

I assume I simply have to insert an entry into an nginx.conf file to resolve the error that is plaguing me (see below), but so far I haven’t had any luck figuring out the syntax. Any help would be appreciated.

I want to run nginx as a regular user while having installed it using homebrew as a user with administrative privileges. nginx is trying to write to the error.log file at /usr/local/var/log/nginx/error.log, which it cannot because my regular user lacks write privilege there.

Another wrinkle is coming from the fact that there are two nginx.conf files, a global and a local, and as far as I can tell they are both being read. They are in the default homebrew location /usr/local/etc/nginx/nginx.conf and my local project directory $BASE_DIR/nginx.conf.

Here is the error that is generated as nginx attempts to start up:

[WARN] No ENV file found
10:08:18 PM web.1 |  DOCUMENT_ROOT changed to 'public/'
10:08:18 PM web.1 |  Using Nginx server-level configuration include 'nginx.conf'
10:08:18 PM web.1 |  4 processes at 128MB memory limit.
10:08:18 PM web.1 |  Starting php-fpm...
10:08:20 PM web.1 |  Starting nginx...
10:08:20 PM web.1 |  Application ready for connections on port 5000.
10:08:20 PM web.1 |  nginx: [alert] could not open error log file: open() "/usr/local/var/log/nginx/error.log" failed (13: Permission denied)
10:08:20 PM web.1 |  2017/03/04 22:08:20 [emerg] 19557#0: "http" directive is duplicate in /usr/local/etc/nginx/nginx.conf:17
10:08:20 PM web.1 |  Process exited unexpectedly: nginx
10:08:20 PM web.1 |  Going down, terminating child processes...
[DONE] Killing all processes with signal  null
10:08:20 PM web.1 Exited with exit code 1

Any help figuring out how to get nginx up and running so I can back to the development side of this project will be much appreciated.

Upvotes: 5

Views: 13926

Answers (2)

Keith
Keith

Reputation: 1917

The whole problem stems from trying to run nginx as my ordinary user self despite the fact that nginx was installed by my user self with administrative privileges. I was able to resolve both the errors shown here with the following commands executed as a user with administrative privileges:

sudo chmod a+w /usr/local/var/log/nginx/*.log
sudo chmod a+w /usr/local/var
sudo chmod a+w /usr/local/var/run

Note that the /usr/local/var directory appears to have been created by homebrew upon installing nginx and this machine is my laptop so I can’t see any reason not to open it up. You might have greater security concerns in other scenarios.

I admit that when I wrote this question I thought it was about moving the error.log file to another directory. Now I see that that is not a full solution, so instead the solution I present here is about giving ordinary users write privileges in the necessary directories.

The reason I changed my mind is that nginx can (and in this case does) generate errors before (or while) reading the nginx.conf files and needs to be able to report those errors to a log file. Modifying the nginx.conf file was never going to solve my problem. What woke me up to this issue was reading this post: How to turn off or specify the nginx error log location?

Upvotes: 15

Farhad Farahi
Farhad Farahi

Reputation: 39527

Did you try to set the log path to a custom location by editing nginx.conf?

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

Change path to somewhere use has write privileges.

Upvotes: 2

Related Questions