Nginx+uwsgi+web.py

基础案例
操作系统:Centos7
文件:
Python-2.7.13.tgz
下载地址:https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tgz
nginx-1.12.0.tar.gz
下载地址:http://nginx.org/download/nginx-1.12.0.tar.gz
uwsgi-2.0.15.tar.gz
下载地址:https://projects.unbit.it/downloads/uwsgi-2.0.15.tar.gz
1、安装Python
configure注意带参数--with-zlib,否则uwsgi会报错
tar -zxvf Python-2.7.13.tgz
cd Python-2.7.13
./configure --with-zlib
make
make install
make clean
python --version 可以查看版本
修改版本会导致一些小问题,可以尝试修改#!/usr/bin/python为#!/usr/bin/python2.7
2、安装配置nginx
安装没什么特别的
tar -zxvf nginx-1.12.0.tar.gz
cd nginx-1.12.0
./configure
make
make install
make clean
配置
如果没有自定义安装路径
nginx.conf文件默认路径为/usr/local/nginx/conf/nginx.conf
如果用yum安装配置路径为/etc/nginx/nginx.conf
可以试着查找
find /|grep nginx.conf
whereis nginx
修改nginx.conf 文件,不必担心改坏了,同目录下还有个nginx.conf.default
如果80端口有其它用处,可以把listen 80; 改成其它端口,避免冲突
内容不妨先仿着server再写一个server与原来的server保持并列,

server { listen 2001; server_name [::]:2001 localhost; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8080; } }

listen应该是监听,那么从外部访问应该访问2001端口
uwsgi_pass 127.0.0.1:8080; 这句与proxy_pass看起来很像,向内转发数据,并且需要在8080端口上有监听,uwsgi会处理这事
启动nginx
/usr/local/nginx/sbin/nginx
可以带配置文件,默认用/usr/local/nginx/conf/nginx.conf
试着访问一下x.x.x.x:80与x.x.x.x:2001
3、写一个test.py
暂且放在/var/test/路径下
#!/usr/bin/python2.7 # -*- coding:utf-8 -*- # test.py import web urls = ( '/', 'index' ) class index: def GET(self): return 'Hello, world!' app = web.application(urls,globals()) # app.run() application = app.wsgifunc()

如果没有web模块可以先pip install web.py
试试会不会报错python code.py
4、安装配置uwsgi
这里有更详细的说明
http://uwsgi-docs.readthedocs.io/en/latest/Install.html
tar -zxvf uwsgi-2.0.15.tar.gz
cd uwsgi-2.0.15
python uwsgiconfig.py --build
配置文件
支持多种格式,这里用ini
为code.py写一个配置文件test.uwsgi.ini
[uwsgi] socket = 127.0.0.1:8080 #与nginx的uwsgi_pass对应 chdir = /var/test/ wsgi-file = test.py processes = 4 threads = 2 stats = 127.0.0.1:2011 daemonize = ./uwsgi.log

启动
./uwsgi --wsgi-file test.uwsgi.ini
如果正常的话
curl x.x.x.x:2001
将会返回Hello,world
否则可以到uwsgi.log中查看错误信息
【Nginx+uwsgi+web.py】查看端口占用
lsof -i:80
报错整理:
urlopen error unknown url type: https
参考:http://www.xinotes.net/notes/note/628/
cd python源码目录/Modules
vi Setup.dist
找到并取消注释成
# Socket module helper for SSL support; you must comment out the other # socket line above, and possibly edit the SSL variable: SSL=/usr/local/ssl _ssl _ssl.c \ -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \ -L$(SSL)/lib -lssl -lcrypto

重新安装
cd ..
./configure --with-zlib
make
make install
fatal error: openssl/sha.h: No such file or directory
参考:http://blog.csdn.net/xxxxxx91116/article/details/7927520
Centos 安装
yum install openssl-devel

    推荐阅读