apache cgi程序的简单配置

首先添加虚拟主机

<virtualhost 127.0.0.1:50001>
</virtualhost>

这里新添加了50001端口来进行监听,所以还需要添加监听端口号

Listen 50001

要让程序能正常运行,还得通过配置ScriptAlias来允许服务器在指定的情况下,以CGI方式运行。

<virtualhost 127.0.0.1:50001>
ScriptAlias /cgi-bin/ /usr/local/ebserver/apache/cgi-bin/
</virtualhost>

所以上述的配置会告诉apache,所以以/cgi-bin/开头的资源都会被映射到/usr/local/webserver/apache/cgi-bin/目录下,并被认为是cgi程序。
然后重启服务器
在apache/cgi-bin/目录里有一些测试例子,我们可能用它来测试下,看看配置是否能正常运行。
比如说apache/cgi-bin/目录下有个test-cgi程序,源码如下:

#!/bin/sh
 
# disable filename globbing
set -f
 
echo "Content-type: text/plain; charset=iso-8859-1"
echo
 
echo CGI/1.0 test script report:
echo
 
echo argc is $#. argv is "$*".
echo
 
echo SERVER_SOFTWARE = $SERVER_SOFTWARE
echo SERVER_NAME = $SERVER_NAME
echo GATEWAY_INTERFACE = $GATEWAY_INTERFACE
echo SERVER_PROTOCOL = $SERVER_PROTOCOL
echo SERVER_PORT = $SERVER_PORT
echo REQUEST_METHOD = $REQUEST_METHOD
echo HTTP_ACCEPT = "$HTTP_ACCEPT"
echo PATH_INFO = "$PATH_INFO"
echo PATH_TRANSLATED = "$PATH_TRANSLATED"
echo SCRIPT_NAME = "$SCRIPT_NAME"
echo QUERY_STRING = "$QUERY_STRING"
echo REMOTE_HOST = $REMOTE_HOST
echo REMOTE_ADDR = $REMOTE_ADDR
echo REMOTE_USER = $REMOTE_USER
echo AUTH_TYPE = $AUTH_TYPE
echo CONTENT_TYPE = $CONTENT_TYPE
echo CONTENT_LENGTH = $CONTENT_LENGTH

然后在浏览器里输入http://127.0.0.1/cgi-bin/test-cgi
如果设置正确就能显示如下信息

CGI/1.0 test script report:
 
argc is 0. argv is .
 
SERVER_SOFTWARE = Apache/2.2.10 (Unix) PHP/5.2.8
SERVER_NAME = 127.0.0.1
GATEWAY_INTERFACE = CGI/1.1
SERVER_PROTOCOL = HTTP/1.1
SERVER_PORT = 50001
REQUEST_METHOD = GET
HTTP_ACCEPT = text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
PATH_INFO = 
PATH_TRANSLATED = 
SCRIPT_NAME = /cgi-bin/test-cgi
QUERY_STRING = 
REMOTE_HOST =
REMOTE_ADDR = 127.0.0.1
REMOTE_USER =
AUTH_TYPE =
CONTENT_TYPE =
CONTENT_LENGTH =

当然你也可以选择用其它语言来写一个cgi程序,其中apache提供了一个用pear写的例子。
下面是用C语言写的一个简单的例子。

#include <stdlib .h>
#include <stdio .h>
int main (int argc, char** argv) {
    char a[] = "-100";
    char b[] = "456";
    int c;
    c = atoi(a) + atoi(b);
    char *p;
    p = getenv("QUERY_STRING");
    printf("Content-type: text/html\n\n");
    printf("c=%d",c);
    printf("%s",p);
    return 0;
}
</stdio></stdlib>

实现功能为:打印打印a+b的值,如果有get参数,同时打印get参数
编译

hqlong@ubuntu:/usr/local/webserver/apache/cgi-bin$ gcc test.c -o test

这时我们可以通过http://127.0.0.1:50001/test来访问了。
如果通过http://127.0.0.1:50001/test?name=hqlong
我们的程序就会打印name=hqlong这个查询参数。
参考资料:http://doc.chinahtml.com/Manual/ApacheManual/howto/cgi.html
机器人 2009-05-05 17:52 于 北京

