I am having an issue with executing my python script through using a button on PHP. I looked through any or all relevant questions regarding my issue, and tried any answers i could find and modifying the file name to my own. This is the PHP code on my website:

    <button type="button" onclick="parent.location='action.php'">open</button>

The PHP code will be executing the python script, which i wrote:

       exec("python /var/www/open.py")

The website works, the python script works when i manually execute it on terminal. However, when i try to click on the button on my website, nothing happens. I also went and changed the configs on the sudoers file so that www-data functions without the use of a password, and also did chmod gu+x on /var/www/open.py.

Is there anything else that I might be missing? Can someone help me please?

Thank you.

  • What do your logs say?
    – EEAA
    Mar 30, 2015 at 3:41
  • Which logs would you be talking about?
    – hachemon
    Mar 30, 2015 at 3:43
  • try echo exec("python /var/www/open.py"); Mar 30, 2015 at 12:00
  • alternatively you could use system instead of exec to avoid need for echo as seen in comment above Mar 30, 2015 at 12:06
  • I gave your suggestion a shot. The line system("python /var/www/open.py"); doesn't seem to function. Could there be an issue with how I have written <button type="button" onclick="parent.location='action.php'">open</button> please let me know
    – hachemon
    Mar 30, 2015 at 23:01

3 Answers 3


It could just be a PATH problem since the user www-data doesn't have a path set. Try using:

   exec("/usr/bin/python /var/www/open.py")

and see if that makes a difference (check that /usr/bin/python is the correct path for your distribution first with which python).


Could 2 paths to resolution:

  1. fetch http://my.site.com/action.php directly and confirm that code execures that way. In case of some PHP issues.

  2. SELinux: if your SELinux is set to "enforcing", try temporary disabling it just to confirm that it's not in the way: "setenforcing Permissive". If that cures the issue - you know you need to adjust things. First and foremost I doubt any default policy would allow executable scripts in /var/www dir, check your distro's default policy and locate appropriate folder. /var/www/cgi-bin may be one of them, or possibly system-wide directory like /usr/local/bin etc.


there are builtin php methods for this job : shell_exec() or system()


Not the answer you're looking for? Browse other questions tagged or ask your own question.