1

I have changed nginx.conf to listen to 8080, not 80. But when I run it without sudo, I got the error could not open error log file: open() "/opt/local/var/log/nginx/error.log" failed (13: Permission denied). If I run it with sudo nginx nginx starts correctly.

Why do I need to sudo nginx when I don't bind it to 80 port? Do I always need to sudo nginx no matter what? I can't find a definite answer to that on macOS because all the articles I googled just starting nginx with sudo nginx without further explanation.

I suspect this is a macport related problem. Because when later I run redis-server(also installed by macport) without sudo and when I want to shutdown redis-server I get the same error, Permission denied for /opt/local/var/log/nginx

8712:M 31 Oct 2023 11:35:39.339 # User requested shutdown...
8712:M 31 Oct 2023 11:35:39.339 * Saving the final RDB snapshot before exiting.
8712:M 31 Oct 2023 11:35:39.340 # Failed opening the temp RDB file temp-8712.rdb (in server root dir /opt/local/var/log/nginx) for saving: Permission denied
8712:M 31 Oct 2023 11:35:39.340 # Error trying to save the DB, can't exit.
8712:M 31 Oct 2023 11:35:39.340 # Errors trying to shut down the server. Check the logs for more information.

Why does redis need to write log into /opt/local/var/log/nginx now? I feel this is a bug for macport and I have raised a ticket with macport.

But in the meantime how do I fix this problem? Do I change the owner of /opt/local/var/log/nginx to my login user (I don't remember that I need to change the folder owner to admin (no prompt for that after installation) or do I always start nginx and redis with sudo?

I think either way can work but I want to know the "proper" way.

2
  • Please don‘t crosspost to several sites. If you expect SO to be the better place, delete the question here.
    – nohillside
    Oct 31, 2023 at 7:12
  • @nohillside delete my question at SO and re-edit my question. Thanks for the advice. Oct 31, 2023 at 7:22

2 Answers 2

1

As the error message says: you can‘t write the log file as a normal user.

You already identified the ways to fix this: either run the processes via sudo or change ownership/access rights of the log directories. Not sure there is a right or wrong and other factors not mentioned in the question may play a role, but in general changing ownership/access rights has less risks than running via sudo.

2
  • I am curious why shutdown redis also needs to access the dir /opt/local/var/log/nginx. Oct 31, 2023 at 7:35
  • 1
    @Qiulang邱朗 because it wants to write to that location, according to the output you’ve included in the question.
    – nohillside
    Oct 31, 2023 at 7:43
0

I figured out why my redis needed to access the directory /opt/local/var/log/nginx. It was my silly mistake that I forgot that when redis shuts down it would write some log to the directory where it is started and I happened to start redis at /opt/local/var/log/nginx/ and now I can't shut it down. So I don't need to run redis with sudo normally.

But to run nginx without sudo in a customer port turns out to be too much trouble for the macport installed version because the ownership problem of /opt/local/var/log/nginx

I also tried homebrew version and it can work without sudo (after the installation brew will output "The default port has been set in /opt/homebrew/etc/nginx/nginx.conf to 8080 so that nginx can run without sudo.”)

So now I use homebrew version when I don't run nginx as a service and I don't want to run it with sudo.

You must log in to answer this question.

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