在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
修改 标签里的内容以下,ExecCGI 表示在cgi-bin目录下执行cgi脚本,全部的cgi脚本都须要放到改目录下。
Options ExecCGI
AllowOverride None
Order deny,allow
Allow from all
取消下面三行的注释:
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"