barryjarvis
Posts: 8
Joined: Fri Feb 22, 2013 8:53 am

PHP - exec() not working - permission issue?

Sun Jun 30, 2013 7:31 pm

hi all,

I've setup lighttpd on my pi along with a couple other things including a bash script to use the google text-to-speech service.

This script is called 'speech.sh' and is stored inside the root dir of my webserver (/var/www/) along with my index.php

My index.php is as follows:

Code: Select all

$json_string = file_get_contents("http://api.wunderground.com/api/<redacted>/geolookup/conditions/q/ll13%208pf.json");
$parsed_json = json_decode($json_string);
$temp_f = $parsed_json->{'current_observation'}->{'temp_c'};
$weather = $parsed_json->{'current_observation'}->{'weather'};

$result = "Current temperature is ${temp_f}&deg;C and it is ${weather}\n";
echo $result.'<br />';

exec('./speech.sh hello this is a test')
As you can see i'm using the wunderground weather API to get weather info and am looking to pass it to the speech.sh script.

The problem is, i can't get exec to trigger the script at the moment...

I think this maybe a permissions or user issue from what i've read? Apparently the script will be being run by user 'www-data', rather than root (or maybe pi?) and so won't be able to run ??

If this is the case, can anyone advise me on how i can either allow www-data user permission to run this script, or how i can get it to run as root from the exec command?

I can just about navigate my way though PHP but command line stuff and especially linux stuff i'm still very much a learner, so don't know what i should be looking for here :(

thanks in advance.

SirLagz
Posts: 1705
Joined: Mon Feb 20, 2012 8:53 am
Location: Perth, Australia
Contact: Website

Re: PHP - exec() not working - permission issue?

Mon Jul 01, 2013 6:18 am

barryjarvis wrote:hi all,

I've setup lighttpd on my pi along with a couple other things including a bash script to use the google text-to-speech service.

This script is called 'speech.sh' and is stored inside the root dir of my webserver (/var/www/) along with my index.php

My index.php is as follows:

Code: Select all

$json_string = file_get_contents("http://api.wunderground.com/api/<redacted>/geolookup/conditions/q/ll13%208pf.json");
$parsed_json = json_decode($json_string);
$temp_f = $parsed_json->{'current_observation'}->{'temp_c'};
$weather = $parsed_json->{'current_observation'}->{'weather'};

$result = "Current temperature is ${temp_f}&deg;C and it is ${weather}\n";
echo $result.'<br />';

exec('./speech.sh hello this is a test')
As you can see i'm using the wunderground weather API to get weather info and am looking to pass it to the speech.sh script.

The problem is, i can't get exec to trigger the script at the moment...

I think this maybe a permissions or user issue from what i've read? Apparently the script will be being run by user 'www-data', rather than root (or maybe pi?) and so won't be able to run ??

If this is the case, can anyone advise me on how i can either allow www-data user permission to run this script, or how i can get it to run as root from the exec command?

I can just about navigate my way though PHP but command line stuff and especially linux stuff i'm still very much a learner, so don't know what i should be looking for here :(

thanks in advance.
You'll probably need to use the full path, or just the filename in exec. Rather than the ./ notation.
My Blog - http://www.sirlagz.net
Visit my blog for Tips, Tricks, Guides and More !
WiFi Issues ? Have a look at this post ! http://www.raspberrypi.org/phpBB3/viewtopic.php?f=28&t=44044

User avatar
rpdom
Posts: 16341
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: PHP - exec() not working - permission issue?

Mon Jul 01, 2013 6:35 am

What are the current owner, group and permissions of the script?

Is it in the /var/www directory?

As Apache runs with user=www-data and group=www-data, setting the group of the script to www-data and adding group execute permissions should work (chmod g+x speech.sh).

Actually, I'm not sure that it is safe to have the script in /var/www. I think if apache has at least read access to it, someone could go to http://your-host/speech.sh and the script may be executed, or just displayed for them to read.

barryjarvis
Posts: 8
Joined: Fri Feb 22, 2013 8:53 am

Re: PHP - exec() not working - permission issue?

Mon Jul 01, 2013 6:29 pm

Ok, so i've run `ls -la` on /var/www and this is the result.

Image
rpi ls-la by barryjarvis85, on Flickr

I've chown'd the file a couple of times to change groups/users etc but i have no idea what i'm doing so have been running blind really.

Since posting the original question, i have also changed to apache2 if that makes any difference?


Any help appreciated.

User avatar
rpdom
Posts: 16341
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: PHP - exec() not working - permission issue?

Mon Jul 01, 2013 8:11 pm

You will need to have something like group of www-data and -rwxr-x--- permissions for your webserver to be allowed to run the script.

Code: Select all

# Set group of file to www-data
chgrp www-data speech.sh
# Set permissions of file to -rwxr-x---
chmod 750 speech.sh
# check permission (should look something like this)
ls -l speech.sh
-rwxr-x---  1 pi       www-data   911 Jun 30 20:41 speech.sh
See if that works.

barryjarvis
Posts: 8
Joined: Fri Feb 22, 2013 8:53 am

Re: PHP - exec() not working - permission issue?

Mon Jul 01, 2013 8:53 pm

rpdom wrote:You will need to have something like group of www-data and -rwxr-x--- permissions for your webserver to be allowed to run the script.

Code: Select all

# Set group of file to www-data
chgrp www-data speech.sh
# Set permissions of file to -rwxr-x---
chmod 750 speech.sh
# check permission (should look something like this)
ls -l speech.sh
-rwxr-x---  1 pi       www-data   911 Jun 30 20:41 speech.sh
See if that works.

Ok, so i got the same result as you in terms of permissions and group on the speech1.sh (changed filename since original post).

It's still not working though :(

My PHP exec command is:

Code: Select all

exec("speech1.sh this is a test");
and also tried

Code: Select all

exec("./speech1.sh this is a test");
I have tested a standard command such as

Code: Select all

echo exec("ls");
and this works, so it's something to do with the speech.sh not being accessed.

I created that speech1.sh file on the pi itself (via ssh) and so it was created by the pi user.
Would it make any difference if i created it on my mac and then ftp'd it into the root dir of my apache install (var/www/) ?

Also, does it make any difference that the script is in the var/www/ dir?? should it be outside this dir?

btw... just to confirm, i can run `./speech1.sh this is a test` from the command line and it works as it should, so it's not the script itself that's the problem.

User avatar
jojopi
Posts: 3143
Joined: Tue Oct 11, 2011 8:38 pm

Re: PHP - exec() not working - permission issue?

Mon Jul 01, 2013 9:44 pm

barryjarvis wrote:btw... just to confirm, i can run `./speech1.sh this is a test` from the command line and it works as it should, so it's not the script itself that's the problem.
A better test would be: sudo -u www-data ./speech1.sh this is a test

There was never anything wrong with the permissions. You do not seem to be checking the return status or getting any error message; you are just guessing that the exec fails. More likely it succeeds but the shell script does nothing. (Presumably there is no error checking in there either.)

At a guess, the script tries to make some noise. So it fails because user www-data is not in group audio.

barryjarvis
Posts: 8
Joined: Fri Feb 22, 2013 8:53 am

Re: PHP - exec() not working - permission issue?

Tue Jul 02, 2013 8:49 pm

jojopi wrote:
barryjarvis wrote:btw... just to confirm, i can run `./speech1.sh this is a test` from the command line and it works as it should, so it's not the script itself that's the problem.
A better test would be: sudo -u www-data ./speech1.sh this is a test

There was never anything wrong with the permissions. You do not seem to be checking the return status or getting any error message; you are just guessing that the exec fails. More likely it succeeds but the shell script does nothing. (Presumably there is no error checking in there either.)

At a guess, the script tries to make some noise. So it fails because user www-data is not in group audio.
Worked a treat, thank you!
I now how my web page triggering speech events via Google tts service :)

SirLagz
Posts: 1705
Joined: Mon Feb 20, 2012 8:53 am
Location: Perth, Australia
Contact: Website

Re: PHP - exec() not working - permission issue?

Wed Jul 03, 2013 1:02 am

barryjarvis wrote:
jojopi wrote:
barryjarvis wrote:btw... just to confirm, i can run `./speech1.sh this is a test` from the command line and it works as it should, so it's not the script itself that's the problem.
A better test would be: sudo -u www-data ./speech1.sh this is a test

There was never anything wrong with the permissions. You do not seem to be checking the return status or getting any error message; you are just guessing that the exec fails. More likely it succeeds but the shell script does nothing. (Presumably there is no error checking in there either.)

At a guess, the script tries to make some noise. So it fails because user www-data is not in group audio.
Worked a treat, thank you!
I now how my web page triggering speech events via Google tts service :)
Mind sharing what was wrong ?
Was it just the sudo access for www-data ?
My Blog - http://www.sirlagz.net
Visit my blog for Tips, Tricks, Guides and More !
WiFi Issues ? Have a look at this post ! http://www.raspberrypi.org/phpBB3/viewtopic.php?f=28&t=44044

