Ngnix+gunicorn+Flag project deployment in Ubuntu

Related environment installation

The following operations are performed on the remote server (ubuntu 16.04)

Update apt related source first

	sudo apt-get update

MySQL installation

	apt-get install mysql-server
	apt-get install libmysqlclient-dev

Redis installation

sudo apt-get install redis-server

Install virtual environment

pip install virtualenv
pip install virtualenvwrapper

Make the installed virtualenvwrapper effective. Edit the ~ /. Bashrc file as follows:

export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/workspace

source /usr/local/bin/virtualenvwrapper.sh

Make the edited document effective

source ~/.bashrc
requirements file

A python project can contain a requirements.txt file to record all dependent packages and their exact version numbers for deployment in a new environment.

In the virtual environment, use the following command to generate the dependent package in the current virtual environment to the file with version number:
PIP free > Requirements. TXT
when you need to create a full copy of this virtual environment, you can create a new virtual environment and run the following command on it:
PIP install - R requirements. TXT
when you install flash MySQL DB, you may report an error, which may be because the dependent package is not installed, Execute the following command to install the dependency package:
sudo apt get build dep Python mysqldb to install the dependency package

Nginx

The program is written in C language
to realize the shunting, forwarding and load balancing
related operations
installation
$sudo apt get install nginx
running and stopping

/etc/init.d/nginx start #start
/etc/init.d/nginx stop  #stop

Configuration file
edit file/etc/nginx/sites available/default

If there are multiple servers, configure them here and modify the proxy under the location node_ pass

upstream flask {
        server 127.0.0.1:5000;
        server 127.0.0.1:5001;
}
server {
        # Listening on port 80
        listen 80 default_server;
        listen [::]:80 default_server;

        root /var/www/html;

        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location/{
                # Requests are forwarded to the gunicorn server
                proxy_pass http://127.0.0.1:5000;
                # Requests are forwarded to multiple gunicorn servers
                # proxy_pass http://flask;
                # Set the request header and pass the header information to the server side 
                proxy_set_header Host $host;
                # Set request header and pass original request ip to gunicorn server
                proxy_set_header X-Real-IP $remote_addr;
        }
}

Gunicorn

Gunicorn (green Unicorn) is a HTTP server for Python WSGI
transplanted from Ruby’s Unicorn project
the gunicorn server is compatible with various web frameworks, with very simple implementation and lightweight resource consumption
gunicorn starts directly with commands, There is no need to write configuration files
related operations
install
PIP install gunicorn
view options
gunicorn - H
run

-w: Indicates a process (worker)
-b: indicates bind ip address and port number (bind) 
-D: daemon
--reload: auto refresh (update code, auto refresh available when debug)

gunicorn - W 2 - B 127.0.0.1:7000 running file name: Flash program instance name
for example:
gunicorn - W 2 - B 0.0.0.0:7000 app:app -D --Reload

copy local code to remote
SCP – R local file path [email protected] : remote save path


Read More: