在bash中,如何配置lighttpd调用特定URL的本地python脚本?
在bash中,最简单的方法是什么,来设置lighttpd,让它调用一个本地的python脚本,同时把URL中包含的查询字符串或名称-值对作为命令行选项传递给这个python应用程序,让它去解析?
Example:
www.myapp.com/sendtopython/app1.py?Foo=Bar
results in the following occurring on the system.
>python app1.py Foo=Bar
www.myapp.com/sendtopython/app2.py?-h
results in the following occurring on the system.
>python app2.py –h
下面是一个lighttpd的安装和配置脚本示例。
#!/bin/bash
# Install and configure web console managed by lighttpd
# Suggested Amazon EC2 AMI : ami-0d729464
#
# The console installed into /opt/web-console and
# available on the http://_the_server_dns_/web-console
set -e -x
export DEBIAN_FRONTEND=noninteractive
function die()
{
echo -e "$@" >> /dev/console
exit 1
}
apt-get update && apt-get upgrade -y
apt-get -y install python
apt-get -y install unzip
apt-get -y install lighttpd
# web directory defaults to /var/www.
WEBDIR=/var/www/logs
mkdir $WEBDIR || die "Cannot create log directory."
PYTHON=`which python`
echo $?
if [ ! $? ]
then
echo "Python interpreter not installed or not found in system path!!!" >> /dev/console
echo "Exiting setup-instance..."
exit 1
fi
#Download web-console
FILE_DOWNLOAD_URL=http://downloads.sourceforge.net/web-console/web-console_v0.2.5_beta.zip
wget $FILE_DOWNLOAD_URL -O web-console.zip || die "Error downloading file web-console.zip"
# Install the web-console
INSTALL_DIR=/opt/web-console
mkdir $INSTALL_DIR
unzip -u -d $INSTALL_DIR web-console.zip || die "Error extracting web-console.zip"
chown www-data:www-data $INSTALL_DIR
# Configure lighttpd
cat > $INSTALL_DIR/webconsole.conf <<EOF
server.modules += ( "mod_cgi" )
alias.url += ( "/web-console/wc.pl" => "/opt/web-console/wc.pl" )
alias.url += ( "/web-console/" => "/opt/web-console/wc.pl" )
\$HTTP["url"] =~ "^/web-console/" {
cgi.assign = ( ".pl" => "/usr/bin/perl" )
}
EOF
ln -s $INSTALL_DIR/webconsole.conf /etc/lighttpd/conf-enabled/
/etc/init.d/lighttpd force-reload
exit 0
1 个回答
3
嗯,首先我建议你不要去改安装脚本,而是先运行一次它,然后再编辑生成的lighttpd配置文件(在你的情况下是webconsole.conf)。
接下来,你需要为Python脚本注册CGI,就像安装脚本中为Perl做的那样。你可以在对应的.pl行下面加一行
cgi.assign = ( ".py" => "/usr/bin/python" )
这样就能让Python成为/web-console/路径的另一个CGI选项(如果你想在任何路径下注册.py文件,可以查一下lighttpd的文档)。
然后,你的Python CGI脚本app1.py、app2.py等需要符合CGI规范。根据我的记忆,它会把URL参数作为环境变量传递。所以你不能简单地使用sys.argv。我相信有一个Python模块可以帮你提取这些参数。(在Perl中,Lincoln Stein的CGI模块可以处理环境变量和命令行参数,但我不太确定Python是否也有类似的模块)。