Running a Python script from PHP

I'm trying to run a Python script from PHP using the following command:

exec('/usr/bin/python2.7 /srv/http/assets/py/ arg1 arg2');

However, PHP simply doesn't produce any output. Error reporting is set to E_ALL and display_errors is on.

Here's what I've tried:

  • I used python2, /usr/bin/python2 and python2.7 instead of /usr/bin/python2.7
  • I also used a relative path instead of an absolute path which didn't change anything either.
  • I tried using the commands exec, shell_exec, system.

However, if I run

    if (exec('echo TEST') == 'TEST')
        echo 'exec works!';

it works perfectly fine while shutdown now doesn't do anything.

PHP has the permissions to access and execute the file.

EDIT: Thanks to Alejandro, I was able to fix the problem. If you have the same problem, don't forget that your webserver probably/hopefully doesn't run as root. Try logging in as your webserver's user or a user with similar permissions and try to run the commands yourself.

Tested on Ubuntu Server 10.04. I hope it helps you also on Arch Linux.



    $command = escapeshellcmd('/usr/custom/');
    $output = shell_exec($command);
    echo $output;


In Python file, verify this text in first line: (see shebang explain):

    #!/usr/bin/env python

Also Python file should have correct privileges (execution for user www-data / apache if PHP script runs in browser or through curl) and/or must be "executable". Also all commands in .py file must have correct privileges.

    chmod +x