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).

2

1 Answer 1

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

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

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.