好久沒更新博客,還是要備忘一些實用工作技巧:
如何簡單輕鬆地用一行命令web分享一個目錄
這要在以前,我和大家是一樣的運行python -m SimpleHTTPServer就O了,但是情況總是在不經意間變得複雜了。
這一回連接的是同事常開的測試服務器,所以:
1.他現在用的就是python -m SimpleHTTPServer,監聽着兩個用戶端口,但是速度極慢,還常常卡死;
2.他機器上裝了apache2,但是隻監聽80端口,用的是系統默認配置;
3.如果我提供一個簡單的方案,他就替換掉這個SimpleHTTPServer。
因爲我自己機器用的是lightthpd(我喜歡light!),所以我火速用google學習了一下:
http://redmine.lighttpd.net/projects/lighttpd/wiki/TutorialConfiguration
配合短平快的瞎猜,編寫如下腳本:
#!/bin/bash
cat >._lighttpd_dir_conf << EOF
server.document-root = "$1"
server.port = "$2"
dir-listing.encoding = "utf-8"
server.dir-listing = "enable"
mimetype.assign = (
".html" => "text/html",
".txt" => "text/plain",
".jpg" => "image/jpeg",
".png" => "image/png"
)
EOF
lighttpd -D -f ._lighttpd_dir_conf
使用方法是lighttpd_dir directory port
自己在機器上一試,效果很好,哈哈,開心。再一看同事的遠程機器,這個開心重現不了~(遠程機器上沒有lighttpd)
好吧,只能用apache2來做同樣的事情了,事實證明apache2的設置更麻煩,參考了萬能的SO\
http://stackoverflow.com/questions/13695391/start-an-apache-server-in-any-directory-from-command-line
我得到了自己的腳本方法:
#!/bin/bash
cat >._apache2_dir_conf << EOF
Include /etc/apache2/mods-enabled/*.load
Include /etc/apache2/mods-enabled/*.conf
ErrorLog $1/._apache2_dir_error.log
HostnameLookups Off
NameVirtualHost *:$2
Listen $2
PidFile $1/._apache2_pid
<VirtualHost *:$2>
ServerAdmin joyer@uc
DocumentRoot $1
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory $1/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
</Directory>
ErrorLog $1/._apache2_dir_error.log
LogLevel warn
</VirtualHost>
EOF
#apache2 -k $3 -X -f $1/._apache2_dir_conf
apache2 -X -f $1/._apache2_dir_conf
使用方法也是apache2_dir directory port,但是directory必須是絕對路徑^_
另外,還學習了同事的nohup命令,真是好東西。
雖然有pache這種好東西:https://github.com/devinrhode2/pache/blob/master/pache
但是遠程機器不能裝,所以還得自己來呀。。。