Linux下搭建基于Nginx+FastCGI+Flup+Webpy+Cheetah的Python Web环境
2012-09-18 08:46
645 查看
一、软件包需求
二、软件包安装
注意:./configure 选择prefix安装路径,安装nginx需要指定pcre软件包路径,setuptools用于python包安装管理,安装flup、webpy时需要使用自己安装的python2.7
(1) python安装
(2) nginx安装
(3) setuptools安装
(4) flup安装
(5) webpy安装
(6) Cheetah安装
三、软件配置
(1) nginx配置文件
四、程序示例
程序安装目录:
(1) 演示一个简单的 index.py,其中使用了 middleware 中间件,用于请求连接的回调处理。
注意:直接运行 ./index.py 启动程序,同时保证 /tmp/webpy.sock 权限为777,或者和nginx为相同权限用户,否则nginx无权限读取webpy.sock数据。
连接 http://localhost:8008,浏览器将返回 “Hello, world!“ 字符串
(2) 使用 cheetah 模板引擎,构建模板系统(直接引用 html 原始文件)
首先创建一个模板目录,并创建模板文件 index.html,内容如下:
其中:$content 为模板变量
然后创建 index.py 程序,输出模板变量内容,如下:
(3) 使用 cheetah 模板引擎,构建模板系统(以直接引用 python 模块的方式)
首先在模板目录创建模板文件,命名为 TmplIndex.tmpl,内容如下:
然后执行命令,将 TmplIndex.tmpl 编译为 python 模块(在相同目录下生成同名 .py 文件):
接下来创建 index.py 文件,引用 TmplIndex.py 模板 模块,内容如下:
(1) Python-2.7.3.tgz (2) nginx-1.2.3.tar.gz (3) pcre-8.30.tar.gz (4) setuptools-0.6c11.tar.gz (5) flup-1.0.1.tar.gz (6) web.py-0.37.tar.gz (7) Cheetah-2.4.4.tar.gz
二、软件包安装
注意:./configure 选择prefix安装路径,安装nginx需要指定pcre软件包路径,setuptools用于python包安装管理,安装flup、webpy时需要使用自己安装的python2.7
(1) python安装
LDFLAGS="-Wl,-rpath /usr/local/xxx/opt/lib" ./configure --prefix=/usr/local/xxx/opt --enable-shared --enable-profiling --enable-ipv6 --with-gcc --with-threads
(2) nginx安装
/configure --prefix=/usr/local/xxx/opt --with-http_stub_status_module --with-mail --with-debug --with-mail_ssl_module --with-ipv6 --with-http_ssl_module --with-pcre=../pcre-8.30
(3) setuptools安装
/usr/local/xxx/opt/bin/python setup.py inistall
(4) flup安装
/usr/local/xxx/opt/bin/python setup.py inistall
(5) webpy安装
/usr/local/xxx/opt/bin/python setup.py inistall
(6) Cheetah安装
/usr/local/xxx/opt/bin/python setup.py inistall
三、软件配置
(1) nginx配置文件
user nobody; worker_processes 1; pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream webpys { server unix:/tmp/webpy.sock; } server { listen 8008; server_name localhost; #charset koi8-r; #access_log /usr/local/xxx/opt/logs/host.access.log main; #location / { # root html; # index index.html index.htm; #} location / { fastcgi_connect_timeout 300; fastcgi_pass webpys; include /usr/local/xxx/opt/conf/fastcgi_params; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
四、程序示例
程序安装目录:
/usr/local/xxx/app/cgi-bin
(1) 演示一个简单的 index.py,其中使用了 middleware 中间件,用于请求连接的回调处理。
#!/usr/local/xxx/opt/bin/python # vim: set ts=4 et sw=4 sts=4 fileencoding=utf-8 : import web class middleware(object): def __init__(self, app): object.__init__(self) setattr(self, "innerapp", app) def __call__(self, environ, start_response): return self.innerapp(environ, start_response) class test: def GET(self): return "Hello, World!\n" if __name__ == "__main__": urls = ("/.*", "test") app = web.application(urls, globals()) web.wsgi.runwsgi = lambda func, addr=("/tmp/webpy.sock"): web.wsgi.runfcgi(func, addr) app.run(middleware)
注意:直接运行 ./index.py 启动程序,同时保证 /tmp/webpy.sock 权限为777,或者和nginx为相同权限用户,否则nginx无权限读取webpy.sock数据。
连接 http://localhost:8008,浏览器将返回 “Hello, world!“ 字符串
(2) 使用 cheetah 模板引擎,构建模板系统(直接引用 html 原始文件)
首先创建一个模板目录,并创建模板文件 index.html,内容如下:
<html> <title> index test template </title> <body> $content </body> </html>
其中:$content 为模板变量
然后创建 index.py 程序,输出模板变量内容,如下:
#!/usr/local/xxx/opt/bin/python # vim: set ts=4 et sw=4 sts=4 fileencoding=utf-8 : import sys import web from web.contrib.template import render_cheetah # 中间件 class middleware(object): def __init__(self, app): object.__init__(self) setattr(self, "innerapp", app) def __call__(self, environ, start_response): return self.innerapp(environ, start_response) class index(object): def GET(self): # 读取 index.html 模板 render = render_cheetah('/usr/local/xxx/app/tmpl/') return render.index(content="hello, world!") if __name__ == "__main__": urls = ("/.*", "index") app = web.application(urls, globals(), autoreload=True) web.wsgi.runwsgi = lambda func, addr=("/tmp/webpy.sock"): web.wsgi.runfcgi(func, addr) app.run(middleware)
(3) 使用 cheetah 模板引擎,构建模板系统(以直接引用 python 模块的方式)
首先在模板目录创建模板文件,命名为 TmplIndex.tmpl,内容如下:
<html> <title> index test template $content </title> <body> $content </body> </html>
然后执行命令,将 TmplIndex.tmpl 编译为 python 模块(在相同目录下生成同名 .py 文件):
/usr/local/xxx/opt/bin/cheetah -c /usr/local/xxx/app/tmpl/TmplIndex.tmpl
接下来创建 index.py 文件,引用 TmplIndex.py 模板 模块,内容如下:
#!/usr/local/xxx/opt/bin/python # vim: set ts=4 et sw=4 sts=4 fileencoding=utf-8 : import sys import web # 中间件 class middleware(object): def __init__(self, app): object.__init__(self) setattr(self, "innerapp", app) def __call__(self, environ, start_response): return self.innerapp(environ, start_response) class index(object): def GET(self): # 导入模块模块 import TmplIndex return TmplIndex.TmplIndex(searchList = [{"content": "hello, world"}]) if __name__ == "__main__": urls = ("/.*", "index") # 引入模板目录 sys.path.append("/usr/local/xxx/app/tmpl/") app = web.application(urls, globals(), autoreload=True) web.wsgi.runwsgi = lambda func, addr=("/tmp/webpy.sock"): web.wsgi.runfcgi(func, addr) app.run(middleware)
相关文章推荐
- Linux系统上Nginx+Python的web.py与Django框架环境
- linux下搭建nginx+uwsgi+web.py的python web服务器
- windows下用nginx + fastcgi搭建web.py生产环境
- Linux系统上Nginx+Python的web.py与Django框架环境
- webpy+python2.7+linux环境搭建
- 树莓派上搭建基于Python+web.py+fastcgi+lighttpd的网站
- nginx python spawn-fcgi Flup webpy搭建python的web环境
- Nginx + webpy 和FastCGI搭建webpy环境 推荐
- centos7简单搭建nginx+uwsgi+web.py的Python web环境(记录)
- nginx python spawn-fcgi Flup webpy搭建python的web环境
- virtualenv 环境下 Nginx + Flask + Gunicorn+ Supervisor 搭建 Python Web
- nginx+python+fastcgi环境配置(flup版本)
- Python Web初学解惑之 WSGI、flup、fastcgi、web.py的关系
- 基于Flask框架的Python web程序的开发实战 <一> 环境搭建
- 搭建nginx + python + django +memcached+ mysql +fastcgi 环境
- web环境搭建之Linux--nginx-php-mysql
- Windows下nginx+web.py+fastcgi服务搭建
- Linux系统下搭建基于Geany+Python开发环境
- Windows系统下使用flup搭建Nginx和Python环境的方法
- Windows下Python Web开发环境搭建 (Notepad++ & Web.py)