天天看點

flask執行個體項目釋出到centos-nginx+uWSGI+flaskflask執行個體項目釋出到centos-nginx+uWSGI+flask

flask執行個體項目釋出到centos-nginx+uWSGI+flask

    flask做的微網誌microblog釋出到centos伺服器,反向代理使用nginx, wsgi采用uWSGI。         nginx采用源碼編譯安裝

第一步.nginx安裝和配置

       參考http://www.nginx.cn/install

centos平台編譯環境使用如下指令

安裝make:

yum -y install gcc automake autoconf libtool make

安裝g++:

yum install gcc gcc-c++

下面正式開始

一般我們都需要先裝pcre, zlib,前者為了重寫rewrite,後者為了gzip壓縮。

1.標明源碼目錄

可以是任何目錄,本文標明的是/usr/local/src

2.安裝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安裝

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.gz yum groupinstall "Development Tools" yum install python yum install python-devel 編輯uWSGI編譯配置檔案
python uwsgiconfig.py --build
################# uWSGI configuration ################# 
pcre = True 
kernel = Linux 
malloc = libc 
execinfo = False 
ifaddrs = True 
ssl = True 
zlib = True 
locking = pthread_mutex 
plugin_dir = . 
timer = timerfd 
yaml = embedded 
json = False 
filemonitor = inotify 
routing = True 
debug = False 
capabilities = False 
xml = libxml2 
event = 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.7 ln -s  /usr/local/python2.7/bin/uwsgi /usr/local/bin/uwsgi

第三步 為flask app配置uWSGI的啟動檔案

為flask項目寫一個配置檔案,配置檔案可以有幾種格式,ini,xml等等。

這裡以ini為例

[uwsgi] socket = 127.0.0.1:5010 processes = 4 threads = 2 master = true pythonpath =/root/apps/microblog module = runp callable = app memory-report = true

uwsgi -i /root/apps/microblog/uwsgi_config.ini -d /root/apps/uwsgi_log

[uWSGI] getting INI configuration from /root/apps/microblog/uwsgi_config.ini

第四步 配置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

繼續閱讀