如何在Ubuntu 20.04上使用Gunicorn和Nginx服务Flask应用程序

介绍 (Introduction)

In this guide, you will build a Python application using the Flask microframework on Ubuntu 20.04. The bulk of this article will be about how to set up the Gunicorn application server and how to launch the application and configure Nginx to act as a front-end reverse proxy.

在本指南中,您将在Ubuntu 20.04上使用Flask微框架构建Python应用程序。 本文的大部分内容将涉及如何设置Gunicorn应用程序服务器以及如何启动应用程序以及如何配置Nginx充当前端反向代理。

先决条件 (Prerequisites)

Before starting this guide, you should have:

在开始本指南之前,您应该具有:

  • A server with Ubuntu 20.04 installed and a non-root user with sudo privileges. Follow our initial server setup guide for guidance.

    安装了Ubuntu 20.04的服务器以及具有sudo特权的非root用户。 请遵循我们的初始服务器设置指南以获取指导。

  • Nginx installed, following Steps 1 and 2 of How To Install Nginx on Ubuntu 20.04.

    遵循如何在Ubuntu 20.04上安装Nginx的步骤1和2 来安装Nginx

  • A domain name configured to point to your server. You can purchase one on Namecheap or get one for free on Freenom. You can learn how to point domains to DigitalOcean by following the relevant documentation on domains and DNS. Be sure to create the following DNS records:

    配置为指向您的服务器的域名。 你可以购买一个Namecheap或免费获得一个上Freenom 。 您可以通过遵循有关域和DNS的相关文档,学习如何将域指向DigitalOcean。 确保创建以下DNS记录:

    • An A record with your_domain pointing to your server’s public IP address.

      A记录,其中your_domain指向服务器的公共IP地址。

    • An A record with www.your_domain pointing to your server’s public IP address.

      www. your_domain的A记录www. your_domain www. your_domain指向服务器的公共IP地址。

  • Familiarity with the WSGI specification, which the Gunicorn server will use to communicate with your Flask application. This discussion covers WSGI in more detail.

    熟悉WSGI规范,Gunicorn服务器将使用该规范与Flask应用程序进行通信。 该讨论将更详细地介绍WSGI。

第1步—从Ubuntu存储库安装组件 (Step 1 — Installing the Components from the Ubuntu Repositories)

Our first step will be to install all of the pieces we need from the Ubuntu repositories. This includes pip, the Python package manager, which will manage our Python components. We will also get the Python development files necessary to build some of the Gunicorn components.

我们的第一步将是从Ubuntu存储库安装我们需要的所有组件。 这包括Python包管理器pip ,它将管理我们的Python组件。 我们还将获得构建某些Gunicorn组件所需的Python开发文件。

First, let’s update the local package index and install the packages that will allow us to build our Python environment. These will include python3-pip, along with a few more packages and development tools necessary for a robust programming environment:

首先,让我们更新本地软件包索引并安装允许我们构建Python环境的软件包。 这些将包括python3-pip ,以及健壮的编程环境所需的其他一些软件包和开发工具:

  • sudo apt update

    sudo apt更新
  • sudo apt install python3-pip python3-dev build-essential libssl-dev libffi-dev python3-setuptools

    须藤apt install python3-pip python3-dev build-essential libssl-dev libffi-dev python3-setuptools

With these packages in place, let’s move on to creating a virtual environment for our project.

有了这些程序包后,让我们继续为项目创建虚拟环境。

第2步-创建Python虚拟环境 (Step 2 — Creating a Python Virtual Environment)

Next, we’ll set up a virtual environment in order to isolate our Flask application from the other Python files on the system.

接下来,我们将建立一个虚拟环境,以便将Flask应用程序与系统上的其他Python文件隔离。

Start by installing the python3-venv package, which will install the venv module:

首先安装python3-venv软件包,该软件包将安装venv模块:

  • sudo apt install python3-venv

    sudo apt安装python3-venv

