我在 Virtualbox 中安装了 OpenBSD 5.3。 Web 服务器是 apache:rc.conf 中的 httpd_flags=""。
httpd.conf
# cat /var/www/conf/httpd.conf
<Directory "/var/www/htdocs/cgi-bin/">
AllowOverride None
Options +ExecCGI
Options None
Order allow,deny
Allow from all
</Directory>
AddHandler cgi-script .cgi
<Location /cgi-bin/*.cgi>
Options +ExecCGI
</Location>
ServerType standalone
ServerRoot "/var/www"
PidFile logs/httpd.pid
ScoreBoardFile logs/apache_runtime_status
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 15
MinSpareServers 5
MaxSpareServers 10
StartServers 5
MaxClients 150
MaxRequestsPerChild 0
MaxCPUPerChild 0
MaxDATAPerChild 0
MaxNOFILEPerChild 0
MaxRSSPerChild 0
MaxSTACKPerChild 0
Include /var/www/conf/modules/*.conf
Port 80
<IfDefine SSL>
Listen 80
Listen 443
</IfDefine>
User www
Group www
ServerAdmin [email protected]
DocumentRoot "/var/www/htdocs"
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory "/var/www/htdocs">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
UserDir disabled
DirectoryIndex index.html
AccessFileName .htaccess
<Files .htaccess>
Order allow,deny
Deny from all
</Files>
UseCanonicalName On
TypesConfig conf/mime.types
DefaultType text/plain
<IfModule mod_mime_magic.c>
MIMEMagicFile conf/magic
</IfModule>
HostnameLookups Off
ErrorLog logs/error_log
LogLevel warn
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
CustomLog logs/access_log common
Alias /icons/ "/var/www/icons/"
<Directory "/var/www/icons">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
IndexOptions FancyIndexing
AddIconByEncoding (CMP,/icons/compressed.gif) x-compress x-gzip
AddIconByType (TXT,/icons/text.gif) text/*
AddIconByType (IMG,/icons/image2.gif) image/*
AddIconByType (SND,/icons/sound2.gif) audio/*
AddIconByType (VID,/icons/movie.gif) video/*
AddIcon /icons/binary.gif .bin .exe
AddIcon /icons/binhex.gif .hqx
AddIcon /icons/tar.gif .tar
AddIcon /icons/world2.gif .wrl .wrl.gz .vrml .vrm .iv
AddIcon /icons/compressed.gif .Z .z .tgz .gz .zip
AddIcon /icons/a.gif .ps .ai .eps
AddIcon /icons/layout.gif .html .shtml .htm .pdf
AddIcon /icons/text.gif .txt
AddIcon /icons/c.gif .c
AddIcon /icons/p.gif .pl .py
AddIcon /icons/f.gif .for
AddIcon /icons/dvi.gif .dvi
AddIcon /icons/uuencoded.gif .uu
AddIcon /icons/script.gif .conf .sh .shar .csh .ksh .tcl
AddIcon /icons/tex.gif .tex
AddIcon /icons/bomb.gif core
AddIcon /icons/back.gif ..
AddIcon /icons/hand.right.gif README
AddIcon /icons/folder.gif ^^DIRECTORY^^
AddIcon /icons/blank.gif ^^BLANKICON^^
DefaultIcon /icons/unknown.gif
ReadmeName README
HeaderName HEADER
IndexIgnore .??* *~ *# HEADER* README* RCS CVS *,v *,t
AddEncoding x-compress Z
AddEncoding x-gzip gz
AddLanguage en .en
AddLanguage fr .fr
AddLanguage de .de
AddLanguage da .da
AddLanguage el .el
AddLanguage it .it
LanguagePriority en fr de
BrowserMatch "Mozilla/2" nokeepalive
BrowserMatch "MSIE 4\.0b2;" nokeepalive downgrade-1.0 force-response-1.0
BrowserMatch "RealPlayer 4\.0" force-response-1.0
BrowserMatch "Java/1\.0" force-response-1.0
BrowserMatch "JDK/1\.0" force-response-1.0
<IfDefine SSL>
AddType application/x-x509-ca-cert .crt
AddType application/x-pkcs7-crl .crl
</IfDefine>
<IfModule mod_ssl.c>
SSLPassPhraseDialog builtin
SSLSessionCache dbm:logs/ssl_scache
SSLSessionCacheTimeout 300
SSLMutex sem
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
SSLRandomSeed startup file:/dev/arandom 512
SSLLog logs/ssl_engine_log
SSLLogLevel info
</IfModule>
<IfDefine SSL>
<VirtualHost _default_:443>
DocumentRoot /var/www/htdocs
ServerName new.host.name
ServerAdmin [email protected]
ErrorLog logs/error_log
TransferLog logs/access_log
SSLEngine on
SSLCertificateFile /etc/ssl/server.crt
SSLCertificateKeyFile /etc/ssl/private/server.key
CustomLog logs/ssl_request_log \
"%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
</VirtualHost>
</IfDefine>
#
文件:
# ls -la /var/www/bin/
total 3284
drwxr-xr-x 2 root daemon 512 Aug 26 10:23 .
drwxr-xr-x 10 root daemon 512 Aug 24 19:09 ..
---------- 1 root bin 250676 Mar 13 01:23 bgpctl
---------- 1 root bin 222004 Mar 13 01:23 ping
---------- 1 root bin 238388 Mar 13 01:23 ping6
-r-xr-xr-x 1 root daemon 10704 Aug 26 09:51 printf
---------- 1 root bin 189236 Mar 13 01:23 traceroute
---------- 1 root bin 193332 Mar 13 01:23 traceroute6
# ls -laR /var/www/htdocs/
total 12
drwxr-xr-x 3 root daemon 512 Aug 26 09:13 .
drwxr-xr-x 10 root daemon 512 Aug 24 19:09 ..
drwxr-xr-x 2 root daemon 512 Aug 26 10:02 cgi-bin
/var/www/htdocs/cgi-bin:
total 16
drwxr-xr-x 2 root daemon 512 Aug 26 10:02 .
drwxr-xr-x 3 root daemon 512 Aug 26 09:13 ..
-rwxr-xr-x 1 root daemon 61 Aug 26 10:24 SEARCH.cgi
#
# cat /var/www/htdocs/cgi-bin/SEARCH.cgi
printf "Content-type: text/html\n\n";
printf hi
#
# which printf
/usr/bin/printf
#
# cksum /usr/bin/printf /var/www/bin/printf
4034907417 10704 /usr/bin/printf
4034907417 10704 /var/www/bin/printf
测试它
[user@notebook ~] wget http://domain.foo:8080/cgi-bin/SEARCH.cgi -O -
--2013-08-26 08:09:16-- http://domain.foo:8080/cgi-bin/SEARCH.cgi
Resolving domain.foo (domain.foo)... ::1, 127.0.0.1
Connecting to domain.foo (domain.foo)|::1|:8080... failed: Connection refused.
Connecting to domain.foo (domain.foo)|127.0.0.1|:8080... connected.
HTTP request sent, awaiting response... 500 Internal Server Error
2013-08-26 08:09:16 ERROR 500: Internal Server Error.
[user@notebook ~]
日志
# cat /var/www/logs/error_log
[Mon Aug 26 10:09:13 2013] [error] [client 10.0.2.2] Premature end of script headers: /htdocs/cgi-bin/SEARCH.cgi
#
我复制了“printf”二进制文件进行测试。
问: 如何在 OpenBSD 上启用 CGI?我只想在 cgi 中使用 echo/sed/ifthenelse/tr/head/awk/egrep ..(目的是使用户能够在文件中使用网站中的“grep”),但即使使用 printf 进行简单的测试也不会不起作用..我错过了什么?
更新#1:有什么问题
printf "Content-type: text/html\n\n";
SEARCH.cgi 中的行? (因为我认为问题就在那里..) - /var/www/bin 中需要 printf 吗?我将其复制到那里,但它仍然不起作用(因为“脚本标题提前结束”)。
PS:“http://域名.foo:8080“是 obsd 机器,我刚刚使用了 virtualbox 的端口转发。
UPDATE#2:我从 cgi 头中删除了 ksh(后来我又添加回来,请参阅评论)
更新#3:
# cat error_log
Abort trap
[Thu Aug 29 09:44:09 2013] [error] [client 10.0.2.2] Premature end of script headers: /htdocs/cgi-bin/SEARCH.cgi
# cat /var/www/htdocs/cgi-bin/SEARCH.cgi
#!/bin/ksh
perl -e 'print"Content-type: text/html\n\n";'
#
答案1
首先检查你的/var/www/logs/error_log
,这将为你提供关于为什么它可能不起作用的最大线索。
如果您将<Directory>
设置放在 httpd.conf 的末尾,较早的设置<Directory "/var/www/htdocs">
将覆盖它。你的error_log
遗嘱显示:
[error] [client x.x.x.x] Options ExecCGI is off in this directory /htdocs/a/a.cgi
确保将其放置在此上方以使其生效。您可能还必须将 cgi chmod 为可执行文件,并根据创建/var/www/htdocs/a
目录的用户设置适当的权限/所有权。
还值得注意的是,OpenBSD 默认使用chroot
,因此如果您想让命令正常工作,则必须将其放入 chroot 中。
假设您想使用 perl 作为示例,首先您需要创建所需的目录。
$ cd /var/www
$ mkdir -p /var/www/usr/{bin,lib,libexec}
接下来我们需要弄清楚 perl 使用哪些库:
$ ldd /usr/bin/perl
/usr/bin/perl:
Start End Type Open Ref GrpRef Name
1c000000 3c004000 exe 1 0 0 /usr/bin/perl
07f3f000 27f58000 rlib 0 1 0 /usr/lib/libperl.so.12.0
0d61c000 2d625000 rlib 0 1 0 /usr/lib/libm.so.7.1
0fb24000 2fb28000 rlib 0 1 0 /usr/lib/libutil.so.11.4
01b37000 21b65000 rlib 0 1 0 /usr/lib/libc.so.66.2
0b671000 0b671000 rtld 0 1 0 /usr/libexec/ld.so
并将它们复制到 chroot:
$ cp /usr/lib/{libperl.so.12.0,libm.so.7.1,libutil.so.11.4,libc.so.66.2} /var/www/usr/lib
$ cp /usr/libexec/ld.so /var/www/usr/libexec
然后是 Perl 本身:
$ cp /usr/bin/perl /var/www/usr/bin
然后你可以检查你的cgi是否可以使用:
$ chroot /var/www/ /htdocs/a/a.cgi
Content-type: text/html
Test!