executing python in the command line inside a controller - PHP/CodeIgniter executing python in the command line inside a controller - PHP/CodeIgniter codeigniter codeigniter

executing python in the command line inside a controller - PHP/CodeIgniter


The problem is not with your code or PHP.

The problem is with your permissions.

php uses permissions which are set in the env-vars of apache.

Which is ideally set as :

User ${APACHE_RUN_USER}Group ${APACHE_RUN_GROUP}

under your apache2 / httpd conf file.

For example:

Try running:

<?= `whoami` ?>

via your shell and via your browser.

Your browser will probably say www-data and shell will say your username or if you are on AWS - default, you would get root

You do not have to use system() , use exec() instead.

We should be close as :

echo json_encode(exec("python --version"));

Performing operations will require you to have correct User and Groups set.

Look up for : In the shell, what does " 2>&1 " mean?

So your code should be :

echo json_encode(exec("python --version 2>&1"));

Hope it helps!


This works fine for my production server

public function deploy_test() {    echo json_encode(system("python --version 2>&1"));}

with the output

Python 2.7.3"Python 2.7.3"

Output of the unix command printed twice as system() itself outputs the result to browser. So exec() can be used in the place of system to avoid this.

public function deploy_test() {    echo json_encode(exec("python --version 2>&1"));}

which outputs

"Python 2.7.3"

as expected.


I suspect it is not in the path. Try:

Type the full path to the python command (such as /usr/bin/python --version)

  1. Find out with the command which, 'which python'

  2. try executing your script from the command line, 'php script.php' => sometimes the web server sets up things differently

  3. make sure the error displaying is enabled with

    ini_set('display_errors',1);