于一 发表于 2018-11-21 13:40:20

Apache配置实现cgi

  装完Apache,需要修改apache的配置文件:
  nano /etc/httpd/conf/httpd.conf
  配置文件中有
  ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"这个指令,指示了默认的cgi-bin的路径。
  /var/www/cgi-bin/,在配置文件中也可以看到。
  还需要配置
  AddHandler cgi-script .cgi .pl
  该配置默认被注释掉了,设置了cgi的后缀名
  编写一个cgi文件,内容如下:
#!/bin/bash
echo "Content-type: text/html"
echo ""
echo "Hello World"  注意,该文件必须是要UNIX文件格式的,可以editpro等工具编写。将该文件命名为first.cgi,然后放入/var/www/cgi-bin/目录中。
  然后运行http://localhost:9000/cgi-bin/first.cgi,可以发现,有错误

  有错误不可怕,默认的httpd的错误日志在/var/log/httpd/error_log里可以看到,也可以去http的软连接中访问/etc/httpd/logs/error_log
  打开error_log文件,可以看到如下的错误:
   (13)Permission denied: exec of '/var/www/cgi-bin/first.cgi' failed
   Premature end of script headers: first.cgi
  说明了权限被拒绝了。将first.cgi的权限设置成755
  chmod 755 /var/www/cgi-bin/first.cgi
  再次运行http://localhost:9000/cgi-bin/first.cgi,发现就正常显示了
  同样的,可以编辑一个perl文件,也可以以cgi的方式运行
#!/usr/bin/perl
print "Content-type: text/html\n\n";
print "Hello, World.";  




页: [1]
查看完整版本: Apache配置实现cgi