ubuntu下用Nginx和uwsgi部署flask项目

之前简单写过一个接口,下面整理一下,如何配置的过程,算是自己的一个总结。

服务器的环境配置

这里选用的是阿里云的服务器,自带了python2.7的环境,也可以自己安装其他版本,最后做一个软链接到/usr/bin目录下。
这里主要说一下nginx的安装。
简单写了个自动安装脚本,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/bash
apt-get purge pcre*
apt-get remove pcre*
apt-get autoremove
wget http://heanet.dl.sourceforge.net/project/pcre/pcre/8.39/pcre-8.39.tar.gz
tar zxvf pcre-8.39.tar.gz
cd pcre-8.39
./configure
make && make install
cd ..
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
cd ..
wget https://www.openssl.org/source/openssl-1.0.2h.tar.gz
tar zxvf openssl-1.0.2h.tar.gz
cd openssl-1.0.2h
./config
make && make install
cd ..
wget http://luajit.org/download/LuaJIT-2.0.4.tar.gz
tar zxvf LuaJIT-2.0.4.tar.gz
cd LuaJIT-2.0.4
make
make install PREFIX=/usr/local/luajit
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0
export LD_LIBRARY_PATH=/usr/local/luajit/lib:$LD_LIBRARY_PATH
cd ..
wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
wget https://github.com/openresty/lua-nginx-module/archive/v0.10.6.tar.gz
tar -zxvf v0.3.0.tar.gz
tar -zxvf v0.10.6.tar.gz
wget http://nginx.org/download/nginx-1.10.1.tar.gz
tar zxvf nginx-1.10.1.tar.gz
cd nginx-1.10.1
./configure --prefix=/usr/local/nginx \
--with-http_realip_module --with-http_sub_module --with-http_gzip_static_module \
--with-http_stub_status_module --with-http_addition_module --with-http_ssl_module \
--with-pcre=/usr/local/src/pcre-8.39 --with-zlib=/usr/local/src/zlib-1.2.8 \
--with-openssl=/usr/local/src/openssl-1.0.2h \
--add-module=/usr/local/src/ngx_devel_kit-0.3.0 --add-module=/usr/local/src/lua-nginx-module-0.10.6
make && make install

这里安装的是带lua模块的Nginx 1.10.1版本
另外写了一个启动脚本,丢在/etc/init.d目录下,记得 chmod +x, 以下是启动脚本。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/sh
# chkconfig: 345 86 14
# description: Startup and shutdown script for nginx
NGINX_DIR=/usr/local/nginx
export NGINX_DIR
case $1 in
'start' )
echo "Starting nginx..."
$NGINX_DIR/sbin/nginx
;;
'reload' )
echo "Reload nginx configuration..."
kill -HUP `cat $NGINX_DIR/logs/nginx.pid`
;;
'stop' )
echo "Stopping nginx..."
kill -15 `cat $NGINX_DIR/logs/nginx.pid`
;;
'list' )
ps aux | egrep '(PID|nginx)'
;;
'testconfig' )
$NGINX_DIR/sbin/nginx -t
;;
*)
echo "usage: `basename $0` {start|reload|stop|list|testconfig}"
esac

以后可以用service nginx start启动,用service nginx reload重新加载配置文件。
服务器基本环境算是完事了。

上传项目代码

在上传之前记得pip -freeze requirement.txt导出用的第三方python包。这里可以直接用rz上传或者用ftp上传,用ftp需要安装vsftpd服务,apt-get安装一下,然后修改一下配置文件就可以了。
在服务器端还需要安装virtualenv环境,使用

1
pip install virtualenv

然后新建项目目录,在目录下键入virtualenv venv,就会帮你自动安装好虚拟环境的一些包文件,用source venv/bin/activate 来启动虚拟环境,在虚拟环境中用

1
pip install -r requirement.txt

安装flask需要的包,requirement是前面导出的。

Nginx配置

首先安装UWSGI这里需要说明一下,其实不用nginx也可以,只不过nginx可以做负载均衡,然后修改端口啥的比较方便,用pip install uwsgi来安装。安装完后在项目目录下新建config.ini文件,打开,修改如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[uwsgi]
# uwsgi 启动时所使用的地址与端口
socket = 127.0.0.1:8888
# 指向网站目录
chdir = xxxxx/xxx/xx
# python 启动程序文件
wsgi-file = manage.py
# python 程序内用以启动的 application 变量名
callable = app
# 处理器数
processes = 4
# 线程数
threads = 2
#状态检测地址
stats = 127.0.0.1:9191
~

保存以后,通过uwsgi config.ini来启动服务。
接着配置nginx,默认的配置目录在/usr/local/nginx/conf下,打开
nginx.conf,增加一个server如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 8600;
server_name localhost; #公网地址
access_log /mnt/nginx/logs/blog_demo.log;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8888; # 指向uwsgi 所应用的内部地址,所有请求将转发给uwsgi 处理
uwsgi_param UWSGI_PYHOME xxxxx; # 指向虚拟环境目录
uwsgi_param UWSGI_CHDIR xxxxx; # 指向网站根目录
uwsgi_param UWSGI_SCRIPT manage:app; # 指定启动程序
}
}

然后service nginx reload就可以了,访问服务器IP + 端口试试。域名啥的如果有可以在server_name配置。
就酱紫~~~