Next, let’s make a parent directory for our Flask project. Move into the directory after you create it:

接下来,让我们为Flask项目创建一个父目录。 创建目录后,移至该目录:

  • mkdir ~/myproject

    mkdir〜/ myproject

  • cd ~/myproject

    cd〜/ myproject

Create a virtual environment to store your Flask project’s Python requirements by typing:

通过键入以下内容,创建一个虚拟环境来存储Flask项目的Python要求:

  • python3 -m venv myprojectenv

    python3 -m venv myprojectenv

This will install a local copy of Python and pip into a directory called myprojectenv within your project directory.

这会将Python的本地副本和pip安装到项目目录中名为myprojectenv的目录中。

Before installing applications within the virtual environment, you need to activate it. Do so by typing:

在虚拟环境中安装应用程序之前,需要激活它。 通过键入以下内容:

  • source myprojectenv/bin/activate

    源myprojectenv / bin / activate

Your prompt will change to indicate that you are now operating within the virtual environment. It will look something like this: (myprojectenv)user@host:~/myproject$.

您的提示将更改以指示您现在正在虚拟环境中操作。 它看起来像这样: ( myprojectenv ) user @ host :~/ myproject $

步骤3 —设置Flask应用程序 (Step 3 — Setting Up a Flask Application)

Now that you are in your virtual environment, you can install Flask and Gunicorn and get started on designing your application.

现在您已经处于虚拟环境中,可以安装Flask和Gunicorn并开始设计应用程序。

First, let’s install wheel with the local instance of pip to ensure that our packages will install even if they are missing wheel archives:

首先,让我们使用pip的本地实例安装wheel ,以确保我们的软件包即使丢失了wheel档案也可以安装:

  • pip install wheel

    点安装轮
Note
Regardless of which version of Python you are using, when the virtual environment is activated, you should use the
注意
无论使用哪种版本的Python,在激活虚拟环境时,都应使用 pip command (not pip命令(不是 pip3). pip3 )。

Next, let’s install Flask and Gunicorn:

接下来,让我们安装Flask和Gunicorn:

  • pip install gunicorn flask

    点安装机枪烧瓶

创建示例应用 (Creating a Sample App)

Now that you have Flask available, you can create a simple application. Flask is a microframework. It does not include many of the tools that more full-featured frameworks might, and exists mainly as a module that you can import into your projects to assist you in initializing a web application.

有了Flask之后,您就可以创建一个简单的应用程序了。 烧瓶是一个微框架。 它不包含功能更全的框架可能提供的许多工具,并且主要作为模块存在,您可以将其导入项目中以帮助您初始化Web应用程序。

While your application might be more complex, we’ll create our Flask app in a single file, called myproject.py:

尽管您的应用程序可能更复杂,但我们将在一个名为myproject .py文件中创建Flask应用程序:

  • nano ~/myproject/myproject.py

    纳米〜/ myproject / myproject .py

The application code will live in this file. It will import Flask and instantiate a Flask object. You can use this to define the functions that should be run when a specific route is requested:

应用程序代码将存在于此文件中。 它将导入Flask并实例化一个Flask对象。 您可以使用它来定义请求特定路由时应运行的功能:

~/myproject/myproject.py
〜/ myproject / myproject.py
from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "<h1 style='color:blue'>Hello There!</h1>"

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

This basically defines what content to present when the root domain is accessed. Save and close the file when you’re finished.

这基本上定义了访问根域时要呈现的内容。 完成后保存并关闭文件。

If you followed the initial server setup guide, you should have a UFW firewall enabled. To test the application, you need to allow access to port 5000:

如果遵循初始服务器安装指南,则应启用UFW防火墙。 要测试该应用程序,您需要允许访问端口5000

  • sudo ufw allow 5000

    须藤ufw允许5000

Now you can test your Flask app by typing:

现在,您可以通过键入以下内容来测试Flask应用程序:

  • python myproject.py

    python myproject .py

