Linux下Apache配置cgi

时间:2021-01-05 19:11:26

基本的配置步骤我就不说了,谈谈我遇到的问题,分享一些比较有用的解决办法。

步骤一:按教程搭建

搭建简易的c语言CGI和Apache服务器的开发环境

上面这篇文章,不仅有相应的步骤,而且还分享了源代码,比较实在。

步骤二:碰到Internal Server Error错误,并试图解决

在按照要求配置了之后,启动服务器测试,发现服务器提示如下的错误,相信很多初次配置的人对这个界面很讨厌。

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, you@example.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.


Apache/2.0.64 (Unix) Server at ***.***.***.*** Port 80

步骤三:查找错误一Permission denied并解决

  1. 查看Linux下apache的一般安装目录为:/usr/local/apache2/bin
  2. 查看错误日志的说明:/usr/local/apache2/logs/error_log
  3. 提示如下错误
    [error] (13)Permission denied: exec of /usr/local/apache2/cgi-bin/test-cgi failed
    [error] [client xxx.xxx.xxx.xxx] Premature end of script headers: 
原因是从客户端上传到服务器上的可执行文件没有执行权限。
-rwxr----- 1 root   root     160 04-28 10:04 filename
解决办法:chmod 755 filename ==============修改过后=============== -rwxr-xr-x 1 root   root     112 04-29 12:29 junk.pl
-rwxr-xr-x 1 root   root   31900 03-04 15:49 libAVAPIs.so
-rwxr-xr-x 1 root   root   72876 03-04 15:49 libIOTCAPIs.so
-rwxr-xr-x 1 root   root   18056 03-04 15:49 libRDTAPIs.so
-rwxr----- 1 root   root     160 04-28 10:04 Makefile
-rw-r--r-- 1 c7user c7user   294 2005-02-04 printenv
-rwxr-xr-x 1 root   root    8295 04-28 11:07 SetPass
-rw-r--r-- 1 root   root    4389 04-28 13:53 setpass.c
-rwxr-xr-x 1 c7user c7user   779 2005-02-04 test-cgi

步骤四:查找错误二error while loading shared libraries并解决

在错误日志中查看,提示如下:

[Mon Apr 29 20:00:41 2013] [error] [client 222.201.146.139] /usr/local/apache2/cgi-bin/SetPass: error while loading shared libraries: libIOTCAPIs.so: cannot open shared object file: No such file or directory

这个问题有两个解决办法:

error while loading shared libraries: xxx.so.x"错误的原因和解决办法 

请记住,在把***.so上传到/usr/local/lib后,记得修改so文件的执行权限(chmod 755 filename)

步骤五:查找错误三Premature end of script headers并解决

在调用cgi后首先输出:Content-type:text/html\n\n

步骤六:调用cgi带有参数

http://121.14.119.122:8090/cgi-bin/test-cgi?we+wwerq+1234

输出如下:

CGI/1.0 test script report:

argc is 3. argv is we wwerq 1234.

SERVER_SOFTWARE = Apache/2.0.64 (Unix)
SERVER_NAME = 121.14.119.122
GATEWAY_INTERFACE = CGI/1.1
SERVER_PROTOCOL = HTTP/1.1
SERVER_PORT = 8090
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 = we+wwerq+1234
REMOTE_HOST =
REMOTE_ADDR = 222.201.146.139
REMOTE_USER =
AUTH_TYPE =
CONTENT_TYPE =
CONTENT_LENGTH =

参考文献:

Perl CGI Debugging: Solving a 500 Internal Server Error

Running CGI's from User Home Directories