1

I'm a beginner to Apache2.

What I want to do

・I want to execute a shell script with PHP in apache2 by using raspberry pi 3.

What I did in order to do above.

  1. I installed apache2 and php7 to my Raspberry pi.
  2. I put index.php which was written that hello world in /var/www/html/ directory.
    The scripts is below.

    /var/www/html/index.php
    <?php echo('hello world'); ?>

  3. I made sure that I can see hello world from the browser in my local PC connected to my raspberry pi.

  4. I changed the PHP script to execute the shell script. The scripts is below.

    /var/www/html/index.php
    <?php exec('sh /var/pi/blink.sh'); ?>

error

・Apache2 don't execute the shell script.

try

  1. I made www-data user have the authority to execute it without password.
    $ sudo visudo # => /etc/sudoers.tmp
    ・I added www-data ALL=NOPASSWD:/bin/sh to the file above.
  2. I made sure that I can run ,by changing the authority, sh /var/pi/blink.sh file in raspberry pi which I want to run in Apache2 in raspberry pi .
  3. I added sudo to index.php which looks like
    <?php exec('sudo sh /var/pi/blink.sh'); ?>

    However, nothing changed.

The devices I'm using

・Mac book air (OSX 10.13.6)
・Raspberry pi (Model B) with Raspbian (Raspbian GNU/Linux 9 (stretch).

mario
  • 144,265
  • 20
  • 237
  • 291
shinzooon
  • 11
  • 2
  • Additionally see: [How can I debug exec() problems?](//stackoverflow.com/q/12199353) – mario Nov 28 '18 at 01:37
  • Possible duplicate of [Executing a script from a PHP script](https://stackoverflow.com/q/17151946/608639), [PHP shell_exec() permission on Linux Ubuntu](https://unix.stackexchange.com/q/115054/56041), etc – jww Nov 28 '18 at 03:30

1 Answers1

2

Have you checked to make sure your script has the proper permissions? Usually 755 will work for external scripts that PHP is trying to execute. Also, have you tried shell_exec() instead of exec()? http://php.net/manual/en/function.shell-exec.php

miles_b
  • 335
  • 3
  • 10
  • Thank you. I can run with `shell_exec()` and apache2 the shell file which is written that `echo 'Hello world'`. but I can't blink the led connected to my raspberry pi. Since I can see the shell script from browser, the problem isn't in PHP but shell script. I try to solve it. Anyway Thank you. – shinzooon Nov 28 '18 at 23:53