Sometimes you may get “Errno 13 Permission Denied” error in Django while requesting a URL. This happens because the linux user used by Django does not have sufficient permissions to access the files & folders used by the request. In this article, we will look at how to fix this issue.
How to Fix Errno 13 Permission Denied Error in Django
Let us say you access your website’s home page and you get this error. This error occurs because the default user used by Django www-data, does not have ownership and/or read-write-execute permissions for the files & folders about your website, at /var/www. Here are the steps to fix it.
1. Check Linux User
Open terminal and run the following command to find out which user you are logged in as
$ sudo whoami ubuntu
2. Make a group for Django users
Run the following command to create a new group of users webusers who can access your website’s files & folders.
$ sudo groupadd webusers
3. Change group owner of www folder
The following command will change the group ownership of /var/www folder which generally hosts a website’s contents.
$ sudo chgrp -R webusers /var/www/
We use -R option above to recursively change the group ownership of all subfolders within /var/www/
4. Add www-data to webuser
Generally, Django and other web servers like NGINX, Apache user www-data linux user to run applications and access files & folders. So we will add this user to webusers group.
sudo adduser www-data webusers
5. Change Folder Permissions
Run the following command to change folder permissions of /var/www.
$ sudo chmod -R 770 /var/www/
6. Add ubuntu to webusers
Finally, we add ubuntu user (from step 1) to user to webusers. If you log into Linux as a different user, then add that user to webusers group. This is important because, very often, we log into Linux as a user other than www-data to create/edit files & folders. In such cases, the owner of these new files will not be www-data but ubuntu (user you logged in as). When Django tries accessing these files with user www-data, it gets an error since it is not the owner of these files. If we add ubuntu to our group, then that group’s ownership will ensure that Django is able to access your website’s files & folders without any problem.
$ usermod -a -G varwwwusers ubuntu
That’s it. As mentioned earlier, this error happens because of wrong file ownership or insufficient access permissions.
NGINX: How to Fix Upstream Sent Too Big Header Error
How to Get Data from URL in NodeJS
How to Install more_set_headers in NGINX
How to Setup Apache Virtual Host in Windows
How to Enable PHP Error Reporting