遥想当年,我也是写过CGI hello world的

不知何年何月某个月黑风高的晚上,我突然想入手一下CGI,于是乎...,,一天快过去了,我确实只是写了个hello world

从此以后就没有以后了。

 

yum install httpd

httpd /? 看帮助

重要目录

/etc/httpd/conf..,配置

/var/log/http.. 日志

/var/www/cgi-bin/ cgi目录

官方 值得看看

https://httpd.apache.org/docs/2.4/howto/cgi.html

 

# 不能-k 参数 ,如重启httpd -k restart

httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for ServerName

解决办法非常简单:

#nano /usr/local/apache2/conf/httpd.conf

找到#ServerName www.example.com:80   把#去掉,再重启apache即可没事了。

 

# 测试 ,这是官方的例子

first.pl

#!/usr/bin/perl print "Content-type: text/html\n\n"; print "Hello, World.\n";

 

# 加权限

[root@localhost cgi-bin]chmod a+x first.pl

[root@localhost cgi-bin]# ./first.pl

Content-type: text/html

 

Hello , World ,cgi perl

[root@localhost cgi-bin]#

 

# curl 测试

[root@localhost cgi-bin]# curl http://localhost/cgi-bin/first.pl

Hello , World ,cgi perl

[root@localhost cgi-bin]#

 

# 想当然来了一段,但为什么用c就不行了呢?

#include <stdio.h>

int main(){

printf("Content-type:text/html\n\n");

printf("hello\n");

return 0;

}

 

[root@localhost cgi-bin]# curl http://localhost/cgi-bin/hello.cgi

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

<html><head>

<title>500 Internal Server Error</title>

 

[root@localhost cgi-bin]# cat /var/log/httpd/error_log

[Mon Jun 26 21:50:41.122497 2017] [cgi:error] [pid 2836] [client ::1:60650] Premature end of script headers: hello.cgi

[Mon Jun 26 21:54:18.561903 2017] [cgi:error] [pid 2832] [client ::1:60652] Premature end of script headers: hello.cgi

 

# 所以关键时候看官方文档

网上查了那么多例子都是perl的,其中虽然有很多c或c++写的,但是都报错Premature end of script,

官方对于错误描述也写的很清楚,再次提醒,把官方的文档看完https://httpd.apache.org/docs/2.4/howto/cgi.html

想来想去,还是觉得输出有问题,关于CGI,其实有一套规范的,不可能就这么简单的printf content-type..这样就可以了

 

 

[root@localhost cgi-bin]# cat hello.c

#include <stdio.h>

int main(){

printf("%c%c",13,10);

printf("%s%c%c","<meta Content-Type:text/html;charset=UTF-8 />",13,10);

printf("<TITLE>title</TITLE> \n");

printf("<HTML>cgi impl form c or c++</HTML> \n");

return 0;

}

[root@localhost cgi-bin]#

 

[root@localhost cgi-bin]# curl http://localhost/cgi-bin/hello.cgi //看结果,大功告成

<meta Content-Type:text/html;charset=UTF-8 />

<TITLE>title</TITLE>

<HTML>cgi impl form c or c++

[root@localhost cgi-bin]#

 

# 图片

 

 

 

# 搞完linux,再搞window啊,尽然输出是的文本?

 

# 不妥协, 继续折腾

配置

<Directory "F:/Apache2.2/cgi-bin">

Options +ExecCGI

AddHandler cgi-script .cgi .exe

AllowOverride None

Order allow,deny

Allow from all

</Directory>

 

 

 

还不错

 

# cgic

用纯c写太麻烦,可以用CGIC框架啊

linux centos7 中测试 ,关防火墙

 

官方有现成的例子,先跑起来,

下载 wget Obtain cgic: gzipped tar file

解压 [root@localhost cgic207]# tar -xvf cgic207.tar.gz

编译 [root@localhost cgic207]# make cgictest.cgi

效果不错吧,感觉不错啊

[root@localhost cgic207]# pwd

/var/www/cgi-bin/cgic207

 

# 结论

对于大部分的博客,误人子弟,浪费了多少人的时间啊

就这么简单的几行代码,搞了快一天,结果一篇好的博客就搞定了,那些所谓的实验,浪费我太多时间,我还以为是window不行,转到linux总算调通了,并不是window不行

也有开源的C包写CGI哦

https://boutell.com/cgic/#obtain //此网站还有很多其它开源的

--------------------------我还是那个漂亮的分割线------------------------

我还是曾经那个少年

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值