12

I deleted the /var/log/nginx/error.log file, and then created a new one using:

sudo nano error.log

Doing ls -la shows that the error.log and access.log have the same permissions.

When I try and start nginx I get the error:

alert: could not open error log file: open() "/var/log/nginx/error.log" failed permission denited.

Update

When trying to start nginx, I am also seeing:

emerg: /var/run/nginx.pid failed 13: permission denied.

1
  • Delete the Log and restart or reload Nginx. It will automatically create the Log. Nov 26, 2020 at 12:24

6 Answers 6

28

This doesn't solve your problem, but in the future, if you do

cat /dev/null > /file/you/want/to/wipe-out

you will copy over the contents of the file with nothing, and keep all permissions in tact.

Not nginx-speicific, but Additionally, make sure you are running the application as the user it is supposed to run as. If you ever ran it as root, all the permissions are going to be owned by root, so other users won't be able to run it.

3
  • 1
    How is it different from using echo "" > /file/to/empty? Are they both the same?
    – Gowtham
    Dec 18, 2016 at 9:18
  • 2
    That should be the same. Additionally, you can leave out all the stuff at the beginning and just do > /file/to/empty
    – Alex
    Dec 20, 2016 at 16:55
  • You can also do > /file/to/empty.
    – TCB13
    Sep 22, 2023 at 15:11
7

The usual way to rotate the logs with NginX is to rename the file, and then /etc/init.d/nginx reload. The server starts a new file, and you can do what you wish with the old.

You may also find it useful to edit the configuration to not log as much....

2

have a look in the nginx config, check who the server is running as - at a guess the user will be nginx

so :

chown nginx: /var/run/nginx.pid chown -R nginx: /var/log/nginx

Should do the trick.

4
  • but why was it working before?
    – Blankman
    Jun 1, 2010 at 1:40
  • 1
    I suspect because you tried to run it as root, and then tried starting it with its init script. Jun 1, 2010 at 3:21
  • well I always start it like that, hmm....
    – Blankman
    Jun 1, 2010 at 9:51
  • nginx.conf says: "user www-data"
    – Blankman
    Jun 1, 2010 at 9:54
2

To re-size the file to 0 bytes, without having to re-load or re-start nginx, use the truncate command:

sudo truncate --size 0 /var/log/nginx/access.log

Followed the guidance of this post due to my use case requiring the use of sudo.

0
1

Delete the error.log file and nginx will create it itself.

2
  • i deleted it, then when I try and start nginx is gives me the same permission denied error on the file /var/log/nginx/error.log
    – Blankman
    May 31, 2010 at 19:45
  • Delete the Log and restart Nginx. It will automatically create the Log.
    – WhiteHorse
    Mar 6, 2016 at 18:45
0

First try deleting the file, if that doesn't work check that the nobody user has the right permissions:
http://derekneely.com/2009/06/nginx-failed-13-permission-denied-while-reading-upstream/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .