首页 > 系统相关 >12_How to deploy Flask apps on Ubuntu VPS Using gunicorn and Ngnix

12_How to deploy Flask apps on Ubuntu VPS Using gunicorn and Ngnix

时间:2023-06-07 22:24:54浏览次数:53  
标签:__ 12 gunicorn VPS app nginx our Copy

 

 

地址:https://www.codewithharry.com/blogpost/flask-app-deploy-using-gunicorn-nginx/

 

How to deploy flask app on Ubuntu VPS using Nginx and gunicorn

In this post, we will see how to deploy flask applications using gunicorn WSGI server and nginx as a reverse proxy and static files server.

Follow the steps below:

Step 1 - Install required packages

sudo apt update

Now let's install python3, python3-pip, and Nginx using the commands below:

sudo apt install python3-pip python3-dev nginx

Step 2 - Creating a directory (for our flask app) and virtual environment

Let's install virtualenv

sudo pip3 install virtualenv

Lets now create a directory to host our flask application

mkdir myFlaskApp && cd myFlaskApp

run the following command to create a virtual environment named env

virtualenv env

Finally activate the virtual environment

source env/bin/activate

Lets now install flask and gunicorn using pip

pip3 install flask gunicorn

Step 3 - Creating a sample project and wsgi entry point

Let us now create a sample project by entering the command below:

vim app.py

Paste the contents below to app.py file

from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
    return "Hello World!"
if __name__ == '__main__':
    app.run(debug=True,host='0.0.0.0')

Next, we’ll create a file that will serve as the entry point for our application. This will tell our Gunicorn server how to interact with the application.

vim wsgi.py

copy the contents below to wsgi.py

from app import app

if __name__ == "__main__":
    app.run()

We can test gunicorn's ability to serve our project by running the command below:

gunicorn --bind 0.0.0.0:5000 wsgi:app

Folder structure so far:

myFlaskApp
  |____ app.py
  |____ wsgi.py
  |____ env

Lets deactivate our virtual environment now:

deactivate

Step 4 - Creating a systemd service

Lets now create a systemd service using the following commands:

vim /etc/systemd/system/app.service

Now paste the contents below to this file:

[Unit]
#  specifies metadata and dependencies
Description=Gunicorn instance to serve myproject
After=network.target
# tells the init system to only start this after the networking target has been reached
# We will give our regular user account ownership of the process since it owns all of the relevant files
[Service]
# Service specify the user and group under which our process will run.
User=harry
# give group ownership to the www-data group so that Nginx can communicate easily with the Gunicorn processes.
Group=www-data
# We'll then map out the working directory and set the PATH environmental variable so that the init system knows where our the executables for the process are located (within our virtual environment).
WorkingDirectory=/home/harry/myFlaskApp/
Environment="PATH=/home/harry/myFlaskApp/env/bin"
# We'll then specify the commanded to start the service
ExecStart=/home/harry/myFlaskApp/env/bin/gunicorn --workers 3 --bind unix:app.sock -m 007 wsgi:app
# This will tell systemd what to link this service to if we enable it to start at boot. We want this service to start when the regular multi-user system is up and running:
[Install]
WantedBy=multi-user.target

Activate this service by typing:

sudo systemctl start app
sudo systemctl enable app

A file named app.sock will be automatically created. Folder structure so far:

myFlaskApp
  |____ app.py
  |____ wsgi.py
  |____ env
  |____ app.sock

 

Step 5 - Configuring Nginx

Create a file named app inside /etc/nginx/sites-available

vim /etc/nginx/sites-available/app

Now copy the below contents to this file:

server {
listen 80;
server_name 165.232.177.116;

location / {
  include proxy_params;
  proxy_pass http://unix:/home/harry/myFlaskApp/app.sock;
    }
}                

Important: I have used my server IP, you should use yours!

Activate this configuration by executing this:

sudo ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled

Restart nginx and your website should work fine!

sudo systemctl restart nginx

Visiting http://165.232.177.116/ on browser shows a page like this

If you are using a firewall (for eg. ufw), don't forget to allow nginx to receive incoming traffic on port 80

sudo ufw allow 'Nginx Full'

Happy coding!

         

标签:__,12,gunicorn,VPS,app,nginx,our,Copy
From: https://www.cnblogs.com/hechunfeng/p/17464739.html

