Python Django project to deploy Linux servers

Project Dependencies: Linux Centos7 (Ali cloud lightweight server) + Python 3.7.2 + Django 2.2.1 + restframework 3.9.4 + mysql 5.7

1 installation python3 to 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

2 Download Pyhton3 to / usr / local directory

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

3 decompression

tar -zxvf Python-3.7.2.tgz

4 to enter decompression good python file

cd Python-3.7.2

5 is mounted to the specified path compiled

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

6 Installation python3

make

make install

Softlinks 7 after installation is complete, easy to use add variables directly in the terminal python3

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

After the installation is complete pip3 8 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.7 /usr/bin/pip3

注: 如果软链建立完成,但是却无法使用的时候执行下面命令:
   mv pip pip2  # (将文件 pip 更名为 pip2 )
   ln -s /usr/local/python3/bin/pip3 /usr/bin/pip  

9 installation virtualenv, convenient different versions of project management.

pip3 install virtualenv

10 softlinks

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

11 installed 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   # 存放虚拟环境
 
mkdir -p /data/wwwroot   # 存放web项目

12 Switch to the / data / env / down, create the specified version of the virtual environment.

       virtualenv3 --python=/usr/bin/python qize_demo (项目名) # 创建虚拟环境

       # 然后进入
       cd /data/env/qize_demo/bin 

       # 进入bin目录后开启虚拟环境,执行命令
       source activate  

    # 注:(启动后出现 (qize_demo),说明是成功进入虚拟环境。)

13 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  #(如果用于生产的话,则需要指定安装和你项目相同的版本)

pip3 install uwsgi

# 给uwsgi建立软链接,方便使用

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

14 projects in the local directory with the following command to export the current environment dependencies to requirements.txt file

pip freeze > requirements.txt 


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

16 into the mounting requirements.txt in good project dependencies decompression path roots.

pip3 install -r requirements.txt

17 to import database server. (If you are using Mysql words)

Export Mysql, django for your database

mysqldump -uroot -p password django>django.sql

The django.sql uploaded to the server in the server with the following command to import

mysql -uroot -ppassword

use dajngo;

source your Path\django.sql

18 by python3 manage.py runserver run the project, if the normal start of the next step, not the normal run-up examination (Here are a few common mistakes and solutions).

报错1:django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.3 or newer is required; you have 0.7.11 解决方案:

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__)

报错2: File "/usr/local/python3/lib/python3.7/site-packages/django/db/backends/mysql/operations.py", line 146, in last_executed_query query = query.decode(errors='replace')

AttributeError: 'str' object has no attribute 'decode'

Solve : Edit

/usr/local/python3/lib/python3.7/site-packages/django/db/backends/mysql/operations.py

Target the 146 line,

# 将 decode 改为 encode 保存并退出
query = query.decode(errors='replace') 改为 query = query.encode(errors='replace')

19 Add uwsgi configuration file (qize_demo.xml) in the project root directory

<uwsgi>       

 <socket>127.0.0.1:8070/</socket><!-- 内部端口,自定义 --> 

 <chdir>/data/wwwroot/qize_demo/</chdir><!-- 项目路径 -->            

 <module>qize_demo.wsgi</module> 

 <processes>4</processes> <!-- 进程数 -->     

 <daemonize>uwsgi.log</daemonize><!-- 日志文件 -->

</uwsgi>

Nginx configuration profiles 20 (the original configuration files to be backed or less, and then substituted into the following).

events {

    worker_connections  1024;

}

http {

    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    server {
        listen 80;
        server_name  127.0.0.1:80;  # 改为自己的域名,没域名修改为127.0.0.1:80
        charset utf-8;
        location / {
           include uwsgi_params;
           uwsgi_pass 127.0.0.1:8070;  #端口要和uwsgi里配置的一样
           uwsgi_param UWSGI_SCRIPT qize_demo.wsgi;     #wsgi.py所在的目录名+.wsgi
           uwsgi_param UWSGI_CHDIR /data/wwwroot/qize_demo/; #项目路径
           
        }
        location /static {
            alias /data/wwwroot/qize_demo/static; #静态资源路径
        }
    
        location /templates {
            alias /data/wwwroot/qize_demo/templates;
        }
    }

}

Note: Be sure to pay attention to 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.

21 Uwsgi and restart Nginx method:

uwsgi start method

uwsgi -x qize_demo.xml

Nginx reboot method

nginx -s reload

Then access the server ip (or domain name) in the browser, you can view the project.

If you want to use the domain name to access the project, the first in the domain name management background to the DNS server IP up, after binding domain in the Nginx configuration file. Then access the project through a binding domain in the browser.

The following summarizes some of the problems:

1 If an error message appears and more than 500 have access to information nginx, and nginx configuration is generally out of the question. Nginx.conf to view the configuration items in the correct path, xml file parameters and port uwsgi_pass uwsgi in port are the same, whether finished modifying the configuration after restart nginx. There is a python manage.py runserver to start the project can go to the root path under the project, to see if there is no error, there may be the program itself is a problem.

2 If an error message appears more than 400 access, focus on examination of the project path, including tempates / static static path is correct, and whether the project settings to configure the access path tempates / static files, and url is correct, then there html file css, js, img other documents introduced in the path is correct, such as:

  css文件:<link rel="stylesheet" href="/static/css/q.css">
  js文件 :<script src="/static/js/q.js"></script>

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': 'sql_base',       # 库名 (根据实际数据库名进行修改)
        'PORT': 3306,         # 端口(不用改)
        'HOST': '127.0.0.1',  # 本地host(不用改)
        "USER": 'root',           # 用户名
        'PASSWORD': '123456'  # 密码
    }
}

Guess you like

Origin www.cnblogs.com/chaoqi/p/11103188.html
Recommended