Total Pageviews

Wednesday 27 November 2013

安装web.py

wget http://webpy.org/static/web.py-0.37.tar.gz
tar zxvf  web.py-0.37.tar.gz
cd  web.py-0.37
python setup.py install

测试:
as3:~# python
Python 2.7.6 (default, Nov 20 2013, 07:15:04)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import web
>>>                   (此处无“import error:no module named web"提示,表明导入web.py成功,即安装web.py成功)


官网:http://webpy.org/install,http://webpy.org/install.zh-cn,
https://github.com/webpy/webpy
-------------------------


web.py 入门介绍

内容来源:webpy.org
介绍:
1、python中web.py 是一个轻量级Python web框架,它简单而且功能强大。web.py是一个开源项目。
2、安装很简单:pip install web.py
-----------------

Nginx uWSGI web.py 站点搭建

一、安装nginx
  在安装nginx前,需要先装nginx的依赖包。
  1、如果没有yum则先安装yum
     删除原有的yum
     rpm -aq|grep yum|xargs rpm -e --nodeps
       自己到下面网站下载对应的rpm包(这里注意一定要对应你的系统,是32位还是64位的,本人就曾犯过类似的错误,总是导致安装失败,原因就是我的系统是32位的,而网上的资料大部分是64的rpm)
     python-iniparse-0.3.1-2.1.el6.noarch.rpm
     yum-metadata-parser-1.1.2-14.1.el6.x86_64.rpm
yum-3.2.27-14.el6.centos.noarch.rpm
yum-plugin-fastestmirror-1.1.26-11.el6.noarch.rpm
     下载上面的4个rpm
       安装的时候需要按照顺序安装,因为yum是依赖python的
     rpm -ivh python-iniparse-0.3.1-2.1.el6.noarch.rpm
rpm -ivh yum-metadata-parser-1.1.2-14.1.el6.x86_64.rpm
rpm -ivh yum-3.2.27-14.el6.centos.noarch.rpm yum-plugin-fastestmirror-1.1.26-11.el6.noarch.rpm
注意最后两个包必需同时安装,相互依赖.
安装好后记得yum update更新下包
2、安装编译工具及库文件
yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel
3、安装PCRE,PCRE 作用是让 Nginx 支持 Rewrite 功能
    wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
    tar zxvf pcre-8.35.tar.gz
cd pcre-8.35
   ./configure
   make && make install
  成功后执行:pcre-config --version,如果显示8.35则安装成功
4、安装nginx
http://nginx.org/download/ 下载自己需要的版本,版本不一定越高越好,还是搞个稳定的吧
wget  http://nginx.org/download/nginx-0.1.10.tar.gz
tar zxvf nginx-0.1.10.tar.gz
cd nginx-0.1.10.tar.gz
./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
安装成功后运行:/usr/local/webserver/nginx/sbin/nginx -v,显示版本号则安装成功
添加用户:
/usr/sbin/groupadd www
/usr/sbin/useradd -g www www
下面附上nginx的说明,来自w3c
  1. user www www;
  2. worker_processes 2; #设置值和CPU核心数一致
  3. error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; #日志位置和日志级别
  4. pid /usr/local/webserver/nginx/nginx.pid;
  5. #Specifies the value for maximum file descriptors that can be opened by this process.
  6. worker_rlimit_nofile 65535;
  7. events
  8. {
  9. use epoll;
  10. worker_connections 65535;
  11. }
  12. http
  13. {
  14. include mime.types;
  15. default_type application/octet-stream;
  16. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  17. '$status $body_bytes_sent "$http_referer" '
  18. '"$http_user_agent" $http_x_forwarded_for';
  19.  
  20. #charset gb2312;
  21.  
  22. server_names_hash_bucket_size 128;
  23. client_header_buffer_size 32k;
  24. large_client_header_buffers 4 32k;
  25. client_max_body_size 8m;
  26.  
  27. sendfile on;
  28. tcp_nopush on;
  29. keepalive_timeout 60;
  30. tcp_nodelay on;
  31. fastcgi_connect_timeout 300;
  32. fastcgi_send_timeout 300;
  33. fastcgi_read_timeout 300;
  34. fastcgi_buffer_size 64k;
  35. fastcgi_buffers 4 64k;
  36. fastcgi_busy_buffers_size 128k;
  37. fastcgi_temp_file_write_size 128k;
  38. gzip on;
  39. gzip_min_length 1k;
  40. gzip_buffers 4 16k;
  41. gzip_http_version 1.0;
  42. gzip_comp_level 2;
  43. gzip_types text/plain application/x-javascript text/css application/xml;
  44. gzip_vary on;
  45.  
  46. #limit_zone crawler $binary_remote_addr 10m;
  47. #下面是server虚拟主机的配置
  48. server
  49. {
  50. listen 80;#监听端口
  51. server_name localhost;#域名
  52. index index.html index.htm index.php;
  53. root /usr/local/webserver/nginx/html;#站点目录
  54. location ~ .*\.(php|php5)?$
  55. {
  56. #fastcgi_pass unix:/tmp/php-cgi.sock;
  57. fastcgi_pass 127.0.0.1:9000;
  58. fastcgi_index index.php;
  59. include fastcgi.conf;
  60. }
  61. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
  62. {
  63. expires 30d;
  64. # access_log off;
  65. }
  66. location ~ .*\.(js|css)?$
  67. {
  68. expires 15d;
  69. # access_log off;
  70. }
  71. access_log off;
  72. }
  73.  
  74. }
检查配置文件ngnix.conf的正确性命令:
/usr/local/webserver/nginx/sbin/nginx -t
Nginx 启动命令如下:
/usr/local/webserver/nginx/sbin/nginx
从浏览器访问我们配置的站点ip:如果出现下图,则表示nginx安装成功.
如果在启动过程出现以下错误,请按照下面的方法解决
启动命令:/usr/local/nginx/sbin/nginx
发现报错了:
error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
经网上查询,这是Linux的通病
[root@localhost nginx]# sbin/nginx
sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
[root@localhost nginx]# error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory
[root@localhost nginx]# whereis libpcre.so.1
libpcre.so: /lib64/libpcre.so.0 /usr/local/lib/libpcre.so /usr/local/lib/libpcre.so.1
[root@localhost nginx]# ln -s /usr/local/lib/libpcre.so.1 /lib64
[root@localhost nginx]# sbin/nginx
先找到libpcre.so.1所在位置,然后做个软链接就可以了.
5、安装uWSGI
最好通过pip install来安装uWSGI
pip install uwgsi
安装成功后,执行uwsgi --version #查看版本
有时候pip 会执行不成功,原因是pip默认执行的是系统python2.6的版本,自己需要重新制定一下,用python2.7的pip来执行
可以通过
which pip
/usr/local/bin/pip
cat /usr/local/bin/pip
找到头部执行python重新指定到python2.7
测试 uwsgi 是否正常:
新建 server.py 文件,内容如下:
  1. def application(env, start_response):
  2. start_response('200 OK', [('Content-Type','text/html')])
  3. return "Hello World"
启动:uwsgi --http :8001 --wsgi-file server.py
浏览器访问,如果出现 Hello World则表示uwsgi安装成功
6、安装web.py
在安装之前,对web.py做下简单的介绍,web.py 是一个Python 的web 框架,它简单而且功能强大。web.py 实现了 WSGI 并能在任何兼容它的服务器上运行。 WSGI 是一个web服务器与应用程序之间的通用API, 就如Java 的 Servlet 接口。 你需要安装 flup 使web.py 支持with CGI, FastCGI 或 SCGI, flup提供了这些API的WSGI接口。但绝大多数网站还是需要更加专业一些的web服务器。很多框架都自带了 WSGI server ,比如 Flask,webpy,Django、CherryPy等等。当然性能都不好,自带的 web server 更多的是测试用途,发布时则使用生产环境的 WSGI server或者是联合 nginx 做 uwsgi 。
安装web.py, 请先下载:
http://webpy.org/static/web.py-0.37.tar.gz
或者获取最新的开发版:
https://github.com/webpy/webpy/tarball/master
解压并拷贝 web 文件夹到你的应用程序目录下。 或者,为了让所有的应用程序都可以使用,运行:
python setup.py install
注意: 在某些类unix系统上你可能需要切换到root用户或者运行:
sudo python setup.py install
查看 推荐设置.
另外一个选择是使用Easy Install. Easy Install 使用如下:
easy_install web.py
或者 PIP
sudo pip install web.py
以一个简单的webpy程序作为示例。以下代码是一个完整的webpy程序(webpyserv.py)
  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. import web
  4.  
  5. urls = (
  6. '/(.*)', 'myweb'
  7. )
  8.  
  9. app = web.application(urls, globals())
  10.  
  11. class myweb:
  12. def GET(self, name):
  13. if not name:
  14. name = "World"
  15. return "Hello %s, this is my first web!" % name
  16.  
  17. application = app.wsgifunc()
  18.  
  19. #最后一句的application = app.wsgifunc()是关键,此时才可以通过wsgi进行访问
uwsgi -s 127.0.0.1:9000 -w webpyserv
7、更改nginx配置
  1. server {
  2. listen 80;
  3. server_name 104.224.128.197;
  4.  
  5. #charset koi8-r;
  6.  
  7. #access_log logs/host.access.log main;
  8.  
  9. location / {
  10. #root html;
  11. #index index.html index.htm;
  12. include uwsgi_params;
  13. uwsgi_pass 127.0.0.1:9000; #这里配置必须和启动uwsgi时的一致
  14. uwsgi_param UWSGI_CHDIR /home/www; #程序所在的目录
  15. uwsgi_param UWSGI_SCRIPT webpyserv; #
  16. }
注意:uwsgi_pass的相关配置必须和启动uwsgi时的一致!UWSGI_CHDIR是指程序所在的目录,UWSGI_SCRIPT是指启动哪个程序(注意,这里必须去掉py后缀)
重启nginx命令为(/usr/local/nginx/sbin/nginx -s realod)
通过http即可访问
http://104.224.128.197/eric
页面打印:Hello eric, this is my first web!
OVER!