Nginx + uWSGI + web.py 搭建
Nginx + uWSGI + web.py 搭建
在Centos或Ubuntu下使用Nginx+UWSGI部署web.py
(1)安装Nginx
1.1 下载nginx-1.0.5.tar.gz并解压
1.2 ./configure (也可以增加--prefix= path指定安装路径)
此时有可能会提示缺少pcre支持,如果要安装pcre的话可以通过 yum install pcre-devel 来实现安装
1.3 make
1.4 make install
注:在ubuntu下,直接用sudo apt-get install nginx
Ubuntu 默认源里面的Nginx版本比较旧,这里需要先添加一个Nginx的源,来通过apt-get安装新版本的Nginx
sudo add-apt-repository ppa:nginx/stable
apt-get update
apt-get install nginx
(2)安装uWSGI
2.1 下载uwsgi-0.9.8.2.tar.gz并解压
2.2 make
在安装uWSGI的时候有可能提示说是libxml2不存在,针对此情况,建议通过 yum install libxml2-devel来解决
(ubuntu下可用apt-get install libxml2-dev)
如编译出现下问题:
In file included from plugins/python/python_plugin.c:1:0:
plugins/python/uwsgi_python.h:2:20: fatal error: Python.h: No such file or directory
compilation terminated.
需安装python-dev(ubuntu下出现依赖问题,可使用aptitude install python-dev)
2.3 编译完后将得到的 uwsgi 复制到系统目录:sudo cp uwsgi /usr/sbin/
拷贝到系统目录的应用程序,可以直接在终端输入命令而不带路径
(3)启动uWSGI
3.1 用IP 方式
uwsgi -s 127.0.0.1:9000 -w webpytest
如提示:unable to load app 0 (mountpoint='') (callable not found or import error),
则是因为uwsgi找不到入口地址:必须为如下:
application=app.wsgifunc()
3.2 用Socket方式(推荐)
uwsgi -s /tmp/uwsgi.sock -C -M -p 4 -t 30 --limit-as 128 -R 10000 –vhost
-d /tmp/uwsgi.log --pidfile /tmp/uwsgi.pid --pythonpath /var/www
表示用 unix socket 方式执行 uwsgi,
注意:/temp/uwsgi.sock文件名和路径为自己命名,运行时自动创建,
nginx用这个文件与uwsgi交互,nginx配置为:
uwsgi_pass=unix:/tmp/uwsgi.sock;
-C 表示将 /tmp/uwsgi.sock 文件权限
改成 666 以便 nginx 可以读取(或使用chmod 666 /tmp/uwsgi.socket),否则会无权限访问导致502
-M 表示启动管理进程,
-p 4 表示预生成 4 个 worker 子进程,
-t 30 是 cgi 程序超时,--limit-as 128 表示限制内存最大 128M,
-R 10000 表示每个 worker 处理的最大请求数,
--vhost 表示启用虚拟服务器,
-d /tmp/uwsgi.log 表示以守护进程方式启动,指定日志文件。
这样启动的 uwsgi 可以被多网站共用
如: uwsgi -s /tmp/uwsgi.sock -c –vhost
在nginx下则:
在enctype=”multipart/form-data”的表单提交如出现异常如:
key: note
value: '\r\n-----------------------------7dc12726d0580\r\nContent-Disposition: form-data; name="desc"\r\n\r\n'
则可以加上- -post-buffering=1024
大于1k的将会以文件形式存储
UWSGI配置WIKI
http://projects.unbit.it/uwsgi/wiki/Doc
http://archive.cnblogs.com/a/2379646/(4)更改Nginx相关配置(nginx.conf)
server { listen 80; server_name localhost; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:9000; uwsgi_param UWSGI_CHDIR /var/www; uwsgi_param UWSGI_SCRIPT main; } |
注意:uwsgi_pass 的相关配置必须和启动uwsgi时的一致!UWSGI_CHDIR是指程序所在的目录,
UWSGI_SCRIPT是指启动哪个程序(注意,这里必须去掉py后缀)
UWSGI_CHDIR 和UWSGI_SCRIPT在部署多个网站共用一个uwsgi进程时用到
(5)启动nginx
/usr/local/nginx/sbin/nginx
重启nginx命令为(/usr/local/nginx/sbin/nginx -s stop)
/usr/local/nginx/sbin/nginx -t 测试配置是否正确
然后再打开网址测试
(6)开机自动启动
可以将启动nginx和uwsgi的命令放入 /etc/rc.local 作为开机自启动。
如:
/etc/local/nginx/sbin/nginx
nohup uwsgi –s /tmp/uwsgi.sock –m –c -p 4 –r 1000 –t 30 –vhost &
注:nohup用于后台运行,不会导致开机挂起
文章由苦瓜博客版权所有,转载请表明出处blog.ops.cc。苦瓜博客为您提供Python,C#,网站开发,SEO,理财方面的信息!

浙公网安备 33010602011771号