Flask + uWSGI + Nginx + Ubuntu deployment tutorial

Source: Internet
Author: User

Flask + uWSGI + Nginx + Ubuntu deployment tutorial

Learn Flask. When you write a Flask application and need to deploy it, you will think about tossing your own server. According to the search tutorial, I have a thorough understanding of the principles. As long as I run it, I am thankful that I will not try again. When the next deployment is required, this process will be repeated once. I don't know how many people have arrows in their knees? I did this too, and it was really stupid to do so. So I decided to write a Flask + uwsgi + Nginx + Ubuntu deployment tutorial to answer some of my questions during this process, from the principle to the solution, I would like to summarize the deployment and O & M from the perspective of Xiao Bai. It should be helpful for beginners who need to deploy Flask.

Environment Introduction Ubuntu

My Ubuntu system version is 14.04. I have used several Linux releases. Now, the first choice for selecting a system is Ubuntu, Because Ubuntu has a commercial company named Canonical for development and maintenance; there are many users, and there is a huge community support; problems are easy to solve. I have been tossing over Linux for a long time. My advice for new users is not to waste time on it. Instead, I should be guided by solving practical problems and improve programming ability. Installing the system, optimizing the system, and recording various cool commands do not actually help improve programming capabilities. So you asked me if I don't have porcelain Ubuntu. Of course I have porcelain, and Ubuntu will also be used, but it will be less than other systems, it will also be easy to solve. In fact, Ubuntu has become the first choice for servers. The most popular Linux release on AWS is Ubuntu. The Linux release used by Quora is also Ubuntu, And the founder Adam d' Angelo explained the reason in this answer. In general, Ubuntu should be the first choice if there is no special reason. After much experience, if you are interested in a release or want to make some special attempts to jump out of the comfort zone, try other systems.

UWSGI

We know that Flask has its own web server. Through Werkzeug, we can build WSGI services to run our website. However, Flask is a Web framework, not a Web server. Although Werkzeug is powerful, but it can only be used for development and cannot be used for production. For Web servers, we have a more professional choice, that is, uWSGI and uWSGI are a full-site hosting service, it implements application servers (supporting multiple programming languages), proxies, process managers, and monitors. The name uWSGI is obtained because it first implemented the Python WSGI.

UWSGI consists of four parts:

  • Uwsgi Protocol
  • Web server built-in support protocol module
  • Application Server Protocol Support Module
  • Process Control Program

UWSGI is written in C and has high performance.

Recommended reading

  • The project Homepage Address is here
  • The official documentation is here
  • Quick Start
Difference between WSGI, uWSGI, and uwsgi

What is the specific process when we deploy an application and browse the Web page? First, we have to have a Web server to process HTTP content. The Web server obtains client requests and submits them to the application. After the application completes processing, it returns the requests to the Web server, then the Web server returns to the client. The interaction between the Web server and the application is obviously required. At this time, many Web servers and applications interact in a standardized manner. The earliest appearance is CGI, later, FasgCGI, Java-specific Servlet specification, and Python-specific WSGI specification were introduced to improve CGI performance. With unified standards, the portability of programs is greatly improved. Here we will only introduce WSGI.

WSGI stands for the Web Server Gateway Interface, that is, the Web Server Gateway Interface, which is a simple and common Interface between the Web Server and Web applications defined by the Python language, based on the existing CGI standard design, similar interfaces were also introduced in many other languages. In general, WSGI can be divided into two parts: Server and application. In fact, WSGI can be understood as a bridge between the server and the application. The bridge side is the server, the other side is the application.

