环境说明
system:Linux luogw-pc 3.5.0-48-generic #72~precise1-Ubuntu SMP Tue Mar 11 20:09:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
Server version: Apache/2.2.22 (Ubuntu)
Server built: Apr 17 2014 21:49:25
第一步:确认apache2服务器是否开启了CGI
查看相关资源发现默认开启着
/etc/apache2/sites-enabled/000-default 有如下内容:
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>
意味着把CGI程度放在/ usr/lib/cgi-bin/目录下即可,然后通过(本地)http://localhost/cgi-bin/脚本名的url去访问
注:在/usr/lin/cgi-bin下看到一个gsearch.cgi
写个demo试试 test.pl
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Hello, World.";
oh no , 没看到熟悉的Hello, World
本能反应,脚本没有执行权限?,do it (sudo chmod +x test.pl)
try again, ok got it
第二步,尝试python版的Hello, World
test.py
#! /usr/bin/python
print "Content-type: text/html\n\n"
print "python : "
print "Hello, World."
run it
调试:
查看 /var/log/apache2下的log文件: 如 error.log
总结
1.ubuntu 12.04 下通过apt-get install 安装的apache2默认是开启了CGI,CGI脚本存放的路径是 /usr/lib/cgi-bin/不需要自定义配置,可以开始简单的学习
2.访问脚本的url 形式是主机域名或IP/cgi-bin/脚本名称
3.脚本需要可执行权限
参考资料
http://apache.jz123.cn/howto/cgi.html