相关文章

  • 8_How to install LEMP stack on Ubuntu VPS_
     地址:https://www.codewithharry.com/blogpost/lemp-stack-on-ubuntu-20/  HowtoinstallLEMPstack(Linux,Nginx,MySQL,PHP)onUbuntu20.04Inthistutorial,wewillinstalltheLEMPstackontheubuntu20basedserver.LEMPconsistsofLinux,Nginx(pr......
  • 10_How deploy a Django application using Nginx & Gunicorn in Production
     地址:https://www.codewithharry.com/blogpost/django-deploy-nginx-gunicorn/  HowtohostDjangoApplicationusinggunicorn&nginxinProductionInthispost,wewillseehowtousenginxwithgunicorntoservedjangoapplicationsinproduction. Dj......
  • Luogu P3224 [HNOI2012]永无乡
    [HNOI2012]永无乡题目描述永无乡包含\(n\)座岛,编号从\(1\)到\(n\),每座岛都有自己的独一无二的重要度,按照重要度可以将这\(n\)座岛排名,名次用\(1\)到\(n\)来表示。某些岛之间由巨大的桥连接,通过桥可以从一个岛到达另一个岛。如果从岛\(a\)出发经过若干座(含\(0\)......
  • 软件测试3班,学员就业前的模拟面试(2019-9-12)(金朝阳)
    1:你用过Fiddler在项目中发现过哪些有价值的bug?2:接口测试,返回的数据格式类型一般有哪些类型?(Json\xml\html等等)3:App兼容性测试怎么做?APP升级测试怎么做?4:你测试过哪些类型的安卓机型?哪些类型的苹果机型?5:你测试过安卓机型的操作系统是多少?苹果机型的操作系统是多少?6:你在做接口测试的......
  • MQ系列12:如何保证消息顺序性
    MQ系列1:消息中间件执行原理MQ系列2:消息中间件的技术选型MQ系列3:RocketMQ架构分析MQ系列4:NameServer原理解析MQ系列5:RocketMQ消息的发送模式MQ系列6:消息的消费MQ系列7:消息通信,追求极致性能MQ系列8:数据存储,消息队列的高可用保障MQ系列9:高可用架构分析MQ系列10:如何保证消......
  • YS9082HC+B27B固件量产工具,YS9082HT可参考,YS9082HC+镁光MT29F512G08EBLCE开卡!YS9082HP
    YS9082HC+B27B,镁光MT29F512G08EBLCE开卡!闪存ID:2C,C3,08,32,E6,00。如下图,不知道为什么检测出来的是9081?开卡设置,从量产部落下载的YS9082HCMPTool,如下图:结果报错:重新设置,更改了大小,240G改到了160G!分析是坏块过多了!我有不少B27颗粒的坏块都多,还是主控问题?我的其他两片B27,开120G都......
  • 12万短文学网句子内容大全ACCESS数据库
    闲来无事,看到“短文学网”文章内容还算整洁,而且非常容易进行采集,于是也就手痒了弄了一下,速度非常快可能与网络没有大量广告啊、JS啊有关。详细的分类信息如下:qq日志包含有:qq空间(2098)条、非主流日(180)条、搞笑日志(132)条、个性日志(204)条、经典日志(260)条、空间文字(848)......
  • SX1262 与 SX1278、SX1276 有什么区别?
    通常的物联网解决方案和设备一直都非常昂贵,或在实施中不切合实际。理想的无线连接技术应该是低成本、高可靠性的,可进行长距离传输,且拥有超长的电池续航时间。像zigbee、Bluetooth和Wi-Fi这样的短距离技术不能满足这些要求,蜂窝移动网络虽然支持更长的距离,但电池续航时间太短。为了......
  • spring boot 集成 Neo4j org.neo4j.ogm.metadata.DomainInfo.useClassgraph(DomainIn
    springboot版本:2.2.13.RELEASE 问题在于引入后,报错spring-boot-starter-data-neo4j<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-neo4j</artifactId></dependency>  *......
  • 7.12 字符串查找
    containsindexOf,lastIndexOf,startsWith,endWithpublicclassHelloWorld{publicstaticvoidmain(Stringargs[]){//Stringargs[]字符串数组的意思Stringstr="www.mldn.cn";System.out.println(str.contains("mldn"));//tru......