Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web local application Apache: run a shell script

Tags:

linux

shell

php

I had developped a shell script and I wanted to create an UI with it. I decided to use a web interface with a local server because I have few knowledges in HTML/PHP, more than QT or Java. I simply want that my html can run a shell script on my computer.

I have an Apache php server which I start in localhost with apachectl.

In /var/www/html/ I have a shell script and a test_web.php file which execute this script with exec('/var/www/html/test.sh')

In my html I have this:

<form method="POST" action="test_web.php?"> 
<input type="submit" value="Execute" name="exec"> 
</form>

If I click on Execute, it opens a new page where I see the output of echo commands from the script but it is not executed on the terminal as a standard script (like if the script is not executed on server side).

I want this script to be executed as started from the terminal.

Thank you for your help

like image 907
elpha01 Avatar asked Mar 09 '23 20:03

elpha01


1 Answers

As suggested by some commentators, you could use the Apache cgi features to be able to execute directly your test.sh file (1), but since you say you are using a Php enabled Apache server, you have an alternative and easier option with Php which you partially already tried (2). In both cases, you will need to take care of the permissions and ownership of the bash script, and its execution environment for it to produce the same results as on the command line (3).

1. CGI

CGI stands for Common Gateway Interface. It allows web servers to execute programs that are executable on the host, and interface with the web server. That is a protocol per se, and CGI can be used not only to execute programs, but also for creating dynamic sites. You could program a dynamic website in any language, provided the executable is programmed to receive data via stdin from the server and interpret the headers, decode any contents properly, and that the program can return proper dynamic data to the server via stdout, with proper headers, Content-type, etc.

There are libraries for helping in this task for many languages (for C, Perl has a CGI module in is Core Modules, etc., and even for Bash).

On Linux, to use CGI with Apache 2, you would need to make sure that your Apache configuration loads the required module for supporting it, and select the directory where your web server will be allowed to execute the programs:

# uncomment or add the directive for the module
LoadModule cgid_module modules/mod_cgid.so

# one way to allow the server to execute cgi programs is to
# set a ScriptAlias directive to a directory dedicated to CGI programs
ScriptAlias "/cgi-bin/" "/var/www/html/cgi-bin/"

You put your test.sh script in /var/www/html/cgi-bin/, and browser can be pointed to http://localhost/cgi-bin/test.sh to directly execute the script. There are other configurations possible, see this Apache CGI tuturial.

If you need to execute the Bash program and want your CGI to send anything to the browser, the first echo sent by the Bash script shall be the following output:

Content-type: text/html

followed by whatever dynamic html you want to produce.

2. Php

Really all these complications may not be necessary since you say you use a Php enabled Apache server, and since you probably do not want the Bash script to send html directly to the browser.

In that case, the shell_exec() command or backticks operator, will allow you to run your Bash script and obtain its output in a Php variable, and do whatever you want to do with it in Php.

You say you have tried using the exec command, which should work also, except that you get a status code in response, and not the program output.

If your execution command does not work at all, this may be because of permissions, ownership or execution environment issues.

3. Permissions, ownership and execution environment

Have a look at your Apache logs for errors produced by the exec or shell_exec commands.

The script needs to have execution permissions, and needs to be readable and executable by the web server.

For instance, on Debian, Apache web server runs as a user www-data, so a script would need to be owned and executable by that user:

chown www-data:www-data test.sh
chmod u+x test.sh

You can find the location of the Apache logs and the user and group under which the web server runs in your Apache configuration file by inspecting (as root) the results of:

apachectl -S

Another possible issue is the environment in which Apache will be running the script. That environment, may or not include the same environment variables, the $PATH will be different and may not include all directories available to a "normal" user executing the same script on the console.

Again, check for errors in the logs.

The most frequent cause for failure is a Bash command not found because it is not in the $PATH of the Apache process. The quick fix is then to change the script to prefix the command with its full path.

So, for instance, if your test.sh uses a program called validjson, open your terminal as a user that can execute the script without errors, and check where it is:

$ which validjson
validjson: /usr/local/bin/validjson

and instead of calling validjson in the script assuming it is in the $PATH, call it instead with its full path /usr/local/bin/validjson reported by the which command.

If you are calling other scripts or programs, they may have the same issues, check that until you have debugged the execution of your script.

like image 199
Tardis Avatar answered Mar 19 '23 13:03

Tardis