以下基本是在文章 在Ubuntu上使用Nginx部署Flask 应用 上做了部分调整,没有使用virtualenv,python版本3.4。
依赖
安装pip3
sudo apt-get install python3-setuptools sudo easy_install3 pip
安装python3-dev,uwsgi依赖,否则直接安装uwsgi可能会遇到python.h找不到的问题
sudo apt-get install python3-dev
安装Flask
sudo pip3 install Flask
安装uwsgi
sudo pip3 install uwsgi
项目路径
将Flask项目移动到/var/www/目录下(非必须),假设工程文件夹叫做pydemo,该文件夹下包含的Flask入口文件为demo.py,
设置一下项目文件权限归xuanzhui所有
sudo chown -R xuanzhui:xuanzhui /var/www/pydemo/
注意:按照Flask官网的说法,部署的python文件中,所有app.run() 必须放在if __name__ == '__main__'中
Please make sure in advance that any app.run()
calls you might have in your application file are inside an if __name__ == '__main__':
block or moved to a separate file. Just make sure it’s not called because this will always start a local WSGI server which we do not want if we deploy that application to uWSGI.
Nginx
安装
sudo apt-get install nginx
配置文件路径 /etc/nginx/conf.d/
启动
sudo /etc/init.d/nginx start
停止
sudo /etc/init.d/nginx stop
重启
sudo /etc/init.d/nginx restart
配置Nginx
删除默认的配置文件(不删也无所谓)
sudo rm /etc/nginx/sites-enabled/default
直接在工程目录下创建一个配置文件/var/www/pydemo/pydemo_nginx.conf
文件内容
server { listen 80; server_name localhost; charset utf-8; client_max_body_size 75M; location / { try_files $uri @yourapplication; } location /static { root /var/www/pydemo/; } location @yourapplication { include uwsgi_params; uwsgi_pass unix:/var/www/pydemo/pydemo_uwsgi.sock; } }
将刚建立的配置文件使用符号链接到Nginx配置文件文件夹中,重启Nginx
sudo ln -s /var/www/pydemo/pydemo_nginx.conf /etc/nginx/conf.d/ sudo /etc/init.d/nginx restart
配置uWSGI
在工程目录下创建配置文件 /var/www/pydemo/pydemo_uwsgi.ini
文件内容
[uwsgi] #application's base folder base = /var/www/pydemo chdir = /var/www/pydemo #python module to import module = demo pythonpath = /usr/bin/python3.4 #socket file's location #%n the file name without extension socket = /var/www/pydemo/%n.sock #permissions for the socket file chmod-socket = 666 #the variable that holds a flask application inside the module callable = app #location of log files logto = /var/log/uwsgi/%n.log
chdir在下面启动uwsgi emperor时是必须的
socket要和nginx中设置uwsgi_pass的文件名一致
然后创建log存放的文件夹并设置该文件夹的权限归xuanzhui
sudo mkdir -p /var/log/uwsgi sudo chown -R xuanzhui:xuanzhui /var/log/uwsgi
此时运行uwsgi,通过localhost就可以访问资源
uwsgi --ini /var/www/pydemo/pydemo_uwsgi.ini
为了让uwsgi在后台运行,继续配置emperor,此处使用systemd
uWSGI Emperor
创建一个systemd unit文件 /etc/systemd/system/emperor.uwsgi.service
文件内容
[Unit] Description=uWSGI Emperor After=syslog.target [Service] ExecStart=/usr/local/bin/uwsgi --master --emperor /etc/uwsgi/vassals --die-on-te rm --uid xuanzhui --gid xuanzhui --logto /var/log/uwsgi/emperor.log # Requires systemd version 211 or newer RuntimeDirectory=uwsgi Restart=always KillSignal=SIGQUIT Type=notify StandardError=syslog NotifyAccess=all [Install] WantedBy=multi-user.target
ExecStart运行uWSGI守护进程并让它到/etc/uwsgi/vassals文件夹查找配置文件。创建这个文件夹,在其中建立一个链到我们刚创建配置文件的符号链接。
sudo mkdir /etc/uwsgi && sudo mkdir /etc/uwsgi/vassals sudo ln -s /var/www/pydemo/pydemo_uwsgi.ini /etc/uwsgi/vassals
sudo systemctl enable emperor.uwsgi.service
enable之后将上面的uwsgi进程kill掉,启动emperor服务,所有配置完成
sudo systemctl start emperor.uwsgi.service
关闭
sudo systemctl stop emperor.uwsgi.service
重启
sudo systemctl restart emperor.uwsgi.service
日志文件
uWSGI Emperor将日志写到/var/log/uwsgi/emperor.log;这个文件夹还包含着每个应用的单独日志:/var/log/uwsgi/pydemo_uwsgi.log。
ref http://uwsgi-docs.readthedocs.org/en/latest/Systemd.html