Strange issue with STATIC and MEDIA files in production

Hi guys,

I am new here so I hope I following the rules, and if I am not sorry in advance.
I have issue and I cant find anything about that.
In production when I set DEBUG to False I have strange error. I have 7-8 projects with same setup and all working good but this one. I am using Django 3.2.19 and python 3.9 on shared hosting with cpanel. Also I am using webpack and thumbnails. My settings setup looks like this:

STATIC_ROOT = '/home/user/public_html/static/'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    '/home/user/public_html/project/frontend/build',
    '/home/user/public_html/project/shop/static',
)

MEDIA_ROOT = '/home/user/public_html/media'
MEDIA_URL = '/media/'

As always before I set DEBUG to False I use collectstatic.
Firstly I thought it has something with my stage and because its a subdomain,
but after when I set project live, I had same problem.
In console I get this errors:

Refused to apply style from ‘://domain/static/css/app.css’ because its MIME type (‘text/html’) is not a supported stylesheet MIME type, and strict MIME checking is enabled.

Refused to execute script from ‘://domain/static/js/runtime.js’ because its MIME type (‘text/html’) is not executable, and strict MIME type checking is enabled.

GET ://domain/static/js/runtime.js net::ERR_ABORTED 404 (Not Found)

://domain/shop/300w 404 (Not Found)

and lot more.
Headers for example:
Request URL:
://domain/static/js/app.js
Request Method:
GET
Status Code:
404 Not Found

And I managed to get static files with whitenoise but still doesnt getting media files…
I drain all solutions and examples and I couldnt fix it.
And I am repeating, I have 7 or 8 projects live with same setup and all work fine.

What is the current issue that you’re trying to address here?

You mention at the beginning that you were having problems with your static files, but at the end of your post you say you’ve got your static files working but the media files are not. However, the only examples you’re showing of any errors are for your static files.

So please clarify what the issue is that you are requesting assistance.

Actually I dont want to use whitenoise, because my other projects work without it.
So errors that you see are without whitenoise. Also there is a more errors, and media files are not loaded also.
All this happens when I change DEBUG to False, but as I mention I did collectstatic and set routes as I did on every other project that I have on same type of hosting.
All files are actually on that rout where I have 404 not found, and just to mention, those files and folders have all permissions that they need.

And thank you for concern.

Ok, you’ve identified two specific files here: app.css and runtime.js. We’ll start with only those two. (Since media and static files are handled differently, I’d want to resolve them one at a time to avoid confusion.

Also, I understand you’re saying that you’ve done everything correctly.

But, it’s not working, which means I don’t assume anything is correct. Everything needs to be checked.

Please specify here the full directory name where those files should be retrieved by the web server.

Show what your server configuration is for referencing your static and media files.

Show that directory, along with the permissions on it and on the files within it.

Which web server are you using? Apache? Nginx? Look for the error logs for them to see what is being reported for those requests.

Both (stage and live) are shared hosting, so I am not sure how to find server configuration and can I.
Its Apache server, and what I have at the moment is htaccess file, dont know is this means something to you.
I will give you routes and setup for both projects on my hosting, where is projects with issue subdomain.

One with issue:
project location: home/goldyrsbrs/public_html/tartufisr_us

STATIC_ROOT='/home/goldyrsbrs/public_html/tartufisr-static'
STATIC_URL='/static/'
STATICFILES_DIR='/home/goldyrsbrs/public_html/tartufisr_us/frontend/build'
MANIFEST_FILE='/home/goldyrsbrs/public_html/tartufisr_us/frontend/build/manifest.json'
MEDIA_ROOT='/home/goldyrsbrs/public_html/tartufisr-media'
MEDIA_URL='/media/'

/home/goldyrsbrs/public_html/tartufisr-static folder have permisions 755
all folders inside have 755, and files have 644
and for /home/goldyrsbrs/public_html/tartufisr-media is same

htaccess:

# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION BEGIN
PassengerAppRoot "/home/goldyrsbrs/public_html/tartufisr_us"
PassengerBaseURI "/"
PassengerPython "/home/goldyrsbrs/virtualenv/public_html/tartufisr_us/3.9/bin/python"
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION END
# DO NOT REMOVE OR MODIFY. CLOUDLINUX ENV VARS CONFIGURATION BEGIN
<IfModule Litespeed>
</IfModule>
# DO NOT REMOVE OR MODIFY. CLOUDLINUX ENV VARS CONFIGURATION END

when is debug True, css for example is on route:
://sobdomain.tld/static/css/app.5b6a84e0f103b9cd2229.css
but when I set debug to False its not there anymore
witch is not case with next domain.

main domain that works:
project location: home/goldyrsbrs/public_html/goldy_site

STATIC_ROOT = '/home/goldyrsbrs/public_html/static'
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
    os.path.join(BASE_DIR, 'main/static'),
    )
