zoukankan      html  css  js  c++  java
  • (转)Nginx + uwsgi + web.py + MySQLdb

    一、背景知识:

    Web服务器上有多种方式运行Python:

    1. 传统上的CGI方式:基本已被淘汰
    2. mod_python的模块方式:仍在广泛使用,但生产环境中也被逐步淘汰
    3. uwsgi方式:目前生产环境下最受推崇的方式,尤其是Nginx+uwsgi服务器组合是上线的首选。

    Python与Web服务器的关系基本上和PHP与Web服务器的关系类似,可以参考该文:[ CentOS6下搭建 Nginx + PHP + MySQL服务器 ]。

    uwsgi服务器的作用类似于java里面的Tomcat服务器。至于在上面跑什么样的Python程序那可能性就太多了。 
    例如:

    • 按照uwsgi的规范直接编写的python代码
    • 重量级Web框架:Django编写的应用
    • 轻量级Web框架:Flask编写的应用
    • 轻量级Web框架:web2py编写的应用

    Python下的Web框架真是太多了。很难说孰好孰坏,应该说没有最好的,只有最合适的。最简单的一个选择原则是:

    • 如果开发的只是一个简单的web应用的话,推荐使用轻量级的。
    • 如果是开发一个复杂的网站,使用Django的话很多成熟的模块都可以直接使用,开发效率会非常高。

    这里选择web.py框架作为例子。

    二、安装Nginx + uwsgi + web.py + MySQLdb

    1.  升级Python至2.7 
    参照: CentOS下编译安装Python2.7

    2. 安装Nginx 
    #rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm 
    #yum install nginx

    3. 安装MySQL数据库 
    #yum -y install mysql mysql-server mysql-devel libdbi-dbd-mysql 
    #service mysqld start 
    #chkconfig mysqld on

    4. 安装MySQLdb ( mysql-python ) 
    #easy_install mysql-python

    5. 安装web.py ( 官网 ) 
    #easy_install web.py

    6. 安装uwsgi ( 官网 ) 
    #easy_install uwsgi

    7. 配置uwsgi 
    uwsgi 的配置文件 可支持xml yaml ini等格式。这里使用ini格式的配置文件。默认路径为/etc/uwsgi.ini。

    [uwsgi] 
    #使用动态端口,启动后将端口号写入以下文件中
    socket = /tmp/uwsgi_vhosts.sock
    #也可以指定使用固定的端口
    #socket=127.0.0.1:9031 
    pidfile=/var/run/uwsgi.pid 
    daemonize=/var/log/uwsgi.log 
    
    master=true 
    vhost=true 
    gid=nginx 
    uid=nginx 
    
    #性能相关的一些参数,具体内容查看官网文档
    workers=50
    max-requests=5000 
    limit-as=512

    8. 创建uwsgi开机自启动脚本,便于进行系统管理 
    vi /etc/init.d/uwsgi,内容如下:

    #! /bin/sh  
    # chkconfig: 2345 55 25  
    # Description: Startup script for uwsgi webserver on Debian. Place in /etc/init.d and  
    # run 'update-rc.d -f uwsgi defaults', or use the appropriate command on your  
    # distro. For CentOS/Redhat run: 'chkconfig --add uwsgi'  
    
    ### BEGIN INIT INFO  
    # Provides:			 uwsgi  
    # Required-Start:	 $all  
    # Required-Stop:	  $all  
    # Default-Start:	  2 3 4 5  
    # Default-Stop:		0 1 6  
    # Short-Description: starts the uwsgi web server  
    # Description:		 starts uwsgi using start-stop-daemon  
    ### END INIT INFO  
    
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin  
    DESC="uwsgi daemon"  
    NAME=uwsgi  
    DAEMON=/usr/bin/uwsgi  
    CONFIGFILE=/etc/$NAME.ini  
    PIDFILE=/var/run/$NAME.pid  
    SCRIPTNAME=/etc/init.d/$NAME  
    
    set -e  
    [ -x "$DAEMON" ] || exit 0  
    
    do_start() {  
       $DAEMON $CONFIGFILE || echo -n "uwsgi already running"  
    }  
    
    do_stop() {  
       $DAEMON --stop $PIDFILE || echo -n "uwsgi not running"  
       rm -f $PIDFILE  
       echo "$DAEMON STOPED."  
    }  
    
    do_reload() {  
       $DAEMON --reload $PIDFILE || echo -n "uwsgi can't reload"  
    }  
    
    do_status() {  
       ps aux|grep $DAEMON  
    }  
    
    case "$1" in  
     status)  
       echo -en "Status $NAME: 
    "  
       do_status  
     ;;  
     start)  
       echo -en "Starting $NAME: 
    "  
       do_start  
     ;;  
     stop)  
       echo -en "Stopping $NAME: 
    "  
       do_stop  
     ;;  
     reload|graceful)  
       echo -en "Reloading $NAME: 
    "  
       do_reload  
     ;;  
     *)  
       echo "Usage: $SCRIPTNAME {start|stop|reload}" >&2  
       exit 3  
     ;;  
    esac  
    
    exit 0

    将脚本属性修改为可执行: 
    #chmod 755 /etc/init.d/uwsgi

    启用开机自动启动: 
    #chkconfig uwsgi on

    启动uwsgi服务: 
    #service uwsgi start

    9. 配置nginx下的uwsgi站点 
    例如新增以下一个站点mysite。 
    vi /etc/nginx/conf.d/mysite.conf, 内容:

    server { 
      listen  9091; 
      server_name  localhost; 
      root /www/mysite; 
      index index.html index.htm; 
      access_log /var/log/nginx/mysite_access.log; 
      error_log /var/log/nginx/mysite_error.log; 
      location / { 
        #使用动态端口
        uwsgi_pass unix:///tmp/uwsgi_vhosts.sock;
        #uwsgi_pass 127.0.0.1:9031; 
    
        include uwsgi_params; 
        uwsgi_param UWSGI_SCRIPT uwsgi;   
        uwsgi_param UWSGI_PYHOME $document_root; 
        uwsgi_param UWSGI_CHDIR  $document_root; 
      } 
    }

    10. 启动Nginx服务 
    #service nginx start 
    #chkconfig nginx on

    三、编写一个Hello World! 
    #vi /www/mysite/hello.py,内容:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-  
    
    import web
    
    urls = (
      '/t', 'test', #测试
      '/', 'home'
    )
    
    app = web.application(urls, globals())
    #返回wsgi接口
    application = app.wsgifunc()
    
    class test:
      '测试'	  
    
      def GET(self):
        # 开发测试用
        referer = web.ctx.env.get('HTTP_REFERER', 'http://google.com')
        client_ip = web.ctx.env.get('REMOTE_ADDR')
        host = web.ctx.env.get('host')
        fullpath = web.ctx.fullpath
        user_agent = web.ctx.env.get('HTTP_USER_AGENT')
    
        data = ""
        data += 'Client: %s<br/>
    ' % client_ip
        data += 'User-agent: %s<br/>
    ' % user_agent
        data += 'FullPath: %s<br/>
    ' % fullpath
        data += 'Referer: %s<br/>
    ' % referer
    
        return data
    
      def POST(self):
        pass
    
    class home:
      '根目录请求的处理'		
      def GET(self):
        return "Hello Web.py"
    
      def POST(self):
        return self.GET()
    
    if __name__ == "__main__":
      app.run()

    浏览器访问:

    http://localhost:9091/

    http://localhost:9091/t

    原文:http://www.tudaxia.com/archives/727?utm_source=tuicool&utm_medium=referral

  • 相关阅读:
    Nginx 之负载均衡与反向代理
    PHP 之快递100接口封装
    Mongodb非关系型数据库
    Sphinx全文索引引擎
    PHP 之文件上传类封装
    PHP 之验证码类封装
    小程序 之左滑删除
    PHP 之实现按日期进行分组、分页
    System.IO 二
    IIS配置MIME类型
  • 原文地址:https://www.cnblogs.com/sfnz/p/5103224.html
Copyright © 2011-2022 走看看