nginx部署flask_使用Nginx单元部署Flask应用

nginx部署flask

Flask is a popular python web framework that is most commonly known for creating REST APIs very easily. They are usually deployed using WSGI servers such as Gunicorn or uWSGI.

Flask是一种流行的python Web框架,因非常容易创建REST API而闻名。 它们通常使用WSGI服务器(例如Gunicorn或uWSGI)进行部署。

Another server solution that is much easier to work with and more versatile is NGINX Unit and that’s what we will be looking at here.

Nginx Unit是另一个更易于使用且功能更广泛的服务器解决方案,这就是我们在这里要介绍的内容。

nginx部署flask_使用Nginx单元部署Flask应用_第1张图片

NGINX Unit is a dynamic application server. It is not to be confused with the popular NGINX web server, although they work great together.

NGINX Unit是一个动态应用服务器。 尽管它们可以很好地协同工作,但不要与流行的NGINX Web服务器混淆。

The great thing about Unit is that it supports a RESTful JSON API. That way you can deploy configuration changes without service disruptions and Unit even runs apps built with multiple languages and frameworks!

Unit的优点在于它支持RESTful JSON API。 这样,您可以部署配置更改而不会中断服务,并且Unit甚至可以运行使用多种语言和框架构建的应用程序!

We will be deploying a very basic flask app and will use the basic hello world template. Create a file named app.py in your project directory. Here all we’re doing is importing the flask module and creating a simple message for the root route.

我们将部署一个非常基本的flask应用程序,并将使用基本的hello world模板。 在您的项目目录中创建一个名为app.py的文件。 在这里,我们要做的就是导入flask模块并为根路由创建一条简单消息。

from flask import Flask
app = Flask(__name__)
application = app




@app.route('/')
def hello_world():
    return 'Hello, World!'

Next, create a file named requirements.txt. This will contain details of the dependencies we have and since flask is the only one, that’s all we have to add! You can add futher dependencies here.

接下来,创建一个名为requirements.txt的文件。 这将包含我们具有的依赖项的详细信息,并且由于flask是唯一的依赖项,因此我们仅需添加! 您可以在此处添加其他依赖项。

flask

Make sure to create a virtual environment and then install flask with the command pip3 install -r requirements.txt. Then, test out the application using the command flask run.

确保创建一个虚拟环境,然后使用命令pip3 install -r requirements.txt安装flask。 然后,使用命令flask run测试该应用程序。

Image for post
Running the server using flask 使用Flask运行服务器

If you see something similar to the above image, the server runs! Head to http://localhost:5000 to test out the server.

如果您看到类似于上图的内容,则服务器正在运行! 转到http:// localhost:5000来测试服务器。

As the warning in the image mentions, using the command flask run starts up the flask server which is for development purposes only and shouldn’t be used for production. Lets use NGINX Unit instead for production.

如图像中的警告所提到的,使用命令flask run启动flask服务器,该服务器仅用于开发目的,不应用于生产。 让我们使用NGINX Unit代替它进行生产。

There are many different ways of installing Unit and they can be seen here. The docker method is one of the most convienient as it only installs what is needed for the application and is easily portable. Unit only supports Unix based operating systems so using Docker ensures your server works everywhere.

有许多不同的安装Unit的方法,可以在这里看到。 docker方法是最便捷的方法之一,因为它仅安装应用程序所需的内容,并且易于移植。 Unit仅支持基于Unix的操作系统,因此使用Docker可确保您的服务器在任何地方都能正常工作。

Make sure you’ve got docker installed. You can install the docker engine here.

确保已安装docker。 您可以在此处安装docker引擎。

Create a new file named config.json. This will contain the configuration we provide to Unit.

创建一个名为config.json的新文件。 这将包含我们提供给Unit的配置。

{
  "listeners": {
    "*:8080": {
      "pass": "applications/python_app"
    }
  },
  "applications": {
    "python_app": {
      "type": "python",
      "path": "/www/",
      "module": "app"
    }
  }
}

