A default install of
apache will serve all URLs pointing to it if no Virtual Hosts have been defined. Therefore you do not need to do anything extra to get
apache to respond.
The scripts can be ran as CGI scripts by placing them in
/var/www/cgi-bin and making sure that they are executable with
chmod 0755 <script name>.
For example, if you create
/var/www/cgi-bin/test.sh as follows:
echo "Content-type: text/html"
then navigate to your browser at:
http://<server name or IP>/cgi-bin/test.sh, you should see something similar to:
Apache/2.2.15 (CentOS) Server at testing.lo Port 80
HTTP_USER_AGENT=Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0
As you can see
HTTP_HOST is the URL of the server (
testing.lo in this instance) which you can then use in your script.
However, this isn't exactly as you requested as you have
/cgi-bin/test.sh in the URL. To fix this, you need to redirect. The simplest way would be to enable the
.htaccess file and configure it.
/etc/httpd/conf/httpd.conf find the line
<Directory "/var/www/html"> and scroll down a few lines until you see a line that starts
AllowOverride. Make sure it is set to
All. You should have:
Now create a file
/var/www/html/.htaccess and add the following:
RewriteRule (.*) /cgi-bin/test.sh/$1 [L]
service httpd restart
Point your browser at the server's URL:
http://<server name or IP>.
You should see similar results to before, but without having to append the script path to the URL. Also notice that there are environment variables that tell you what URL was entered so that your script can use them.
HTTP_HOST is the name of the server.
PATH_INFO is the path appended to the server. (
test if you enter
http://<server name or IP>/test in your browser).
bash script, you simply need to use these variables instead of parsing the script's arguments as your question suggested.
The script doesn't have to be
bash though. Use any language you wish as long as it can access the environment variables passed to it.