Error 13: Permission Denied

Hey everyone,

I encountered an issue at the production level Whenever I am trying to update/add a model that contains media files it shows error 13: Permission denied

  • First I checked whether I have the read/write and update permission of the directory under the project and found that yes they do have the permission.

Screenshot 2024-04-16 185600

  • I checked out the settings.py file but didn’t get anything wrong.

settings.py

MEDIA_URL = "/media/"
MEDIA_ROOT = BASE_DIR / "media"
STATIC_URL = "/static/"
STATIC_FILE_ROOT = BASE_DIR / "static"
STATICFILES_DIRS = ((BASE_DIR / "static"),)
STATIC_ROOT = BASE_DIR / "assets"

urls.py

urlpatterns = (
    [
        path("admin/", admin.site.urls),
        path("", include("web.urls", namespace="web")),
        path("", include("order.urls", namespace="order")),
        path("main/", include("main.urls", namespace="main")),
        path("product/", include("products.urls", namespace="product")),
        path("accounts/", include("accounts.urls", namespace="accounts")),
        path("accounts/", include("registration.backends.simple.urls")),
        path(
            "sitemap.xml",
            TemplateView.as_view(template_name="sitemap.xml", content_type="text/xml"),
        ),
        path(
            "robots.txt",
            TemplateView.as_view(template_name="robots.txt", content_type="text/plain"),
        ),
        path(
            "OneSignalSDKWorker.js",
            TemplateView.as_view(
                template_name="OneSignalSDKWorker.js", content_type="text/javascript"
            ),
        ),
    ]
    + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
)

I know there are some issues with media files but I can’t figure it out.

It is working correctly in the local host

How are you running your project? What UID is the process running under?

Note: For checking permissions, you need to look through the complete directory tree.

Also, I always recommend that media and static files not be stored under “home”. Since they should be served primarily by the web server, the directories in which they are saved should be stored in a common location, such as /var/www for static files or perhaps /opt/media for media files.

Yes @KenWhitesell I knew that we should store the static and media files in /var/www and opt/media but actually this is my client project and someone else has hosted it on digital ocean with ubuntu and apache2 server

what information should I send you so you can detect the issue

Start with the questions I asked before:

Then you have the professional responsibility to identify the security implications of doing things wrong, and explaining how and why they’re putting their system at risk by doing it this way.

@KenWhitesell I am using Apache2 and ubuntu - 22.04.1 to run my project hosted on digital ocean

  • Python - 3.10.12
  • Django - 4.2.7
  • Postgresql: 14.11

The process is running under root , systemd+ , postgres , django , www-data , message+ , syslog , postfix , and sshd UID

Can you specify me for which process do you want the UID

The project UID is root

Do let me know if you need something else

Basically, I’m trying to understand what UID your python code is running as.

Are you running it under mod_wsgi? If so, then what is the UID running it?

Or, are you proxying the requests through to something like uwsgi or gunicorn? If that’s the case then I need to know the UID for those processes (either uwsgi or gunicorn, whichever is being used).

Ohh, So I am using mod_wsgi and the UID is www-data

So that means that every directory above the static and media directories need to be readable by www-data, and the media directory itself needs to be writable by www-data.

Thank you @KenWhitesell I have fixed it thank you a lot for your guidance and yeah I will alert the client about the potential security issues.

Man, you have a lot of experience.
Privileged that you are helping us on the Django Community

That’s the big reason I love Django the support system is very very strong due to people like you.:green_heart: