丢掉python -m SimpleHTTPServer,一行命令用http分享目录(单行web服务器)

好久没更新博客,还是要备忘一些实用工作技巧:

如何简单轻松地用一行命令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

但是远程机器不能装,所以还得自己来呀。。。



猜你喜欢

转载自blog.csdn.net/DelphiNew/article/details/30242059