Based on web Components, WSGI can be divided into three types: web applications, web servers, and web middleware. The application end is implemented through various Web frameworks in the Python language, such as Flask and Django. With the framework, developers do not need to process WSGI. The framework will help solve these problems, developers only need to process HTTP requests and responses. The web server must be a little more complex. It can be implemented through uWSGI or the most common Web servers, such as Apache, Nginx, however, these Web servers do not have the built-in WSGI implementation, which is completed through expansion. For example, Apache supports WSGI through the extension module mod_wsgi. Nginx can encapsulate requests and send them to the application server, such as uWSGI, through proxy. UWSGI can complete WSGI server, process management, and application calls. The part of WSGI middleware can be understood as follows: we regard WSGI as a bridge. This bridge has two piers, one is the application end and the other is the server end. Then the bridge deck is the WSGI middleware, middleware has two roles: Server and application. Of course, it must comply with the restrictions and requirements on both the WSGI server and the WSGI application. For more details, see the description of PEP-333 middleware.

Werkzeug on which Flask depends is a WSGI toolkit. The official document defines Werkzeug as an HTTP and WSGI utility library designed for Python. Note that the Werkzeug provided by Flask is used for development and cannot be used in the production environment. Flask is a Web framework, while Werkzeug is not a Web framework or a Web server, it is just a WSGI toolkit. It serves as the underlying library of the Web framework in Flask, which facilitates our development.

We will describe uwsgi and uWSGI together. UWSGI is a Web server program, WSGI. As mentioned above, it is a protocol, and uwsgi is also a protocol. uWSGI implements uwsgi, WSGI, http, and other protocols. The introduction of uwsgi can be seen here. uwsgi is a proprietary protocol used by uWSGI. It defines the transmission data type description in four bytes. Even though they are all protocols, uwsgi and WSGI are irrelevant. We need to distinguish these two words.

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

Relationship between uWSGI and Nginx

From the above explanation, we know that uWSGI can act as a Web server. Why does uWSGI still need Nginx?

The most common saying is that Nginx has more advantages in processing static files and better performance. In fact, for a small website, there is no static file to be processed. It is also possible to use only uWSGI, but with Nginx, the advantages can be very specific:

  1. It is convenient for O & M. If the server is attacked by an IP address, add this IP address to the Nginx configuration file blacklist. If you only use uWSGI, You need to modify it in the code. On the other hand, Nginx is a Web server, and uWSGI looks more professional in terms of performance. For example, uWSGI does not support https in earlier versions, so Nginx is more secure.

  2. Nginx is capable of load balancing and HTTP caching. If there are more than one server, Nginx is required. Through Nginx, resources can be allocated to different server nodes, only one server can improve the performance, because Nginx can use headers's Expires or E-Tag, gzip compression, and other methods to handle static resources. After all, it is written in C, the native function is called, which is optimized for I/O. For dynamic resources, Nginx can also implement the cache function with CDN optimization (which is not possible for uWSGI ). Nginx supports efficient network libraries such as epoll and kqueue, which can handle highly concurrent short connection requests with lower performance than uWSGI.

  3. If multiple applications written in PHP, Python, and other languages run on the server host, you must listen to port 80. Nginx is required at this time. Because we need a forwarding service.

Therefore, Nginx is also required.

Deployment preparation

Here I assume we have a brand new server. Generally, Python is pre-installed in Linux, but easy_install is not necessarily installed.apt-get install python-setuptoolsInstall easy_install, and then install pip through easy_install.

Python Environment
$ sudo apt-get install python-setuptools$ sudo easy_install pip

We can also directly install pip:

$ sudo apt-get install python-pip

In this way, we can install virtualenv Through pip to build a virtual environment for the flask project.

$ sudo pip install virtualenv
Nginx
$ sudo apt-get install nginx

How to start nginx:

$ sudo /etc/init.d/nginx start

Enter the ip address of the server in the address bar of the browser. The following page indicates that Nginx has been started:

Install uWSGI

Before installing uWSGI, you need to solve the uWSGI dependency problem. Because uWSGI is an application written in C language, we need the C compiler and python development components:

$ sudo apt-get install build-essential python-dev$ sudo pip install uwsgi

At this point, we have installed uWSGI,

Start dry

First, we upload the application to the server. I am using git to manage the project, so you only need to clone it:

$ git clone http://url/of/you/git/repo

If you need to upload a project file locally, you can use the scp command. In short, we put the project file on the server, and then we can use virtualenv to manage the Python environment:

