简体   繁体   中英

Running shell commands from PHP - With root permissions or sudo

I'll explain what I'm trying to do, because someone might have a much better and completely different approach to this.

I'm trying to install FFMPEG, but not just install it, I'm making an automated installer that installs some PHP scripts and also FFMPEG if it's not already installed.

I can't find any automated PHP scripts that can install FFMPEG, so I'm guessing this is more difficuilt than I thought.

I had 3 options to do this:

  • Build from source on the server using the exec function, this requires root permissions.
  • Find a pre-compiled binary of FFMPEG, easily done, and I did it, but then I realised it didn't have the correct codecs so wasn't working.
  • Use YUM or APT-GET to install FFMPEG, this would be great but it requires root permissions.

So, how can I either:

Use YUM/APT-GET from PHP and set sudo

OR

Install codecs for FFMPEG after it has been compiled. Using the pre-compiled binaries that I found, if it were possible then I could just install the missing codecs (lame mp3 I think. Maybe others too). Or do the codecs need to be built into the binary.

I think the prefered option is to use YUM/APT-GET as my targets are mostly Centos 5 and maybe Ubuntu aswell.

If you end up compiling from source or installing from a repo you can handle the install processs through this project that allows PHP to obtain and interact with a real Bash shell. 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('yum -y install FFMPEG');
//the return will be a string containing the return of the command
echo $return1;

You will need to deal with the edge cases when ie there is a GPG key that need to be accepted or dependencies are conflicting. But the project lets you script all Bash interaction.

sudo in php is a lot easier if you use SSH. Among other things you can make use of "expect" to wait for the appropriate prompts. If there's a way to do that with the built in exec() or system() I'm not aware of it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM