In this article, we will build a simple Web application, in a virtual environment based on the Flask framework, using Gunicorn to do Wsgi container, with Supervisor management process, and then use OneAPM Python probe to monitor application performance, forming a "closed loop"! Hope to be helpful to everyone, first of all to introduce the environment:
System environment: Ubuntu 14.04 Python 2.7.6
Installing the component library
The first step is to install the required repositories because you intend to use the virtual environment to install and manage the Python components with PIP, so update the local package first and then install the components:
sudo apt-get update sudo apt-get install python-pip python-dev nginx
Create a virtual Environment virtualenv
Create a different Python isolated environment in one system without affecting each other, in order to keep the system clean, decided to run the application with Virtualenv, create an easy-to-identify directory, start the installation, create the project directory Super, and then activate the environment:
sudo pip install virtualenv mkdir ~/supervisor && cd ~/supervisor virtualenv super source super/bin/activate
Installing the Flask Framework
OK, now in the virtual environment, start to install the Flask framework, Flask relies on two libraries Werkzeug and JINJIA2, the PIP way to install, Pip is an important tool, Python use it to manage the package:
pip install flask
Write a simple Web service myweb.py with Flask, because there are some tests to be done, and several requests are set up:
from flask import Flask app = Flask(__name__) @app.route(‘/‘)def index(): return ‘hello world supervisor gunicorn ‘@app.route(‘/1‘)def index1(): return ‘hello world supervisor gunicorn ffffff‘@app.route(‘/qw/1‘)def indexqw(): return ‘hello world supervisor gunicorn fdfdfbdfbfb ‘if __name__ == ‘__main__‘: app.debug = True app.run()
Start Flask look!
python myweb.py
Access http://127.0.0.1:5000 in the browser to see "A few paths to try"
Deploying the Python Web with Gunicorn
We are now using Flask's own server to complete the launch of the Web service. In a production environment, Flask comes with a server that does not meet performance requirements. So here we use Gunicorn to do WSGI container, to deploy Python, first or install Gunicorn:
pip install gunicorn
When we install the Gunicorn, we need to start the flask,flask with the Gunicorn boot, the code inside the Flask name starts the App.run (). And here we use gunicorn,myweb.py is equivalent to a library file, is called by Gunicorn, so start:
gunicorn -w 4 -b 0.0.0.0:8000 myweb:app
Where MyWeb refers to Myweb.py,app is the name of the Wsgifunc, so that the operation of monitoring 8000 port, the original 5000 port is not enabled,-W indicates how many worker,-b to open the Gunicorn development of the access address.
Want to end Gunicorn only need to perform pkill gunicorn, but sometimes PS find PID process number to kill. However, this is too cumbersome for a development, so there is another artifact---supervisor, a tool dedicated to managing processes, and a tool process for managing the system.
Installing Supervisor
pip install supervisor echo_supervisord_conf > supervisor.conf # 生成 supervisor 默认配置文件 gedit supervisor.conf # 修改 supervisor 配置文件,添加 gunicorn 进程管理
The configuration for adding myweb.py at the bottom of the supervisor.conf /home/wang/supervisor/super
is my project directory "
[program:myweb]command=/home/wang/supervisor/super/bin/gunicorn -w 4 -b 0.0.0.0:8000 myweb:app directory=/home/wang/supervisor startsecs=0 stopwaitsecs=0 autostart=false autorestart=false user=wang stdout_logfile=/home/wang/supervisor/log/gunicorn.log stderr_logfile=/home/wang/supervisor/log/gunicorn.err
Basic use commands for supervisor:
supervisord -c supervisor.conf supervisorctl -c supervisor.conf status 查看supervisor的状态 supervisorctl -c supervisor.conf reload 重新载入 配置文件 supervisorctl -c supervisor.conf start [all]|[appname] 启动指定/所有 supervisor 管理的程序进程 supervisorctl -c supervisor.conf stop [all]|[appname] 关闭指定/所有 supervisor 管理的程序进程
Supervisor also has a Web management interface that can be activated. Change the configuration below:
[inet_http_server] ; inet (TCP) server disabled by defaultport=127.0.0.1:9001 ; (ip_address:port specifier, *:port for alliface) username=wang ; (default is no username (open server) password=123 ; (default is no password (open server))[supervisorctl]serverurl=unix:///tmp/supervisor.sock ; use a unix:// URL for a unix socket serverurl=http://127.0.0.1:9001 ; use an http:// url to specify an inet socket username=wang ; should be same as http_username if set password=123 ; should be same as http_password if set ;prompt=mysupervisor ; cmd line prompt (default "supervisor");history_file=~/.sc_history ; use readline history if available
You can now start Gunicorn with Supervsior. To run the command:
supervisord -c supervisor.conf
The browser access http://127.0.0.1:9001 can get Supervisor Web management interface, Access http://127.0.0.1:8000 can see Gunicorn start the returned page.
Configure Nginx
In the front we have installed Nginx in the system environment, installed Nginx binary files placed in the/usr/sbin/folder, the next use Supervisor to manage Nginx. Here you need to be aware of a problem, a permissions issue. Nginx is the way to install the sudo, start the appropriate is also the root user, then we also need to start supervisor with the root user. To add a configuration file under supervisor.conf:
[program:nginx]command=/usr/sbin/nginx startsecs=0 stopwaitsecs=0 autostart=false autorestart=false stdout_logfile=/home/wang/supervisor/log/nginx.log stderr_logfile=/home/wang/supervisor/log/nginx.err
OK, after the configuration is complete, start Supervisor:
supervisord -c supervisor.conf
Visit the page or use AB for stress testing:
ab -c 100 -n 100 http://127.0.0.1:8000/qw/1
-C is used to specify the number of concurrency for a stress test, and-n is used to specify the total number of executions.
Installing the Python probe
Set up the web, want to monitor the application data in real time, what good tools, with OneAPM python probe test, first of all, is to install the Python probe:
pip install -i http://pypi.oneapm.com/simple --upgrade blueware
Generate the configuration file according to License Key:
blueware-admin generate-config (License Key) = blueware.ini
Because it is in the virtual environment, so pay special attention to the path, modify the supervisor.conf inside two:
[program:myapp]command = /home/wang/supervisor/super/bin/blueware-admin run-program /home/wang/supervisor/super/bin/gunicorn -w 4 -b 0.0.0.0:8000 myapp:app environment = BLUEWARE_CONFIG_FILE=blueware.ini
Restart your app
supervisorctl # 进入命令行 supervisor> reload # 重新加载
Access the page as above, or you can use AB for stress testing after a few minutes, you can see the page load time, Web things, page throughput, which is followed by setting custom things "business transaction".
Reference article:
How to Serve Flask applications with Uwsgi and Nginx on Ubuntu 14.04
OneAPM Python Probe Installation
Python Web Deployment: Nginx + Gunicorn + Supervisor + Flask Deployment Notes
Tornado+nginx+supervisor deployment on a production environment
Supervisor + Tornado + Nginx use detailed
This article is compiled and collated by OneAPM engineers. OneAPM is an emerging leader in application performance management, enabling enterprise users and developers to easily implement slow program code and real-time crawling of SQL statements. To read more technical articles, please visit the OneAPM official blog.
Django + Nginx + gunicorn+ Supervisor build Python Web in virtualenv environment