Flask + uwsgi + Nginx + Ubuntu deployment, flaskuwsgi

Source: Internet
Author: User

Flask + uwsgi + Nginx + Ubuntu deployment, flaskuwsgi

I learned flask for a while, but I have never done any deployment. So I thought about how to deploy it. Think about it. Let's get through the service first, so I thought about how to implement the service first, here we chose Flask + uwsgi + Nginx + Ubuntu, and Python chose 2.7.2. This is the built-in learning experience of the Ubuntu system. It is still simple and does not require soft connection, at present, my own flask is written in python3, and it's time to transition. Let's get it done first, so optimization is quite smooth. As a matter of fact, I know a lot about the principles. Let's take a look at the logic.

Nginx

Nginx is an efficient Web server and reverse proxy server. It can be used as a server Load balancer (when n users access the server, they can achieve shunting and share the load on the server). Compared with Apache, nginx supports high concurrency, millions of TCP connections, 100,000 concurrent connections, simple deployment, low memory consumption, and low cost. However, Nginx modules are not rich in Apache. Nginx supports uWSGI's uwsgi protocol. Therefore, we can combine Nginx with uWSGI.uwsgi_passSubmit the dynamic content to uWSGI for processing.

The official documentation is here

The best Nginx tutorial is here

Uwsgi

UWSGI is a Web server that implements the WSGI, uwsgi, http, and other protocols. In Nginx, HttpUwsgiModule is used to exchange with the uWSGI server. Pay attention to the difference between the three concepts WSGI/uwsgi/uWSGI.
  • The students who read the preceding section clearly understand that WSGI is a communication protocol.
  • Uwsgi is a line protocol rather than a communication protocol, which is commonly used for data communication between uWSGI servers and other network servers.
  • UWSGI is a Web server that implements the uwsgi and WSGI protocols.
The uwsgi protocol is a proprietary protocol of the uWSGI server. It defines the type of information transmitted. The first 4 bytes of each uwsgi packet are the description of the transmission information type, it is two different from WSGI. Preparations: first, we will install the package we need first. First, I am using my new system, so there is no pip, so I will install pip first.
sudo apt-get install python-pip

Run the following command to install flask:

pip install flask

After installation, we can test it,

Import flask

If no error is reported, our flask is successfully installed. Next, we will install ngnix and uwsgi.

sudo apt-get install nginx

After the installation is complete, we can start nginx start directly with the command line, which is simple and crude.

In this way, nginx is successfully started. Next, we will use pip to install uwsgi.

After the installation is complete, let's start working on it,

First, I create an app python package under hellowflak,

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

Next, create view. py.

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

Create hello. py in the app directory at the same level.

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

So we can use Python locally to debug our program,

Then we can go to the browser and enter the address to get this. In this case, we can see that the flask program is correct.

So what we need to do next is to let nginx undertake web Services.

What I am doing here is simply and roughly deleting the nginx configuration file.

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

Next, a configuration file created 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;    }}

To explain this, server_name can be a domain name or an ip address. uwsgi_pass indicates the way Nginx communicates with uwsgi. Here I select a specific port number.

Then, we will connect our configuration to nginx.

Sudo ln-s/home/liwanlei/Desktop/hellowflask/helloflask_nginx.conf/etc/nginx/conf. d/
So that we can start our nginx again,
sudo /etc/init.d/nginx restart

This is not a welcome, but a 502 error. Because our uwsgi file is not configured yet and we have not started uwsgi, we will go out to this uwsgi, the following example shows 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:9000    callable = app    logto = %n.log    plugins = python    processes = 8    master = true

At this time, we have configured the uwsgi, so we can start it,

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

Let's restart our nginx,

Sudo nginx reload
Smooth restart can be used to re-load the configuration file and replace the old working process with a new working process.
sudo nginx -s reload

After it is started, I changed the address here and can directly access it here, so we can simply deploy it like this.
 

After the completion of the process, it is still very easy to look at the log. As long as the log is properly configured, troubleshooting is very fast.

I have successfully deployed what I wrote to the ubuntu system. It's just a simple change of configuration.

 

If you have any questions, add my qq: 952943386 or my qq Group 194704520

I hope you can fly to a higher level together with cainiao.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.