How do I call a local shell script from a web server?

前端 未结 1 1788
野趣味
野趣味 2021-02-02 01:39

I am running Ubuntu 11 and I would like to setup a simple webserver that responds to an http request by calling a local script with the GET or POST parameters. This script (alre

相关标签:
1条回答
  • 2021-02-02 02:15

    This tutorial looks good, but it's a bit brief.

    I have apache installed. If you don't: sudo apt-get install apache2.

    cd /usr/lib/cgi-bin
    
    # Make a file and let everyone execute it
    sudo touch test.sh && chmod a+x test.sh 
    

    Then put the some code in the file. For example:

    #!/bin/bash
    # get today's date
    OUTPUT="$(date)"
    # You must add following two lines before
    # outputting data to the web browser from shell
    # script
     echo "Content-type: text/html"
     echo ""
     echo "<html><head><title>Demo</title></head><body>"
     echo "Today is $OUTPUT <br>"
     echo "Current directory is $(pwd) <br>"
     echo "Shell Script name is $0"
     echo "</body></html>"
    

    And finally open your browser and type http://localhost/cgi-bin/test.sh

    If all goes well (as it did for me) you should see...

    Today is Sun Dec 4 ...
    Current directory is /usr/lib/cgi-bin Shell
    Shell Script name is /usr/lib/cgi-bin/test.sh

    0 讨论(0)
提交回复
热议问题