Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python makedirs permission denied [closed]

I have an apache httpd server running python code using django framework and mod_wsgi. I my view.py i need to make a subprocess call to execute another python file which in the process needs to create some directories.

However, I am getting OSERROR 13: Permission denied no matter where I try to create the directory.

Only creating a directory in /tmp is successfull.

Can anyone guide me on how to fix this problem?

Thanks in advance!!!

EDIT: I found the answer in serverfault.com. Here it is for reference apache2 runs under root, but it forks processes which run under 'www-data' group. So create a directory where you want to create files/directories

sudo mkdir /srv/www/writable

Change the group

sudo chgrp www-data /srv/www/writable

Grant the group write access

sudo chmod g+w /srv/www/writable

And you are done. Now you can create any files/directories in this directory using a script run by apache2

like image 640
Amm Sokun Avatar asked Jun 20 '26 08:06

Amm Sokun


2 Answers

Might be worth adding that this can also be achieved in Python

import os
import pwd

path = '/var/www/myapp/foo/'
if not os.path.exists(path):
        os.makedirs(path) # creates with default perms 0777
        uid, gid =  pwd.getpwnam('root').pw_uid, pwd.getpwnam('www-data').pw_uid
        os.chown(path, uid, gid) # set user:group as root:www-data
like image 196
igniteflow Avatar answered Jun 22 '26 22:06

igniteflow


I found the answer in serverfault.com. Here it is for reference

apache2 runs under root, but it forks processes which run under 'www-data' group. So create a directory where you want to create files/directories

sudo mkdir /srv/www/writable Change the group

sudo chgrp www-data /srv/www/writable Grant the group write access

sudo chmod g+w /srv/www/writable And you are done. Now you can create any files/directories in this directory using a script run by apache2

like image 41
Amm Sokun Avatar answered Jun 22 '26 22:06

Amm Sokun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!