0

Suppose a domain name has been pointed to my server's IP address..

soon as the domain name comes to me..

i want apache to execute...

 python_script domain.tld

or perhaps..

 bash_script domain.tld

if the incoming domain looks like this:

domain.tld/test

then..

 bash_script domain.tld/test

should be executed.

from within the bash script i can run calculations and decide what to display.

perhaps things do not work this way due to security concerns but security is not my concern. just would like to test it out.

1

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:

#!/bin/bash
echo "Content-type: text/html"
echo ""
echo "<pre>"
env
echo "</pre>

then navigate to your browser at: http://<server name or IP>/cgi-bin/test.sh, you should see something similar to:

SERVER_SIGNATURE=
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
SERVER_PORT=80
HTTP_HOST=testing.lo
DOCUMENT_ROOT=/var/www/html
SCRIPT_FILENAME=/var/www/cgi-bin/test.sh
REQUEST_URI=/cgi-bin/test.sh
SCRIPT_NAME=/cgi-bin/test.sh
HTTP_CONNECTION=keep-alive
REMOTE_PORT=43964
PATH=/sbin:/usr/sbin:/bin:/usr/bin
PWD=/var/www/cgi-bin
SERVER_ADMIN=root@localhost
HTTP_ACCEPT_LANGUAGE=en-gb,en;q=0.5
HTTP_ACCEPT=text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
REMOTE_ADDR=192.168.122.1
SHLVL=1
SERVER_NAME=testing.lo
SERVER_SOFTWARE=Apache/2.2.15 (CentOS)
QUERY_STRING=
SERVER_ADDR=192.168.122.40
GATEWAY_INTERFACE=CGI/1.1
SERVER_PROTOCOL=HTTP/1.1
HTTP_CACHE_CONTROL=max-age=0
HTTP_ACCEPT_ENCODING=gzip, deflate
REQUEST_METHOD=GET
_=/bin/env

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.

In /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:

<Directory "/var/www/html">
...
...
AllowOverride All
...
...
</Directory>

Now create a file /var/www/html/.htaccess and add the following:

RewriteEngine on
RewriteRule (.*) /cgi-bin/test.sh/$1 [L]

Restart apache:

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

In your 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.