此条目发表在 c/c++, linux/server 分类目录,贴了 , , , 标签。将固定链接加入收藏夹。

apache cgi程序的简单配置》有 93 条评论

  1. Pingback 引用通告: Dubstep Software

  2. Pingback 引用通告: lower blood pressure naturally

  3. Pingback 引用通告: businesses for sale

  4. hp mini 210 说:

    I know you probably have to disagree, but if you can’t beat them, join them.

  5. Randy 说:

    Terribly nice site your site, I prefer it, mind if I send your link to a couple friends?

  6. I am not sure where you are getting your info, but good topic. I needs to spend some time learning much more or understanding more. Thanks for fantastic information I was looking for this info for my mission.

  7. Pingback 引用通告: Las Vegas Strip Hotels

  8. Pingback 引用通告: minneapolis art galleries

  9. Pingback 引用通告: scrapbook projects

  10. Pingback 引用通告: toddcardin.com

  11. Pingback 引用通告: toddcardin.org

  12. Pingback 引用通告: toddcardin.info

  13. Pingback 引用通告: toddcardin.net

  14. Pingback 引用通告: Cronograph Watches

  15. Pingback 引用通告: Certified Diamonds Jewelry

  16. Pingback 引用通告: Limo service seattle

  17. Pingback 引用通告: rhinestone jewelry

  18. site 说:

    Mate. This blog site is amazing. How do you make it look like this .

  19. An interesting blog post right there mate ! Thank you for that .

  20. this month 说:

    An cool post right there mate . Cheers for it .

  21. I like the blog layout ! How do you make it. It is really nice!

  22. Just to let you know your webpage looks a little bit different on Safari on my computer using Linux .

  23. I adore the blog layout ! How did you make it? It is really good.

  24. account 说:

    It appears to me that this web site doesnt download in a Motorola Droid. Are other folks getting the same problem? I like this web site and dont want to have to miss it whenever Im gone from my computer.

  25. 18k gold ring 说:

    Hey there, I just hopped over to your web page through StumbleUpon. Not somthing I might normally read, but I appreciated your thoughts none the less. Thank you for creating something worth browsing.

  26. If you dont mind, where do you host your web site? I am searching for a great web host and your webpage appears to be quick and up just about all the time

  27. Jesus Christ theres plenty of spammy feedback on this web site. Have you ever before thought about trying to remove them or installing a plugin?

  28. Great Stuff, do you have a myspace profile?

  29. Jesus Christ theres a lot of spammy feedback on this website. Have you actually thought about attempting to eliminate them or installing a tool?

  30. I was curious about if you ever considered switching the page layout of your site? It is well written; I enjoy what youve got to state. But maybe you could add a a bit more in the way of content so people might connect with it better. Youve got an awful lot of text for only having one or two photographs. Maybe you could space it out better?

  31. Do youve a spam issue on this site; I also am a blogger, and I was questioning your scenario; we now have produced some great approaches and were looking to trade options with other individuals, be certain to fire me an e-mail if interested.

  32. Could you email me with a few tips on how you made your blog look this awesome, I would be appreciative!

  33. Odd , your site shows up with a black hue to it, what shade is the primary color on your web site?

  34. like i said 说:

    Could you message me with any hints & tips on how you made your blog look this awesome, I would be thankful.

  35. I feel one of your current ads triggered my internet browser to resize, you might well want to set up that on your blacklist.

  36. Have you considered adding some videos to your article? I think it will really enhance viewers understanding.

  37. this month 说:

    How come you dont have your site viewable in wap format? cant see anything in my Droid.

  38. Have you given any consideration at all with converting your current webpage into French? I know a small number of translaters right here that will would help you do it for free if you wanna make contact with me.

  39. A cool blog post there mate ! Cheers for it !

  40. I enjoy your wordpress format, where did you get a hold of it?

  41. Good Stuff, do you currently have a facebook account?

  42. An cool post right there mate ! Cheers for posting !

  43. This is often a wonderful weblog, would you be interested in going through an interview concerning just how you produced it? If so e-mail me and my friends!

  44. Jesus Christ theres a great deal of spammy comments on this webpage. Have you actually believed about attempting to remove them or putting in a tool?

  45. official link 说:

    very useful material, all round I imagine this is worthy of a bookmark, thanks

  46. After I open up your Feed it seems to be a lot of garbage, is the problem on my side?

  47. Have you considered including some social bookmarking buttons to these blogs. At the very least for bebo.

  48. I have got one suggestion for your site. It looks like there are a few cascading stylesheet troubles when opening a selection of web pages inside google chrome and internet explorer. It is operating okay in internet explorer. Perhaps you can double check this.

  49. Do youve a spam problem on this website; I also am a blogger, and I was asking yourself your situation; we have developed some great approaches and we are searching to exchange solutions with other folks, be sure to shoot me an e-mail if interested.

  50. While I truly like this post, I think there was an punctuational error shut to the finish of the third sentence.

  51. I was curious about if you ever considered switching the page layout of your website? It is well written; I really like what you have got to state. But maybe you could create a little more in the way of content so people could connect with it better. Youve got a great deal of wording for only having one or two images. Maybe you can space it out better?

  52. Weird , this post turns up with a dark color to it, what shade is the primary color on your web site?

  53. Have you thought about adding some relevant links to your article? I think it will really enhance everyones understanding.

  54. I believe that one of your current advertisements caused my web browser to resize, you might well want to set that on your blacklist.

  55. article 说:

    I believe that one of your current ads caused my internet browser to resize, you might well want to place that on your blacklist.

  56. Nearly all of the comments on this site dont make sense.

  57. silver hair 说:

    I had been curious about if you ever considered replacing the design of your site? Its very well written; I love what you have got to state. But maybe you could include a little more in the way of written content so people could connect with it better. Youve got an awful lot of wording for only having one or two pictures. Maybe you could space it out better?

  58. sponsor 说:

    Do youve a spam issue on this internet site; I also am a blogger, and I was asking yourself your situation; we have created some great approaches and we are searching to exchange solutions with other folks, be certain to fire me an e-mail if serious.

  59. wall art 说:

    When I originally commented I clicked the Notify me whenever new comments are added checkbox and currently every time a comment is added I get four email messages with the same comment.

  60. Pingback 引用通告: toms shoes ukThe Layers Of Your Brand - Page 13 - VW GTI Forum / VW Rabbit Forum / VW R32 Forum / VW Golf Forum - ozGTI.com

  61. payday fast loans online 说:

    I’ve read enough of your infantile questions to know who hates who.

  62. UBAHbIG 说:

    Пришла пора поменять ремень и ролики ГРМ.
    Стоимость оригинальных запчастей ощутимо высока. Аналогов полно, что очевидно отлично.
    И поскольку запланировал после продать тачку – ощутимо высокое качество не нужно.
    По цене значительно дешевле — детали TSN – хрен его знает, прежде не использовал,
    в описании упоминают что производитель РФ.
    Может кто чего знает? После замены как работают? Или как бы слегка доплатить?
     
    Звиняю, коли вдруг не туда разместил.. не разобрался куда запостить…

  63. apache cgi程序的简单配置 | hqlong’s blog

  64. I as well as my pals ended up checking the nice tricks found on the website and then suddenly I got a terrible feeling I had not thanked the blog owner for those strategies. The guys became as a consequence stimulated to see all of them and now have in fact been tapping into these things. Thanks for truly being simply accommodating and then for choosing certain great ideas millions of individuals are really needing to discover. Our own sincere apologies for not expressing appreciation to you earlier.

  65. steroids 说:

    interesting article but I’m not sure if the author is correct here.want ot learn about our website you should visit our website we are the top leader in the interest industry of online information and planning of ph@rma stuff, go to us

  66. I every time spent my half an hour to read this blog’s
    articles or reviews every day along with a cup of coffee.

  67. Admiring the dedication you put into your blog and detailed information you offer.
    It’s awesome to come across a blog every once in a while that isn’t the same out of date rehashed information.
    Great read! I’ve saved your site and I’m adding your RSS feeds to my Google account.

  68. You will get plenty of ways to acquire these coupons.
    Several services including most major banks offer online budget tracking.
    Ever as it opened in 1946, Kohl’s supermarket has offered great shopping
    chances to males and females of ages and sizes.

  69. 于承究眈 说:

    杭州最好的夜场是杭州东方魅力www.dongfangmeili.cn

    杭州最好的ktv是哪儿?是杭州东方魅力www.dongfangmeili.cn

    杭州夜场招聘首选杭州东方魅力www.dongfangmeili.com.cn

    杭州最好的夜总会是杭州东方魅力www.dongfangmeili.com.cn

    杭州东方魅力官方网站欢迎你 联系电话:13666653761 官方微信:dongfangmeili

  70. I’m no longer positive the place you’re getting your info, but great
    topic. I needs to spend some time finding out more
    or working out more. Thank you for wonderful information I used to be in search of this information for my mission.

  71. Thank you for some other wonderful article. The place else may anybody get that kind of info in such an ideal method of writing?
    I have a presentation subsequent week, and I
    am at the search for such information.

  72. Weight Loss 说:

    I have been browsing online more than 3 hours these days, yet I never discovered any attention-grabbing article like
    yours. It’s pretty worth enough for me. In my view, if all
    webmasters and bloggers made good content material as you probably did, the net
    might be a lot more helpful than ever before.

  73. 江向汾甚 说:

    杭州最好的夜场是杭州东方魅力www.dongfangmeili.cn
    杭州最好的ktv是哪儿?是杭州东方魅力www.dongfangmeili.cn

    杭州夜场招聘首选杭州东方魅力www.dongfangmeili.com.cn

    杭州最好的夜总会是杭州东方魅力www.dongfangmeili.com.cn

    杭州东方魅力官方网站欢迎你 联系电话:13666653761 官方微信:dongfangmeili
    杭州最好的ktv是www.dongfangmeili.cn
    杭州夜场招聘杭州夜场招聘www.dongfangmeili.cn

  74. il connettore dell’hard disk non

  75. Intriguing post. Positive that I

  76. Habibi 说:

    Very great post. I simply stumbled upon your weblog and wished to mention that I have truly enjoyed surfing around your blog posts. In any case I

  77. Habibi 说:

    Hello. excellent job. I did not anticipate this. This is a excellent story. Thanks!

  78. Habibi 说:

    But wanna admit that this is extremely valuable, Thanks for taking your time to write this.

  79. Great video, Fun and succinctly explained.. Will use it an introduction when I am giving talks to genealogists and historians on blogging

  80. 孔呢角圭 说:

    杭州东方魅力官方博客http://donfangmeili.qzone.qq.com

    欢迎大家积极光顾

    杭州东方魅力官方网址:www.dongfangmeili.cn

  81. Habibi 说:

    I was reading some of your blog posts on this website and I believe this web site is very informative! Continue putting up.

  82. Somebody necessarily help to make seriously articles I would state. That is the first time I frequented your website page and so far? I surprised with the research you made to create this particular post extraordinary. Wonderful process!

  83. eduzwsqp 说:

    ve5cdtj0

    t40xc9hg

    insurance

    zmm11n4n

    wec7y08a

  84. 侯姒分征 说:

    杭州最好的夜场是杭州东方魅力www.dongfangmeili.cn

    杭州最好的ktv是哪儿?是杭州东方魅力www.dongfangmeili.cn

    杭州夜场招聘首选杭州东方魅力www.dongfangmeili.com.cn

    杭州最好的夜总会是杭州东方魅力www.dongfangmeili.com.cn

    杭州东方魅力官方网站欢迎你 联系电话:13666653761 官方微信:dongfangmeili

  85. Pingback 引用通告: best halloween creative ideas and horror artwork

  86. Pingback 引用通告: free download

  87. Pingback 引用通告: This is why this video will make you like Indie Euro Rock again!

  88. Pingback 引用通告: ingray band music videos live

发表评论

电子邮件地址不会被公开。 必填项已用*标注

您可以使用这些HTML标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>