? CGI (public gateway interface [CommonGatewayInterface]) defines the interaction method between the website server and external content negotiation program. It usually refers to CGI programs or CGI scripts, it is the simplest and most commonly used method for implementing dynamic pages on websites. This article introduces how to create CGI on the Apache website server and How to Write CGI programs. Configuration
? CGI (public Gateway Interface [Common Gateway Interface]) defines the interaction between the website server and the external content negotiation program. It usually refers to CGI programs or CGI scripts, it is the simplest and most commonly used method for implementing dynamic pages on websites. This article introduces how to create CGI on the Apache website server and How to Write CGI programs. Configuration
?
CGI (public Gateway Interface [Common Gateway Interface]) defines the interaction between the website server and the external content negotiation program. It usually refers to CGI programs or CGI scripts, it is the simplest and most commonly used method for implementing dynamic pages on websites. This article introduces how to create CGI on the Apache website server and How to Write CGI programs.
Configure Apache to allow CGI
To make the CGI program run properly, you must configure Apache to allow CGI execution. There are multiple methods.
ScriptAlias
The ScriptAlias command allows Apache to execute CGI programs in a specific directory. When the client requests resources in a specific directory, Apache assumes that all files in the directory are CGI programs and tries to run them.
The ScriptAlias command is shown as follows:
ScriptAlias/cgi-bin // usr/local/apache/cgi-bin/
If Apache is installed to the default location, the above configuration will be available in the default configuration file httpd. conf. The ScriptAlias command defines the URL prefix mapped to a specific directory. Similar to the Alias command, the two are generally used to specify directories outside the DocumentRoot directory, the difference is that ScriptAlias has another meaning, that is, any file in its URL prefix is considered as a CGI program. Therefore, the above example indicates that Apache,/cgi-bin/should point to the/usr/local/apache/cgi-bin/directory and regard it as a CGI program.
For example. Of course, this file must exist and be executable and output in a specific way. Otherwise, Apache returns an error message.
ScriptAliasCGI outside the Directory
For security reasons, CGI programs are usually restricted in the directory specified by ScriptAlias, so that the administrator can strictly control who can use CGI programs. However, if appropriate security measures are taken, there is no reason not to allow CGI programs in other directories to run. For example, you may want users to store pages in the home directory specified by UserDir, but they have their own CGI programs, but do not have access to the cgi-bin directory, the requirement to run CGI programs in other directories is generated.
Use Options to explicitly allow CGI execution
You can use the Options command in the configuration file of the master server to explicitly allow CGI execution in a specific directory:
Options + ExecCGI
The preceding command allows Apache to execute CGI files. In addition, you must tell the server which files are CGI files. The following AddHandler Command tells the server that all the files with cgi or pl suffixes are CGI programs:
AddHandler cgi-script. cgi. pl
Write CGI program
There are two major differences between compiling CGI programs and ''regular ''programs.
First, there must be a MIME-type header before all the output of the CGI program, that is, the HTTP header, indicating the type of the content received by the browser, in most cases, such:
Content-type: text/html
Secondly, the output must be in HTML format, or other forms that the browser can display. In most cases, the output is in the form of HTML, but the CGI program is occasionally written to output a GIF image or other non-HTML content.
Apart from these two points, writing CGI programs is roughly the same as writing other programs.
The first CGI program
This CGI program example prints a line of text in the browser. Save the followingfirst.plFile and put it in yourcgi-binDirectory.
#!/usr/bin/perl
print "Content-type: text/html/n/n";
print "Hello, World.";
Even if you are not familiar with the Perl language, you should be able to see what it has done. The first line tells Apache that this file can be used/usr/bin/perl(Or any shell you are using) Explain and execute. (Note: For windows: 1 download the Perl interpreter ActivePerl in Windows. Official Website: http://www.activestate.com /. My perl installation path is: E:/program/devel/perl/bin/perl.exe, so change it to this path. Otherwise, apache cannot find the program interpreter and reports Error 500 .)
The second line prints the description of the content type required above, with two line breaks. Leave blank lines behind the header to show the end of the HTTP header. The third line prints the text ''hello, World .''. The program ends here.
Open your favorite browser and enter the address:
http://www.example.com/cgi-bin/first.pl
Or another location where you store the program, you can see a line in the browser windowHello, World.. Although not so excited, once the program works properly, it may run any other program.
There are some test examples in the apache/cgi-bin/directory. We may use it to test whether the configuration can run normally.
For example, the apache/cgi-bin/directory contains a test-cgi program. The source code is as follows:
#! /Bin/sh
# Disable filename globbing
Set-F
Echo"Content-type: text/plain; charsets = iso-8859-1"
Echo
EchoCGI/1.0TestScript report:
Echo
EchoArgc is $ #. argv is "$ *".
Echo
EchoSERVER_SOFTWARE = $ SERVER_SOFTWARE
EchoSERVER_NAME = $ SERVER_NAME
EchoGATEWAY_INTERFACE = $ GATEWAY_INTERFACE
EchoSERVER_PROTOCOL = $ SERVER_PROTOCOL
EchoSERVER_PORT = $ SERVER_PORT
EchoREQUEST_METHOD = $ REQUEST_METHOD
EchoHTTP_ACCEPT = "$ HTTP_ACCEPT"
EchoPATH_INFO = "$ PATH_INFO"
EchoPATH_TRANSLATED = "$ PATH_TRANSLATED"
EchoSCRIPT_NAME = "$ SCRIPT_NAME"
EchoQUERY_STRING = "$ QUERY_STRING"
EchoREMOTE_HOST = $ REMOTE_HOST
EchoREMOTE_ADDR = $ REMOTE_ADDR
EchoREMOTE_USER = $ REMOTE_USER
EchoAUTH_TYPE = $ AUTH_TYPE
EchoCONTENT_TYPE = $ CONTENT_TYPE
EchoCONTENT_LENGTH = $ CONTENT_LENGTH
Enter http: // 127.0.0.1/cgi-bin/test-cgi in the browser.
If set correctly, the following information is displayed:
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 =
Of course, you can also choose to write a cgi program in other languages. apache provides an example of writing with pear.
Below is a simple example written in C language.
# Include
# Include
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");
Printf ("c = % d", c );
Printf ("% s", p );
Return 0;
}
?
Function: print the value of a + B. If the get parameter exists, print the get parameter.