skip to Main Content

I am not able to upload my media files on Cpanel. Initially I was able to upload files but now it shows Error 404 URL Not Found.

There is nothing wrong with my code or my url as it works fine on localhost.

I have checked for permissions of directory in my CPanel File Manager (its 0755).

I have specified + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) in my urls.py file.

My settings.py is:

MEDIA_ROOT = '/my/path/public_html/media'
MEDIA_URL = '/media/'

I am using Django=2.1 and CPanel Shared Hosting

I know its recommended to have a web server to store and serve media files in Production Environment but it would be helpful if I get a solution of this error.

3

Answers


  1. add this code to passenger_wsgi.py file and change project_name at line 4:

    import os
    import sys
    sys.path.append(os.getcwd())
    os.environ['DJANGO_SETTINGS_MODULE'] = 'project_name.settings'
    import django.core.handlers.wsgi
    from django.core.wsgi import get_wsgi_application
    SCRIPT_NAME = os.getcwd()
    class PassengerPathInfoFix(object):
        def __init__(self, app):
            self.app = app
        def __call__(self, environ, start_response):
            from urllib.parse import unquote
            environ['SCRIPT_NAME'] = SCRIPT_NAME
            request_uri = unquote(environ['REQUEST_URI'])
            script_name = unquote(environ.get('SCRIPT_NAME', ''))
            offset = request_uri.startswith(script_name) and len(environ['SCRIPT_NAME']) or 0
            environ['PATH_INFO'] = request_uri[offset:].split('?', 1)[0]
            return self.app(environ, start_response)
    application = get_wsgi_application()
    application = PassengerPathInfoFix(application)
    

    for more: go to here

    Login or Signup to reply.
  2. try adding slash at the end of your path

    MEDIA_ROOT = ‘/my/path/public_html/media/’

    Login or Signup to reply.
  3. Edit your settings.py and add the following lines at the end:

    STATIC_URL = '/static/'
    STATIC_ROOT = '/home/username/public_html/static'
    

    Replace username with your cPanel username. The STATIC_ROOT basically points to a folder named static inside your domain root directory. You will use public_html only if you are working on your main domain.

    Now, if you are working on a subdomain, you will use the folder name instead of public_html as shown.

    STATIC_URL = '/static/'
    STATIC_ROOT = '/home/username/test.mydomain.com/static'
    

    Now open the terminal in cpanel, activate the virtual environment and execute the following command:

    python manage.py collectstatic
    

    The static files should be copied to the target folder.
    Add:

    MEDIA_URL = '/media/'
    
    MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
    

    Go to setup python app in cPanel and restart the python app.
    You can see more here link

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search