serve media files using apache and gunicorn

this is my apache configuration

<IfModule mod_ssl.c>
<VirtualHost *:443>
    DocumentRoot /var/www/django_projects/myproject

    Alias /media /var/www/django_projects/media
    <Directory /var/www/django_projects/media>
        Require all granted

    <Directory /var/www/django_projects/myproject>
           Require all granted

 ProxyPreserveHost On
 ProxyPass /
 ProxyPassReverse /

   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined

Include /etc/letsencrypt/options-ssl-apache.conf

SSLCertificateFile /etc/letsencrypt/live/
SSLCertificateKeyFile /etc/letsencrypt/live/


this is how i run the app using gunicorn

gunicorn myproject.wsgi:application --bind

Note: When posting code here, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted. (I’ve taken the liberty of modifying your original post for you.)

What are your MEDIA_ROOT and MEDIA_URL settings for your project?

oh sorry my bad, this my media in settings

#MEDIA_ROOT = '/var/www/django_projects/media'
MEDIA_URL = "/media/"

and also in my

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

What is your BASE_DIR?

You have:


Those two entries need to identify the same directories.

I see where you have this line commented out:

This is actually what you probably want active.

Also, your apache process needs to have read permissions to that directory. (Your gunicorn process needs to have read-write permissions to that same directory.)

Finally, this:

Is absolutely not needed when you’re using apache to serve media files. (Nor is that other line needed when you’re using apache to serve your static files.)

can you correct me which part has error in this -

Alias /media /var/www/django_projects/media
    <Directory /var/www/django_projects/media>
        Require all granted

this my base dir

 BASE_DIR = Path(__file__).resolve().parent.parent

this is my permissions in my project directory , i use the 750 chmod for media

rdpuser@srv489581:/var/www/django_projects$ ls -l
total 231224
-rwxr-xr-x 1 rdpuser rdpuser        515 Mar  8 03:19 Dockerfile
-rwxr-xr-x 1 rdpuser rdpuser         50 Mar  8 03:19 Procfile
-rwxr-xr-x 1 rdpuser rdpuser         79 Mar  8 03:19 commands.txt
-rwxr-xr-x 1 rdpuser rdpuser     188416 Mar  8 03:19 db.sqlite3
-rw-r--r-- 1 rdpuser rdpuser    3624845 May 22 21:12 django_errors.log
-rwxr-xr-x 1 rdpuser rdpuser        216 Mar  8 03:19 docker-compose.yml
-rwxr-xr-x 1 rdpuser rdpuser        665 Mar  8 03:19
-rw-r--r-- 1 rdpuser rdpuser  232895721 Mar 22 13:20
drwxr-x--- 3 rdpuser www-data      4096 Mar 19 08:16 media
drwxr-xr-x 6 rdpuser rdpuser       4096 Mar 16 16:36 myapp
drwxr-xr-x 3 rdpuser rdpuser       4096 Mar 11 14:12 myproject
-rwxr-xr-x 1 rdpuser rdpuser        439 Mar  8 03:19 requirements.txt
drwxr-xr-x 6 rdpuser rdpuser       4096 Mar 11 15:48 sta_ex
drwxr-xr-x 5 rdpuser rdpuser       4096 Mar 19 08:16 static
drwxrwxr-x 6 rdpuser rdpuser       4096 May 10 08:09 staticfiles
drwxr-xr-x 6 rdpuser rdpuser       4096 Mar 15 11:26 venv

Side note: The lines of ``` must be lines by themselves and not part of another line.


identifies how BASE_DIR is calculated, but does not show what BASE_DIR is for your project.

Also, in addition to looking at your media directory itself, you need to confirm that Apache has the necessary permissions to the files and the parent directories.

Now, I’m quite rusty with my Apache configuration directives, so I suggest you follow a resource like
How to setup Apache with Gunicorn | by Antonis Christofides | Django Deployment | Medium

for more specific details on the rest of the configuration.

Also, with using Apache as a proxy, there’s no need - and a lot of reasons - to not put your project itself into /var/www. As a proxy, Apache doesn’t directly access those files, and shouldn’t have visibility to them.

still I cannot fix it, if i use the apache with mod_wsgi i can access my media files througth this
https://mydomain/media, but if i use the gunicorn and apache i cannot