barryjarvis
Posts: 8
Joined: Fri Feb 22, 2013 8:53 am

Re: PHP - exec() not working - permission issue?

Thu Jul 04, 2013 6:09 pm

The problem was with the www-data not being in the audio group. A quick `usermod` later and hey presto, it works :)

snowtech
Posts: 9
Joined: Tue Mar 18, 2014 11:08 am

Re: PHP - exec() not working - permission issue?

Wed Mar 19, 2014 9:57 am

barryjarvis wrote:The problem was with the www-data not being in the audio group. A quick `usermod` later and hey presto, it works :)
Hi, I have the exact same problem (but Im trying to use mpg123 alarm.mp3).
I followed all those www-data, "sudo usermod -a -G audio www-data", my command is executing "ls", but when I change to 'mpg123 alarm.mp3' still does work...

Could somebody give me more information ?!

Thanks !

Ganesh-RaspberryPi
Posts: 2
Joined: Thu Aug 28, 2014 6:31 am

Re: PHP - exec() not working - permission issue?

Tue Dec 02, 2014 5:48 am

In PHP, You have to use

exec("sh speech.sh ".escapeshellarg("hello this is a test"));

Refer this, http://php.net/manual/en/function.escapeshellarg.php

txt3rob
Posts: 365
Joined: Sat Aug 11, 2012 3:45 pm
Contact: Website

Re: PHP - exec() not working - permission issue?

Thu Dec 18, 2014 1:46 pm

for me i put www-data in the sudo users and then set it to the only file it can run.

it prob needs root for some reason.
The Raspberry Pi Hell Guy - Random Ramblings to assist me and others.
http://raspberrypihell.blogspot.com
My Github - http:/www.github.com/random-robbie/
http://www.smspi.co.uk - send free uk sms via your raspberry pi from here

User avatar
DougieLawson
Posts: 37604
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: PHP - exec() not working - permission issue?

Thu Dec 18, 2014 1:50 pm

txt3rob wrote:for me i put www-data in the sudo users and then set it to the only file it can run.

it prob needs root for some reason.
That's a massive security hole that you've opened there.
It's much better to have a server (like pigpiod) that runs with root permission and a client (pigpio) that sends commands to the server.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Return to “Other programming languages”