Take the 2-minute tour ×

I have a Python script that is executed through PHP but the script runs under the user ubuntu. How do I change my Python script to run as the same user as my PHP script which is www-data?

If this is not a safe way to do this, what would be?

The Python script performs tasks on files that are uploaded via PHP and PHP automatically sets the owner and group to www-data. I execute the python script from the command line and it works but when executed through PHP the Python script doesnt have permission to execute its task on the uploaded files.

share|improve this question
add comment

migrated from stackoverflow.com Oct 9 '11 at 23:41

This question came from our site for professional and enthusiast programmers.

2 Answers

A possible solution would be to add the ubuntu user to the www-data group:

sudo usermod -a -G www-data ubuntu

This will work if the files have group write permission. Otherwise you'll have to detail how you are executing your python script to determine why it is running as the ubuntu user.

share|improve this answer
add comment

Shell access is disabled for www-data. You could another user add add it to the same group as www-data: sudo useradd -G www-data {new_username} -s /bin/bash

Now change the password of the new_username to something you want: sudo passwd new_username

Change permissions on the directory you want to execute your script.

cd /project/path
sudo chown -R www-data:www-data project
cd project
find . -type f | xargs sudo chmod 660
find . -type d  | xargs sudo chmod 770

The last two lines enable write access only to users in the group www-data. Then you can execute your script in the directory as new_username: sudo new_username python myscript.py. Or simply su new_username - and then python myscript.py.

share|improve this answer
add comment

Your Answer

