------------------------实验一 http 配置 可执行 CGI 脚本--------------------
1、装载相关模块(可写在/etc/httpd/conf/httpd.conf 里也可写在虚拟主机配置文件里)
LoadModule cgi_module modules/mod_cgi.so #cgi相关模块
LoadModule alias_module modules/mod_alias.so #别名相关模块
2、虚拟主机配置文件加以下 cgi和别名 部分内容。
说明:别名/cgi-bin/只在当前站点有效,目录不一定要在站点目录下
<VirtualHost *:80>
DocumentRoot "/data/www/class.com"
ServerName www.class.com
ErrorLog "/data/www/class.com/logs/www.class.net-error_log"
CustomLog "/data/www/class.com/logs/www.class.net-access_log" common
#授权站点目录
<Directory "/data/www/class.com">
Options None
AllowOverride None
Require all granted
</Directory>
#-----------cgi脚本-(重点)--------
<Directory "/var/www/cgi-bin">
AllowOverride None
SetHandler cgi-script #必写
Options ExecCGI #必写
Require all granted #授权所有人能访问
</Directory>
#给脚本目录一个别名
<IfModule alias_module>
ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>
</VirtualHost>
3、在 /var/www/cgi-bin/下创建cgi脚本 : vim /var/www/cgi-bin/a.html
#!/bin/sh
echo Content-type:text/plain
#以上为固定字段,必写,下面为你的shell指令
netstat -ntl
4、赋予脚本执行权限。( 确保该文件必须允许 apache 用户 来执行)
chmod +x /var/www/cgi-bin/a.html
5、重新装载服务 配置文件。
systemctl reload httpd
6、客户端浏览器测试:
访问: http://www.class.com/cgi-bin/a.html
实验总结:
主要用到虚拟主机配置、cgi脚本配置与编写、站点下目录别名配置
---------实验2:具有传递参数功能的, CGI 脚本示例--------------------
在上个实验基础上完成
1、站点目录下创建abc.html
vim /data/www/class.com/cat abc.html
<html>
<body>
<h1>欢迎访问用户查询系统</h1>
<A HREF="http://www.class.com/cgi-bin/a.html"> 显示当前的端口信息 </A>
<form action="http://www.class.com/cgi-bin/search.html" method="get">
<p>请输入账号: <input type="text" name="fname1" /></p>
<p>亲输入密码: <input type="text" name="fname2" /></p>
<input type="submit" value="查询" />
</form>
</body>
</html>
2、在cgi脚本目录下创建新脚本search.html(编写完记得授予执行权限)
vim /var/www/cgi-bin/search.html
#!/bin/bash
echo Content-type:text/plain
echo
user=`echo "$REQUEST_URI" | awk -F "=|&" '{print $2}'`
passd=`echo "$REQUEST_URI" | awk -F "=|&" '{print $4}'`
echo "账号:$user"
echo "密码:$passd"
echo ""
echo ""
echo "==============还可以通过 env 命令 当前其余的 apache 提供的其它的 环境变量列表: ======================="
env
3、检测结果:
实验总结:
在上个实验基础上,从一个界面get请求到一个cgi脚本,cgi脚本可用env变量得到请求的一些列参数,包含表单信息