【问题标题】:Unable to call shell script from apache httpd无法从 apache httpd 调用 shell 脚本
【发布时间】:2019-12-02 17:17:01
【问题描述】:

我正在尝试从 /etc/httpd/sites-available 中的 000-default.conf 调用 shell 脚本。

000-default.conf 文件内容为:

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

我的 /etc/httpd/conf/httpd.conf 添加了以下内容:

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

我在 /var/www/html/ 中保留了一个虚拟 html 文件 index.html的内容:

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

当我点击http://localhost:80 时,根本不会调用 shell 脚本。 shell 脚本是可执行的,它只打印“Hello World”。

但是当我从 000-default.conf 文件中调用 apache kafka 二进制文件时,它可以正常工作。

修改 000-default.conf 文件:

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

现在,当我点击 http://localhost:80 时,消息会发送到远程 kafka 服务器。

任何人都可以在这里帮助我如何从 apache httpd 调用 shell 脚本?

【问题讨论】:

    标签: apache shell


    【解决方案1】:

    考虑使用“shebang”($!) 来使 test.sh 脚本可执行,而不是使用 sh。

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

    或者使用完整路径运行“sh” CustomLog "| /bin/sh /scratch/user/test.sh" custom_user_tracking

    另外,请仔细检查 /scratch/user/test.sh 的执行权限以及文件夹的权限。 https 通常作为非特权帐户运行。

    【讨论】:

    • 感谢您的回答。现在从 conf 文件调用脚本。
    • 出于好奇,您使用了哪种方法? ('#!' 行,或 /bin/sh)
    • 现在我面临另一个问题。每隔 5 秒会自动调用 shell 脚本。理想情况下,当我们点击localhost:80 时,应该调用该脚本。但它以 5 秒的间隔在内部调用。我已经验证了 /etc/crontab 并且它包含以下内容: SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root
    • 考虑共享“test.sh”。是否符合自定义日志指令的要求?
    • 这里是/etc/httpd/sites-available/000-dafult.conf的内容
    【解决方案2】:

    这里是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 
    

    和/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>
    
    

    每 5s 间隔在 topi access_log 中发送消息。虽然http://localhost:80 不是从浏览器触发的。

    如果我将 CustomLog 替换为以下条目:

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

    然后在从浏览器触发http://localhost:80 时发送消息。

    谁能告诉我我在这里缺少什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-29
      • 1970-01-01
      • 2012-03-13
      • 2016-05-27
      • 2013-07-10
      • 1970-01-01
      • 2016-06-22
      相关资源
      最近更新 更多