Search code examples
phplinuxrootsudo

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.


Solution

  • 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.