You will see output like the following, including a helpful warning reminding you not to use this server setup in production:

您将看到类似以下的输出,包括一个有用的警告,提醒您不要在生产中使用此服务器设置:


   
   
Output
* Serving Flask app "myproject" (lazy loading) * Environment: production WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. * Debug mode: off * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

Visit your server’s IP address followed by :5000 in your web browser:

在网络浏览器中访问服务器的IP地址,然后输入:5000

http://your_server_ip:5000

You should see something like this:

您应该会看到以下内容:

When you are finished, hit CTRL-C in your terminal window to stop the Flask development server.

完成后,在终端窗口中按CTRL-C以停止Flask开发服务器。

创建WSGI入口点 (Creating the WSGI Entry Point)

Next, let’s 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.

接下来,让我们创建一个文件,该文件将用作我们应用程序的入口点。 这将告诉我们的Gunicorn服务器如何与应用程序进行交互。

Let’s call the file wsgi.py:

我们将其称为wsgi.py文件:

  • nano ~/myproject/wsgi.py

    纳米〜/ myproject /wsgi.py

In this file, let’s import the Flask instance from our application and then run it:

在此文件中,让我们从应用程序中导入Flask实例,然后运行它:

~/myproject/wsgi.py
〜/ myproject / wsgi.py
from myproject import app

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

Save and close the file when you are finished.

完成后保存并关闭文件。

第4步-配置Gunicorn (Step 4 — Configuring Gunicorn)

Your application is now written with an entry point established. We can now move on to configuring Gunicorn.

现在,您的应用程序已建立一个入口点。 现在,我们可以继续配置Gunicorn。

Before moving on, we should check that Gunicorn can serve the application correctly.

在继续之前,我们应该检查Gunicorn是否可以正确为应用程序提供服务。

We can do this by simply passing it the name of our entry point. This is constructed as the name of the module (minus the .py extension), plus the name of the callable within the application. In our case, this is wsgi:app.

我们可以通过简单地为其传递入口点的名称来实现。 这被构造为模块的名称(减去.py扩展名),加上应用程序中可调用的名称。 在我们的例子中,这是wsgi:app

We’ll also specify the interface and port to bind to so that the application will be started on a publicly available interface:

我们还将指定要绑定的接口和端口,以便应用程序将在公共可用接口上启动:

  • cd ~/myproject

    cd〜/ myproject

  • gunicorn --bind 0.0.0.0:5000 wsgi:app

    古尼康--bind 0.0.0.0:5000 wsgi:app

You should see output like the following:

您应该看到如下输出:


   
   
Output
[2020-05-20 14:13:00 +0000] [46419] [INFO] Starting gunicorn 20.0.4 [2020-05-20 14:13:00 +0000] [46419] [INFO] Listening at: http://0.0.0.0:5000 (46419) [2020-05-20 14:13:00 +0000] [46419] [INFO] Using worker: sync [2020-05-20 14:13:00 +0000] [46421] [INFO] Booting worker with pid: 46421

Visit your server’s IP address with :5000 appended to the end in your web browser again:

