4

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 
1
  • 1
    Prova.py != prova.py, check what's really the name of your python script. Do you get an error message (check your server logs)? Also, www-data needs to have the permission to access /home/pi/Desktop/Python/ Commented Sep 8, 2013 at 23:36

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried to move prova.py in a subdirectory of var/www and now...fortunately it works!
I tried to complicate python script in order to play a sound. So I imported time and pygame libraries, the problem has returned. It works from command line but no via browser
What permissions are currently set on the file?
-2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.