Execute sudo commands as www-data in PHP

1.7k views Asked by At

I'm trying to execute bash script commands in PHP.

In the php file (commands.php) some commands doesn't work:

// works
echo shell_exec('whoami');
echo "<br>";

// works
echo shell_exec('ls -al');
echo "<br>";

// doesn't work
echo shell_exec('mkdir testfolder');
echo "<br>";

// doesn't work
shell_exec('sudo -S spleeter separate -p spleeter:2stems -o output audio_example.mp3');
echo "<br>";

This is the output:

www-data
total 272
drwxr-sr-x 2 root www-data   4096 Jul 12 23:06 .
drwxr-xr-x 4 root root       4096 Jul 12 21:20 ..
-rw-r--r-- 1 root www-data 262867 Jul 12 21:36 audio_example.mp3
-rw-r--r-- 1 root www-data   1417 Jul 12 23:11 commands.php

The www-data user have permissions to this folder:

drwxr-sr-x 2 root www-data  4096 Jul 12 23:06 files

I have no problems to execute the commands as root user at the terminal. I guess there are some permission issues. What is the best way to execute bash commands with PHP?

(Ubuntu 18; Apache2; PHP7)

0

There are 0 answers