0

I've set up a new server on Debian Stretch.

The web server user is nginx:

ps aux | grep -E '[a]pache|[h]ttpd|[_]www|[w]ww-data|[n]ginx' | grep -v root | head -1 | cut -d\  -f1

nginx

I have already set the web server path to 775 and nginx recursively.

chown -R nginx:nginx /var/www/html/
chmod -R 775 /var/www/html 

When I create a file, the user is root with the following permissions:

<?php file_put_contents ('/var/www/html/settings/test.json', 'Test file'); ?>
php test.php

-rw-r--r-- 1 root root 9 Apr 29 09:19 /var/www/html/settings/test.json

What else can I try?

Asa Carter
  • 249
  • 1
  • 3
  • 15

1 Answers1

2

If you run PHP from the command line when you are logged in as root, then the PHP binary will execute as root. This means the files it creates are owned by root.

To properly test your setup, you need to execute the PHP script by calling it via http / nginx. For example:

curl http://example.com/test.php
Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
  • You're right, creating a file via http creates the file as nginx. It's causing me permission problems because the files are written to by both http and a backend process. How can I make the files work for both? – Asa Carter Apr 30 '19 at 08:20
  • You should run the backend process as the user as nginx is running as. – Tero Kilkanen May 01 '19 at 09:12
  • The backend process is run by Supervisor. I've just tried setting the user to ngnix and restarting but the process is still running as root. – Asa Carter May 01 '19 at 14:42
  • Actually, If I set `user=nginx` in supervisord.conf, the process will not start. – Asa Carter May 01 '19 at 14:45
  • Then you need to find out why the process does not start as another user and fix that issue. – Tero Kilkanen May 01 '19 at 15:46