I have defined a custom error_log for a virtual server inside it's directory like so:
server {
listen 80;
server_name www.example.com;
root /home/www.example.com;
error_log /home/www.example.com/error.log;
}
this is my nginx.conf:
user nginx;
pid /run/nginx.pid;
worker_processes 1;
Nginx starts as root with it's commands with no problems:
nginx -t
nginx
But when I try to start it using systemctl it won't start and status shows this:
nginx: [emerg] open() "/home/www.example.com/error.log" failed (13: Permission denied)
This is my nginx.service :
[Unit]
Description=The nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
I have almost tried anything like changing the user:group of the web directory to nginx:nginx. Even if I remove the error_log it starts using systemctl but it can't access the web directory although it's running under the same user.
I can't figure out why nginx can't access that directory when it's started with systemctl while it has access when it's started directly.
I'm on centos 7.