在您的Web浏览器中再次访问服务器IP地址(末尾附加:5000

http://your_server_ip:5000

You should see your application’s output:

您应该看到应用程序的输出:

When you have confirmed that it’s functioning properly, press CTRL-C in your terminal window.

确认其功能正常后,请在终端窗口中按CTRL-C

We’re now done with our virtual environment, so we can deactivate it:

现在我们已经完成了虚拟环境,因此可以将其停用:

  • deactivate

    停用

Any Python commands will now use the system’s Python environment again.

现在,所有Python命令都将再次使用系统的Python环境。

Next, let’s create the systemd service unit file. Creating a systemd unit file will allow Ubuntu’s init system to automatically start Gunicorn and serve the Flask application whenever the server boots.

接下来,让我们创建systemd服务单元文件。 创建一个systemd单位文件将使Ubuntu的init系统可以在服务器启动时自动启动Gunicorn并为Flask应用程序提供服务。

Create a unit file ending in .service within the /etc/systemd/system directory to begin:

/etc/systemd/system目录中创建一个以.service结尾的单元文件以开始:

  • sudo nano /etc/systemd/system/myproject.service

    须藤nano / etc / systemd / system / myproject .service

Inside, we’ll start with the [Unit] section, which is used to specify metadata and dependencies. Let’s put a description of our service here and tell the init system to only start this after the networking target has been reached:

在内部,我们将从[Unit]部分开始,该部分用于指定元数据和依赖项。 让我们在这里对服务进行描述,并告诉init系统仅在达到网络目标后才启动此服务:

/etc/systemd/system/myproject.service
/etc/systemd/system/myproject.service
[Unit]
Description=Gunicorn instance to serve myproject
After=network.target

Next, let’s open up the [Service] section. This will specify the user and group that we want the process to run under. Let’s give our regular user account ownership of the process since it owns all of the relevant files. Let’s also give group ownership to the www-data group so that Nginx can communicate easily with the Gunicorn processes. Remember to replace the username here with your username:

接下来,让我们打开[Service]部分。 这将指定我们要在其中运行进程的用户和组。 让我们为该流程的普通用户帐户拥有权,因为它拥有所有相关文件。 我们还将组所有权赋予www-data组,以便Nginx可以轻松地与Gunicorn流程进行通信。 请记住,将此处的用户名替换为您的用户名:

/etc/systemd/system/myproject.service
/etc/systemd/system/myproject.service
[Unit]
Description=Gunicorn instance to serve myproject
After=network.target

[Service]
User=sammy
Group=www-data

Next, let’s map out the working directory and set the PATH environmental variable so that the init system knows that the executables for the process are located within our virtual environment. Let’s also specify the command to start the service. This command will do the following:

接下来,让我们映射工作目录并设置PATH环境变量,以便init系统知道该进程的可执行文件位于我们的虚拟环境中。 我们还指定启动服务的命令。 该命令将执行以下操作:

  • Start 3 worker processes (though you should adjust this as necessary)

    启动3个工作进程(尽管您应根据需要进行调整)
  • Create and bind to a Unix socket file, myproject.sock, within our project directory. We’ll set an umask value of 007 so that the socket file is created giving access to the owner and group, while restricting other access

    在我们的项目目录中创建并绑定到Unix套接字文件myproject .sock 。 我们将umask值设置为007以便创建套接字文件以授予所有者和组访问权限,同时限制其他访问权限

  • Specify the WSGI entry point file name, along with the Python callable within that file (wsgi:app)

    指定WSGI入口点文件名,以及该文件中可调用的Python( wsgi:app )

Systemd requires that we give the full path to the Gunicorn executable, which is installed within our virtual environment.

Systemd要求我们提供Gunicorn可执行文件的完整路径,该文件已安装在虚拟环境中。

Remember to replace the username and project paths with your own information:

请记住用您自己的信息替换用户名和项目路径:

/etc/systemd/system/myproject.service
/etc/systemd/system/myproject.service
[Unit]
Description=Gunicorn instance to serve myproject
After=network.target

[Service]
User=sammy
Group=www-data
WorkingDirectory=/home/sammy/myproject
Environment="PATH=/home/sammy/myproject/myprojectenv/bin"
ExecStart=/home/sammy/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:myproject.sock -m 007 wsgi:app

Finally, let’s add an [Install] section. 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]部分。 如果我们启用该服务以在启动时启动,它将告诉systemd该服务链接到什么。 我们希望该服务在常规多用户系统启动并运行时启动:

/etc/systemd/system/myproject.service
/etc/systemd/system/myproject.service
[Unit]
Description=Gunicorn instance to serve myproject
After=network.target

