skip to Main Content

I am trying to call shell script from 000-default.conf inside /etc/httpd/sites-available.

The 000-default.conf file content is :

    <VirtualHost *:80>
        ServerName localhost
        DocumentRoot /var/www/html/
        CustomLog "| sh /scratch/user/test.sh" custom_user_tracking
    </VirtualHost>

My /etc/httpd/conf/httpd.conf has below content added:

ServerName localhost
LogFormat "%t [%h] [%m] [%U] [%B] [%b] [%D] [%q] [%s] [%{Referer}i] [%{User-Agent}i]" custom_user_tracking
IncludeOptional sites-enabled/*.conf
IncludeOptional sites-available/*.conf

I have kept a dummy html file inside /var/www/html/
Content of index.html:

    <!DOCTYPE html>
    <html>
    <body>
        <h1>Hello World!</h1>
    </body>
    </html>

When ever I hit http://localhost:80 the shell script is not called at all. The shell script is executable and it just prints “Hello World”.

But when I call apache kafka binary from 000-default.conf file then it works properly.

Modified 000-default.conf file:

    <VirtualHost *:80>
        ServerName localhost
        DocumentRoot /var/www/html/
        CustomLog "| [HOME_DIR_KAFKA]/bin/kafka-console-producer.sh --topic access-log --broker- list <Remote_host_ip>:9092" custom_user_tracking
    </VirtualHost>

Now when I click on http://localhost:80 then message is send in remote kafka server.

Can any one help here how can I call shell script from apache httpd?

2

Answers


  1. Chosen as BEST ANSWER

    Here is the content of send_message.sh:

    #! /bin/sh
    
    
    PRIVATE_KEY=$1
    HOST=$2
    TOPIC_NAME=$3
    MESSAGE=$4
    
    echo "OCI VM IP : $HOST"
    echo "PRIVATE KEY : $PRIVATE_KEY"
    echo "TOPIC-NAME: $TOPIC_NAME"
    echo "MESSAGE: $MESSAGE"
    
    ## Run Cmd in vm
    ssh -o "StrictHostKeyChecking no" -i $PRIVATE_KEY opc@$HOST /bin/bash << EOF
           sudo su
           echo $MESSAGE | [HOME_DIR]/bin/kafka-console-producer.sh --broker-list [VM_IP]:9092 --topic $TOPIC_NAME
    EOF 
    

    And content of /etc/httpd/sites-available/000-dafult.conf

    <VirtualHost *:80>
        ServerName localhost
        DocumentRoot /var/www/html/
        CustomLog "| /home/opc/send_message.sh /home/opc/dev2oci.key 100.111.69.61 access-log apache-httpd" custom_user_tracking
    </VirtualHost>
    
    

    The message is sent in topi access_log for every 5s interval. Though the http://localhost:80 is not triggered from browser.

    If I replace the CustomLog with below entry:

    CustomLog "| [HOME_DIR]/bin/kafka-console-producer.sh --topic access-log --broker-list [VM_IP]:9092" custom_user_tracking
    

    Then message is sent on triggering http://localhost:80 from browser.

    Can anyone let me know what I am missing here.


  2. Consider making the test.sh script executable using “shebang” ($!), instead of using sh.

    000-default.conf :
        CustomLog "| /scratch/user/test.sh" custom_user_tracking
    
    /scratch/user/test.sh   
        #! /bin/sh
        ...
    

    Alternatively run ‘sh’ using full path
    CustomLog “| /bin/sh /scratch/user/test.sh” custom_user_tracking

    Also, double check the execute permission on /scratch/user/test.sh, and the permission on the folder. https is usually running as non-privileged account.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search