title: What's the best user/permission setup on Linux for a folder PHP can write to?
body: <p>After years of using <code>chmod 777</code> to solve PHP write permission woes, I want to know the proper way of solving the problem.</p>
  
  <p>I have a website on my server owned by <code>user1</code> in group <code>user1</code>. There is a folder in this website called <code>uploads</code>.</p>
  
  <p>Normally to get writing with PHP to work, I have to <code>chmod</code> this folder to <code>777</code>. But I obviously recognise this is dangerous and incorrect, and I want to setup the permissions properly to minimise risk.</p>
  
  <p>From my limited knowledge, I see two options,</p>
  
  <ol>
  <li>I <code>chown</code> the <code>uploads</code> folder so that it's owned by <code>apache</code>. This way, I can just use the default permissions and <code>apache</code> can happily write to the folder.</li>
  <li>I add a second group to <code>apache</code> of <code>user1</code>. I then give write permission to the owner and group on <code>uploads</code>, which should allow <code>apache</code> to write to <code>uploads</code>?</li>
  </ol>
  
  <p><strong>My question is, what is the best approach?</strong> Is it one of the above or something completely different?</p>
  
  <p>If the best solution is #1, how can <code>user1</code> also write to uploads over SFTP as that solution will not let them?</p>