1

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.

2 Answers 2

3

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.

0

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.

You must log in to answer this question.