Tutorial on configuring Nginx + Gunicorn + Python + Flask environment on CentOS, centosng.pdf

Source: Internet
Author: User
Tags virtual environment virtualenv

Tutorial on configuring Nginx + Gunicorn + Python + Flask environment on CentOS, centosng.pdf

Basic Python Environment Construction
Python 2.6 is installed by default in CENTOS 6.X series. Currently, Python 2.7 is mainly used in development. There are still many differences between the two versions, and the program is often faulty in Python 2.6.

For example, the re. sub function 2.7 supports the flags parameter, but 2.6 does not.

Therefore, we plan to install Python 2.7 to run the Flask application, but 2.6 cannot be deleted because the system depends on it.

1. Install sqlite-devel
Because the Flask application may use the Sqlite database, this must be installed (because this was not installed before, so Python cannot import the sqlite3 database.
Of course, you can also compile and install it from the source code.

yum install sqlite-devel -y

2. Install Python 2.7

wget https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgztar xf Python-2.7.8.tgzcd Python-2.7.8./configure --prefix=/usr/localmake && make install

After the installation is successful, you can find Python 2.7 in/usr/local/bin/python2.7.

3. Install setuptools + pip
Note that you must use python2.7 to execute related commands.

# First get the setup script for Setuptools:wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py# Then install it for Python 2.7 :python2.7 ez_setup.py# Now install pip using the newly installed setuptools:easy_install-2.7 pip# With pip installed you can now do things like this:pip2.7 install [packagename]pip2.7 install --upgrade [packagename]pip2.7 uninstall [packagename]

4. Use virtualenv

# Install virtualenv for Python 2.7 and create a sandbox called my27project:pip2.7 install virtualenvvirtualenv-2.7 my27project# Check the system Python interpreter version:python --version# This will show Python 2.6.6# Activate the my27project sandbox and check the version of the default Python interpreter in it:source my27project/bin/activatepython --version# This will show Python 2.7.Xdeactivate

This is basically the case. Many tutorials on the Internet say soft links, but I feel that doing so will have some unknown impact on the system. This method can maintain system integrity as much as possible. Many built-in Python programs actually specify # in the header #! /Usr/bin/python, so they use Python 2.6 instead of the newly installed Python 2.7.

Deployment of Flask applications using Nginx + Supervisor + Gunicorn

1. Install supervisor

$ sudo pip install supervisor

Create a Flask Program
Create a virtual environment:

$ mkdir /tmp/wwwroot/web1$ cd /tmp/wwwroot/web1$ virtualenv deps$ source deps/bin/activate$ pip install flask gunicorn

Create a simple Flask program:

$ cat > myapp.py << EOF
from flask import Flaskapp = Flask(__name__)@app.route("/")def index():  return "hello flask 01"

Run the Flask program using gunicorn:

The simplest usage:

$ gunicorn -b 127.0.0.1:3721 myapp:app

Visit http: // 127.0.0.1: 3721. You can see "hello flask 01 ".

Port 3721 is just a demonstration.

2. Configure the supervisor
Create a configuration file:

$ cd /tmp/wwwroot$ echo_supervisord_conf > supervisor.conf$ cat >> supervisor.conf << EOF
[program:myapp];user=digwtxcommand=/tmp/wwwroot/web1/deps/bin/gunicorn -b 127.0.0.1:3721 myapp:appdirectory=/tmp/wwwroot/web1process_name=%(program_name)s ; process_name expr (default %(program_name)s)numprocs=1          ; number of processes copies to start (def 1)stopsignal=QUIT        ; signal used to kill process (default TERM)redirect_stderr=true     ; redirect proc stderr to stdout (default false)stdout_logfile=/tmp/myapp.log    ; stdout log path, NONE for none; default AUTO

Start Process:

$ supervisord -c supervisor.conf

Management process:

$ supervisorctl -c supervisor.conf

3. Configure nginx:
The request is mainly forwarded to gunicorn for processing.

Server {listen 8080; # default Request location/{# request to the local ip Address: 3721 proxy_pass http: // 127.0.0.1: 3721; # The gunicorn listening address is proxy_redirect off; proxy_set_header Host $ host: 8080; # If the port is not 80, enter proxy_set_header X-Real-IP $ remote_addr; proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for ;}}

Restart nginx and access http: // 127.0.0.1: 8080. You can see "hello flask 01 ".

Automatic Start:
What should I do if I want to automatically start it when I start it? Or, if the machine restarts, the WEB service will be disconnected.

In fact, it is also very simple, as long as you add a sentence in/etc/rc. d/rc. local:

supervisord -c /tmp/wwwroot/supervisor.conf

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.