title: Can't create text file or append to it in NGINX without CHMOD 777
body: <p>Using Lightsail NGINX instance and got a one line in a PHP file that won't do what I need</p>
  
  <pre><code>$s = 'Hello world';
  $myfile = file_put_contents('log.txt', $s.PHP_EOL , FILE_APPEND | LOCK_EX);
  </code></pre>
  
  <p>I want it to create the log.txt file if it's not there. If it is there, I want it to append. It does not do either.</p>
  
  <p>If I create the file, it doesn't work. Only if I chmod the file to 777 via SSH will it work.</p>
  
  <p>However a future piece of work would be to make the file so for reasons beyond the security of 777, this manual approach won't be feasible.</p>
  
  <p>I understand this is down to security settings but really unsure on what to do to make this as secure as I can whilst still giving the PHP access to create/append to a text file</p>
  
  <p>It appears I have a user called www-data. I have tried:</p>
  
  <pre><code>sudo chown -R www-data:www-data html
  sudo chmod -R g+s html
  </code></pre>
  
  <p>Still can't create the file.</p>
  
  <p>If I put error reporting on I get:</p>
  
  <pre><code>file_put_contents(log.txt): failed to open stream: Permission denied
  </code></pre>
  
  <p>Thanks</p>