Category Archives: bash

Setting array as environment variable for exec() in apache2

I am noob in php and apache so don't be strict with me. I use apache2 server and i need to execute bash scripts on server. So i use exec() function. For my scripts i should define environment variables but apache sends me an error when i'm trying to start server with my bash-like definition:

/etc/apache2/envvars: Syntax error: "(" unexpected

Syntax export MY_ARRAY = (1 2 3) works in terminal but doesn't works by placing in apache's envvars file.

So how can i define array environment variable to work with in other scripts by using php function exec()?


Using PHP shell_exec() to execute BASH script, can’t write files

I'm using PHP to write an API to pass JSON parameters to a BASH script via POST. Here's a distilled version of what I'm doing:


$params = json_decode(file_get_contents('php://input'), true);

$cmd = "/bin/bash /path/to/myscript " . $params["value1"] . " " . $params["value2"] . " " . $params["value3"];
$output = "Command: " . $cmd . "\n\nResponse: " . shell_exec($cmd);



'myscript' is a simple test script with the following:


echo -e "Executing script...your parameters are: [$1] [$2] [$3]\n"
echo -e "test" > /tmp/test.txt

Executing the script works and I get the first echo back showing the parameters were successfully passed. However trying to do anything within the script which writes to the filesystem (the second echo) does not happen. It is as if permissions are a problem, but I have made sure to write into directories where the 'apache' user has full permissions. I have also tried adding apache and my script command to sudoers and running via sudo passwordless, no luck. I have verified that paths are explicit and correct, users are correct, etc via adding the appropriate debug commands into both my php and bash scripts as suggested by the million other questions already posted. The script obviously works fine if I run it from the command line. It also works (and can write to the filesystem) if I 'sudo -u apache' to run the script, so I know permissions aren't a problem. I am stuck. Any ideas?

Dynamically create vhost (Apache/nginx) [on hold]

I'm creating a web portal from which I can create websites. Each website should have a vhost for nginx and Apache, because I'm running Apache behind nginx. I have read that PHP cannot reload Apache/nginx because that would be a security issue, but what other way is there? How do control panels like cPanel and DirectAdmin do it? I've also tried creating a bash script and execute that from PHP, but that (of course) also didn't work.

How can I solve this?


I plan to create more and more servers in the future. I know I can use visudo to enable my PHP script to execute a set of predefined scripts, but that would mean I'd have to add those lines every time I deploy a new server. Or can I create my own sudoers file to deploy with every new server?

Execute shell scripts on apache http server running on Windows server

We have a legacy application deployed on apache http server running linux environment. The application logic is written in bash/shells scripts in different cgi files. Now we'd like to run this CGI application on a windows environment for testing purposes.

I have configured cygwin before to run shell scripts on windows but I don't know if there is a way to configure apache http server to run these shell scripts on windows without installing cygwin.

Maybe there are apache http modules which am not aware of that can be added to support the execution of shell scripts on non-windows environment. Can anyone suggest a way on how to make these work? or is it even possible to make it work?

Using awk to generate report from apache http logs

Hoping someone can help me with a bash linux script to generate report from http logs.

Logs format: - r.c.bob [14/Feb/2017:11:31:20 +1100] "POST /webmail/json HTTP/1.1" 200 1883 "" "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko" 1588 2566 "" 9FC1CC8A6735D43EF75892667C08F9CE 84670 - - - -  

Output require:

time in epoch,host,Resp Code,count  


what I have so far but nothing near what I am trying to achieve:

tail -100 httpd_access_*.log | awk '{print  $5 " " $2 " " $10}' | sort | uniq

a super light cgi web-server? [on hold]

I have tried:

Apache: too heavy for me.

Nginx : they see cgi as a security issue so they only allow fast-cgi

BusyBox : allows cgi but in cgi-bin folder only and its not just a server

nweb : by ibm, i compiled & tested. slower than php server & no cgi

python cgi server : cgi-bin path can be changed to root I kind of like it

nc : well its a tool, not a core way of listening to port like python server.

php built in server : can not execute shell scripts directly. must use exec()

hiawatha : it was in yum repos. installed it.. nice web site. bu no cgi i think

lighttpd : I never hear people say anything about it although its famous.

so what am I suppose to do ?

building my own port 80 listener 

not possible with bash/shell.. has to be done via "C" language. maybe one day.. but right now I just dont wanna learn "C". I Need to design a web site that is all with bash/shell scripts.

What I want:

a web server like python's CGI web server.
it must be easy to configure.
it must run cgi in root folder (documentroot)
POST should be allowed GET should be allowed..
I am gonna code shell scripts as if they are php scripts. etc..

security: not an issue.
speed: not an issue for now.

I am inclined to create a python script but the world of python is a big world.. I do not want to dive into their universe. its just bells and whistles . confussion.

Parameter from URL into shell script

How is it possible to get an URL parameter like /?photo=1.png into a shell script as a variable, running into a cgi-bin container on apache?

Edit Iam generating a list of all files in a directory.

echo "Content-type: text/html"

for file in /var/www/html/export/tui/*;
        echo "<a href='/cgi-bin/test.cgi?file="${file: -27}"'>"${file: -27}"</a><br>";

Now, i want to give the file name as a parameter into a second script, who needs this for reading it.

No way to run a web site w bash scripts? [on hold]

suppose I have a folder named /1

in it I have bash scripts.


I want to connect my foder /1 to the web. ( port 80 )

Currently the world we live in offers the following solutions:

Apache A very large program with lots of bells and whistles.

NginX A very large program with lots of bells and whistles.

PHP A very large program with lots of bells and whistles.

All I want to do is.. serve folder /1 to my public ip4.
which is connected to my domain name.

I would like to be executed unless other files are

I do not want to learn programs with lots of bells and whistles.

Sure they say "this is not secure way of desigining a web site"

A: I will make it secure. I will escape all incoming strings.

They say.. You need fast-cgi so that programs can be loaded into the memory and run faster..

A: I want to deal with that LATER. perhaps via bash scripts something can be done about it.

I feel like playing with a tablet OS designed for the super dumb.
I want to design my web sites with bash scripts.
what can I do ?

Option 1:

use php's built in server and then use php's system_exec()
to execute your bash scripts and echo the output on the browser.

Option 2: use python's web server.

No I can not do that. it deos not look like something
designed for production.

So what am I suppose to do now ?