1

I have the development machine set up to use tmpfs for /var/log. Given that, at every boot the content is blank and gradually filled with whatever processes create while logging their guts.

The problem is (according to my interpretation of error messages): nginx is very reluctant to just create it's own log directory (/var/log/nginx in this case), so during start it keeps throwing the error at me:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
2012/05/08 21:42:35 [emerg] 2368#0: open() "/var/log/nginx/error.log" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed

As soon as I create the dir everything works just fine. Is there a way to tell nginx to (at least attempt to) create the log dir?

topp
  • 150
  • 1
  • 8

2 Answers2

3

As a first line of the do_start function in the script /etc/init.d/nginx, add the following command:

# Create the folder "nginx" in /var/log otherwise nginx can't start
mkdir -p /var/log/nginx

I don't like this solution but that's the easiest/quickest I found too.

Regards

EDIT - If using Debian, the init.d script is a bit different. The "mkdir" line goes just after the line "start":

start)
    mkdir -p /var/log/nginx
VinceOPS
  • 2,602
  • 18
  • 21
  • 2
    If you use systemd on Debian, you have to add it into the `nginx.service` file (which you can find by running `systemctl show nginx.service | grep FragmentPath`). You have to add `ExecStartPre=/bin/mkdir -p /var/log/nginx` to it before the `ExecStartPre=/usr/bin/nginx -t ...` line (because this tests the configuration before starting, and it will fail if it doesn't find the directory.) Your system uses systemd if you get `Starting nginx (via systemctl) ...` when running /etc/init.d/nginx. – matega Apr 30 '17 at 06:37
  • `sudo sed -i '/^ExecStartPre.*/i ExecStartPre=/bin/mkdir -p /var/log/nginx' $(systemctl show nginx.service | grep FragmentPath | cut -d'=' -f2)` a short command to do what @matega told. – boredfromboredom Jan 17 '21 at 12:23
0

I realize this thread is older than dirt on an asteroid, but I wanted to include this answer here to provide for future generations of intrepid searchers so they could avoid the hours of toil I just put myself through.

If you get the messages:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
2012/05/08 21:42:35 [emerg] 2368#0: open() "/var/log/nginx/error.log" failed (2: No such file or directory)
nginx: configuration file /etc/nginx/nginx.conf test failed

Be sure your var/log folders are not set to be tmpfs!!!! I had set mine that way to reduce writes to the SD card in my system, and that means every time nginx went to start, it couldn't find the logs or the nginx folder at /var/log/nginx - and so it fails and exits. If you see /var/log entered in /etc/fstab, comment it out, restart, manually create the nginx folder ONCE (I also manually created the error.log and access.log (blank files)) and restart nginx service. Your nginx server will run fine from then on (unless you do something else to break it, but that's for another thread).

David Xanatos
  • 75
  • 1
  • 9