Question

[Errno 13] Permission denied: '/var/www/html/sai/uploads/foods/prawn-malai_QprldrT.jpg'

Posted January 19, 2021 1.1k views
ApachePythonDjangoUbuntu 20.04

my error is :

[Errno 13] Permission denied: '/var/www/html/sai/uploads/foods/prawn-malai_QprldrT.jpg'
Request Method: POST
Request URL:    https://hotelodisha.com/admin/Food/recipe/add/
Django Version: 3.1.5
Exception Type: PermissionError
Exception Value:    
[Errno 13] Permission denied: '/var/www/html/sai/uploads/foods/prawn-malai_QprldrT.jpg'
Exception Location: /var/www/html/sai/venv/lib/python3.8/site-packages/django/core/files/storage.py, line 267, in _save
Python Executable:  /var/www/html/sai/venv/bin/python
Python Version: 3.8.5
Python Path:    
['/var/www/html/sai',
 '/usr/lib/python38.zip',
 '/usr/lib/python3.8',
 '/usr/lib/python3.8/lib-dynload',
 '/var/www/html/sai/venv/lib/python3.8/site-packages']
Server time:    Tue, 19 Jan 2021 12:45:05 +0000

I am use Apache2 server . Django project

file location of 000-default.conf file: /etc/apache2/sites-available

Alias /static /var/www/html/sai/static
    <Directory /var/www/html/sai/static>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>


    <Directory /var/www/html/sai/Web>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>



    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

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

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
RewriteEngine on
RewriteCond %{SERVER_NAME} =hotelodisha.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

<VirtualHost *:443>
   ServerName   hotelodisha.com:80
    ServerAlias  hotelodisha.com
    ServerAdmin  "[ MOD SNIP ]"
    #DocumentRoot /var/www/html

    Alias /static /var/www/html/sai/static
    <Directory /var/www/html/sai/static>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>


    <Directory /var/www/html/sai/Web>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>



    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

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

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
SSLCertificateFile /etc/letsencrypt/live/hotelodisha.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/hotelodisha.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

my settings.py

"""
Django settings for Web project.

Generated by 'django-admin startproject' using Django 3.1.5.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""
import os
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')
STATIC_DIRS = os.path.join(BASE_DIR, 'static')

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = this is edited

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'WebSetting',
    'HotelBook',
    'Partner',
    'Food',
    'Cart',
    'Customer',
    'mobile',
    'django.contrib.sitemaps'
]
SITE_ID = 1

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django_user_agents.middleware.UserAgentMiddleware',
    'compression_middleware.middleware.CompressionMiddleware',
]

ROOT_URLCONF = 'Web.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [TEMPLATE_DIR],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'Web.wsgi.application'

SESSION_ENGINE = "django.contrib.sessions.backends.cache"
SESSION_CACHE_ALIAS = "default"  # or  anything else
CACHES = {
    'default': {
        "BACKEND": "django.core.cache.backends.locmem.LocMemCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    }
}

# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
# STATICFILES_DIRS = [STATIC_DIRS, ]
MEDIA_URL = "/images/download/"
MEDIA_ROOT = BASE_DIR

# SMTP Configuration
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = '[ MOD SNIP ]'
EMAIL_HOST_PASSWORD = '[ MOD SNIP ]'


# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

I use database - sqlite3 .
i try to upload image file . then this error occur . please help me

edited by MattIPv4

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.

×
Submit an Answer
1 answer

Hi @srikantasethy,

This issue is usually caused by incorrect file permissions or ownerships on your server (rather than being a problem with the plugin itself). Basically, your app isn’t able to properly access its own folder, and as a result can’t put the files in there.

Permissions

Permissions can be set to 644 for files and 755 for folders. To do so, you can run two quick find commands.

To make all folders in your website path with 755 permissions run the following command

find /path/to/website -type d -exec chmod 755 {} \;

To make all files in your website root directory with 644 permissions, you can run

find /path/to/website -type f -exec chmod 644 {} \;

Please make sure to change /path/to/website with your real path.

Ownership

Ownership means which user and group are controlling the files. Usually, that’s www-data. So what you’ll need to do si

sudo chown -R www-data:www-data /path/to/website

Please make sure to change /path/to/website with your real path.

Once you do it, that’s it, you are good to go.

Regards,
KFSys