Learner
Learner

Reputation: 1

How to execute scripts in root folder from Apache using php

I need to run a python script before which I need to activate a virtual env for the script to run and it is in root folder. This is the command to do that (without using source command)

exec(root/Envs/ate/bin/python /var/www/cgi-bin/TStool/box_upgrade.py).

how do I execute from apache. I get 126 error in php. I have given the actual path.

126 error means- "Command invoked cannot execute. A permission or command not executable problem.".

How do I do this. The actual path is in the root folder, I can't move it to outside of root.

<?php
  $op=exec('/root/Envs/ate/bin/python /var/www/cgi-bin/TStool     /box_upgrade.py',$output,$return);
  echo "Dir returned $return, and output:\n";
  var_dump($output);
  echo $return;  
  echo $op;
    ?>

Any suggestions? Thanks.

Upvotes: 0

Views: 982

Answers (1)

MerlinTheMagic
MerlinTheMagic

Reputation: 605

I recently published a project that allows PHP to obtain and interact with a real Bash shell (as root if requested), it solves the limitations of exec() and shell_exec(). Get it here: https://github.com/merlinthemagic/MTS

After downloading you would simply use the following code:

$shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1  = $shell->exeCmd('/root/Envs/ate/bin/python /var/www/cgi-bin/TStool/box_upgrade.py');
//the return will be a string containing the return of the command
echo $return1;

In terms of security it is far better than running apache as root. But letting PHP anywhere near root is always tricky.

The project i built achieves a root bash shell in one of 2 ways:

1) You allow apache the right to sudo python.

OR

2) You pass root credentials to the object every time you need a shell with root setup.

Pick your poison. :) Read the documentation.

Upvotes: 1

Related Questions