Linux add python script to run as user

command-line-interfacelinuxshellweb-server

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.

Best Answer

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.