Ubuntu uwsgi + nginx / bottle 的配置方法

首先赞PT大牛帮了很多忙, 本文大部分原型来源于 http://apt-blog.net/moinmoin-on-nginx-via-fastcgi-and-uwgi

UPDATE: 更新到 nginx 1.0 / uwsgi 0.9.6.8 的配置 – 2011/4/15
然而由于现在那几个玩意版本更新挺厉害, 有许多配置都不一样了, 再加上本猫是要让它运行bottle, 所以决定重新整理一下:)

下面这段关于 uwsgi 的介绍 来自PT牛的那篇文章:

uwsgi充当了python解析器的角色,使用wsgi的接口和Python程序交互,这个过程中做了优化,和上层nginx之间则设计了更加轻量的协议。nginx0.8.40以后官方默认带了uwsgi的协议模块,所以使用很方便。

恩, 由上面这段话 我们可以淡定的看出, 官方源里那个残念的0.7.x的nginx肯定是满足不了要求的啦!


所以, 首先添加nginx的源:

sudo add-apt-repository ppa:nginx/stable

然后添加uwsgi的源:

sudo add-apt-repository ppa:uwsgi/release

由于uwsgi源暂无natty版本, 如果你的系统是 Natty, 那麻烦手动把uwsgi源的发行版从natty改回maverick=.=

然后, 装上他们吧!

sudo apt-get update
sudo apt-get install uwsgi-python nginx

然后配置一下uwsgi, 如果你不知道怎么配置, 全部照抄下面PT牛的吧!

<uwsgi>
  <memory-report>
  <vhost>
  <no-site>
</no-site></vhost></memory-report></uwsgi>

如果你安装的uwsgi是上面的版本, 这个文件应该放在 /etc/uwsgi-python/apps-available/<confname>.xml
别忘记在 /etc/uwsgi-python/apps-enabled/ 里建立这个文件的软链接 🙂

ln -s /etc/uwsgi-python/apps-available/.xml /etc/uwsgi-python/apps-enabled/

然后, 修改nginx配置, 让它调用uwsgi来处理py服务:
修改 /etc/nginx/sites-enabled/default 为:

server {
        listen   80; ## listen for ipv4
	# Make site accessible from http://localhost/
	server_name localhost;
	location / {
                include uwsgi_params;
                uwsgi_pass unix:///var/run/uwsgi-python//socket;
                uwsgi_param UWSGI_PYHOME /source/to/your/file/;
                uwsgi_param UWSGI_CHDIR /source/to/your/file/;
                uwsgi_param UWSGI_SCRIPT ;
        }
}

附上一个测试用的 bottle helloworld:

import os
from bottle import route, run, default_app
@route('/')
def main():
    return "Hello world!"
if __name__ == "__main__":
    # Interactive mode
    run()
else:
    # Mod WSGI launch
    os.chdir(os.path.dirname(__file__))
    application = default_app()

提醒: bottle.py必须也拷贝到目录中, 不要以为系统的python能直接import bottle就够了=.=

然后, 重启 nginx 和 uwsgi

sudo /etc/init.d/nginx restart
sudo /etc/init.d/uwsgi-python restart

尝试访问一下 http://localhost ?

PS: 如果想修改uwsgi的默认设置(比如processes之类的), 配置文件在 “/etc/default/uwsgi-python”, 顺便附上我的设置:

DAEMON_OPTS=" \
  --master \
  --no-orphans \
  --processes 8 \
  --limit-as 512 \
  --logdate \
  --chmod-socket=660 \
  --uid www-data \
  --gid www-data \
"

30 thoughts on “Ubuntu uwsgi + nginx / bottle 的配置方法”

  1. upstream prematurely closed connection while reading response header from upstream, client: 127.0.0.1, server: localhost, request: “GET / HTTP/1.1”, upstream: “uwsgi://unix:///var/run/uwsgi/app/uwsgi/socket:”, host: “localhost” 这个错误你遇到过吗

    1. Sat May 12 19:08:41 2012 – current working directory: /
      Sat May 12 19:08:41 2012 – writing pidfile to /run/uwsgi/app/uwsgi/pid
      Sat May 12 19:08:41 2012 – detected binary path: /usr/bin/uwsgi-core
      Sat May 12 19:08:41 2012 – setgid() to 33
      Sat May 12 19:08:41 2012 – setuid() to 33
      Sat May 12 19:08:41 2012 – your memory page size is 4096 bytes
      Sat May 12 19:08:41 2012 – *** WARNING: you have enabled harakiri without post buffering. Slow upload could be rejected on post-unbuffered webservers ***
      Sat May 12 19:08:41 2012 – uwsgi socket 0 bound to TCP address 127.0.0.1:8888 fd 6
      Sat May 12 19:08:41 2012 – uwsgi socket 1 bound to UNIX address /run/uwsgi/app/uwsgi/socket fd 7
      Sat May 12 19:08:41 2012 – your server socket listen backlog is limited to 100 connections
      Sat May 12 19:08:41 2012 – *** Operational MODE: preforking ***
      Sat May 12 19:08:41 2012 – *** no app loaded. going in full dynamic mode ***
      Sat May 12 19:08:41 2012 – *** uWSGI is running in multiple interpreter mode ***
      Sat May 12 19:08:41 2012 – spawned uWSGI master process (pid: 3220)
      Sat May 12 19:08:41 2012 – spawned uWSGI worker 1 (pid: 3228, cores: 1)
      Sat May 12 19:08:41 2012 – spawned uWSGI worker 2 (pid: 3229, cores: 1)
      Sat May 12 19:08:44 2012 – — unavailable modifier requested: 0 —
      Sat May 12 19:09:39 2012 – — unavailable modifier requested: 0 —
      Sat May 12 19:12:55 2012 – — unavailable modifier requested: 0 —

        1. 我想说,我碰到了和这位仁兄@abcd一样的问题。很可惜他没贴出解决方案。我最后的解决办法是参看stackoverflow,在uwsgi启动的时候加上python插件=。=当然,他不一定用的python

          1. 如果用我blog里给的ppa:uwsgi/release 安装 uwsgi-python 包, 默认就配置好了python插件, 不需要手动干预.

      1. 路径应该没错的,会不会是uWSGI 1.0.3-debian 这个版本配置不一样呢

  2. 那个xml建议精简成:

    /tmp/uwsgi.sock

    因为其他的项在/etc/default/uwsgi-python里面已经做了定义,重复了。

Leave a Reply

Your email address will not be published. Required fields are marked *

QR Code Business Card