Question

problem in serving static files in django webapp

The urls.py file:

from django.contrib import admin
from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
    path('admin/', admin.site.urls),
]+ static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

The settings.py file:

STATIC_URL = '/web/static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
STATIC_ROOT = '/home/domain.com/public_html/web/public/static'

I am using cyberpanel with openlightspeed server with contex:

context / {
  type                    appserver
  location                /home/domain.com/public_html/web
  binPath                 /usr/local/lsws/fcgi-bin/lswsgi
  appType                 wsgi
  startupFile             web/wsgi.py
  envType                 1
  env                     LS_PYTHONBIN=/home/domain.com/public_html/bin/pyhton
  env                     PYTHONHOME=/home/domain.com/public_html/
}

My web app is not serving static files I am getting:

404 Not Found

Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Bobby Iliev
Site Moderator
Site Moderator badge
August 21, 2023

Hi there,

Here’s a general approach you could try to diagnose and fix the issue:

  1. Check the Paths: Make sure that the paths in your settings.py are correct and accessible by the web server.

  2. Configure OpenLiteSpeed to Serve Static Files: You’ll need to add a context for serving static files in your OpenLiteSpeed configuration. This will tell OpenLiteSpeed where to find the static files and serve them directly without going through your Django app.

    You can add the following context to your OpenLiteSpeed configuration file or through the admin interface:

    context /web/static/ {
        type static
        location /home/domain.com/public_html/web/public/static
        allowBrowse 1
        rewrite  {
            enable 0
        }
        addDefaultCharset off
        expireInSeconds 604800
    }
    

    Make sure to adjust the paths as needed based on your directory structure.

  3. Collect Static Files: Run the collectstatic command to make sure all your static files are in the correct location:

    python manage.py collectstatic
    
  4. Check Permissions: Ensure that the permissions are set correctly on the static files directory so that OpenLiteSpeed can read them.

  5. Restart OpenLiteSpeed: Finally, you’ll need to restart OpenLiteSpeed to apply the changes:

    sudo /usr/local/lsws/bin/lswsctrl restart
    
  6. Verify URLs: Verify that the URLs to your static files in your HTML templates are being rendered with the correct path based on your STATIC_URL setting.

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel