python3.8.0 Django development api backend interface to deploy on Linux Centos7

I experienced a day when finally used locally python3 django development interface (API) deployed to the server, or record it, so as not to forget after, ha ha

Attention to the fact that, centos7 is based python2, and my side default is python2.7.5, remember not to delete python2, otherwise the consequences are serious, yum it will complain because python3 and python2 version conflict caused

The first step to install python3 Centos7, install the software management packages and dependencies that may be used

yum -y groupinstall "Development tools"
yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel

The second step, download Pyhton3 to / usr / local directory

wget https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tgz

The third step, extract

tar -zxvf Python-3.8.0.tgz

The fourth step is to enter cd Python-3.8.0

The fifth step, unzip to the specified path

./configure --prefix=/usr/local/python3

The sixth step, install python3

make

make install

Seventh step, to establish a soft link after the installation is complete easy to use add variables directly in the terminal python3

ln -s /usr/local/python3/bin/python3.8 /usr/bin/python3

The eighth step, after the installation is complete pip3 Python3 also an installation is complete, do not need to install separate software, like to establish a link
also gives pip3 softlinks

ln -s /usr/local/python3/bin/pip3.8 / usr / bin / pip3 

Note: If the soft chain is created, but when they can not be used to perform the following command: 
   mv pip pip2 # (the renamed file pip pip2 ) 
   LN -s / usr / local / to python3 / bin / PIP3 / usr / bin / PIP

If after installing the python3 found no pip pip is available for download in the following way into a directory that you want to download it which directory to enter which directory

Execute the command: wget https://bootstrap.pypa.io/get-pip.py --no-check-certificate

File downloads get-pip.py

Then execute: python3 get-pip.py

 

The ninth step, the installation virtualenv, convenient different versions of project management.

pip3 install virtualenv

The tenth step softlinks

ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv3

The tenth step, the installation is successfully established two folders in the root directory, mainly used for storage and env website files (files created at random, according to actual demand).

mkdir -p / data / env # virtual storage environment 
 
mkdir -p / data / www # store web project

The twelfth step, switch to the / data / env /, create the specified version of the virtual environment.

virtualenv3 --python = / usr / bin / python web ( project name) to create a virtual environment # 

       # and then enter 
       cd / the Data / env / Web / bin 

       # After entering the bin directory open a virtual environment, execute the command 
       Source of an activate   

    # Note :( start after the emergence of (web), instructions are successfully entered the virtual environment.)

Thirteenth Step, virtual environments with pip3 security django and uwsgi (note: uwsgi to be installed twice, once installed in the system, then enter the corresponding virtual environment installed once.)

pip3 install django # (if used to produce it, you need to specify and install the same version of your project) 

PIP3 install uwsgi 

# uwsgi to the soft links, ease of use 

ln -s / usr / local / python3 / bin / uwsgi / usr / bin / uwsgi

Step XIV, in the local project directory with the following command to export the current environment dependencies to requirements.txt file

pip freeze > requirements.txt 

Step 15, the program source code compression package. The items uploaded to the server corresponding directory (/ data / www /), decompression.

Here you can import local database, django actually built model, as long as the establishment of the project on-line mysql database in the name of the database, eg: python

You can then migrate the same database as the local development, the premise is already installed database ah

 

Step 16, project into the root path installed in the decompression good requirements.txt dependencies.

pip3 install -r requirements.txt

Step 17, install mysql

wget -P /home/liudong http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

cd / home / liudong

Mysql installation, YUM sources: rpm -ivh mysql57-community-release-el7-11.noarch.rpm

YUM source mysql check whether the installation was successful: yum repolist enabled | grep "mysql * - community *.."

Check mysql version, execute: yum repolist all | grep mysql

MySQL enabled to view current version: yum repolist enabled | grep mysql

yum install mysql-community-server

The installation process has been input "y" on it, the following results occur when, on behalf of the mysql database installation was successful

Start mysql service: systemctl start mysqld

Log into Mysql (I do not password immediately after the installation): mysql -uroot -p

View the default password

grep 'temporary password' /var/log/mysqld.log

 

 Reset your password go after

 

set password for 'root'@'localhost' = password('123456');

If a link is not navicat for mysql on may be the reason  https://blog.csdn.net/qq_19306197/article/details/83820319

 

Step 18, by  python3 manage.py runserver  run the project, if the normal start of the next step, not the normal run-up inspection 

If the error: django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.7.11 Solution:

Solve : in edit Python installation path  Python36-32 \ Lib \ site-packages \ django \ db \ backends \ mysql \ base.py

将文件中的如下代码注释

#if version < (1, 3, 3):
#    raise ImproperlyConfigured("mysqlclient 1.3.3 or newer is required; you have %s" % Database.__version__)

Step 19, add uwsgi configuration file (uwsgi.ini) in the project root directory

[uwsgi] 
socket = 127.0.0.1:8070 
# whether to use the main thread 
Master = to true 
# project directory (absolute path) in the server 
chdir = / the Data / the WWW / Web 
# Django WSGI file's directory 
wsgi-file = web / wsgi. Py 
# maximum number of processes 
processes = 4 
# number of threads per process 
threads = 2 
# status listening port 
stats = 127.0.0.1:9191 
# automatically clean up the environment configuration when you exit 
vacuum = true 
to automatically restart when the # directory file changes 
touch-reload = / Data / WWW / Web 
#Python file changes automatically restart 
# = Py-Auto-reload. 1 
# background and saved to the log file .log 
daemonize = /data/www/web/uWSGI.log

Step 20, enter deployed to the project root directory of the server, execute

--This uwsgi uwsgi.ini

Step 21, see the port, whether there is a look uwsgi

netstat -nltp

 

 Step 22 configure Nginx configuration file (the original configuration files can back up the following, and then go below replacement).

{Events 

    worker_connections 1024; 

} 

HTTP { 

    the include the mime.types; 
    default_type file application / OCTET-Stream; 
    the sendfile ON; 
    Server { 
        the listen 80; 
        server_name 127.0.0.1:80; # to their own domain name, the domain name is not modified as 127.0.0.1: 80 
        charset UTF-. 8; 
        lOCATION / { 
           the include uwsgi_params; 
           as # uwsgi in port and arranged to; uwsgi_pass 127.0.0.1:8070 
           uwsgi_param UWSGI_SCRIPT web.wsgi; # wsgi.py directory name where .wsgi + 
           uwsgi_param UWSGI_CHDIR / Data / www / web /; # item path 
           
        } 
        LOCATION / static {  
            Alias / Data / WWW / Web / MyApp / static; static resource path #
        } 
    
        LOCATION / {Templates
            alias /data/www/web/templates;
        }
    }

}

Twenty-three steps, we must pay attention Uwsgi and Nginx configuration file in the project path and the path static resource, fill in the correct order to successfully access. Otherwise, the 502 error. There is, after Django modify files and other configuration files, be sure to restart Uwsgi and Nginx, or do not take effect.

uwsgi start method 
    uwsgi --ini uwsgi.ini 
Nginx reboot method 
    nginx -s reload

The following summarizes Caution:

Item 1 on the line to remember to set DEBUG = False so, the error message so your project will not have access failures of the Django

2 sets all IP can access (settings.py)

 ALLOWED_HOSTS = ['*']

3 database settings (the settings.py)

= {DATABASES 
    'default': { 
        'ENGINE': 'django.db.backends.mysql', 
        'NAME': 'Python', # library name (modified according to the actual database name) 
        'PORT': 3306, port # ( do not change) 
        'the HOST': '127.0.0.1', the local Host # (no change) 
        "the uSER": 'the root', # username 
        'pASSWORD': '123456' # password 
    } 
}

Q Thank you very much here bloggers  https://www.cnblogs.com/chaoqi/p/11103188.html 

Guess you like

Origin www.cnblogs.com/ldlx-mars/p/12075566.html