$ Virtualenv ENV $ source ENV/bin/activate # activate the virtual environment $ pip install-r requirement.txt # solve the dependency problem $ deactivate # exit the dependency Environment

Here we will use the 7 lines of Flask code as an example. I have created a new folder named helloflask, which will show the following content:

from flask import Flaskapp = Flask(__name__)@app.route("/")def hello():    return "Hello World!"if __name__ == "__main__":    app.run(host='0.0.0.0', port=5001)

Save as hello. py. Run the command. Enter the public ip address of the server in the browser and add the port number 5001.

Now we use Nginx to undertake Web Services.

Delete the default Nginx configuration file:

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

If you are interested, you can learn about some configuration parameters from the default Nginx configuration. Of course, the most reliable way is to read the Nginx documentation. Here we only try Nginx, and a simple configuration is given below:

server {    listen 80;    server_name your.website.url    charset utf-8;    client_max_body_size 75M;    location / { try_files $uri @yourapplication; }    location @yourapplication {        include uwsgi_params;        uwsgi_pass unix:/home/frank/Documents/helloflask/helloflask_uwsgi.sock;    }}

We can save the above content as helloflask_nginx.conf. For some explanations, server_name can be a domain name or an ip address. uwsgi_pass indicates the communication between Nginx and uwsgi, the sock file is used here. Of course, you can also specify the port number. For details, refer to here. Link the Nginx configuration file to the Nginx configuration folder with a soft link:

sudo ln -s /home/frank/Documents/helloflask/helloflask_nginx.conf /etc/nginx/conf.d/

Restart Nginx:

sudo /etc/init.d/nginx restart

At this time, refresh the public ip address (or bound domain name) of the server that was previously opened. What we see is not "Welcome to Nginx", but "502 Bad Way 」, because we have not started uWSGI, now we save the following content as helloflask_uwsgi.ini (you can also use the xml format. For details, refer to the document ):

#application's base folderbase = /home/frank/Documents/helloflask#python module to importapp = hellomodule = %(app)home = %(base)/ENVpythonpath = %(base)#socket file's locationsocket = /home/frank/Documents/helloflask/%n.sock#permissions for the socket filechmod-socket    = 666#the variable that holds a flask application inside the module imported at line #6callable = app#location of log fileslogto = /home/frank/Documents/helloflask/%n.log

The socket specifies the port file for communication with nginx. Other parameters, such as the number of threads and number of processors, can be viewed and configured. The above content can be specified through the uwsgi command parameters. You can just input a line of command in the command line. For "sustainable development", of course, it is better to save it with a file.

Run the uwsgi command, -- ini parameter:

$ uwsgi --ini helloflask_uwsgi.ini &

Specify the configuration file and run uwsgi in the background. Then, refresh the previously opened page to see that the application is running normally.

I tried to run multiple applications on one server. In fact, I only need to change the file name and process the uWSGI and Nginx configuration files respectively (the Nginx configuration can be written in the same file, write two servers)

Common commands for nginx

Start command:

$ sudo nginx

Or

$ sudo /usr/sbin/nginx

Stop nginx

$ sudo nginx -s stop

Enable nginx smoothly

sudo nginx -s reload

The so-called smooth start means restarting nginx without stopping nginx, re-loading the configuration file, and replacing the old working process with a new working process.

Summary

I used to play with PHP. Compared with PHP's almost one-click deployment, Python deployment is indeed much more complicated. However, the powerful feature of Python is that the language is concise and elegant. After all, life is too short and there is a loss of it, however, I believe this is temporary.

The last simple example is not standardized enough. For example, the application file should be stored in/var/www/, and the log file should be placed in the system's log folder, this is just a simple example. For more configuration content, we should learn through uWSGI and Nginx documents.

Install Nginx + uWSGI + Django on Ubuntu Server 12.04

Deployment of Django + Nginx + uWSGI

Deployment of Nginx + uWSGI + Django + Python application architecture

Ubuntu Server 14.04.2 LTS configure Nginx + Uwsgi + Django

This article permanently updates the link address:

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.