flask实例项目发布到centos-nginx+uWSGI+flask
flask做的微博microblog发布到centos服务器,反向代理使用nginx, wsgi采用uWSGI。
nginx采用源码编译安装
第一步.nginx安装和配置
参考http://www.nginx.cn/installcentos平台编译环境使用如下指令
安装make:
yum -y install gcc automake autoconf libtool make
安装g++:
yum install gcc gcc-c++
下面正式开始
一般我们都需要先装pcre, zlib,前者为了重写rewrite,后者为了gzip压缩。
1.选定源码目录
可以是任何目录,本文选定的是/usr/local/src2.安装PCRE库
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:cd /usr/local/src
cd /root/download
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.38.tar.gz
tar -zxvf pcre-8.38.tar.gz
cd pcre-8.38
./configure
make && make install
3.安装zlib库
http://zlib.net/zlib-1.2.8.tar.gz 下载最新的 zlib 源码包,使用下面命令下载编译和安装 zlib包:cd /usr/local/src
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf zlib-1.2.8.tar.gz
cd zlib-1.2.8
./configure
make
make install
4.安装ssl(某些vps默认没装ssl)
cd /usr/local/src
wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz
tar -zxvf openssl-1.0.1c.tar.gz
5.安装nginx
Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到 /usr/local/nginx 目录下的详细步骤:
cd /usr/local/src
wget http://nginx.org/download/nginx-1.4.2.tar.gz
wget http://nginx.org/download/nginx-1.9.9.tar.gz
tar -zxvf nginx-1.9.9.tar.gz
cd nginx-1.9.9
./configure --sbin-path=/usr/local/nginx/nginx--conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.38 --with-zlib=/usr/local/src/zlib-1.2.8 --with-openssl=/usr/local/src/openssl-1.0.1c
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/nginx --conf-path=/etc/nginx/nginx.conf --with-http_stub_status_module --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx/nginx.pid --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/tmp/nginx/client --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fcgi
make && make install
--with-pcre=/usr/src/pcre-8.34 指的是pcre-8.34 的源码路径。
--with-zlib=/usr/src/zlib-1.2.7 指的是zlib-1.2.7 的源码路径。
6、配置nginx
配置文件时确保80端口没有被占用
netstat -ano|grep 80
/usr/local/nginx/nginx.conf
server {listen 80;server_name localhost;
location / {include uwsgi_params;uwsgi_pass 127.0.0.1:5010;root html;index index.html index.htm;}
如果原来有apache httpd,还需要修改httpd的配置文件,以免端口冲突,因为默认apache httpd的端口是80.
Apache httpd使用8080端口,配置文件:/etc/httpd/conf/httpd.conf
第二步安装uWSGI
准备uwsgi(参考linux_flask_uWSGI_NGINX_Deployed.pdf)
centos服务器上面的自带python版本是2.6,所以我安装了2.7以后,全部在后面加上2.7,比如python2.7,pip2.7。
使用pip安装
使用pip安装
pip2.7 install uwsgi当前版本是uwsgi-2.0.13.1或者pip2.7 install http://projects.unbit.it/downloads/uwsgi-latest.tar.gz
或者源代码编译
wget http://projects.unbit.it/downloads/uwsgi-latest.tar.gzyum groupinstall "Development Tools"yum install pythonyum install python-devel编辑uWSGI编译配置文件
python uwsgiconfig.py --build################# uWSGI configuration #################pcre = Truekernel = Linuxmalloc = libcexecinfo = Falseifaddrs = Truessl = Truezlib = Truelocking = pthread_mutexplugin_dir = .timer = timerfdyaml = embeddedjson = Falsefilemonitor = inotifyrouting = Truedebug = Falsecapabilities = Falsexml = libxml2event = epoll############## end of uWSGI configuration #############
*** uWSGI is ready, launch it with ./uwsgi ***
在执行路径中添加上软link
ln -s /usr/local/python2.7/bin/uwsgi /usr/local/bin/uwsgi2.7ln -s /usr/local/python2.7/bin/uwsgi /usr/local/bin/uwsgi
第三步 为flask app配置uWSGI的启动文件
为flask项目写一个配置文件,配置文件可以有几种格式,ini,xml等等。
这里以ini为例
[uwsgi]socket = 127.0.0.1:5010processes = 4threads = 2master = truepythonpath =/root/apps/microblogmodule = runpcallable = appmemory-report = true
uwsgi -i /root/apps/microblog/uwsgi_config.ini -d /root/apps/uwsgi_log
第四步 配置centos启动文件
centos的启动配置很简明
在/etc/rd.local文件后面加上你的启动命令
/usr/local/bin/nginx/usr/local/bin/uwsgi -i /root/apps/microblog/uwsgi_config.ini -d /root/apps/uwsgi_log