title: Django [Errno 13] Permission denied: '/var/www/media/animals/user_uploads'
body: <p>I am developing a django API which will be running on top of Apache2 via WSGI on a server running Ubuntu. </p>
  
  <p>Users will be able to upload pictures they take to the server using a POST request. The API processes this request and then attempts to write the image to <code>/var/www/media/animals/user_uploads/&lt;animal_type&gt;/&lt;picture_name&gt;.jpg</code>. In case there is no directory <code>/var/www/media/animals/user_uploads/&lt;animal_type&gt;/</code> it will create it. </p>
  
  <p>When testing during development everything was fine, both using Windows and Scientific Linux. When testing on the deployment server, I receive this error:</p>
  
  <p><img src="https://i.stack.imgur.com/eh4kN.png" alt="Django Error"></p>
  
  <p>From what I understand, the Apache2 server is running using the user <code>www-data</code>. In my case, running <code>cat /etc/passwd</code> to get the list of users, this is what I get for <code>www-data</code>:</p>
  
  <blockquote>
    <p>www-data:x:33:33:www-data:/var/www:/bin/sh</p>
  </blockquote>
  
  <p>I am assuming this means that <code>www-data</code> has access to everything in <code>/var/www/</code>. I have tried: </p>
  
  <blockquote>
    <p>chmod 777 -R media</p>
  </blockquote>
  
  <p><strong>This worked but it is obviously a very bad way to solve this. Is there a better way to solve this?</strong></p>
  
  <p>This is my wsgi.py:</p>
  
  <pre><code>import os, sys
  os.environ.setdefault("DJANGO_SETTINGS_MODULE", "serengeti.settings")
  sys.path.append('/serengeti/django/serengeti')
  sys.path.append('/serengeti/django')
  
  from django.core.wsgi import get_wsgi_application
  application = get_wsgi_application()
  </code></pre>
  
  <p>I have this in my <code>settings.py</code> file:</p>
  
  <pre><code>MEDIA_ROOT = '/var/www/media/'
  MEDIA_URL = os.path.join(BASE_DIR,'/media/')
  </code></pre>
  
  <p>My <code>vhost.conf</code> contains this:</p>
  
  <pre><code>Alias /media/ /var/www/media/
  </code></pre>