当前位置:网站首页>Deploy web using gunicorn Py application
Deploy web using gunicorn Py application
2022-06-29 20:19:00 【Brother Xing plays with the clouds】
I often see people ask python Of web How to deploy the application .
A common solution is to use apache Of mod_wsgi, Or a little high-end uWsgi. But personally, I think these are too complicated , It is inconvenient to use and maintain , I usually recommend gunicorn, coordination Apache or Nginx, It can realize simple and efficient application deployment and maintenance .
For this reason, I specially wrote such a document to introduce , And this article submits pull request To web.py Project cookbook, It's just Aaron Swartz After death ,web.py I don't know who is maintaining , It was submitted for a month and no one handled it , Look at the pull request list , also 2 The request years ago is hanging ……
So I had to fork One. , Add this page , The maintenance I will do in the future , Will be updated on this branch .
adopt Gunicorn stay Apache and Nginx Deploy
The following code is based on Gunicorn 0.14.6 stay Debian 6.0.6 and FreeBSD 9.0 Test under the system .
The latest version of Gunicorn There is no difference in the method of use . Other Linux Distribution version 、 Various BSD、Mac OS X The system should be OK . But it is not recommended. Windows System , Because there are many unnecessary difficulties in visual inspection .
Note:
- You can rename
code.pyFor any name you like , This example is based on code.py For example . /path-to/webpy-appTo include yourcode.pyThe path of the code ./path-to/webpy-app/code.pyIt should be yours **python file** Full path to .
You can run... On the command line gunicorn --version View the current gunicorn Version of .
install Gunicorn
See Gunicorn Official website :
http://gunicorn.org/#quickstart
The suggestion on the official website is to use virtualenv Way to install , It's a good way . I won't introduce virtualenv( Personal recommendation virtualenvwrapper), The following is the installed virtualenv Environment as an example , Of course, you can also skip virtualenv, Direct global installation ( Need to add sudo).
pip install gunicornuse Gunicorn Deploy web.py application
Gunicorn It's for deployment wsgi Applied , Any support wsgi All applications are OK , It's not just web.py.
The whole deployment process is divided into two parts :
- use Gunicorn function web.py/wsgi application
- To configure web server Front end reverse proxy
use Gunicorn function web.py application
I've said that before ,Gunicorn It's used to deploy wsgi Applied , So first of all, we need to modify code.py, Make it one wsgi application .
# ...
app = web.application(urls, globals())
# Add the following sentence here , that will do
application = app.wsgifunc()The simplest way to run it is :
gunicorn code:applicationamong code Is refers to code.py,application That's it. wsgifunc Name .
If it works like this , gunicorn It is used as a listener by default 127.0.0.1:8000 Of web server, It can be accessed locally : http://127.0.0.1:8000 visit .
If you want to access through the network , You need to bind different addresses ( You can also set the listening port at the same time ):
gunicorn -b 192.168.0.123:8080 code:application
In multicore The server On , In order to support more concurrent access and make full use of resources , You can use more gunicorn process :
gunicorn -w 8 code:application
So you can start 8 Two processes at the same time HTTP request , Improve the efficiency and performance of the system .
in addition , gunicorn The default network model is synchronous blocking (-k sync), It may not perform well enough for large concurrent accesses , It also supports other better patterns , such as :gevent or meinheld.
# gevent
gunicorn -k gevent code:application
# meinheld
gunicorn -k egg:meinheld#gunicorn_worker code:applicationOf course , To use these two things, you need to install them separately , Please refer to the respective documents for details .
The above settings can also be set through -c Parameters are passed into a configuration file to implement .
To configure Apapache Reverse proxy of
The simple reverse proxy configuration is as follows ( In the VirtualHost Li as an example ):
ProxyPass / http://127.0.0.1:8000/
ProxyPassReverse / http://127.0.0.1:8000/
ProxyPreserveHost On
ProxyErrorOverride Off Proxy all access requests to the root path to http://127.0.0.1:8000 Of gunicorn Service .
To configure Nginx Reverse proxy of
The simple reverse proxy configuration is as follows ( The same is true of virtual host For example ):
location / {
try_files $uri @test;
}
location @test {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8000;
} Proxy all access requests to the root path to http://127.0.0.1:8000 Of gunicorn Service .
In practical application, you may need to set more proxy_set_header Variable , It depends on the application requirements .
边栏推荐
- CorelDRAW2022全新版V24.1.0.360更新
- [buuctf.reverse] 142_ [SUCTF2019]babyunic
- mapbox-gl开发教程(十二):加载面图层数据
- 2021 CCPC Harbin E. power and modulo (thinking questions)
- Zotero期刊自動匹配更新影響因子
- 18. `bs对象.节点名.next_sibling` previous_sibling 获取兄弟节点
- Defense cornerstone in attack and defense drill -- all-round monitoring
- 4-1 port scanning technology
- Flume configuration 4 - Custom source+sink
- ETCD数据库源码分析——服务端PUT流程
猜你喜欢

【编译原理】类型检查
![[notes] take notes again -- learn by doing Verilog HDL – 008](/img/7f/0ca73446247455ac4d8f9667083a87.png)
[notes] take notes again -- learn by doing Verilog HDL – 008
![[compilation principle] syntax analysis](/img/9e/6b1f15e3da9997b08d11c6f091ed0e.png)
[compilation principle] syntax analysis

Finally, Amazon~

Flume配置4——自定義Source+Sink

Sentinel的快速入门,三分钟带你体验流量控制

As the "only" privacy computing provider, insight technology is the "first" to settle in the Yangtze River Delta data element circulation service platform
![[notes] take notes again -- learn by doing Verilog HDL – 014](/img/92/ba794253f1588ff9ad87d2571a453e.png)
[notes] take notes again -- learn by doing Verilog HDL – 014

The explain statement in MySQL queries whether SQL is indexed, and several types in extra collate and summarize

【编译原理】语法分析
随机推荐
fastadmin后台设置单选按钮
Sword finger offer 66 Building a product array
Zotero journal Automatic Matching Update Influencing Factors
Linux安装MySQL5
Liunx instruction
Software engineering - principles, methods and Applications
. NETCORE unified authentication authorization learning - first authorization (2)
wangeditor富文本编辑器使用(详细)
.NetCore统一认证授权学习——Run(1)
60天远程办公经验分享 | 社区征文
雪花id,分布式唯一id
Jupyter服务安装及启动
3-2 host discovery - layer 3 discovery
Regular expression series of mobile phone numbers
Hangfire details
File contains vulnerability
SSH command and instructions
JMeter BeanShell explanation and thread calling
苹果iPhone手机升级系统内存空间变小不够如何解决?
4-1 port scanning technology