How Do I Manage Static and Media Files in Django Hosting? Introduction When hosting a Django application on shared Django hosting, properly managing static and media files is essential. Static files include CSS, JavaScript, and images used by your app, while media files are user-uploaded files like profile pictures or documents. In a shared hosting environment powered by CloudLinux Passenger, you can handle both without needing root access just using cPanel, your terminal, and a few Django settings. By the end of this guide, you’ll know how to configure your static and media directories, run collectstatic, and ensure your files load correctly from your domain. Understanding Static and Media Files in Django Static Files These are the files used to style and enhance your website, such as: CSS and JavaScript files Fonts Site images (e.g., logos, icons) Django uses the STATIC_URL and STATIC_ROOT settings to define where these files are stored and how they’re accessed. Media Files Media files are uploaded by users and stored separately.These include: Uploaded images (avatars, product photos) Documents or attachments They’re configured through MEDIA_URL and MEDIA_ROOT. Note: Always separate static and media files to avoid overwriting or exposure of sensitive content. Default Django Settings for Static and Media Files In your Django project’s settings.py, add or update the following: import os STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media') Note: Never place your media directory inside your static directory. Passenger will serve static files automatically if correctly configured in cPanel. Step-by-Step: Configure Static and Media Files in cPanel Log in to your cPanel account. Navigate to your domain’s cPanel dashboard. 2. Locate your Django app directory.Go to the folder where your Django project resides (e.g., /home/username/projectname/). 3. Create static and media folders.Inside your project directory, create two folders: /staticfiles/ /media/ Update Passenger settings.In your cPanel Setup Python App section, confirm your application root points to your Django project folder and that the passenger_wsgi.py file exists. 5.Save and restart the app.Click Restart App to reload the Passenger environment. 3. This gathers all static files into your STATIC_ROOT folder. 4. Verify file creation.Use File Manager to confirm that your static files are stored under /staticfiles/. Note: Run collectstatic after every change in your CSS or JS files to ensure updates are visible. Serving Static Files with CloudLinux Passenger Passenger automatically serves static content if it’s in your app directory. To confirm this: Check your .htaccess file under the project root. Add the following rule if missing: PassengerEnabled on Alias /static /home/username/projectname/staticfiles Alias /media /home/username/projectname/media Save changes and restart the app in cPanel. Note: Some shared servers restrict manual .htaccess edits. If static files still don’t load, contact support to verify that Passenger is configured to serve them. Managing Media Uploads via cPanel File Manager You can manage user-uploaded files without SSH access. Go to Files → File Manager. Navigate to your app’s /media/ folder. Upload files directly or view user uploads. Ensure correct permissions (755) for directories and 644 for files. Note: To prevent unauthorized access, avoid storing private user data in publicly accessible folders. Common Issues and Fixes Issue Cause Solution Static files not loading collectstatic not run or wrong path Check STATIC_ROOT path and rerun collectstatic. Media files inaccessible Permissions too strict Set proper read permissions (755/644). 404 error for static URLs Alias missing in .htaccess Add Passenger alias rules. Changes not reflecting Browser cache or old files Clear cache and restart app. Conclusion Managing static and media files in Django on shared hosting follows a clear and predictable process. By defining proper paths in settings.py, keeping static and media directories separate, and running collectstatic after changes, files can be served reliably through CloudLinux Passenger without root access. cPanel tools and optional SSH access provide enough control to upload files, verify permissions, and restart applications when needed. As long as directory paths, permissions, and Passenger settings are correct, static assets and user-uploaded media will load consistently in a shared hosting environment. Need Help? If you require assistance at any point while using this guide, our Support Team is here to help: Email: support@bigcloudy.com Submit a support ticket FAQ Do I need SSH to manage static files?No. You can use cPanel File Manager to upload or replace static and media files, though SSH access makes collectstatic faster. Why are my static files not updating?Run:# python manage.py collectstatic again after making changes, then restart your app. Where are my uploaded media files stored?They’re stored in your project’s /media/ directory as defined in MEDIA_ROOT. Can I use a CDN for Django static files?Yes. Configure STATIC_URL to point to your CDN endpoint once files are synced. How can I check which static directory Django is using?Run:# python manage.py findstatic <filename>