Static Files not loading when DEBUG is set to False

When I set DEBUG to false in production, it does not load staticfiles.
I am using apache with WSGI. In apache configuration I have added directive for it as well.

Alias /static/ /path/to/your/staticfiles/
<Directory /path/to/your/staticfiles>
Require all granted

Managing static files in production (DEBUG=False) is completely different from managing them in a development (DEBUG=True) environment.

See the docs at How to deploy static files | Django documentation | Django for the details of everything that needs to be done.

Note: One of the issues not emphasised in the docs is that you need to ensure that the process running Apache has read access to the contents of your STATIC_ROOT directory.

In debug mode, there is no problem because you add the media root to urls.py, but if not, the program you are deploying will fetch the media files.

You need to grant the program you are using permission to access the media folder.

If you are using nginx, the following article may be helpful.

Hello.
In urls patterns i have it in the end,

] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

There is not any permission error. I have given the permission www-data:www-data to whole folder.

Hello. I have followed steps in guide Deploy static files but I still cannot find the problem.

  1. I have given the permissions.
  2. In settings, the relevant lines are:
INSTALLED_APPS = [  
'django.contrib.staticfiles',
]
MIDDLEWARE = [
    'whitenoise.middleware.WhiteNoiseMiddleware',
]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

STATIC_URL =  '/static/'
STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]

STATIC_ROOT = BASE_DIR / 'staticfiles'

4.In urls patterns i have it in the end,
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

  1. I am using apache with WSGI. In apache configuration I have added directive for it as well.
Alias /static/ /path/to/your/staticfiles/
 <Directory /path/to/your/staticfiles>
Require all granted
 </Directory

I know staticfiles only works in debug mode. Try this:

# settings.py
if DEBUG:
  STATICFILES_FINDERS = {your static files}
else:
  STATICFILES_FINDERS = []
  STATIC_ROOT = {your static folder path}

Post the access and error log file entries for the requests to these files.

Also, please use the real directory names and file contents, along with showing the permissions you have granted and the complete set of directives for your Apache configuration.

Side note: When you post code, configuration data, error messages, or any other preformatted text here, please enclose that code (or text, etc) 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 fixing your post for you.)

<VirtualHost *:80>
   ServerAdmin webmaster@xyz.com
   DocumentRoot /var/www/html/Web_northwindengineering
   ServerName northwindengineering.com
   Redirect "/" "https://northwindengineering.com/"
   # Static files
   Alias /static /var/www/html/Web_northwindengineering/northwindapp/static
   <Directory /var/www/html/Web_northwindengineering/northwindapp/static>
       Require all granted
   </Directory>

   # Media files
   Alias /media /var/www/html/Web_northwindengineering/media
   <Directory /var/www/html/Web_northwindengineering/media>
       Options -Indexes
       Require all denied
  </Directory>

  # Configure WSGI Deamon process group
   WSGIDaemonProcess  northwindengineering.com python-home=/var/www/html/Web_no                                                                             rthwindengineering/myenv python-path=/var/www/html/Web_northwindengineering
   WSGIProcessGroup  northwindengineering.com
   WSGIScriptAlias / /var/www/html/Web_northwindengineering/northwindengineerin                                                                             g/wsgi.py

   # WSGI configuration
   <Directory /var/www/html/Web_northwindengineering/northwindengineering>
       <Files wsgi.py>
           AllowOverride None
           Require all granted
       </Files>
   </Directory>

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

RewriteEngine on
RewriteCond %{SERVER_NAME} =northwindengineering.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

# Block traffic via the IP address
<VirtualHost *:80>
   ServerName 129.151.207.180
   <Location />
       Order allow,deny
       Deny from all
   </Location>
</VirtualHost>

permissions to run python commands:

sudo chown -R ubuntu:ubuntu /var/www/html/Web_northwindengineering/

permissions for the server(apache)

sudo chown -R www-data:www-data /var/www/html/Web_northwindengineering/


error log

i tried it, the error log

STATIC_URL = '/static/'


if DEBUG:
    STATICFILES_FINDERS = [
        'django.contrib.staticfiles.finders.FileSystemFinder',
        'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    ]
else:
    STATICFILES_FINDERS = []
    STATIC_ROOT = BASE_DIR / 'staticfiles'  # Specify your static folder path here


STATIC_ROOT = BASE_DIR / 'staticfiles'

I am not seeing any requests for any urls starting with /static in the log files you’ve posted. You might want to verify that the html being generated in the page is correct.

(Also, please do not post images of these log files. They’re hard enough to read as it is. Copy/paste the log text into the body of your post, formatted like you do with code.)

it seems numpy package error.

# settings.py
if DEBUG:
  STATICFILES_FINDERS = {your static files}
  STATIC_ROOT = None
else:
  STATICFILES_FINDERS = []
  STATIC_ROOT = {your static folder path}