0

My problem is, that the following script executes in the terminal fine but on my Apache2-Webserver it doesn't do anything.

<?php
exec("/usr/bin/pactl set-sink-volume 0 60%");
?>

I can hear the music getting louder, when its executed from the terminal using

sudo php /var/www/html/test2.php

Other commands with exec work perfectly fine on the webserver. Working example:

<?php    
exec("/sbin/shutdown -r now");
?>

Setup:

  • Apache 2.4
  • PHP 7
  • Raspberry Pi 3b+
  • Raspian
  • Speaker connected via Headphone Jack

Thanks for any help in advance!

Silvan
  • 1
  • 3
  • Is apache (typically user www-data) part of the sudoers group? – Kasper Agg Dec 09 '18 at 13:47
  • I also think you shouldn't be invoking command line commands with sudo through a webapp. Why not add www-data to the group that is allowed to execute pactl? – Kasper Agg Dec 09 '18 at 13:49
  • Okay, how can I add www-data to the execution group? – Silvan Dec 09 '18 at 14:01
  • You could try google for that: https://www.google.nl/search?q=linux+add+user+to+group&oq=linux+add+&aqs=chrome.1.69i57j0l3.3481j0j7&client=ms-android-google&sourceid=chrome-mobile&ie=UTF-8 – Kasper Agg Dec 09 '18 at 14:05

2 Answers2

0

First check the group. One way is to use ls -lah /usr/bin/pactl. It should show amongst other information, the user and group who have permission. It also shows if the user, group and others have permission to execute at all.

To add an existing user account to a group on your system, use the usermodcommand, replacing examplegroup with the name of the group you want to add the user to andexampleusername  with the name of the user you want to add.

usermod -a -G examplegroup exampleusername

For example, to add the user www-data to the group music, use the following command:

usermod -a -G music www-data

Source

edit

Another way would be to allow all users to execute /usr/bin/pactl by adding execute bit to all.

Kasper Agg
  • 107
  • 1
  • 7
  • I executed the following command in the terminal an it worked: sudo -u pi /usr/bin/pactl set-sink-volume 0 20% When I execute it on the webserver it doesn't work. In the sudoers file I have added, that it doesn't need a password to execute this specific command. – Silvan Dec 09 '18 at 14:45
  • I do not understand your comment. In order for apache to execute the same command without using sudo, the apache user (www-data) needs to be added to the group of the executables' group. – Kasper Agg Dec 09 '18 at 14:48
  • But the sbin folder requires sudo rights too and the shutdown command in this folder works from the website, and I didn't change anything on the permissions – Silvan Dec 09 '18 at 14:51
  • Are all flags the same those binaries? Are they for example 777 vs 755? – Kasper Agg Dec 09 '18 at 14:56
  • So I changed the permissions of the /usr/bin/pactl to the exact same as /sbin/shutdown ones, still not working, the flag is now 777. – Silvan Dec 09 '18 at 15:10
  • In that case I'm a bit out of ideas. Did you restart apache after the changes? – Kasper Agg Dec 09 '18 at 15:13
  • Yes, I restarted the Apache Webserver. Thank you for your suggestions anyway! – Silvan Dec 09 '18 at 15:20
  • And the users for user and group are the same too? – Kasper Agg Dec 09 '18 at 15:23
  • Yeah, both root and root – Silvan Dec 09 '18 at 15:24
  • Strange... Just one more thing: you did try the command on the commandline without sudo? – Kasper Agg Dec 09 '18 at 15:26
  • I've tried the following in the terminal (Without sudo and the same script):pi@raspberrypi:/bin $ php /var/www/html/test2.php – Silvan Dec 09 '18 at 15:28
  • The php is this: – Silvan Dec 09 '18 at 15:29
  • Unfortunally not – Silvan Dec 09 '18 at 15:43
  • Giving up for now, sorry mate! – Kasper Agg Dec 09 '18 at 15:47
  • Since I can't pm you, I'm just going to continue here anyway. You mentioned both binaries are in /sbin, but the pactl bin is in /usr/bin, are you sure they both look the same in terms of permissions etc? – Kasper Agg Dec 09 '18 at 15:56
  • Maybe this may help: https://stackoverflow.com/questions/17151946/executing-a-shell-script-from-a-php-script – Kasper Agg Dec 09 '18 at 16:13
  • I am able to execute the Shell Script, but there is still an error message "Home directory not accessible", when im trying to execute the pactl command, permission are changed, but thanks for the link. – Silvan Dec 09 '18 at 16:43
  • Just to isolate the issue, have you tried copying the binary to an apache directory and execute it from that location? – Kasper Agg Dec 09 '18 at 19:31
0

My problem is solved. I'm writing files into a folder and a script checks this folder for the files and executes than the code to control the audio (I'm controlling it now with amixer instead of pactl).

Thanks for your suggestions anyway!

Silvan
  • 1
  • 3