uwsgi菜鸟使用教程

介绍

uWSGI项目旨在为构建托管服务开发全栈。

使用通用的API和通用的配置风格来实现应用服务器 (对于各种编程语言和协议),代理,进程管理器和监控器。

由于其可插拔架构,可以对其扩展以支持更多的平台和语言。

目前,你可以用C, C++和Objective-C来编写插件。

名字中的”WSGI”部分归功于同名Python标准,因为它是该项目第一个开发的插件。

通用性、高性能、低资源使用和可靠性是该项目的强项(也是唯一遵循的法则)。

1, 安装

pip install uwsgi
注意点: 需要保证uwsgi大于1.4

2, 命令行http部署

  • 1, 创建文件foobar.py

    def application(env, start_response):
        start_response('200 OK', [('Content-Type','text/html')])
        return [b"Hello World"] # python3 以上的写法
    
  • 2, 将该文件部署在uwsgi服务器上,

    • 单进程, 单线程启动
      uwsgi --plugin python --http :8001 --wsgi-file foobar.py
    • 多进程, 多线程启动
      uwsgi --plugin python --http :8001 --wsgi-file foobar.py --master --processes 4 --threads 2
    • 多进程, 多线程启动, 并绑定uwsgi服务运行信息获取ip和端口
      uwsgi --plugin python --http :8001 --wsgi-file foobar.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191 --stats-http

3, 终端, 或者浏览器测试

3, 配置文件http部署

  • 1, 编写config.ini配置文件, 内容如下

    	[uwsgi]
    	#监听的ip和端口
    	http = 127.0.0.1:8899
    	
    	#指定python解释器
    	plugin = python
    	
    	#启动文件
    	wsgi-file = foobar.py
    
    	进程
    	processes = 4
    	
    	线程
    	threads = 2
    	
    	uwsgi运行信息获取(json格式)
    	stats = 127.0.0.1:9191
    
  • 2, 启动config.ini文件
    uwsgi config.ini --stats-http

4, 配置文件socket部署

  • 1, socket启动文件配置
	[uwsgi]
	#监听的ip和端口
	socket = 127.0.0.1:8899
	
	#指定python解释器
	plugin = python
	
	#启动文件
	wsgi-file = foobar.py
	
	#进程
	processes = 4
	
	#线程
	threads = 2
	
	#uwsgi运行信息获取(json格式)
	stats = 127.0.0.1:9191
  • 2, 启动
    uwsgi config_socket.ini --stats-http

5, nginx转发http部署

  • 1, nginx配置

    	  server {
    	  	  listen 80;
    		  location / {
    		  	 proxy_pass http://127.0.0.1:8899
      		  }
    	  }
    
  • 2, 启动
    uwsgi --plugin python --http :8001 --wsgi-file foobar.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191 --stats-http

  • 3, 访问

6, nginx转发socket部署

  • 注意点: 使用socket运行程序后只能通过nginx做中转, 不能直接通过浏览器访问

  • 1, nginx配置

    	  server {
    	  	  listen 80;
    		  location / {
    		  	include uwsgi_params;
    			uwsgi_pass 127.0.0.1:8899;
      		  }
    	  }
    
  • 2, 创建config_socket.ini配置文件

    [uwsgi]
    #监听的ip和端口
    socket = 127.0.0.1:8899
    
    #指定python解释器
    plugin = python
    
    #启动文件
    wsgi-file = foobar.py
    
    #进程
    processes = 4
    
    #线程
    threads = 2
    
    #uwsgi运行信息获取(json格式)
    stats = 127.0.0.1:9191
    
    
  • 3, 运行配置文件, 部署项目
    uwsgi config_socket.ini --stats-http

  • 4, 运行测试

7, http部署django项目

  • 1, 创建django项目, 并创建一个简单子应用, 编写类视图helloworld
  • 2,运行
    uwsgi --virtualenv=/Users/heJing/.virtualenvs/django_py3 --http :3031 --chdir /Users/heJing/Desktop/Python_Class/basic45测试/day06_test/test01 --wsgi-file test01/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191
  • 3,访问测试

更多推荐

uwsgi菜鸟使用教程