前端利用 CGI(公共网关接口)调用系统命令(Ubuntu / Debian)

检查apache是否具有cgi模块

cd /etc/apache2/mods-available
ls *cgi*      //使用此条命令能看到以下文件说明模块存在
cgid.conf  cgid.load  cgi.load

创建软连接

ln -s /etc/apache2/mods-available/cgi.load /etc/apache2/mods-enabled/cgi.load
ln -s /etc/apache2/mods-available/cgid.conf /etc/apache2/mods-enabled/cgid.conf
ln -s /etc/apache2/mods-available/cgid.load /etc/apache2/mods-enabled/cgid.load

查看cgi默认路径

cat /etc/apache2/conf-enabled/serve-cgi-bin.conf                
Define ENABLE_USR_LIB_CGI_BIN
Define ENABLE_USR_LIB_CGI_BIN
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Require all granted

指向自定义路径

ScriptAlias 为url访问链接,后面的就是cgi默认目录,这里可以自定义目录指向,/var/www/cgi-bin/

Define ENABLE_USR_LIB_CGI_BIN
Define ENABLE_USR_LIB_CGI_BIN
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Require all granted

创建目录和脚本文件并赋予执行权限  

cd /var/www/
mkdir cgi-bin 
touch interface.cgi
sudo chmod +x interface.cgi

重启apache使配置生效

service apache2 restart

编辑CGI脚本

cgi脚本可以是pl,可以是py,可以是bash,本例使用bash   

#!/bin/bash
echo "Content-type: text/html"
echo ""
echo "Bash as CGI"
echo ""

echo "General system information for host $(hostname -s)"
echo ""

echo "Memory Info"
echo " $(free -m) "

echo "Disk Info:"
echo " $(df -h) "

echo "Logged in user"
echo " $(w) "

echo "Information generated on $(date)"
echo ""

#!/bin/bash: bash文件头标识,必填

echo "Content-type: text/html": 发送html header,必填

echo "": 发送一个空白行,必填

通过浏览器访问测试查看结果

http://xxx.xxx.xxx.xxx/cgi-bin/interface.cgi

General system information for host jdu4e00u53f7

Memory Info

               total        used        free      shared  buff/cache   available
Mem:           2000         207        1225          31         567        1604
Swap:             0           0           0 
Disk Info:

 Filesystem      Size  Used Avail Use% Mounted on
udev            981M     0  981M   0% /dev
tmpfs           201M   24M  178M  12% /run
/dev/vda1        30G  2.2G   26G   8% /
tmpfs          1001M     0 1001M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs          1001M     0 1001M   0% /sys/fs/cgroup
tmpfs           201M     0  201M   0% /run/user/0 
Logged in user

  22:18:46 up 2 days, 22:51,  4 users,  load average: 0.00, 0.00, 0.00
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    183.213.241.211  20:30    1:45m  0.01s  0.01s -bash
root     pts/1    183.213.241.211  20:50    1:21m  0.12s  0.12s -bash
root     pts/2    183.213.241.211  21:17   49:25   0.06s  0.06s -bash
root     pts/3    183.213.241.211  22:02    7:27   0.02s  0.02s -bash

如何接受前端的参数

#!/bin/bash
echo "Content-type: text/html"
echo ""
echo "Method:$REQUEST_METHOD"
echo "Query:$QUERY_STRING"

以get方式访问  

http://xxx.xxx.xxx.xxx/cgi-bin/interface.cgi?type=student

返回结果  

Method:GET Query:type=student

这里也可以用ajax进行get,post提交,bash使用系统变量 $REQUEST_METHOD 和 $REQUEST_METHOD 来获取http请求方法以及参数  

 

上一篇: Linux配置SVN上篇

下一篇: Ubuntu Debian 下Apache开启ssl

分享

版权属于:破冰者博客

文章链接:https://ice-breaker.cn/post/21

转载时必须以链接形式注明原始出处及本声明。

表情