CentOS deployment method of flask project

Recently, I am learning python and implemented a personal blog program using flask. I want to deploy it to the server when I'm finished. Because I was a novice, I stumbled all the way and finally got it basically done. The information on the Internet is not very friendly to novices, and they are all fragmentary, so I sorted it out, on the one hand, as my own record, which is convenient for future reference, and on the other hand, I hope to help novices like me.

premise

  1. There is a server (otherwise it will be messy), you can refer to high-quality foreign vps recommendations for purchase
  2. There is a personal domain name (Of course, you can directly use the IP access, but it’s a bit strange, isn’t it? You can go to GoDaddy to buy a domain name

1. Install git

You can choose github or Bitbucket. Of course, you can also build a git server yourself, but I don’t think it is necessary. I choose Bitbucket mainly because its private library is free

sudo yum install git

The follow-up will be no different from our local development. Configure the ssh key and clone code, it will not be expanded. The project directory is recommended to be placed under /home/www/

**2. Install **Mysql

Add MySQL YUM source

$wget 'https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm'
$sudo rpm -Uvh mysql57-community-release-el7-11.noarch.rpm
$yum repolist all | grep mysql

mysql-connectors-community/x86_64 MySQL Connectors Community         36
mysql-tools-community/x86_64   MySQL Tools Community            47
mysql57-community/x86_64     MySQL 5.7 Community Server         187

Install the latest version

$sudo yum install mysql-community-server

Start the MySQL service

$sudo service mysqld start 
$sudo systemctl start mysqld #CentOS 7
$sudo systemctl status mysqld
● mysqld.service - MySQL Community Server
 Loaded:loaded(/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
 Active:active(running) since Sat 2017-05-2712:56:26 CST; 15s ago
 Process:2482 ExecStartPost=/usr/bin/mysql-systemd-start post(code=exited, status=0/SUCCESS)
 Process:2421 ExecStartPre=/usr/bin/mysql-systemd-start pre(code=exited, status=0/SUCCESS)
 Main PID:2481(mysqld_safe)
 CGroup:/system.slice/mysqld.service
  ├─2481/bin/sh /usr/bin/mysqld_safe --basedir=/usr
  └─2647/usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/...

It is already running

change Password

$ mysql -uroot -p 

You are required to enter a password here. When Mysql is installed, a default password will be generated. Use the grep "temporary password" /var/log/mysqld.log command, and the string after the last quote is the default password of root.

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'NewPassword';

Modify code

Set the default encoding in /etc/my.cnf

[ client]default-character-set= utf8

[ mysqld]default-storage-engine = INNODB

character-set-server = utf8

collation-server = utf8_general_ci #not case sensitive

collation-server = utf8_bin #Case sensitive

collation-server = utf8_unicode_ci #Than utf8_general_ci is more accurate

Create database

mysql> CREATE DATABASE <datebasename> CHARACTER SET utf8;

3. Install python3 pip3

CentOS 7 installs Python 2 by default. When you need to use Python 3, you can manually download the Python source code and compile and install it.

Install Python 3

sudo mkdir /usr/local/python3 #Create installation directory
$ wget --no-check-certificate https://www.python.org/ftp/python/3.6.2/Python-3.6.2.tgz #Download Python source files
# Note: When wget gets https, add:--no-check-certifica
$ tar -xzvf Python-3.6.2.tgz #Unzip the package
$ cd Python-3.6.2 #Enter the unzipped directory
sudo ./configure --prefix=/usr/local/python3 #Specify the created directory
sudo make
sudo make install #Compile and install

When executing ./configure, an error may be reported, configure: error: no acceptable C compiler found in $PATH, this is because the appropriate compiler is not installed, just install it.

sudo yum install gcc-c++ (When using sudo yum install gcc-c++, gcc and other dependent packages will be automatically installed/upgraded.)

Configure two versions to coexist

Create a soft link for python3:

$ sudo ln -s /usr/local/python3/bin/python3 /usr/bin/python3

In this way, you can use Python 2 through the python command, and python3 to use Python 3.

Install pip

$ sudo yum -y install epel-release #First install epel extension source
$ sudo yum -y install python-pip #Install python-pip
$ sudo yum clean all #Clear cache

It seems that only pip2 can be installed in this way. If you want to install the pip of Python 3, you can install it through the following source code.

# Download source code
$ wget --no-check-certificate https://github.com/pypa/pip/archive/9.0.1.tar.gz

$ tar -zvxf 9.0.1.tar.gz  #unzip files

$ cd pip-9.0.1

$ python3 setup.py install #Install with Python 3

Create link:

$ sudo ln -s /usr/local/python3/bin/pip /usr/bin/pip3

Upgrade pip

$ pip install --upgrade pip

4. Install gunicorn

Gunicorn (Unicorn) is an efficient Python WSGI Server, which is usually used to run wsgi application (written by ourselves to follow the writing specifications of WSGI application) or wsgi framework (such as Django, Paster), and its status is equivalent to Tomcat in Java . WSGI is such a protocol: it is an interface between a Python program and user requests. The role of the WSGI server is to accept and analyze the user's request, call the corresponding python object to complete the processing of the request, and then return the corresponding result. To put it simply, gunicorn encapsulates the underlying implementation of HTTP. We start the service through gunicorn, and user requests and services are transmitted through gunicorn.

Create a virtual environment

cd /home/www/blog
mkdir venv
python3 -m venv venv

Activate the virtual environment:

source venv/bin/activate

Then install the dependent packages according to the requirements.txt file:

pip3 install -r requirements.txt

Install gunicorn

pip3 install gunicorn

Create a wsgi.py file in the project root directory

from app import create_app

application =create_app('production')if __name__ =='__main__':
 application.run()

No longer start the service through manage.py, it is only used during development

Start the service:

gunicorn -w 4-b 127.0.0.1:8000 wsgi:application

5. Install Nginx

nginx is a high-performance web server. Usually used as a reverse proxy server on the front end. The so-called forward and reverse (reverse) are just English translations. Proxy service, in short, a request is sent from a local area network through a proxy server, and then reaches a server on the Internet. The proxy in this process is a forward proxy. If a request comes from the Internet, it first enters the proxy server, and then the proxy server forwards it to the target server on the LAN. At this time, the proxy server is a reverse proxy (relatively forward).

Forward Proxy: {Client—"Proxy Server}—"Server

Reverse proxy: Client—" {Proxy Server—" Server}

{} Represents local area network

Nginx can do both forward and reverse.

$ yum -y install nginx

Start nginx service

$ service nginx start

Stop nginx service

$ service nginx stop

Restart nginx service

$ service nginx restart

Graceful restart

The nginx configuration is changed, you can reload it without first closing and opening

$ nginx -s reload

After starting, enter the ip address of the server in the browser, you can see

Here yum installs nginx and it is complete

Add configuration

The configuration file of nginx is: /etc/nginx/nginx.conf

server {
 listen 80;
 server_name adisonhyh.com;

 location /{
  proxy_pass http://127.0.0.1:8000;
  proxy_set_header Host $host;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}
  1. Listen http default port number 80
  2. server_name: personal website domain name
  3. Proxy the request to port 8000 of the machine (the port specified by gunicorn to start the service) and copy the proxy_set_header left

The relationship between gunicorn and nginx:

Gunicorn can provide services separately, but production environments generally do not. First of all, static resources (jscssimg) will take up a lot of request resources. For gunicorn, it should pay more attention to actual business requests and processing instead of wasting resources on static resource requests; in addition, there is no way to run gunicorn alone Start multiple processes and multiple ports to [Load Balancing] (https://cloud.tencent.com/product/clb?from=10680).

The role of nginx is to make up for the above problems. First, as a front-end server, it can handle all static file requests. At this time, gunicorn is used as a back-end server. Nginx will forward dynamic requests to the back-end server, so we can start multiple gunicorn processes. Let nginx load balance and forward requests to multiple gunicorn processes to improve server processing efficiency and processing capabilities. Finally, nginx can also configure a lot of security-related, authentication-related and many other processes, which can make your website more focused on business writing, and leave some forwarding rules and other business-unrelated things to nginx.
After configuration, open the local browser, enter the domain name, and you should be able to access it.

6. supervisor

If you need the process to be executed all the time, if the process is interrupted for various reasons, it will automatically restart, then supervisor is a good choice. The supervisor management process is to start these managed processes as child processes of the supervisor through fork/exec, so we only need to add the path of the executable file of the process to be managed to the configuration file of the supervisor. At this time, the managed process is regarded as a child process of the supervisor. If the child process has an abnormal terminal, the parent process can accurately obtain the information of the abnormal terminal of the child process. By setting autostart=true in the configuration file, the abnormal interruption can be realized Automatic restart of the child process.

Install supervisor

$ pip install supervisor
$ echo_supervisord_conf > supervisor.conf  #Generate supervisor default configuration file
$ vim supervisor.conf            #Modify the supervisor configuration file and add gunicorn process management

Add at the bottom of the blog supervisor.conf configuration file (note that my working path is www/home/blog/)

[ program:blog]
command=/home/www/blog/venv/bin/gunicorn -w4 -b0.0.0.0:8000 wsgi:application  ;supervisor start command
directory=/home/www/blog                         ;Project folder path
startsecs=0;Start Time
stopwaitsecs=0;Termination waiting time
autostart=false;Whether to start automatically
autorestart=false;Whether to restart automatically
stdout_logfile=/home/www/blog/logs/gunicorn.log              ;log
stderr_logfile=/home/www/blog/logs/gunicorn.err              ;Error log

Use supervsior to start gunicorn

$ sudo supervisord -c supervisor.conf 
$ sudo supervisorctl start blog

Enter the configured address in the browser address bar to access the website.

7. fabric

In the last step, we use fabric to implement remote operation and deployment. Fabric is a tool similar to Makefiles under Python, but can execute commands on remote servers.

Install fabric

pip install fabric

Create a new fabfile.py file in the blog directory

import os
from fabric.api import local, env, run, cd, sudo, prefix, settings, execute, task, put
from fabric.contrib.files import exists
from contextlib import contextmanager

env.hosts =['204.152.201.69']
env.user ='root'
env.password ='****'#password
env.group ="root"

DEPLOY_DIR ='/home/www/blog'
VENV_DIR = os.path.join(DEPLOY_DIR,'venv')
VENV_PATH = os.path.join(VENV_DIR,'bin/activate')

@ contextmanager
def source_virtualenv():withprefix("source {}".format(VENV_PATH)):yield

def update():withcd('/home/www/blog/'):sudo('git pull')

def restart():withcd(DEPLOY_DIR):if not exists(VENV_DIR):run("virtualenv {}".format(VENV_DIR))withsettings(warn_only=True):withsource_virtualenv():run("pip install -r {}/requirements.txt".format(DEPLOY_DIR))withsettings(warn_only=True):
   stop_result =sudo("supervisorctl -c {}/supervisor.conf stop all".format(DEPLOY_DIR))if not stop_result.failed:
   kill_result =sudo("pkill supervisor")if not kill_result:sudo("supervisord -c {}/supervisor.conf".format(DEPLOY_DIR))sudo("supervisorctl -c {}/supervisor.conf reload".format(DEPLOY_DIR))sudo("supervisorctl -c {}/supervisor.conf status".format(DEPLOY_DIR))sudo("supervisorctl -c {}/supervisor.conf start all".format(DEPLOY_DIR))

@ task
def deploy():execute(update)execute(restart)

Now if the code is updated, you can directly perform remote deployment locally

fab deploy

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

CentOS deployment method of flask project
Summary of linux (centos) project deployment phase related commands
Centos6.5 installation and deployment of KVM
Concise summary of Ceph deployment on Centos7
Centos-6.5 installation and deployment of LNMP environment
Centos7 installation and deployment of gitlab server
CentOS8 yum/dnf configuration method of domestic sources
Centos7 installation and deployment of Airflow detailed
CentOS deployment Harbor
Summary of CentOS method to set static IP
Distributed deployment of Apollo configuration center under CentOS8
Use Cobbler to automate batch deployment of CentOS / Ubuntu
Centos8 (minimal installation) a new installation of Python3.8+pip method tutorial
Graphical installation of CentOS8
ubuntu environment deployment project
Docker CentOS installation method
Rapid deployment of Kubernetes (k8s) cluster in CentOS7 environment
CentOS7.6 server deployment VNC
Deployment of vulnerability scanning and analysis software Nessus under CentOS
Detailed method of installing Kernel 5.x kernel version on CentOS 8 system
Centos8 minimal deployment and installation of OpenStack Ussuri detailed tutorial
[CentOS environment deployment] Java7/Java8 deployment under CentOS
Centos7.4 deployment configuration Elasticsearch5.6 cluster
Centos7.6 method to install Tomcat-8.5.39
Centos7 silent installation of Oracle11g
Implementation of CentOS8.0 Network Configuration
Centos7.2 deployment vnc service record
CentOS environment installation of Docker
Python method of parameter passing
Ceph rapid deployment (Centos7 + Jewel)
Using Elastic Stack on CentOS 8: Deployment and authentication configuration of Elasticsearch/Kibana 7.8