[Service]
User=sammy
Group=www-data
WorkingDirectory=/home/sammy/myproject
Environment="PATH=/home/sammy/myproject/myprojectenv/bin"
ExecStart=/home/sammy/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:myproject.sock -m 007 wsgi:app

[Install]
WantedBy=multi-user.target

With that, our systemd service file is complete. Save and close it now.

这样,我们的systemd服务文件就完成了。 立即保存并关闭。

We can now start the Gunicorn service we created and enable it so that it starts at boot:

现在,我们可以启动我们创建的Gunicorn服务并启用它,以便它在启动时启动:

  • sudo systemctl start myproject

    sudo systemctl启动myproject

  • sudo systemctl enable myproject

    sudo systemctl启用myproject

Let’s check the status:

让我们检查一下状态:

  • sudo systemctl status myproject

    sudo systemctl状态为myproject

You should see output like this:

您应该看到如下输出:


   
   
Output
● myproject.service - Gunicorn instance to serve myproject Loaded: loaded (/etc/systemd/system/myproject.service; enabled; vendor preset: enabled) Active: active (running) since Wed 2020-05-20 14:15:18 UTC; 1s ago Main PID: 46430 (gunicorn) Tasks: 4 (limit: 2344) Memory: 51.3M CGroup: /system.slice/myproject.service ├─46430 /home/sammy/myproject/myprojectenv/bin/python3 /home/sammy/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:myproject.sock -m 007 wsgi:app ├─46449 /home/sammy/myproject/myprojectenv/bin/python3 /home/sammy/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:myproject.sock -m 007 wsgi:app ├─46450 /home/sammy/myproject/myprojectenv/bin/python3 /home/sammy/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:myproject.sock -m 007 wsgi:app └─46451 /home/sammy/myproject/myprojectenv/bin/python3 /home/sammy/myproject/myprojectenv/bin/gunicorn --workers 3 --bind unix:myproject.sock -m 007 wsgi:app

If you see any errors, be sure to resolve them before continuing with the tutorial.

如果看到任何错误,请确保在继续本教程之前解决它们。

步骤5 —配置Nginx代理请求 (Step 5 — Configuring Nginx to Proxy Requests)

Our Gunicorn application server should now be up and running, waiting for requests on the socket file in the project directory. Let’s now configure Nginx to pass web requests to that socket by making some small additions to its configuration file.

我们的Gunicorn应用服务器现在应该已启动并正在运行,正在等待项目目录中套接字文件的请求。 现在,通过对其配置文件进行一些小的添加,将Nginx配置为将Web请求传递到该套接字。

Begin by creating a new server block configuration file in Nginx’s sites-available directory. Let’s call this myproject to keep in line with the rest of the guide:

首先在Nginx的sites-available目录中创建一个新的服务器块配置文件。 让我们将此myproject称为与该指南的其余部分保持一致:

  • sudo nano /etc/nginx/sites-available/myproject

    须藤纳米/ etc / nginx / sites-available / myproject

Open up a server block and tell Nginx to listen on the default port 80. Let’s also tell it to use this block for requests for our server’s domain name:

打开一个服务器块,告诉Nginx在默认端口80上侦听。 让我们还告诉它使用此块来请求我们服务器的域名:

/etc/nginx/sites-available/myproject
/ etc / nginx / sites-available / myproject
server {
    listen 80;
    server_name your_domain www.your_domain;
}

Next, let’s add a location block that matches every request. Within this block, we’ll include the proxy_params file that specifies some general proxying parameters that need to be set. We’ll then pass the requests to the socket we defined using the proxy_pass directive:

接下来,让我们添加一个匹配每个请求的位置块。 在此块中,我们将包含proxy_params文件,该文件指定一些需要设置的常规代理参数。 然后,我们将使用proxy_pass指令将请求传递到我们定义的套接字:

/etc/nginx/sites-available/myproject
/ etc / nginx / sites-available / myproject
server {
    listen 80;
    server_name your_domain www.your_domain;

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/sammy/myproject/myproject.sock;
    }
}

