0

I am trying to use PHP to play a sound ON SERVER SIDE once someone visits my webpage. It's running on Nginx/Php5 (raspberry pi).

This is how I'm trying to do it:

I have executed command:

sudo usermod -aG audio www-data

The php file:

<?php
$audio = shell_exec('sh play.sh');
echo $audio;
?>

But it results nothing.

Running 'id' command with the same method results:

uid=33(www-data) gid=33(www-data) groups=33(www-data),29(audio),44(video)

I don't know if its relevant but:

ls -lah /dev/snd

drwxr-xr-x   3 root root      160 Oct 26 17:22 .
drwxr-xr-x  15 root root     3.5K Oct 26 17:22 ..
drwxr-xr-x   2 root root       60 Oct 26 17:22 by-path
crw-rw----+  1 root audio 116,  0 Oct 26 17:22 controlC0
crw-rw----+  1 root audio 116, 16 Oct 26 17:22 pcmC0D0p
crw-rw----+  1 root audio 116, 17 Oct 26 17:22 pcmC0D1p
crw-rw----+  1 root audio 116,  1 Oct 26 17:22 seq
crw-rw----+  1 root audio 116, 33 Oct 26 17:22 timer

Executing the following command plays the file properly:

sudo -u www-data play ding.mp3

But using the following one:

sudo -u www-data sh play.sh

Results:

Sorry, user www-data is not allowed to execute '/usr/bin/play /var/www/ding.mp3' as www-data on raspberrypi.

The script contains the following:

#!/bin/sh
sudo -u www-data /usr/bin/play '/var/www/ding.mp3'

Any other command in that script is executed properly. Anyone could give me some advice?

0

1 Answer 1

1

I have figured out the solution.

The solution was to delete "sudo -u www-data" part from the shell script. Now it contains only:

#!/bin/sh
/usr/bin/play '/var/www/ding.mp3'

I leave it here, so maybe someone will find it useful in the future.

Sign up to request clarification or add additional context in comments.

2 Comments

Why were you using sudo in the first place?
I tought that will eliminate any permission problem of playing the sound. I was wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.