Here, we specify the port as 8080, the type of application as python_app, the location of the module in the www folder and the module name as app.

在这里,我们将端口指定为8080,将应用程序类型指定为python_app,将模块在www文件夹中的位置以及模块名称指定为app。

Create a new file named Dockerfile. We place the commands used to interact with the NGINX Unit image in this.

创建一个名为Dockerfile的新文件。 我们在其中放置用于与NGINX单元图像进行交互的命令。

# keep our base image as small as possible
FROM nginx/unit:1.19.0-python3.7


# port used by the listener in config.json
EXPOSE 8080


COPY requirements.txt /config/requirements.txt
RUN apt update && apt install -y python3-pip    \
    && pip3 install -r /config/requirements.txt \
    && rm -rf /var/lib/apt/lists/*


COPY config.json /docker-entrypoint.d/config.json
COPY app.py /www/app.py

To begin with, we start with the Unit image for Python 3.7. By pulling only this image, the overall container size stays low.

首先,我们从Python 3.7的Unit图像开始。 通过仅拉出此图像,容器的整体尺寸会保持较低。

Then we expose the port 8080. This is the port we declared earlier on which the server will run. After that we copy over the requirements.txt file, and then install pip and the requirements.

然后,我们公开端口8080。这是我们先前声明的服务器将在其上运行的端口。 之后,我们复制了requirements.txt文件,然后安装pip和需求。

Next, the configuration file is copied into the docker-entrypoint.ddirectory. The files in this directory are used when starting up the application server.

接下来,将配置文件复制到docker-entrypoint.d目录。 启动应用程序服务器时,将使用此目录中的文件。

Finally the flask server code itself is copied over into the www folder.

最后,flask服务器代码本身被复制到www文件夹中。

And that’s all the code is! Its pretty simple and very versatile.

这就是所有的代码! 它非常简单且用途广泛。

In order to run the docker container, first build it with the command.sudo docker build -t build — tag=unit-flask . We give it the tag name “unit-flask”.

为了运行docker容器,首先使用命令构建它。 sudo docker build -t build — tag=unit-flask . 我们给它加上标签名称“ unit-flask”。

nginx部署flask_使用Nginx单元部署Flask应用_第2张图片
What the docker build process should look like docker构建过程应该是什么样的

If running on Windows, omit the sudo keyword in the docker commands.

如果在Windows上运行,请在docker命令中省略sudo关键字。

Now run the built image using sudo docker run -p 8080:8080 unit-flask Here, we are specifiying that port 8080 from inside the container has to be mapped to port 8080 on our local machine.

现在使用sudo docker run -p 8080:8080 unit-flask运行构建的映像。在这里,我们指定容器内部的端口8080必须映射到本地计算机上的端口8080。

nginx部署flask_使用Nginx单元部署Flask应用_第3张图片

Now head to http://localhost:8080. You should see the hello world message and your server is sucessfully running!

现在转到http:// localhost:8080。 您应该看到hello world消息,并且服务器已成功运行!

nginx部署flask_使用Nginx单元部署Flask应用_第4张图片

NGINX Unit supports multiple languages and frameworks and can run multiple applications at the same time. The configuration file has to be modified accordingly and more info can be gained from the documentation. Unit can also be intergrated with the Open Source Nginx server to use as a reverse proxy and load balancer.

NGINX Unit支持多种语言和框架,并且可以同时运行多个应用程序。 必须相应地修改配置文件,并且可以从文档中获取更多信息。 该单元还可以与开源Nginx服务器集成在一起,用作反向代理和负载平衡器。

The complete code used here can be found at https://github.com/srujandeshpande/flask-unit-deploy

此处使用的完整代码可以在https://github.com/srujandeshpande/flask-unit-deploy中找到

Drop a comment for any queries or suggestions!

对任何疑问或建议发表评论!

翻译自: https://medium.com/@srujandeshpande/deploying-a-flask-app-with-nginx-unit-a91ce37fcc18

nginx部署flask

你可能感兴趣的:(nginx,java,leetcode)