美文网首页
Ubuntu16.04中使用Nginx部署Flask

Ubuntu16.04中使用Nginx部署Flask

作者: AricWu | 来源:发表于2018-06-01 15:26 被阅读88次

OS:ubuntu16.04
python版本号:python3.52

一、安装pip3

Ubuntu默认有安装python3,但是没有安装pip3

sudo apt install python3-pip

二、安装flask

pip3 install flask

可能会提示Error: unsupported locale setting
是语言环境问题,运行
export LC_ALL=C
可解决
尝试调用import flask可以测试是否安装成功

三、安装Nginx

安装并运行Nginx
sudo apt-get install nginx
sudo /etc/init.d/nginx start
Nginx 需要配合uWSGI才能托管Python程序
sudo pip3 install uwsgi

四、安装virtualenv

virtualenv是一个用于创建虚拟环境的脚本。可以让不同的项目使用自己的一套环境,避免项目间的冲突
使用
pip3 install virtualenv
然后创建虚拟环境
cd /var/www/WebSite
virtualenv venv #创建虚拟环境
source venv/bin/activate #运行虚拟环境
此时命令提示符变了,有个(venv)前缀,表示当前环境是一个名为venv的Python环境
然后正常安装各种包,在venv环境下,用pip安装的包都被安装到venv这个环境下,系统Python环境不受任何影响。
使用deactivate命令可以退出当前环境

五、创建网站代码

cd /var/www/WebSite
touch hello.py
在hello.py中创建

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8080)

运行python3 hello.py可以判断代码是否可运行

六、配置Nginx

首先删除默认的Nginx配置文件
sudo rm /etc/nginx/sites-enabled/default
然后创建一个给网站使用的配置文件
touch /etc/nginx/sites-enabled/website_nginx.conf
编辑此文件
uwsgi_pass为后续我们要创建文件的位置

server {
    listen      80;
    server_name localhost;
    charset     utf-8;
    client_max_body_size 75M;
    location / { try_files $uri @yourapplication; }
    location @yourapplication {
        include uwsgi_params;
        uwsgi_pass unix:/var/www/WebSite/website_uwsgi.sock;
    }
}

将刚建立的配置文件使用符号链接到Nginx配置文件文件夹中,重启Nginx:
sudo ln -s /etc/nginx/sites-enabled/website_nginx.conf /etc/nginx/conf.d/
sudo /etc/init.d/nginx restart
此时访问网站时。状态应该为502.

七、配置uWSGI

/var/www/WebSite目录下创建uwsgi.ini文件
touch /var/www/WebSite/website_uwsgi.ini
使用以下配置

[uwsgi]
#application's base folder
base = /var/www/WebSite
#python module to import
app = hello
module = %(app)
home = %(base)/venv
pythonpath = %(base)
#socket file's location
socket = /var/www/WebSite/%n.sock
#permissions for the socket file
chmod-socket    = 666
#the variable that holds a flask application inside the module imported at line #6
callable = app
#location of log files
logto = /var/log/uwsgi/%n.log

其中logto是存放日志的地方需要自己创建
sudo mkdir -p /var/log/uwsgi

运行uwsgi --ini website_uwsgi.ini然后尝试公网访问。已经可以访问成功

八、配置SUPERVISOR

supervisor 是一个进程管理器。可以用来在后台进程意外卡死时重启进程。从而保证后台服务器的稳定性。

1.安装

sudo apt-get install supervisor或使用pip安装(不推荐)
创建Log文件位置
touch /var/log/supervisor/webSiteLogs/website_supervisor.log
touch /var/log/supervisor/webSiteLogs/website_supervisor_err.log
/etc/supervisor/conf.d下创建网站专用的配置文件
touch /etc/supervisor/conf.d/website
配置该文件

[program:website]
command=uwsgi --ini /var/www/WebSite/website_uwsgi.ini                               
stopsignal=QUIT
autostart=true                                                                        
autorestart=true                                                                       
stdout_logfile=/var/log/supervisor/webSiteLogs/website_supervisor.log                 
stderr_logfile=/var/log/supervisor/webSiteLogs/website_supervisor_err.log   

然后运行
cd /etc/supervisor
sudo supervisord -c supervisord.conf
如果遇到提示
Error: Another program is already listening on a port that one of our HTTP servers is configured to use. Shut this program down first before starting supervisord.
运行一遍
sudo unlink /var/run/supervisor.sock
来停止当前的运行
或者运行supervisorctl -c supervisord.conf reload
使用以下命令可以查看supervisor运行状态或者停止开始运行某个项目

supervisorctl status
supervisorctl stop program_name
supervisorctl start  program_name
...

此时一个简单的网站后台已经完成部署与运行

相关文章

网友评论

      本文标题:Ubuntu16.04中使用Nginx部署Flask

      本文链接:https://www.haomeiwen.com/subject/pghksftx.html