Use gunicorn to deploy the web. py Application

Source: Internet
Author: User
Tags virtualenv

I often see people asking how to deploy python web applications. A common solution is to use apache mod_wsgi or uWsgi at a higher level. However, I personally think this is too complicated and inconvenient to use and maintain. I generally recommend using gunicorn with Apache or Nginx for simple and efficient application deployment and maintenance. For this reason, this document is specially written for introduction, and this article submits the pull request to the web. the cookbook Of The py project, only after the death of Aaron Swartz, web. py does not know who is maintaining the service. It has not been processed after being submitted for a month. After reading the pull request list, there are still requests hanging two years ago ...... So I have to fork one. With this page added, my maintenance work will be updated on this branch in the future. Use Gunicorn to deploy the following code in Apache and Nginx Based on Gunicorn 0.14.6 to test in Debian 6.0.6 and FreeBSD 9.0 systems. Currently, the latest Gunicorn version has no difference in usage. Other Linux distributions, various BSD, Mac OS X, and other systems should be fine. However, Windows is not recommended, Because visual testing will lead to many unnecessary difficulties. Note: You can rename code. py to any name you want. This example uses code. py as an example. /Path-to/webpy-app is the path containing your code. py code. /Path-to/webpy-app/code. py should be the complete path of your ** python file. You can run gunicorn -- version on the command line to view the current gunicorn version. Install Gunicorn see Gunicorn Official Website: http://gunicorn.org/#quickstart official website is recommended to use virtualenv installation, this is a good way. Virtualenv is not introduced here (virtualenvwrapper is recommended for personal use). The following uses the installed virtualenv environment as an example. Of course, you can skip virtualenv and install it globally (sudo is required ). Pip install gunicorn use Gunicorn to deploy the web. py application Gunicorn, which is used to deploy wsgi applications. Any application that supports wsgi can, not just web. py. The entire deployment process is divided into two parts: running web with Gunicorn. the py/wsgi application configures the reverse proxy of the web server front-end to run the web with Gunicorn. the py application has already said that Gunicorn is used to deploy wsgi applications, so you must first modify the code. py to make it a wsgi application. #... App = web. application (urls, globals () # Add the following sentence here to apply = app. the simplest running method of wsgifunc () is: gunicorn code: application, where code refers to code. py. application is the name of wsgifunc. By default, gunicorn serves as a web server listening for 127.0.0.1: 8000. It can be accessed through http: // 127.0.0.1: 8000 on the local machine. If you want to access through the network, you need to bind a different address (you can also set the listening port): gunicorn-B 192.168.0.123: 8080 code: application on the multi-core server, to support more concurrent access and make full use of resources, you can use more gunicorn processes: gunicorn-w 8 code: application to start eight processes to process HTTP requests at the same time, improve system efficiency and performance. In addition, gunicorn uses the synchronous blocking network model (-k sync) by default, which may not be good enough for High-concurrency access. It also supports other better modes, such as gevent or meinheld. # Geventgunicorn-k gevent code: application # meinheldgunicorn-k egg: meinheld # gunicorn_worker code: application. Of course, you need to install these two items separately. For details, refer to your own documents. You can also use the-c parameter to input a configuration file. Configure the Apapache reverse proxy. The simple reverse proxy configuration is as follows (in VirtualHost for example): ProxyPass/http: // 127.0.0.1: 8000/ProxyPassReverse/http: // 127.0.0.1: 8000/ProxyPreserveHost On proxyerroverride Off www.2cto.com proxy all access requests to the root path to the gunicorn service of http: // 127.0.0.1: 8000. Simple reverse proxy configuration for configuring Nginx reverse proxy is as follows (also using virtual host as an example): location/{try_files $ uri @ test ;} location @ test {www.2cto.com 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 the gunicorn service of http: // 127.0.0.1: 8000. In actual applications, you may need to set more proxy_set_header variables, depending on application requirements.

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.