search
HomeBackend DevelopmentPython TutorialDetailed introduction to deployment in Python

I have been learning flask for a while, but I have never deployed it, so I was thinking about how to deploy it. Think about it, let’s get the service started first, so I thought about getting the service started first. Here is the choice It is Flask+uwsgi+Nginx+Ubuntu. The Python option is 2.7.2. This is the one that comes with the Ubuntu system. It feels simple to learn without having to go through the software connection. Currently, my own flask is written in python3, and I will slowly transition to it. , let’s get this figured out first, then it will be very easy to optimize. In fact, I don't know much about many principles. Let's set this up first and slowly understand the logic inside.

Nginx

Nginx is an efficient web server and reverse proxy server that can be used as a load balancing (when n users access the server, it can achieve offloading and share the server's load. pressure), compared with Apache, Nginx supports high concurrency, can support millions of TCP connections, and hundreds of thousands of concurrent connections. It is simple to deploy, consumes less memory, and has low cost. However, Nginx does not have as many modules as Apache. Nginx supports uWSGI's uwsgi protocol, so we can combine Nginx with uWSGI, and Nginx hands over dynamic content to uWSGI for processing through uwsgi_pass.

Official documentation is here

The best Nginx tutorial is here

uwsgi

uWSGI is a web server that implements WSGI protocol, uwsgi, http and other protocols. The function of HttpUwsgiModule in Nginx is to exchange with uWSGI server.
Pay attention to the distinction between the three concepts of WSGI / uwsgi / uWSGI.
  • Those who have read the previous section will know that WSGI is a communication protocol.
  • uwsgi is a line protocol rather than a communication protocol. It is often used for data communication between the uWSGI server and other network servers.
  • uWSGI is a web server that implements two protocols, uwsgi and WSGI.
uwsgi protocol is a uWSGI server's own protocol. It is used to define the type of information to be transmitted. The first 4 bytes of each uwsgi packet is a description of the type of information to be transmitted. It Compared with WSGI, they are two different things.
Preparation, first, let’s install the packages we need. First of all, this is my newly installed system, so there is no pip, so I will install pip first
sudo apt-get install python-pip

Use the following command to install flask

pip install flask

After installation, we can test it,

import flask

No error is reported, which proves that our flask is installed successfully. So the next thing we have to do is install ngnix and uwsgi.

sudo apt-get install nginx

After installation, we can start it first, nginx start directly starts from the command line, simple and crude

This way we nginx has started successfully. Next, we use pip to install uwsgi

After we install it, let’s start working.

First I create an app under hellowflak Python package,

#app/__init__.pyfrom flask import Flask
app = Flask(__name__)from app import view

Next we create view.py

from app import app
@app.route('/')def index():return 'hellow'

Then we create hello in the same directory as app. py

from app import appif __name__ == "__main__":
    app.run()

, then we can use Python to debug our program locally,

then we can use the browser to Take a look, enter the address and you can get this. From this point of view, there is no problem with our flask program.

Then what we have to do next is to let nginx take on the web service.

What I did here was to delete the nginx configuration file simply and rudely

$ sudo rm /etc/nginx/sites-enabled/default

Next, I created a configuration file under hellowflask

server {
    listen      8081;
    server_name 127.0.0.1;
    charset     utf-8;
    client_max_body_size 75M;

    location / { try_files $uri @app; }
    location @app {
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:9000;
    }
}

A brief explanation: server_name can be a domain name or an IP address. uwsgi_pass indicates the communication method between Nginx and uwsgi. What I chose here is the specified port number.

Then let’s soft-connect our configuration to nginx.

sudo ln -s <span class="hljs-regexp"><span class="hljs-regexp">/home/liwanlei/Desktop/hellowflask/<span class="hljs-regexp">helloflask_nginx.conf /etc<span class="hljs-regexp">/nginx/conf.d/<br/>这样我们再去启动我们的nginx,</span></span></span></span>
sudo /etc/init.d/nginx restart

What’s here is not a welcome, but a 502 error, because our current uwsgi file has not been configured yet, and we have not started uwsgi, so the next step is to get it out For this uwsgi, the example below is my configuration.

[uwsgi]    
    base = /home/liwanlei/Desktop/hellowflask
    app = hello#module = %(app)pidfile = /var/run/uwsgi.pid
    master = true
    wsgi-file = /home/liwanlei/Desktop/hellowflask/hello.py
    pythonpath = /usr/bin/python
    chdir = /home/liwanlei/Desktop/hellowflask
    socket = 127.0.0.1:9000callable = app
    logto = %n.log
    plugins = python
    processes = 8master = true

At this time our uwsgi has been configured, so let’s start it,

sudo /usr/bin/uwsgi --ini/home/liwanlei/Desktop/hellowflask/helloflask_uwsgi.ini

我们去重新启动我们的nginx,

sudo nginx <span class="hljs-_">reload<br/>平滑重启可以用用,重新加载配置文件,用新的工作进程代替旧的工作进程。<br/></span>
sudo nginx -s reload
<span class="hljs-_"><br/>启动后,我这里修改了地址,这里就可以直接访问了,那么我们的部署这样就算可以了,简单的。<br/><img src="/static/imghwm/default1.png"  data-src="https://img.php.cn/upload/article/000/000/001/25f4d9be3a729ae5fc17c2fefb915c9c-3.jpg?x-oss-process=image/resize,p_40"  class="lazy" alt=""/></span>
 <br>

完工之后,感觉还是很简单的 有问题那么就去看log,只要log配置得当,那么排除错误是很快的。

The above is the detailed content of Detailed introduction to deployment in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
如何使用Jenkins Pipeline构建PHP程序的持续打包部署流程?如何使用Jenkins Pipeline构建PHP程序的持续打包部署流程?Jul 30, 2023 pm 07:41 PM

如何使用JenkinsPipeline构建PHP程序的持续打包部署流程?Jenkins是一款非常流行的持续集成和部署工具,它提供了丰富的插件和功能,使得构建和部署过程变得简单而高效。而JenkinsPipeline是Jenkins最新推出的插件,它允许我们使用一种完整的、可扩展的DSL(DomainSpecificLanguage)来定义持续集成和部

如何在Linux服务器上部署可信赖的Web接口?如何在Linux服务器上部署可信赖的Web接口?Sep 09, 2023 pm 03:27 PM

如何在Linux服务器上部署可信赖的Web接口?简介:在如今信息爆炸的时代,Web应用已经成为了人们获取信息和进行交流的主要途径之一。为了确保用户的隐私安全和信息的可靠性,我们需要在Linux服务器上部署一个可信赖的Web接口。本文将介绍如何在Linux环境下进行Web接口的部署,并提供相关的代码示例。一、安装和配置Linux服务器首先,我们需要准备一个Li

Laravel开发:如何使用Laravel Envoyer部署应用程序?Laravel开发:如何使用Laravel Envoyer部署应用程序?Jun 14, 2023 am 10:15 AM

Laravel是一个极受欢迎的PHP开发框架,它以其简洁、优雅和高效的特性得到了众多开发者的青睐。随着Laravel的不断发展,LaravelEnvoyer作为一种部署工具,可帮助开发者更容易地将应用程序部署在服务器上。本文将向您介绍如何使用LaravelEnvoyer快速、轻松地部署应用程序。LaravelEnvoyer是什么?LaravelEnv

如何在Linux上部署Web应用程序如何在Linux上部署Web应用程序Jul 05, 2023 am 09:09 AM

如何在Linux上部署Web应用程序随着互联网的发展,Web应用程序的开发和部署变得越来越流行。而Linux是Web服务器的首选操作系统。本文将介绍如何在Linux上部署Web应用程序,并附上一些常见的代码示例。安装必要的软件在开始之前,我们需要安装一些必要的软件,包括Web服务器(如Apache、Nginx等)、PHP解释器(如果你的应用程序使用了PHP)

Scrapy如何实现Docker容器化与部署?Scrapy如何实现Docker容器化与部署?Jun 23, 2023 am 10:39 AM

随着现代互联网应用程序的不断发展和复杂性的增加,网络爬虫已经成为数据获取和分析的重要工具。而Scrapy作为Python最流行的爬虫框架之一,拥有强大的功能和易于使用的API接口,可以帮助开发人员快速地抓取和处理Web页面数据。但是,当面对大规模抓取任务时,单个Scrapy爬虫实例很容易受到硬件资源限制,因此通常需要将Scrapy容器化并部署到Docker容

使用Webman实现网站的持续集成和部署使用Webman实现网站的持续集成和部署Aug 25, 2023 pm 01:48 PM

使用Webman实现网站的持续集成和部署随着互联网的迅猛发展,网站开发和维护的工作也变得越来越复杂。为了提高开发效率和保证网站的质量,采用持续集成和部署的方式成为了一个重要的选择。在这篇文章中,我将介绍如何使用Webman工具来实现网站的持续集成和部署,并附上一些代码示例。一、什么是WebmanWebman是一个基于Java的开源持续集成和部署工具,它提供了

如何在Linux上部署微服务架构如何在Linux上部署微服务架构Jul 05, 2023 pm 02:21 PM

如何在Linux上部署微服务架构微服务架构已经成为现代软件开发中的热门话题。它将一个大型应用程序拆分成多个独立的小型服务,每个服务都可以独立开发、测试、部署和扩展。这种架构能够改善系统的可维护性、可扩展性和可测试性。在本篇文章中,我们将讨论如何在Linux操作系统上部署微服务架构。首先,我们需要为每个微服务创建一个独立的容器。容器是一种虚拟化技术,它可以提供

微服务架构中如何处理服务的自动化测试和部署?微服务架构中如何处理服务的自动化测试和部署?May 17, 2023 am 08:10 AM

随着互联网技术的快速发展,微服务架构也越来越被广泛应用。使用微服务架构可以有效避免单体应用的复杂度和代码耦合,提高应用的可扩展性和可维护性。然而,与单体应用不同,在微服务架构中,服务数量庞大,每个服务都需要进行自动化测试和部署,以确保服务的质量和可靠性。本文将针对微服务架构中如何处理服务的自动化测试和部署进行探讨。一、微服务架构中的自动化测试自动化测试是保证

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software