2

I am trying to use the shell_exec() function on PHP to install a package with apt. To do so, I've figured that I will also need the -y operator to assume yes to all questions. So I have got:

$output = shell_exec("apt-get -y install <package-id>");
echo $output;

However, by doing this, the install only manages to output up to the "After this operation, 0B of additional disk space will be used" point and doesn't return an error. When running this from the terminal however, it runs perfectly. Also, shell_exec is running from root. So is this a problem with my PHP server configuration, or something else? Thanks.

Edit: I forgot to mention to mention that the PHP is running through lighttpd which could be causing the issue. Also I have figured that using apt-get install actually downloads the .deb but fails to install it without giving an error. From this, I've concluded that it's something to do with permissions, but I haven't figured out how to fix it. Thanks.

Andrew Deniszczyc
  • 3,298
  • 3
  • 19
  • 16
  • Could be your [environment vars](http://stackoverflow.com/questions/9967217/inkscape-inside-php-apache-doesnt-render-fonts-to-png) like PATH and HOME? Ah, if you're running this on the CLI, possibly not - but nevertheless do check 'em! – halfer May 06 '12 at 11:36
  • I think that you should use `proc_open()` instead `shell_exec()` because you need to interact with all pipeslines. – Fabio Mora May 06 '12 at 11:45
  • also try adding --force-yes to that command line and try. – thevikas May 06 '12 at 11:55
  • Even with -y, this does not apply to debconf questions, which will need to be answered prior to the non-interactive installation of the package. – jordanm May 06 '12 at 14:01
  • Sounds like a permissions issue to me. Like it doesn't have permission to write to the necessary directories when downloading. I'm not too familiar, but can you pipe the output to a file like this >> logfile.txt? Some of these functions will only return back the last thing that happened to your output variable rather than all statements. – John Zumbrum May 06 '12 at 15:48
  • I'd really recommend not using php via a webserver for this, especially if your `` is any sort of external input. Bash scripting run as root is typically a better tool for installing packages. Or even better, try something like chef or puppet. – Chris Henry May 06 '12 at 23:27

0 Answers0