Apache > HTTP Server > Documentation > Version 2.4 > How-To / Tutorials

Hġ 丮: CGI ȝ~ e

݋b _ _ Ɯ”. ٿ ~@ ݋ O[.
Support Apache!




CGI (Common Gateway Interface)b ݋L CGI α׷ Ȥ@ CGI zrxhO θb, ( ~@ b) ܺ α׷ _[b Ҏ^@ rѴ. ȝh݋ b [O Z Ҏ^ƴ. ݋b Hġ ݋L CGI 伺[b Ҏ^@ Ұ[O, CGI α׷@ ۼغ.


CGI A~[nU Hġ r[

CGI α׷ ùٷ ۸[R CGI ɸ[nU Hġ rؾ Ѵ. r[b Ҏ^@ \.


ScriptAlias C ȝ~[R Hġb Ưr 丮 CGI α׷~ڷ . Hġb 丮 b Hϓ CGI α׷ƶO r[ Ŭƾh ڿ@ ][R ڿ@ [O CnѴ.

ScriptAlias Cb ٞ ȝ~Ѵ.

ScriptAlias /cgi-bin/ /usr/local/apache2/cgi-bin/

b Hġ ⺻ ҿ ġ apache2.conf rHϿ b ~ƴ. ScriptAlias Cb Alias C URL պκ@ Ưr 丮 [Ѵ. Alias ScriptAliasb DocumentRoot 丮 ^ b 丮 ȝ~Ѵ. Alias ScriptAlias m@ ScriptAlias ߰ URL պκЙڷ C۸[b H@ CGI α׷ڷ ޸[b ƴ. E݋ r@ Hġ /cgi-bin/ڷ C۸[b ڿ@ ][R /usr/local/apache2/cgi-bin/ 丮݋ ã݋ CGI α׷ڷ Q[O ˸.

齁, URL http://www.example.com/cgi-bin/test.pl@ ][R Hġb /usr/local/apache2/cgi-bin/test.pl H@ [ ^ȯѴ. Hϓ Z[O డɸ[ Ҏ^ڷ @ ؾ Ѵ. ׷ ʙڔR Hġb @ .

ScriptAlias 丮 ^ b CGI

Ȼ ϞV얿 CGI α׷@ ScriptAlias 丮 ўrѴ. E݋ ھb CGI α׷@ ȝ~ ] b Qݞ } ] . ׵\ ȟġ ߴٔR ƹ 丮݋ CGI α׷@ [ @ Ϟ ^. 齁, UserDir C ȝ~[ ȝ~ڰ _ 丮 b 츦 r[. ȝ~ڰ _ CGI α׷@ ȝ~[O ͻ@ cgi-bin 丮 ٱѓ ^ٔR, ٸ ^݋n CGI α׷@ [O @ ͓ƴ.

ƹ 丮݋ CGI @ A~[R r ʿ[. v, AddHandler SetHandler C ȝ~[ cgi-script ڵ\ ۵ؾ Ѵ. 偓, Options C ExecCGI rؾ Ѵ.

Options ȝ~[ Cڷ CGI @ A~[

݋L ּrHϿ Options C ȝ~[ Ưr 丮݋ CGI @ A~ ] .

<Directory /usr/local/apache2/htdocs/somedir>
Options +ExecCGI

C Hġb CGI H @ A~Ѵ. Hϓ CGI Hn ݋L ˷ Ѵ. ٞ AddHandler Cb ݋L Ȯڰ cgi pl Hϻ@ CGI α׷ƶO ˸.

AddHandler cgi-script .cgi .pl

.htaccess H

.htaccess@ apache2.conf ٱѓ ^b 쿡 CGI α׷@ ȝ~ ] b Ҏ^@ ˷ش.

ȝ~ 丮

E r@ ȝ~[R ȝ~ 丮݋ .cgi b H@ CGI α׷ڷ Ѵ.

<Directory /home/*/public_html>
Options +ExecCGI
AddHandler cgi-script .cgi

ٞ@ ȝ~[R ȝ~ 丮 cgi-bin [丮 b H@ CGI α׷ڷ ΃eѴ.

<Directory /home/*/public_html/cgi-bin>
Options ExecCGI
SetHandler cgi-script


CGI α׷ ۼ[

``^'' αEְ CGI αE ȝƿb ΰ ֵ m .

ù偓 mƾb CGI α׷@ ٸ @ [ v MIME-type c ؾ Ѵپb ƴ. HTTP cb Ŭƾh Ŭƾh ~@ ްԵ ̸ ˸. ٞ .

Content-type: text/html

偓 mƾb HTML Ȥ@ v R ] b eڷ ؾ Ѵپb ƴ. κ HTML@ ¸[, VV gif ׸ HTML Ƈ ~@ ¸[b CGI α׷@ ۼ[b n .

ΰ ܸ[Ob CGI α׷ ۼ@ ƹ 齁 @ ٸ α׷ ſ 񔇸[.

Qڷ CGI α׷

ٞ@ v R@ b CGI α׷ . ״ first.plƶb HϿ v[O, cgi-bin 丮 ȝѴ.

print "Content-type: text/html\n\n";
print "Hello, World.";

Perl ڙ[ ʴn ϓ Ͻb ] . ù偓 R@ Hġ(Ȥ@ ȝ~[b ) /usr/bin/perl ġ b @ ȝ~[ α׷ H@ [O ˸. 偓 R@ Ҏ content-type @ ¸[O carriage-return Rَ@ Ѵ. ׵\R c ڿ HTTP c @ [b R eO, 얓 CѴ. 偓 R@ "Hello, World." ڄP@ Ѵ. ư͙ڷ ƴ.

v [O ּҸ Ѵ


H Ҹ ¸[R, vâ Hello, World. R δ. еb , ۸[b @ ҙ” ٸ @ Cn ] .


׵\ ۸[ ʾƿ!

݋ CGI α׷ V v ] b ~@ ⺻ڷ װ.

CGI α׷
! ͓ Ѵپb ƴ. »@ rȮ[ v ùٷ Q[ oѴٔR, CGI α׷݋ ùٸ Content-Type@ r[b ȮѴ.
CGI α׷ zȵ Ȥ@ "POST Method Not Allowed" 얂
CGI α׷@ [nU Hġ r[ ʾҴپb ƴ. Hġ r[ @ ٳC нO W@ κГ b ãƺ.
"Forbidden"ڷ C۸[b 얂
پb ƴ. Hġ α E @ Ȯθ[.
"Internal Server Error"b 얂
Hġ α R Ƹn CGI α׷ 얰 ` "Premature end of script headers" ͓ƴ. E ~@ [ Ȯθ[ Ϟ CGI α׷ HTTP c ¸[ o߾b ˾ƺ.

݋Lb _ rڷ ۸[ ʞ@ U[. , ݋L C۸[R ݋Lb Ư ȝ~ ( nobody www)ڷ Ѵ. E݋ _ Ϟ H@ [R ѓ ʿ[. HϿ nobody [⿡ @ ֱ ο H @ ش.

chmod a+x first.pl

, α׷ ٸ H@ аų ٔR HϿn ѓ ʿ[.

r ȯ

w࿡݋ α׷@ [R ڵڷ r ޵ȴ. 齁, PATHb _ H@ ã@ Ҹ ˷ش.

݋L α׷@ CGI α׷ڷ Vb PATH ٸ ] . ( 齁, sendmail ) CGI α׷ ȿ݋ [b wb η Cؾ w ã@ ] .

b ٞ CGI α׷ ù偓 R b zrxh ( perl) ο݋ TeѴ.



, CGI α׷ ٸ ȯ溯] ȝ~ѴٔR Hġ ]@ α׷ ؾ Ѵ.


CGI α׷ и[b κ α׷ ü V얓ƴ. Ư ΰ ] [ ʾҽO @ O ٔR ׷. v ݋L݋ [ w࿡݋ α׷@ غ. 齁, ٞ Ѵ.

cd /usr/local/apache2/cgi-bin

(perl ͸ [ . Hġb zrxh ù偓 R b r ȝ~[ ͸ ãƾ Ѵ.)

α׷@ v Content-Type@ ` HTTP c@ ¸[O R@ ؾ Ѵ. ٸ @ ѴٔR ݋L݋ Hġb Premature end of script headers ^ȯѴ. ڼ ~@ CGI α׷ ۼ[ O[.


α׾b _ ƴ. ߽oǔR α׿ 얂䰡 e׃. α׸ v Ѵ. ȝh xz[b ^݋ α׸ o[ ѴٔR, Ƹn ٸ Eü ˾ƺ Ѵ. α׸ b Ҏ^@ ڞR, κ H[ ذ ] .


suexec α׷@ ȝ~[R xzh Ȥ@ ȝ~ 丮 b CGI α׷@ ٸ ȝ~ љڷ ] . Suexecb ſ Qݸ[ @ ȝ[, ȝ [n [ o[R CGI α׷@ [ ʽO Premature end of script headers ^ȯѴ.

suexec ȝ~[O b ˷R apache2ctl -V [ SUEXEC_BIN ġ ȮѴ. Hġ CV ҿ݋ suexec H@ T߸[R, suexec ȝ~ ] .

suexec ظ[ oߴٔR ȝ~݋b ȵȴ. suexec ȝ~[ ʙڷR SUEXEC_BIN ġ b suexec H@ O (Ȥ@ Hϸ@ ٲٽO) ݋L C۸[R ȴ. suexec л@ ٞ En ȝ~[O ʹٔR, suexec -V [ suexec αH ġ ˾ƳO αHϿ݋ _ Ģ@ O b ãb.


ڿ݋b ϓ b?

CGI αEֿ ڙ]U ڿ݋ b @ ظ[R n ȴ. üڷ v ݋L ݋ _[b Ҏ^@ [b ͓ƴ. n "Hello, World." ¸[b α׷@ ۼ ] Ʒ α׷@ R ^V얓ƴ.


ȯ溯]b _ ǻ͸ ȝ~[b _ ”b @ƴ. ȯ溯]b path (ǻͰ _ w ش[b H@ ãb ), ȝ~ڸ, ͹̳ v Ϟ~ r. ^ ȯ溯] R w h݋ env Ѵ.

CGI Vn ݋L vb ȯ溯] ݋ ȯѴ. rb v v (Netscape, IE, Lynx), ݋L v (Hġ, IIS, WebSite), [b CGI α׷ .

CGI αEMb Ʒ ]@ ȝ~ ] O, ȯ溯]b Ŭƾh-݋L _b Ϻκ@ mѴ. ü ] ] U@ http://hoohoo.ncsa.uiuc.edu/cgi/env.html .

E Z Perl CGI α׷@ _ ޵ ȯ溯] ش. Hġ cgi-bin 丮 ƿ α׷ ΰ . ]b ]ƽO Mb ƴ. E݋ e U ^b ]n δ. , Hġb ⺻ڷ [b ȯ溯] ܿ \ Ҏ^ڷ ȯ溯] ߰ ] .

print "Content-type: text/html\n\n";
foreach $key (keys %ENV) {
print "$key --> $ENV{$key}<br>";


, ݋L Ŭƾhb F؝(STDIN) F(STDOUT)ڷ _Ѵ. ϻ STDIN@ Ű峪 α׷ Q[b H@ tO, STDOUT@ ZƳ ȭR@ Ѵ.

CGI α׷ e(form)@ POST[R e S Ư eڷ ݋ CGI α׷ STDINڷ Ѵ. ׵\R α׷@ Ű峪 HϿ݋ @ S Q[ S Q ] .

"Ư e"@ ſ Z[. H Ɠ @@ x(=) [O, H Ɠ @ ֵ@ ݋ ۓ(&) Ѵ. w, ۓ, x @ ڿz\ ھb ȥ[ ʙnU 16] ȯѴ. S ڄP@ ٞ e.


vv URL ڿ݋ Ʒ ڄP@ ȴ. ݋Lb ڄP@ QUERY_STRINGƶb ȯ溯] vѴ. Ƹ GET ]ƶO Ѵ. FORM ± METHOD Ӽ@ r[ HTML e(form) S GET POST rѴ.

α׷@ Ʒ ڄP@ Ϟ~ r ɰ Ѵ. n Ʒ S Q O CGI α׷ ٸ \ R@ ̾b ƺ\ .


CGI /ƺ\

CGI α׷@ ۼV L E@ _־b ȵ ƺ\ Ȥ@ @ ȝ~ Oغ Ѵ. Ʒ @ ȝ~[R Lװ RO α׷@ T ] .

Perl CGI α׷@ ۼѴٔR CPAN݋ L @ ã@ ] . CGI T θ ȝ~Ǿb @ CGI.pmƴ. κ α׷ @ CGI::Liten O ] .

C CGI α׷@ ۼѴٔR . [ http://www.boutell.com/cgic/ b CGIC ƺ\.


@ r...

ſ @ CGI r . z׿p comp.infosystems.www.authoring.cgi݋ \ ȝ CGI ] . HTML Writers Guild -servers ϸzhb 얿 @ ã⿡ Ǹ Ҵ. http://www.hwg.org/lists/hwg-servers/݋ @ @ ] .

׸O CGI α׷ ۿ ~@ CGI Ծ@ н n 𸥴. NCSA ݋ O, ]r ʾȻ@ Common Gateway Interface RFC h .

ϸzh z׿p ݽO b CGI Vb Te E , Te ٸ, ȝ~[b ݋L, CGI α׷@ ۼ , ɸ[R ش ȵ带 ڼ . ׵\R ذå@ ã {r.

Hġ zȵ尡 ߽oǾٽO Ȯ_[ ʾb CGI @ Hġ L Γͺz øR ȵȴ.

:  en  |  es  |  fr  |  ja  |  ko 



This is not a Q&A section. Comments placed here should be pointed towards suggestions on improving the documentation or server, and may be removed again by our moderators if they are either implemented or considered invalid/off-topic. Questions on how to manage the Apache HTTP Server should be directed at either our IRC channel, #httpd, on Freenode, or sent to our mailing lists.