Nginx+uwsgi+python配置

版本:Nginx 1.0.5

   uwsgi 1.1.2

   Python 2.7.2

原理: 

  • nginx配置 
location /python {
         include uwsgi_params;
         uwsgi_pass 127.0.0.1:9090;
}
  • uwsgi启动参数

    uwsgi --socket :9090 --wsgi-file /var/www/project/hello.py

  • hello.py 文件
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return "Hello World"

上述过程没有记录细节,感谢热心网友aho友情提供他的完整体验过程

1.    Nginx安装:
  a.    下载Nginx:
      wget http://nginx.org/download/nginx-1.3.9.tar.gz
  b.    解压安装:
      tar -zvxf nginx-1.3.9.tar.gz
      ./configure --prefix=/usr/local/nginx
      sudo make && make install
  c.    配置[可选]
      nginx安装目录下的conf/nginx.conf
      端口号:listen       80; #如果80端口被占,可换别的端口
      html文件目录路径:root html; #此处是相对路径,可根据自身需求表更,比如/usr/local/xxoo等
2.    uwsgi安装:
  a.    下载并安装uwsgi:
      wget http://projects.unbit.it/downloads/uwsgi-1.0.4.tar.gz
      tar -zxvf uwsgi-1.0.4.tar.gz # 解压
      mv uwsgi-1.0.4 uwsgi # 重命名为uwsgi,仅仅为了方便
      cd uwsgi # 切换到uwsgi目录
      python setup.py build # 编译安装
      make
  b.    将编译产生的可执行文件移动到/usr/bin里面去
      mv uwsgi /usr/bin
3.    配置并启动第一个实例:
  a.    在nginx的配置文件中加上
      location /py {
         include uwsgi_params;
         uwsgi_pass 127.0.0.1:8080;
      }
  b.    创建第一个python文件hello.py,选择目录(比如我的/var/www/application/)
      def application(env, start_response):
          start_response('200 OK', [('Content-Type','text/html')])
          ret = 'hello world'
          return ret
  c.    重启nginx
      sudo service nginx restart
  d.    启动uwsgi
      uwsgi --socket :9090 --wsgi-file /var/www/application/hello.py
  e.    在浏览器地址栏输入localhost/py,查看结果!

 

原文地址:https://www.cnblogs.com/un4sure/p/2483130.html