Save and close the file when you’re finished.

完成后保存并关闭文件。

To enable the Nginx server block configuration you’ve just created, link the file to the sites-enabled directory:

要启用刚刚创建的Nginx服务器块配置,请将文件链接到sites-enabled目录:

  • sudo ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled

    须藤ln -s / etc / nginx / sites-available / myproject / etc / nginx / sites-enabled

With the file in that directory, you can test for syntax errors:

使用该目录中的文件,您可以测试语法错误:

  • sudo nginx -t

    须藤Nginx -t

If this returns without indicating any issues, restart the Nginx process to read the new configuration:

如果返回结果没有指示任何问题,请重新启动Nginx进程以读取新配置:

  • sudo systemctl restart nginx

    sudo systemctl重启nginx

Finally, let’s adjust the firewall again. We no longer need access through port 5000, so we can remove that rule. We can then allow full access to the Nginx server:

最后,让我们再次调整防火墙。 我们不再需要通过端口5000访问,因此我们可以删除该规则。 然后,我们可以允许对Nginx服务器的完全访问:

  • sudo ufw delete allow 5000

    sudo ufw删除允许5000
  • sudo ufw allow 'Nginx Full'

    sudo ufw允许'Nginx Full'

You should now be able to navigate to your server’s domain name in your web browser:

现在,您应该能够在Web浏览器中导航到服务器的域名:

http://your_domain

You should see your application’s output:

您应该看到应用程序的输出:

If you encounter any errors, trying checking the following:

如果遇到任何错误,请尝试检查以下内容:

  • sudo less /var/log/nginx/error.log: checks the Nginx error logs.

    sudo less /var/log/nginx/error.log :检查Nginx错误日志。

  • sudo less /var/log/nginx/access.log: checks the Nginx access logs.

    sudo less /var/log/nginx/access.log :检查Nginx访问日志。

  • sudo journalctl -u nginx: checks the Nginx process logs.

    sudo journalctl -u nginx :检查Nginx进程日志。

  • sudo journalctl -u myproject: checks your Flask app’s Gunicorn logs.

    sudo journalctl -u myproject :检查Flask应用程序的Gunicorn日志。

步骤6 —保护应用程序的安全 (Step 6 — Securing the Application)

To ensure that traffic to your server remains secure, let’s get an SSL certificate for your domain. There are multiple ways to do this, including getting a free certificate from Let’s Encrypt, generating a self-signed certificate, or buying one from another provider and configuring Nginx to use it by following Steps 2 through 6 of How to Create a Self-signed SSL Certificate for Nginx in Ubuntu 20.04. We will go with option one for the sake of expediency.

为了确保到服务器的流量保持安全,让我们为您的域获取SSL证书。 有多种方法可以执行此操作,包括从Let's Encrypt获得免费证书, 生成自签名证书 ,或从另一提供商购买一个 证书 ,并按照如何创建自签名的步骤2至6配置Nginx以使用它。 Ubuntu 20.04中Nginx的SSL证书 。 为了方便起见,我们将选择第一种方法。

Install Certbot’s Nginx package with apt:

使用apt安装Certbot的Nginx软件包:

  • sudo apt install python3-certbot-nginx

    sudo apt安装python3-certbot-nginx

Certbot provides a variety of ways to obtain SSL certificates through plugins. The Nginx plugin will take care of reconfiguring Nginx and reloading the config whenever necessary. To use this plugin, type the following:

Certbot提供了多种通过插件获取SSL证书的方法。 Nginx插件将负责重新配置Nginx并在必要时重新加载配置。 要使用此插件,请键入以下内容:

  • sudo certbot --nginx -d your_domain -d www.your_domain

    sudo certbot --nginx -d your_domain -d www。 your_domain

This runs certbot with the --nginx plugin, using -d to specify the names we’d like the certificate to be valid for.

