Django project deployment (1)

Django project deployment: Linux (centos7)+Nginx+Uwsgi+Python3+Mysql+Redis The
notes come from teacher Liu Jianping. Self-study and self-use, infringement and deletion.

在windows系统中开发的django项目部署到centos系统中 并且可以在浏览器中可以访问项目
详细步骤(下面步骤都是ROOT权限执行):
1)	先安装python
2)	安装pip
3)	安装django
4)	把项目从windows系统拷贝到centos系统中django框架所在的项目的路径  通过命令行
5)	先通过django自带的web服务器运行项目  python manage.py runserver 0.0.0.0:8000   X
6)	安装uwsgi web服务器
7)	安装  web服务器  nginx (小巧 性能特别高  10000)   
8)	通过nginx来启动django项目

Preliminary understanding of the principle of Django+uWsgi+Nginx

Insert picture description here

Nginx 是一个反向代理服务器,负责静态资源处理、动态请求转发以及结果的回复。

uWSGI 是一个Web服务器,负责接收 Nginx 请求,转发并处理后发给 Django 以及
接收 Django 返回信息转发给 Nginx。uWSGI是 实现了uwsgi协议,WSGI规范和HTTP协议的 一个C语言实现的软件。

Django 是一个Web应用框架,在收到请求后进行逻辑和数据处理,然后响应结果给 uWSGI 服务器。
通信关系:

Insert picture description here

1. Update the system software package; install the software management package and possible dependencies

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

2. Install Python (Storage location: eg: /home/soft)

Log in to https://www.python.org/ftp/python/ website to see all versions, select the appropriate version to download

wget https://www.python.org/ftp/python/3.5.4/Python-3.5.4.tgz
tar -zxvf Python-3.5.4.tgz
cd Python-3.5.4

Insert picture description here

#编译安装
./configure --prefix=/usr/local/python3
make && make install

Insert picture description here
The result after make && make install:
Insert picture description here
View the file content of the py directory after installation
Insert picture description here
Insert picture description here

#创建软连接
ln -s /usr/local/python3/bin/python3.5 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3.5 /usr/bin/pip3
#查看Python3和pip3安装情况
python3  --version
pip3    --version

Insert picture description here
The Linux soft link is equivalent to the shortcut of Windows, and it can also refer to the environment variables of Windows. You can directly use Python3 to operate instructions, otherwise you will have to input a large string of content /usr/local/python3/bin/python3.5.
At this time, python3 --version is equivalent to /usr/local/python3/bin/python3.5 --version

Three, install virtualenv to store the expansion packages used by each project

pip3 install virtualenv
#建立软链接
ln -s /usr/local/python3/bin/virtualenv /usr/bin/virtualenv
#安装成功在根目录下建立两个文件夹,主要用于存放env和网站文件的。
mkdir -p /data/env
mkdir -p /data/wwwroot

4. Switch to /data/env/ and create a virtual environment of the specified version.

cd /data/env
virtualenv --python=/usr/bin/python3 pyweb

Insert picture description here

#然后进入/data/env/pyweb/bin ,启动虚拟环境:
cd /data/env/pyweb/bin
source activate

#Pay attention to the location of the mark, and (pyweb) appears, indicating that you have successfully entered the virtual environment
Insert picture description here

Five, install django and uwsgi with python3 in the virtual environment

Insert picture description here

# (如果用于生产的话,则需要指定安装和你项目相同的版本)
pip3 install django
#切换到网站目录/data/wwwroot,把windows系统中的Django项目拷贝到linux系统的存放项目的路径中(可用rz命令来拷贝 )
cd /data/wwwroot 
#上传py项目的zip包,然后解压

Insert picture description here
At this time, the path of the py project is /data/wwwroot/cms
Insert picture description here
and a manager.py must be uploaded .
Insert picture description here

Six, run the project through the web server that comes with django

Error:
Insert picture description here
The above error occurred, the possible reason is that the django has not been installed just now. Re-operation can be
python3 manage.py runserver 0.0.0.0:8000
Insert picture description here
At this time, another error is reported, prompting to install pymysql
Insert picture description here

At this point again: python3 manage.py runserver 0.0.0.0:8000. There is an error again
(to modify base.py) and
Insert picture description here
another error:
Insert picture description here

Repair reference : https://blog.csdn.net/weixin_45476498/article/details/100098297 After
Insert picture description here
modifying the content of the comment base.py, run python3 manage.py runserver 0.0.0.0:8000 again. There are still errors, because mysql is not installed yet. First point to the mysql of the public network, and let the project run first.
Insert picture description here

Insert picture description here
Modify the host of the database in settings.py. This points to a public network address 47.100.xxx.xxx luzancms
Insert picture description here

Insert picture description here
Insert picture description here
Modify allowed_hosts of setting.py
Insert picture description here

Add the port allowed by the firewall
https://blog.csdn.net/weixin_34332905/article/details/92673905

#防火墙端口、开启的命令
firewall-cmd --list-ports 

Run again after processing a series of errors
Insert picture description here

Visit 192.168.1.186:8000/admin/login. At this point, Django's own web server can run the py project normally.Insert picture description here
Insert picture description here

Summary of steps:

启动虚拟环境:source activate
8000端口一定要开启
版本的问题注释一些代码
进入项目的根目录python3 manage.py runserver o.o.o.o:8000

This deployment note process also records the error report and its handling method, and solves the problem through the online processing method through the given error keyword.

END

Guess you like

Origin blog.csdn.net/Nightwish5/article/details/111475126