STATIC_ROOT = '/home/goldyrsbrs/public_html/static'

MEDIA_ROOT = '/home/goldyrsbrs/public_html/media'
MEDIA_URL = '/media/'

htaccess:

# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION BEGIN
PassengerAppRoot "/home/goldyrsbrs/public_html/goldy_site"
PassengerBaseURI "/"
PassengerPython "/home/goldyrsbrs/virtualenv/public_html/goldy_site/3.8/bin/python"
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION END
# DO NOT REMOVE OR MODIFY. CLOUDLINUX ENV VARS CONFIGURATION BEGIN
<IfModule Litespeed>
</IfModule>
# DO NOT REMOVE OR MODIFY. CLOUDLINUX ENV VARS CONFIGURATION END

and also I have htaccess-sh.txt :

Options -Indexes

#<IfModule mod_fcgid.c>
#	AddHandler fcgid-script .php
#	FcgidWrapper /home/[CPANEL_USER_NAME]/php5.fcgi .php
#</IfModule>

#<IfModule mod_env.c>
#        SetEnv PHPRC /home/CPANEL_NAME/php.ini
#</IfModule>

And logs for both domains are stopped on 31.10.
dont know why

Some notes:

First, changing DEBUG from False to True changes a lot with how the staticfiles app handles static files. It’s expected and normal that there are drastic differences in behavior when you change it.

  • There is no setting STATICFILES_DIR. It’s STATICFILES_DIRS, and you must supply an interable as the setting.

  • If you can’t see the configuration, there’s the possibility that Apache is only configured to serve static files from ‘/home/goldyrsbrs/public_html/static’. If that’s the case, you may want to create project-level directories under it for your static files to keep your projects seperate. That means setting STATIC_ROOT = '/home/goldyrsbrs/public_html/static/tartufisr' and STATIC_URL = '/static/tartufisr/'
    (And do the same type of thing with the media directory.)

sorry about this, it is like that because I copied from env variables and I called like that because its only one DIR, in settings it still STATICFILES_DIRS. So thats not the problem.

I did what you asked, I did seperate static folders in static like:
‘/home/goldyrsbrs/public_html/static/tartufisr’
and:
‘/home/goldyrsbrs/public_html/static/goldy’

Main domain still working fine, but subdomain doesnt work even on DEBUG set to True.
In DEBUG=True I can see media files, but there is no statatic files, also in DEBUG=False there is nothing.

I forgot to mention important thing.
During development I forgot to set MEDIA_ROOT and MEDIA_URL
And everything was working just fine, on stage and live. Even I set DEBUG to False
But when I noticed that I added those setups, and then everything broke.
First on stage, then when I pulled changes on live it was same. And after that I reverted changes to commit before this issue but I still had issue.
I dont know does this have something with webpack (but other projects work), or django by itself, or server… Its really frustrating.

Finally I found the issue.
Apparently this shared hosting set config for serving files only for main domain. (or first one that you set for python application) So any other domain does not serve files.
Since stage was subdomain that was reason why it didnt work,
and on live this was second domain. After I set it to main domain, and deleted others everything was fine.

5 days of agony and frustration, good thing is that I joined this community :slight_smile:

Thank you once again on your guidance!