这将使用--nginx插件运行certbot ,并使用-d指定我们希望证书对其有效的名称。

If this is your first time running certbot, you will be prompted to enter an email address and agree to the terms of service. After doing so, certbot will communicate with the Let’s Encrypt server, then run a challenge to verify that you control the domain you’re requesting a certificate for.

如果这是您首次运行certbot ,则系统将提示您输入电子邮件地址并同意服务条款。 完成此操作后, certbot将与Let's Encrypt服务器通信,然后进行质询以验证您是否控制了要为其申请证书的域。

If that’s successful, certbot will ask how you’d like to configure your HTTPS settings:

如果成功, certbot将询问您如何配置HTTPS设置:


   
   
Output
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access. ------------------------------------------------------------------------------- 1: No redirect - Make no further changes to the webserver configuration. 2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for new sites, or if you're confident your site works on HTTPS. You can undo this change by editing your web server's configuration. ------------------------------------------------------------------------------- Select the appropriate number [1-2] then [enter] (press 'c' to cancel):

Select your choice then hit ENTER. The configuration will be updated, and Nginx will reload to pick up the new settings. certbot will wrap up with a message telling you the process was successful and where your certificates are stored:

选择您的选择,然后按ENTER 。 配置将被更新,并且Nginx将重新加载以获取新设置。 certbot将以一条消息结束,告诉您该过程已成功完成,并且证书的存储位置:


   
   
Output
IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/your_domain/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/your_domain/privkey.pem Your cert will expire on 2020-08-18. To obtain a new or tweaked version of this certificate in the future, simply run certbot again with the "certonly" option. To non-interactively renew *all* of your certificates, run "certbot renew" - Your account credentials have been saved in your Certbot configuration directory at /etc/letsencrypt. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Certbot so making regular backups of this folder is ideal. - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate Donating to EFF: https://eff.org/donate-le

If you followed the Nginx installation instructions in the prerequisites, you will no longer need the redundant HTTP profile allowance:

如果按照先决条件中的Nginx安装说明进行操作,则将不再需要冗余HTTP配置文件配额:

  • sudo ufw delete allow 'Nginx HTTP'

    sudo ufw delete allow'Nginx HTTP'

To verify the configuration, navigate once again to your domain, using https://:

要验证配置,请使用https://再次导航到您的域:

https://your_domain

You should see your application output once again, along with your browser’s security indicator, which should indicate that the site is secured.

您应该再次看到应用程序输出以及浏览器的安全指示器,该指示器应指示该站点已安全。

结论 (Conclusion)

In this guide, you created and secured a simple Flask application within a Python virtual environment. You created a WSGI entry point so that any WSGI-capable application server can interface with it, and then configured the Gunicorn app server to provide this function. Afterwards, you created a systemd service file to automatically launch the application server on boot. You also created an Nginx server block that passes web client traffic to the application server, relaying external requests, and secured traffic to your server with Let’s Encrypt.

在本指南中,您在Python虚拟环境中创建并保护了一个简单的Flask应用程序。 您创建了一个WSGI入口点,以便任何支持WSGI的应用程序服务器都可以与之交互,然后配置Gunicorn应用程序服务器以提供此功能。 之后,您创建了systemd服务文件以在启动时自动启动应用程序服务器。 您还创建了一个Nginx服务器块,该块将Web客户端流量传递到应用程序服务器,中继外部请求,并使用Let's Encrypt保护到服务器的安全流量。

Flask is a very simple, but extremely flexible framework meant to provide your applications with functionality without being too restrictive about structure and design. You can use the general stack described in this guide to serve the flask applications that you design.

Flask是一个非常简单但非常灵活的框架,旨在为您的应用程序提供功能,而又不必过于限制结构和设计。 您可以使用本指南中描述的常规堆栈来服务您设计的烧瓶应用程序。

翻译自: https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-gunicorn-and-nginx-on-ubuntu-20-04

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值