1

I'm a Java Developer who is trying to learn HTML in painful baby steps. I'd like my nacent website to run a simple bash shell script (or better yet, a PHP script) when the user clicks a button or hyperlink. It is surprisingly hard to find a simple example of this!

I found this example on StackOverflow. The post's second answer lays out a step-by-step example, where the HTML code calls a PHP script; the PHP script calls a bash shell script. I've tried to replicate the example on my own web server. (FYI, I'm developing on an Ubuntu machine running Apache2.)

Here's my index.html:

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <form action="/testexec.php">
        <input type="submit" value="Run My Script">
    </form>
  </body>
</html>

Keeping it simple, this creates a button to call testexec.php, which is in the Apache home directory (/var/www/html/) There's not much to that script, either:

<?php
shell_exec("/var/www/html/myBashScript.sh");
header('Location: http://10.10.10.10/myBashScript.sh?success=true');
?>

I'm not sure about those file pathnames. Strictly speaking, myBashScript.sh is in directory /var/www/html/ (although I suppose I could move it later). When I troubleshoot, I can't tell if the pathnames are screwing up anything. Also, I really hate that I have to refer to the server by its IP Address (here, 10.10.10.10.) There's gotta be a way to just say localhost, right?

Here's myBashScript.sh:

#!/bin/bash
echo "HELLO WORLD"

...and that should do it. So I thought. Both scripts are set properly with chmod and chown and chgrp, and I can manually run them without errors from the command line.

But when I surf to my webpage and click "Run My Script", I see this in my web browser:

#!/bin/bash
echo "HELLO WORLD"

In other words, the text of the script is printed to the screen. But I want the script to run and the output of the script to appear on screen. Gah! FWIW, I see no errors entered into the Apache2 error log (/var/log/apache2/error.log), either.

Soooooo... anyone spot my error? Thank you in advance.

Pete
  • 1,511
  • 2
  • 26
  • 49
  • 1
    Are you sure you have PHP running properly on your server? You can test it by trying to run a simple test page with the content of ``. Also, in the script you posted, why run `shell_exec` and `header`? – j08691 Mar 25 '22 at 16:32
  • 1
    You can't use `header()` if the script has already generated output. – Barmar Mar 25 '22 at 16:33
  • 1
    Why are you trying to redirect to the bash script? You already executed it with `shell_exec()`, there's no need to go to it again. – Barmar Mar 25 '22 at 16:34
  • 1
    If you want to execute a bash script directly, without going through PHP, put it in the `cgi-bin` directory and enable that feature in Apache. – Barmar Mar 25 '22 at 16:35
  • 1
    You can leave out `http://10.10.10.10`, e.g. `Location: /myBashScript.sh`. When you omit this, it defaults to the same server as the original PHP URL. – Barmar Mar 25 '22 at 16:36
  • @j08691 Thanks J! You ask great questions. Yes, your test PHP script shows that I'm running PHP v8.1.0. The scripts I posted were taken verbatum and adapted from the other SO posting; that's why I'm running `shell_exec` and `header`. I take it that this is a bad approach? – Pete Mar 25 '22 at 16:46
  • @Barmar Wow, you ask great questions! My code is adapted from the other SO posting, which is prob why it looks so egregious to you. So the `header()' might be tripping me up, huh? That's very useful information. If I put my myBashScript.sh in `/usr/lib/cgi-bin/` will I have to supply the pathname when calling it? In my `serve-cgi-bin.conf` file, I've enabled LIB_CGI__BIN and listed that directory. THANK YOU!!! – Pete Mar 25 '22 at 16:51
  • 1
    yes, if you put it in cgi-bin, the url will be `/cgi-bin/myBasScript.sh` – Barmar Mar 25 '22 at 16:52
  • @Barmar Excellent! You rock! I'll try this immediately... – Pete Mar 25 '22 at 16:52

2 Answers2

1

I think that could enable CGI scripts in your webserver, send the script to /cgi-bin/ folder, rename it to myscript.cgi (remember to put the appropiate Shebang), set 0755 permissions, correct owner/group and put in the very beginning of the script:

#!/bin/bash
echo "Content-Type: text/html"
... more code... 

The first echo line tells browser that the output of CGI script must be rendered as HTML (you could modify MIME type to your specific needs). There's a functional example:

#!/bin/bash
echo -e "Content-type: text/html\r\n\r\n"
echo "<html>"
echo "<head>"
echo "<title>TEST</title>"
echo "</head>"
echo "<body>"
echo "<h1>It Works!, Bash script running as CGI</h1>"
echo "</body>"
echo "</html>"
itsdavidar
  • 36
  • 3
  • Excellent, yes this works great! I'll need to study MIME types because I need the Bash Script to do other things beyond echo statements in HTML format. But this is an excellent first step, thank you! – Pete Mar 25 '22 at 17:11
1

I really hate that I have to refer to the server by its IP Address (here, 10.10.10.10.) There's gotta be a way to just say localhost, right?

To reply to the question above, you can add the entry in /etc/hosts file in linux server. for e.g., 10.10.10.10 localhost localhost.localdomain