linux(树莓派)安装web.py+nginx

python想web方式运行需要安装如下软件!

nginx

web.py

Spawn-fcgi

flup

1.安装nginx(web服务器)

wget http://nginx.org/download/nginx-1.4.7.tar.gz
tar zxvf nginx-1.4.7.tar.gz
cd nginx-1.4.7
yum -y install pcre pcre-devel #centos 
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_stub_status_module
make && make install

若出现如下错误

./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using –without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using –with-pcre=<path> option.

参考这里:http://www.phpbulo.com/archives/667.html

2.安装spawn-fcgi

wget http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz
tar zxvf spawn-fcgi-1.6.3.tar.gz
./configure
make &amp;&amp; make install

3.安装flup和web.py

pip install flup
pip install web.py

若出现

pip command not found

参考如下

wget --no-check-certificate https://pypi.python.org/packages/source/p/pip/pip-1.4.tar.gz
tar -zxvf ./pip-1.4.tar.gz
cd pip-1.4
sudo python setup.py install

出现

ImportError: No module named setuptools

参考这里:http://www.phpbulo.com/archives/671.html

4.添加nginx虚拟主机

复制如下代码到虚拟机中

server {
listen 80;
server_name localhost;
root /home/wwwroot/
location / {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_pass 127.0.0.1:9002;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /static/ {
if (-f $request_filename) {
rewrite ^/static/(.*)$ /static/$1 break;
}
}
}

检查配置文件

sudo /usr/local/nginx/sbin/nginx -t

若出现 test failed则说明配置文件有错误的地方,若出现test is successful则配置文件成功

如下图,测试成功

20150212190228

5.新建测试文件,在vhost的根目录中新建个index.py,即上面nginx规则里面的root对应的/home/wwwroot/目录

复制下面python代码到文件中

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import web
urls = (&quot;/.*&quot;, &quot;hello&quot;)
app = web.application(urls, globals())
class hello:
def GET(self):
return 'Hello, world! 3305'
if __name__ == &quot;__main__&quot;:
web.wsgi.runwsgi = lambda func, addr=None: web.wsgi.runfcgi(func, addr)
app.run()

赋予文件执行权限

chmod +x index.py

6.启动spawn-fcgi

spawn-fcgi -d /home/wwwroot/ -f /home/wwwroot/index.py -a 127.0.0.1 -p 9002

其中

-d 对应目录

-f 对应文件

127.0.0.1和端口9002对应上面nginx规则的fastcgi_pass的值

7.验证

20150212185203

我是直接在服务器上使用curl测试正常,

显示

Hello, world! 3305

此条目发表在树莓派, 笔记分类目录,贴了, , 标签。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用*标注