ubuntu下apache+C语言+CGI配置


一、安装及配置apache服务器

安装apache2服务器
#sudo apt-get install apache2

配置apache2服务器
配置文件位于 /etc/apache2/sites-enabled/000-default
用vi打开配置文件:
#sudo vi /etc/apache2/sites-enabled/000-default
修改其中两句为:
DocumentRoot /var/www/html
ScriptAlias /cgi-bin/ /var/www/html/cgi-bin/
注意其中空格的问题。

这两个目录你可以自己设定,并且设定的目录要真实存在,如果不存在就 mkdir 出一个。
DocumentRoot后面是放HTML文件的目录。
ScriptAlias后面是指定/cgi-bin/连接到/var/www/html/cgi-bin/ ,也就是在这个目录中的文件均被认作是cgi程序。
例如如果浏览器中访问 http://127.0.0.1/cgi-bin/hello.cgi 就可以执行cgi-bin下的hello.cgi
配置完保存。

重启apache2:
#sudo /etc/init.d/apache2 restart


二、编写hello.cgi程序

在cgi-bin/下新建一个hello.c文件:
#sudo vi hello.c
在其中写入以下内容:
#include <stdio.h>

int main()

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

printf("Hello, world\n");

return 0;
}
保存退出。

编译:
#sudo gcc hello.c -o hello.cgi
这里可以先运行一下
#sudo ./hello.cgi
看看是不是输出helloworld。

然后可以在浏览器中键入http://127.0.0.1/cgi-bin/hello.cgi
浏览器就可以显示出helloworld了。

三、权限问题

我的方法比较野蛮就是把所有相关的文件和目录的权限均设置为777
#sudo chmod 777 [文件名或目录]
在这里主要是先入个门,以后有时间在慢慢研究权限的问题。

<br>我在改造APACHE服务器授权访问时,需要对不合法的客户端请求进行过滤。对不合法请求需要立即发送一个错误提示页面给客户端。<br>发送错误提示页面的程序片断如下:<br> //非法请求作错误跳转<br> char *location = "/error/error.jsp";<br> r->status = HTTP_OK;<br> r->method = apr_pstrdup(r->pool, "GET");<br> r->method_number = M_GET;<br> ap_internal_redirect_handler(location, r);<br> //杀死子请求<br> ap_update_child_status(r->connection->sbh, SERVER_IDLE_KILL, r);<br>后来发现这样写有问题,以上代码对于没有启用mod_proxy的HTTP或HTTPS请求都是可以正确处理的。但是如果启用了mod_proxy功能后,就不会正确执行了。<br><br>于是我做了如下修改:<br> apr_table_setn(r->headers_out, "Http", "302");<br> //我们设置这个错误跳转到http://www.yahoo.com。<br> apr_table_setn(r->headers_out, "Location", "http://www.yahoo.com");<br> r->status = HTTP_TEMPORARY_REDIRECT; <br> //ap_send_error_response函数第二个参数设置为NULL(既0)<br> ap_send_error_response(r, 0); <br> //处理掉子请求<br> ap_update_child_status(r->connection->sbh, SERVER_IDLE_KILL, r);<br>就可以正确跳转了。<br>ap_send_error_response()在http_protocol.h定义为:<br>/**<br> * Send error back to client.<br> * @param r The current request<br> * @param recursive_error last arg indicates error status in case we get <br> * an error in the process of trying to deal with an ErrorDocument <br> * to handle some other error. In that case, we print the default <br> * report for the first thing that went wrong, and more briefly report <br> * on the problem with the ErrorDocument.<br> * @deffunc void ap_send_error_response(request_rec *r, int recursive_error)<br> */<br>AP_DECLARE(void) ap_send_error_response(request_rec *r, int recursive_error);<br><br>其实ap_send_error_response函数也可以向客户端发送一个指定的页面。设置好<br>apr_table_setn(r->headers_out, "Http", "302");<br>apr_table_setn(r->headers_out, "Location", "http://www.yahoo.com");<br>ap_send_error_response的第二个参数设为"NULL",他就跳转到"http://www.yahoo.com"了。<br>这是我看了ap_send_error_response函数源代码后发现的。<br><br><br>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值