Luca
Luca

Reputation: 41

Can't execute python script from php

Solved

Before writing a new question i search for a solution all over the web. I have a raspberry pi with apache2, php5.4,ssl. I want to execute a python script from php one. The php script is inside /var/www, which has 777 permission. Php file:

shell_exec('python /home/pi/Desktop/Python/prova.py');

Prova.py has 750 permission but his group owner is www-data, which is the user printed by shell_exec('whoami'); which works. Prova.py:

print "Hello World"

The script works directly from the command line:

php filename.php

It does not work from broswer!

Finally i succeed in execute the script from the browser. I had to add www-data user to sudoers file with its related permission:

www-data ALL=(ALL) NOPASSWD: /etc/bin/python 

Upvotes: 4

Views: 9495

Answers (2)

TheExpertNoob
TheExpertNoob

Reputation: 1

I had an issue similar while trying to build a web interface to change the image on an inky pHAT. Turns out www-data doesn't have access to GPIO.OUT. Tested by running "sudo -u www-data php index.php" and it returned the error I was looking for. So I give www-data GPIO permission "usermod -a -G gpio www-data" instead of adding to suddoers.

Upvotes: -2

Tim Bodeit
Tim Bodeit

Reputation: 9693

Your problem is this: When launching the php script through the console, you are launching php with the permissions of your current user. When a process is executed through opening it in the browser, it will have the permissions of the user of your web-server-process.

Your group permissions will have to be 7 no matter what. Either change the python scripts permissions to 777 (read-write-execute for everybody) or change it to 770 and make sure that your web-servers user is inside the group set to the file.

Upvotes: 4

Related Questions