1.安装apache2
sudo apt update
sudo apt install apache2
2.配置cgi
apache2所有的配置文件的目录在/etc/apache2/,进入该目录进行相关配置
第一步,进入sites-enabled 编辑000-default.conf,更改为如下所示
注意上图中有删除最下面一行注释,记得删除
第二步,进入mods-enabled目录,打开mime.load 文件,添加模块
第三步,进入conf-enabled,打开serve-cgi-bin.conf添加与修改图中一致
第四步,重启apache服务器
sudo /etc/init.d/apache2 restart
第五步,在/var/www/目录下新建cgi-bin文件夹,之后在cgi-bin中新建hello.c
sudo mkdir cgi-bin
在hello.c中添加以下代码
#include <stdio.h>
int main()
{
printf("Content-Type: text/html\n\n");
printf("Hello, world\n");
return 0;
}
输入下面的命令
sudo gcc -o hello.cgi hello.c
第六步,浏览器中输入localhost/cgi-bin/hello.cgi可以成功出现下面效果
有兴趣可以测试其他类型的脚本,本篇仅以.c文件为例