Learned a period of time Flask, but has not done the deployment, so think about how to deploy it, think, first bar service to straightened out bar, so it is to think about the service to get up, here the choice is Flask+uwsgi+nginx+ubuntu, Python chose 2.7.2 This is the Ubuntu system comes with a sense of learning or simply do not have to go soft, at present their flask is python3 write, slowly to transition, first of all straightened out, then the optimization is also very handy. In fact, for many of the principles of their own smattering, first of all this to put up, slowly to understand the logic inside what.
Nginx
Nginx is an efficient WEB server and reverse proxy server, can be used as load balancer (when there are N users to access the server, can realize the shunt, share the pressure of the server), compared to Apache, nginx support high concurrency, can support millions TCP connection, 100,000 level of concurrent connection, Simple deployment, less memory consumption, low cost, but Nginx modules are not rich in Apache. Nginx Support Uwsgi UWSGI protocol, so we can combine nginx with Uwsgi, nginx by uwsgi_pass the dynamic content to UWSGI processing.
Official documents in this
The best Nginx tutorials in this
Uwsgi
UWSGI is a Web server that implements protocols such as WSGI Protocol, UWSGI, HTTP, and so on. The role of Httpuwsgimodule in Nginx is to exchange with the UWSGI server. Be aware of the distinction between the three concepts of WSGI/UWSGI/UWSGI.
- Wsgi saw the previous section of the classmate is very clear, is a communication protocol.
- Uwsgi is a line protocol rather than a communication protocol, which is commonly used in data communication between UWSGI servers and other network servers.
- UWSGI is a Web server that implements the two Protocols of Uwsgi and WSGI.
The UWSGI protocol is a UWSGI server-owned protocol used to define the type of transmission information (type of information), each UWSGI packet the first 4byte for the transport information type description, which is two things compared to WSGI. preparation, first of all, we go to install the package we need, first of all I am here is my newly installed system, so there is no pip, so I go first to install PIP
sudo apt-get install Python-pip
Use the following command to install Flask
Pip Install flask
After installation, we can go to the test,
Import Flask
There was no error to prove that our flask was successfully installed. So what we're going to do next is install Ngnix and UWSGI.
sudo apt-get install Nginx
After installation, we can start the next, nginx start direct command line startup, simple rough
So our nginx started successfully, then we just use Pip to install Uwsgi
After we've installed it, we'll start working on it,
First I create a Python package for the app under Hellowflak,
# app/__init__.py from Import = Flask (__name__)fromimport view
Next we're going to create view.py
from Import App@app.route ('/') def index (): return ' Hellow '
So we're going to create hello.py in the app sibling directory.
from Import app if __name__ " __main__ " : app.run ()
, then we can use Python locally to debug our program,
Then we can look in the browser, enter the address, you can get this, so we flask program is not a problem.
So what we're going to do next is let Nginx take on Web services.
What I'm doing here is simply rude directly delete nginx configuration file
$ sudo rm/etc/nginx/sites-enabled/default
Next, I created a configuration file under Hellowflask
server { listen 8081; 127.0.0.1; CharSet UTF-8; Client_max_body_size 75M; / {try_files $uri @app;} Location @app { include uwsgi_params; 127.0.0.1:9000;} }
A little explanation: server_name can be a domain name, you can also write an IP address, Uwsgi_pass is to show Nginx and Uwsgi the way of communication. What I have chosen here is the set of port numbers.
So we're going to go to the soft-connect our configuration to Nginx.
/home/liwanlei/Desktop/hellowflask/helloflask_nginx.conf /etc/nginx/conf.d/
这样我们再去启动我们的nginx,
Sudo/etc/init.d/nginx restart
Here is not welcome, but the 502 error, because our current Uwsgi file is not configured, nor to start Uwsgi, then we are going to go out this uwsgi, the following example is my configuration.
[uwsgi] base =/home/liwanlei/desktop/ = 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 our UWSGI has been configured number, then we go to start,
Sudo/usr/bin/uwsgi--ini/home/liwanlei/desktop/hellowflask/helloflask_uwsgi.ini
We're going to restart our nginx,
reload
平滑重启可以用用,重新加载配置文件,用新的工作进程代替旧的工作进程。
-s reload
启动后,我这里修改了地址,这里就可以直接访问了,那么我们的部署这样就算可以了,简单的。
After the completion of the feeling is still very simple problem then go to the log, as long as the log is properly configured, then the elimination of errors is very fast.
In doubt can add me qq:952943386 or my QQ group 194704520
I hope we all rushed to the rookie to a higher
Flask+uwsgi+nginx+ubuntu deployment