How to serve media files for Django website

Operating System & Version
x86_64, linux, kernel version 3.10.0-962.3.2.lve1.5.63.el7.x86_64
cPanel & WHM Version
cPanel 94.0 (build 24)

Fabius

Member
May 11, 2022
9
3
3
UK
cPanel Access Level
Website Owner
I have a Django website set up on cPanel that is working fine - pages are served on my domain
Code:
www.example.org
Any images etc uploaded through Django's admin are saved to a directory I created at
Code:
~/example-org-media/
. That's good, but I'm not sure how to serve those images.

For example, an image at
Code:
~/example-org-media/hello.jpg
should be visible if I visit
Code:
www.example.org/media/hello.jpg
in a browser. At the moment it's a 404.

How do I serve those images as part of my domain, that's otherwise serving a Django/python app?
 

Fabius

Member
May 11, 2022
9
3
3
UK
cPanel Access Level
Website Owner
I suppose the files don't even have to be in that folder - I could put them anywhere. So the question is: Given the site is a python site served with passenger_wsgi.py, how do I also serve a folder full of images etc?
 

Fabius

Member
May 11, 2022
9
3
3
UK
cPanel Access Level
Website Owner
In the end I gave up on doing it that way, and instead set up a new Subdomain
Code:
media.example.org
for serving the media files, from a directory at
Code:
~/media.example.org/
. I then put this in my Django settings file:

Code:
MEDIA_ROOT = "/home/USERNAME/media.example.org/"

MEDIA_URL = "https://media.example.org/"
I added a .htaccess file in that directory containing this:

Code:
<IfModule mod_headers.c>  
    Header set Access-Control-Allow-Origin "https://www.example.org"
</IfModule>
 
  • Like
Reactions: Spirogg

cPRex

Jurassic Moderator
Staff member
Oct 19, 2014
10,360
1,627
363
cPanel Access Level
Root Administrator