Use popular servers written in Python to host WSGI applications and provide HTTP services. These servers are independent at run time: you can set up proxies from your Web server. Gunicorn ‘Green Unicorn’ is a WSGI HTTP server for UNIX.

1. Install PIP and Virtualenv

sudo apt-get install python-pip python-virtualenv
Copy the code

2. Install nginx

sudo apt-get install nginx
Copy the code

3. Create a test item

Create a new test project folder under ‘/var/ WWW /’ :

cd /var/www
sudo mkdir test
Copy the code

Upload local project files to remote server using SCP:

scp -r test.py [email protected]:/var/www/test//@ indicates the public IP address of the serverCopy the code

Here, we test test.py with a minimum item:

# file_name='test.py'
from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello_world(a):
    return 'Hello World! '

if __name__ == '__main__':
    from werkzeug.contrib.fixers import ProxyFix
    app.wsgi_app = ProxyFix(app.wsgi_app)
    app.run(host='0.0.0.0',port=5000)   Set external access restrictions. In this example, all IP addresses are accessible on port 5000
Copy the code

4. Configure the project environment

PIP can easily install Python related tools, frameworks, etc. Virtualenv can configure an independent working environment for each of our projects, so that the dependencies of different projects are isolated from each other, easier to manage, and do not affect each other.

Create virtualenv under project path ‘/var/ WWW /test’ :

Virtualenv -p /usr/bin/python3.6 venv // I am using python3.6. If you are using the default Python version, just enter: virtualenv venvCopy the code

Then activate the environment:

source venv/bin/activate
Copy the code

If the command line is preceded by an extra: ‘(venv)’, the environment is successfully activated.

However, all of the frameworks and tools installed below are specific to this project and are not related to other projects.

  • Install the flask:
pip install flask
Copy the code
  • Install the Gunicorn
pip install gunicorn
Copy the code
  • Install project dependencies according to requirements.txt
pip install -r requirements.txt
Copy the code

5. Start the project

  • Simple and quick start:
gunicorn test:app
Copy the code

Here test is our entry module name and app is the flask instance name.

  • Parameter start:
Gunicorn -w 4-B 127.0.0.1:5000test:app
Copy the code
  • The background to start
Nohup Gunicorn -w 4-b 127.0.0.1:5000test:app& // The program continues to run in the background when the remote connection is closedCopy the code

Run a Flask application with four worker processes (-w 4), bound to port 5000 of localhost (-b 127.0.0.1:5000)

Note: If deployed on a server, in addition to setting up a security group for the relevant port (such as 5000), you also need to change the address in the startup code above: ‘127.0.0.1’ to your server’s private IP address, or ‘0.0.0.0’, otherwise external access to the project will not be possible!

Configuration of 6.

Sometimes we need nginx proxy requests. Next we configure nginx to fetch flask projects by requesting port 80:

First backup nginx configuration file:

sudo cp /etc/nginx/sites-available/default default.bak
Copy the code

Then modify the nginx configuration file:

sudo nano /etc/nginx/sites-available/default
Copy the code

Change it to:

# file_name=default
server {
    listen 80;
    server_name example.org; This is the external domain name of the HOST machinelocation / { proxy_pass http://your. Private IP: 5000;Gunicorn Host: private IP address of our server
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; }}Copy the code

The location block in the code means to pass the request for port 80 to the address represented by proxy_pass, where we need to fill in our private IP and the port of the project!

Our HTTP requests for public IP can now be mapped to our Flask project deployed at Gunicorn.

complete

Exit the virtual environment:

deactivate
Copy the code

Close the gunicorn:

Pkill Gunicorn // Disable GunicornCopy the code

To view the Gunicorn process:

pstree -ap|grep gunicorn
Copy the code