https://linoxide.com/linux-how-to/install-flask-python-ubuntu/

1. prerequisites

> create a new user: sudo adduser bob

> grant admin privileges: sudo usermode -aG sudo bob

2. install components from the ubuntu repositories

sudo apt-get update

sudo apt-get install python3-pip python3-dev nginx

3. create python3 virtual environment
sudo pip3 install virtualenv
> generate a project folder
> then: mkvirtualenv flask 4. install flask
> activate flask
> pip3 install gunicorn flask 5. create a sample app: flaskproject.py
~/flaskproject/flaskproject.py
##########
from flask import Flask
app = Flask(__name__) @app.route("/")
def greeting():
  return "<h1 style='color:green'>Hello World!</h1>" if __name__ == "__main__":
  app.run(host='0.0.0.0')
#############
6. open server
(flaskprojectenv) $ sudo ufw allow 5000
(flaskprojectenv) $ python flaskproject.py
http://0.0.0.0:5000
then you can get a "Hello world" on the browser 7. create the wsgi entry point:create wsgi.py
~/flaskproject/wsgi.py
from flaskproject import app if __name__ == "__main__":
app.run() 8. check gunicorn works or not:
(in the flaskproject folder) > gunicorn --bind 0.0.0.0:5000 wsgi:app 9. create a systemd unit file: allow unbuntu automatically start Gunicorn and serve the Flask application whenever the server boots
create a unit file ending in .service in /etc/systemd/system folder
> sudo vim /etc/systemd/system/flaskproject.service
###
[Unit]
Description=Gunicorn instance to serve flaskproject
After=network.target [Service]
User=westmole
Group=www-data
WorkingDirectory=/home/westmole/workspace/prj/flaskproject
Environment="PATH=/home/westmole/workspace/prj/flaskproject/flask/bin"
ExecStart=/home/westmole/workspace/prj/flaskproject/flask/bin/gunicorn --workers 3 --bind unix:flaskproject.sock -m 007 wsgi:app [Install]
WantedBy=multi-user.target
###
> group=www-data: Nginx can communicate with Gunicorn processes
> gunicorn is in the virtualenv of flask
> --workers 3: 3 worker projcesses
> -m 007: umask value so the socket file to give access to the owner and group
> wsgi:app : pass in the WSGI entry point 10. start the gunicorn service and enable it to start at boot:
> sudo systemctl start flaskproject
> sudo systemctl enable flaskproject 11. configuring Nginx to proxy request
Nginx to pass web requests to scoket; create a new server block configuration file in Nginx's 'sites-available' directory
> sudo vim /etc/nginx/sites-available/flaskproject
server {
listen 80;
server_name server_domain_or_IP; location / {
include proxy_params;
proxy_pass http://unix:/home/bobby/flaskproject/flaskproject.sock;
}
} > sudo ln -s /etc/nginx/sites-available/flaskproject /etc/nginx/sites-enabled
> sudo nginx -t
> sudo systemctl restart nginx
> sudo ufw delete allow 5000
> sudo ufw allow 'Nginx Full'


												

python-flask-2 安装及设定 flask的更多相关文章

  1. python 全栈开发,Day119(Flask初识,Render Redirect HttpResponse,request,模板语言 Jinja2,用户登录例子,内置Session)

    一.Flask初识 首先,要看你学没学过Django 如果学过Django 的同学,请从头看到尾,如果没有学过Django的同学,并且不想学习Django的同学,轻饶过第一部分 三大主流Web框架对比 ...

  2. Python 的 Flask 框架安装应用

    Flask是一个使用 Python 编写的轻量级 Web 应用框架.其 WSGI 工具箱採用 Werkzeug ,模板引擎则使用 Jinja2 ,使用 BSD 授权. Flask也被称为 " ...

  3. Flask 学习篇一: 搭建Python虚拟环境,安装flask,并设计RESTful API。

    前些日子,老师给我看了这本书,于是便开始了Flask的学习 GitHub上的大神,于是我也在GitHub上建了一个Flask的项目. 有兴趣可以看看: https://github.com/Silen ...

  4. [Python]安装和运行flask框架

    随着你的 Python 项目越来越多,你会发现不同的项目会需要 不同的版本的 Python 库.同一个 Python 库的不同版本可能不兼容.虚拟环境可以为每一个项目安装独立的 Python 库,这样 ...

  5. python web开发flask框架 安装与环境

    # encoding:utf-8 # 从flask这个框架中导入Flask这个类 from flask import Flask # 初始化一个Flask对象 # Flasks() # 需要传递一个参 ...

  6. Flask简介,安装,demo,快速入门

    1.Flask简介 Flask是一个相对于Django而言轻量级的Web框架. 和Django大包大揽不同,Flask建立于一系列的开源软件包之上,这其中 最主要的是WSGI应用开发库Werkzeug ...

  7. Python 【web框架】之Flask

    flask 是Python实现的轻量级web框架.没有表单,orm等,但扩展性很好.很多Python web开发者十分喜欢.本篇介绍flask的简单使用及其扩展. 文中示例源码已经传到github:h ...

  8. 安装虚拟环境和Flask

    一.Flask 使用前准备 一. 安装及创建虚拟环境 1. 安装虚拟环境 win + R -> cmd -> pip install virtualenv -> 出现 Success ...

  9. python 全栈开发,Day142(flask标准目录结构, flask使用SQLAlchemy,flask离线脚本,flask多app应用,flask-script,flask-migrate,pipreqs)

    昨日内容回顾 1. 简述flask上下文管理 - threading.local - 偏函数 - 栈 2. 原生SQL和ORM有什么优缺点? 开发效率: ORM > 原生SQL 执行效率: 原生 ...

随机推荐

  1. HDOJ 4857 逃生

    BestHack.....真乱..... 逃生 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/ ...

  2. Ejb in action(四)——购物车实例演示有状态会话Bean

    前面.我们介绍了一个入门实例.事实上那就是无状态回话Bean的经常使用情况. 上一篇文章中.我们介绍了无状态会话Bean和有状态会话Bean的相关概念.为了加深大家对它们的理解,我们一起来实现一个有状 ...

  3. Python图像处理库PIL中图像格式转换(一)

    在数字图像处理中,针对不同的图像格式有其特定的处理算法. 所以,在做图像处理之前,我们须要考虑清楚自己要基于哪种格式的图像进行算法设计及事实上现.本文基于这个需求.使用python中的图像处理库PIL ...

  4. Extjs TabPanel页签转换事件

    listeners : { tabchange : function(tp, p) { var allmapDIV = document.getElementById("allmap&quo ...

  5. Entity Framework Utility .ttinclude File

    https://msdn.microsoft.com/en-us/library/ff477603(v=vs.100).aspx This topic provides an overview of ...

  6. hdoj Radar Installation

    Problem Description Assume the coasting is an infinite straight line. Land is in one side of coastin ...

  7. bzoj 1556 墓地秘密 —— 状压DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1556 预处理出两个障碍四个方向之间的距离(转弯次数),就可以状压DP了: 但预处理很麻烦.. ...

  8. ecshop类的解析1

    前面写了一下我理解的ecshop数据库表,现在看一下我理解的ecshop的类. ecshop类,ECS是一个基础类,它的取得域名的函数我感觉是比较不错的. function get_domain() ...

  9. html5 历史管理

    1.onhashchange:改变hash值来进行历史管理. <!DOCTYPE html> <html> <head> <meta charset=&quo ...

  10. autofac的小知识点

    autofac 注入中i遇到的泛型传参问题 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 ...