經過CGI實如今Html頁面上執行shell命令

在mac上配置cgi(不用系統自帶的apache cgi.)

安裝cgi
1. brew update

2. brew install httpd24

  

安裝完後,會有以下提示html

DocumentRoot is /usr/local/var/www.shell

The default ports have been set in /usr/local/etc/httpd/httpd.conf to 8080 and in
/usr/local/etc/httpd/extra/httpd-ssl.conf to 8443 so that httpd can run without sudo.apache

To have launchd start httpd now and restart at login:
brew services start httpd
Or, if you don't want/need a background service you can just run:
apachectl start瀏覽器


打開apache
3. brew services start httpd

  

在瀏覽器輸入
localhost:8080

  

若是獲得 It’works,說明apache運行成功

brew安裝的apache默認端口是8080,mac自帶的apache默認端口是80bash

 

修改httpd.conf文件
添加兩行:

LoadModule cgi_module lib/httpd/modules/mod_cgi.so
LoadModule cgid_module lib/httpd/modules/mod_cgid.so

修改 <Directory "/usr/local/var/www/cgi-bin">標籤裏的內容以下,ExecCGI 表示在cgi-bin目錄下執行cgi腳本,全部的cgi腳本都須要放到改目錄下。
<Directory "/usr/local/var/www/cgi-bin">
    Options ExecCGI  
    AllowOverride None  
    Order deny,allow  
    Allow from all
</Directory>

取消下面三行的註釋:
AddHandler cgi-script .cgi 
AddType text/html .shtml
AddOutputFilter INCLUDES .shtml

在AddHandler cgi-script .cgi 後面添加支持的cgi腳本格式, 好比.sh .pl. 因爲我這裏用shell腳本,全部修改成
AddHandler cgi-script .cgi .sh

 

修改權限
chmod +x /usr/local/var/www
chmod +x /usr/local/var/www/cgi-bin
重啓服務器
brew services restart httpd

  

在 /usr/local/var/www/cgi-bin 下放置測試腳本delete.sh
#!/bin/bash
echo "Content-type: text/html"
echo ""

# ok, we've sent the header, now send some content
rm -f $QUERY_STRING
if [ "$?" -eq 0 ]; then
  echo "Deleted!"
else
  echo "Delete failed!"
fi

  須要給delete.sh 權限服務器

chmod +x /usr/local/var/www/cgi-bin/delete.sh
 

  在瀏覽器中打開curl

localhost:8080/cgi-bin/delete.sh?1.png

  或者用curlide

curl "localhost:8080/cgi-bin/delete.sh?1.png"
相關文章
相關標籤/搜索