因為要用到 perl 網頁做一些測試,想說沒試過 lighttpd + perl 的組合
就藉這機會操練一下
網路上有很多文章說明如何安裝設定,不過還是花了不少功夫
系統是 vbox + ubuntu 12.04 x64
以下是安裝過程筆記...
$ sudo /etc/init.d/lighttpd start
sudo: /etc/init.d/lighttpd: command not found
先查一下系統裡有沒有裝 lighttpd,結果是無
$ sudo apt-get install lighttpd
安裝完成後會自動啟動,然後要建立 cgi 的資料夾
$ sudo mkdir /var/www/cgi-bin
一般習慣是放在 /var/www/cgi-bin,下面會說明如何移到其他位置
或是設定多個可執行 cgi 的資料夾
建立一個測試首頁...
$ sudo vim /var/www/cgi-bin/index.pl
(我習慣用副檔名 .pl,要改用 .cgi 也可以)
#!/usr/bin/perl
print "Content-type: text/html\n\n";
$pwd = `pwd`;
print "<h1>Hello Perl!!</h1>($pwd)\n";
exit;
(存檔離開)
第一個 print 是必要的,任何網頁在送出網頁內容前
都要先通知瀏覽器資料的格式.
將 index.pl 設為可執行檔,否則主機無法執行 index.pl
瀏覽器會得到 『403 - Forbidden』 的回應
$ sudo chmod +x /var/www/cgi-bin/index.pl
$ ls -l /var/www/cgi-bin/index.pl
-rwxr-xr-x 1 root root 113 Sep 14 22:10 /var/www/cgi-bin/index.pl
用瀏覽器開網站根目錄,有多一個 cig-bin 資料夾
像這樣...
Index of /
Name Last Modified Size Type
Parent Directory/ - Directory
cgi-bin/ 2012-Sep-17 22:10:10 - Directory
index.lighttpd.html 2012-Sep-17 22:07:53 3.4K text/html
lighttpd/1.4.28
點進去也有看到 index.pl,但是點 index.pl 卻出現
403 - Forbidden因為還未設定讓 lighttpd 可以執行 cgi 程式
有兩個檔案要設定...
-----------------
$ sudo vim /etc/lighttpd/lighttpd.conf
server.modules = (
"mod_access",
"mod_alias",
"mod_compress",
"mod_redirect",
# "mod_rewrite",
)
server.document-root = "/var/www"
server.upload-dirs = ( "/var/cache/lighttpd/uploads" )
server.errorlog = "/var/log/lighttpd/error.log"
server.pid-file = "/var/run/lighttpd.pid"
server.username = "www-data"
server.groupname = "www-data"
index-file.names = ( "index.php", "index.html", "index.pl", "index.cgi",
"index.htm", "default.htm",
" index.lighttpd.html" )
url.access-deny = ( "~", ".inc" )
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )
## Use ipv6 if available
#include_shell "/usr/share/lighttpd/use-ipv6.pl"
dir-listing.encoding = "utf-8"
server.dir-listing = "enable"
compress.cache-dir = "/var/cache/lighttpd/compress/"
compress.filetype = ( "application/x-javascript", "text/css", "text/html", "text/plain" )
include_shell "/usr/share/lighttpd/create-mime.assign.pl"
include_shell "/usr/share/lighttpd/include-conf-enabled.pl"
跟 apache 比起來,lighttpd 的設定檔簡單許多
我只在 『index-file.names』裡加了兩個預設首頁檔名
-----------------
$ sudo vim /etc/lighttpd/conf-available/10-cgi.conf
# /usr/share/doc/lighttpd-doc/cgi.txt
server.modules += ( "mod_cgi" )
$HTTP["url"] =~ "^/cgi-bin/" {
cgi.assign = ( "" => "" )
}
alias.url = ( "/cgi-bin/" => "/var/www/cgi-bin/")
## Warning this represents a security risk, as it allow to execute any file
## with a .pl/.py even outside of /usr/lib/cgi-bin.
#
#cgi.assign = (
# ".pl" => "/usr/bin/perl",
# ".py" => "/usr/bin/python",
#)
10-cgi.conf 裡加了 mod_cgi 模組進來,然後指定 cgi-bin 資料夾的實際路徑
以上兩個設定檔我試過網路上不同的設定,有的成功有的失敗
上面是整理後我覺得最簡單的設定方式
最後要啟動 mod_cgi,並重新載入設定檔內容
$ sudo lighty-enable-mod cgi
$ sudo /etc/init.d/lighttpd force-reload
* Reloading web server configuration lighttpd
或是直接重新啟動 lighttpd 也可以...
$ sudo /etc/init.d/lighttpd restart
* Stopping web server lighttpd [ OK ]
* Starting web server lighttpd
重新整理剛才的網頁,可以出現正確網頁內容了
Hello Perl!!
(/var/www/cgi-bin )
----------------------
再來,如果我想將 cgi 程式放在 pl 資料夾裡,不要放 cgi-bin
或是保留 cgi-bin,另外再開一個 pl 資料夾
$ sudo mkdir /var/www/pl
$ sudo cp /var/www/cgi-bin/index.pl /var/www/pl/.
$ sudo vim /etc/lighttpd/conf-available/10-cgi.conf
alias.url = ( "/cgi-bin/" => "/var/www/cgi-bin/")
上面這行改成...
alias.url = ( "/cgi-bin/" => "/var/www/cgi-bin/",
"/pl/" => "/var/www/pl/"
)
然後加一個設定.....
$HTTP["url"] =~ "^/pl/" {
cgi.assign = ( "" => "" )
}
$ sudo /etc/init.d/lighttpd force-reload
* Reloading web server configuration lighttpd
瀏覽 /pl/ .....
Hello Perl!!
(/var/www/pl)
括號裡 cgi 程式的實際路徑跟剛才不一樣了
如果資料夾裡沒有首頁檔且不想顯示目錄內容
編輯 /etc/lighttpd/lighttpd.conf
server.dir-listing = "enable"
在這行前面加#註解起來,再 force-reload 一次就可以了!!
完工~~ ^_^