Deploy the Book Management System Project Deploy the Book Management System Project

Deploy the library management system project

 

Deployment preparation

To deploy a book management project you will use the following software

  • nginx
  • uWSGI
  • CentOS7
  • Deploying the Book Management Project File
  • virtualenv
  • supervisor

WEDNESDAY

Python web server development uses WSGI protocol (Web Server Gateway Interface)

The python web project will generate a wsgi.py file by default to determine the application module.

uWSGI is used in the production environment, which implements all the interfaces of WSGI, and is written in C language, which is a very efficient web server.

uWSGI is a full-featured HTTP server that implements WSGI protocol, uwsgi protocol, http protocol, etc. All it has to do is convert the HTTP protocol into a network protocol supported by the language. For example, the HTTP protocol is converted into the WSGI protocol, so that Python can use it directly.

Nginx

Nginx is used for its reverse proxy function, and the project will be deployed online through Django+uWSGI+Nginx.

CentOS

1. Package the project CRM folder and compress the file

2. Upload files to Centos server through xftp, scp, lrzsz, etc.

Linux usage tips

1. Use software such as xshell or iTerm to operate your linxu with multiple terminals, so that when debugging uwsgi, nginx, and project code, you can avoid switching directories back and forth and improve work efficiency.

2. Note that the configuration file of the linux software has been modified, and the service must be restarted to take effect.

Virtualenv

Build a clean, isolated python interpreter environment to prevent software dependencies, conflicts and other issues, it is recommended to use.

Supervisor

Supervisor (http://supervisord.org/) is a client/server service developed in Python. It is a process management tool under Linux/Unix systems and does not support Windows systems. It can easily monitor, start, stop, and restart one or more processes. For a process managed by Supervisor, when a process is accidentally killed, supervisort will automatically restart the process after monitoring the death of the process.

start deployment

1. Familiar with linux operation

Linux basic command operations, omit.....

2. Installation of python3 interpreter

Reference blog: https://www.cnblogs.com/tiger666/articles/10312522.html

3. Configuration of virtualenvwrapper tool to solve virtual environment problems

#Confirm the virtualenvwrapper configuration in the ~/.bashrc configuration file 
WORKON_HOME=~/ Envs
VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages'
VIRTUALENVWRAPPER_PYTHON=/opt/python36/bin/python3
source /opt/python36/bin/virtualenvwrapper.sh

#Create and activate a virtual environment for the book management system project 
mkvirtualenv book_manage_env

4. Install and configure the MariaDB database, create database data, and migrate the data imported into the library management system

(1) Install and start the MariaDB database

Under CentOS7, MariaDB is the mysql database, but the package name is different

#Use yum to download and install mariadb 
yum install MariaDB-server MariaDB-client - y

#Start mariadb server 
systemctl start mysql

(2) Connect and authorize root user remote access

#Use the client to link the mysql server 
mysql -uroot -p log in to mariadb on linux

#Note 1, linux database, you need to set remote link permissions for the root user, the password is qishi3q 
grant all privileges on *.* to root@ ' % ' identified by ' qishi3q ' ;
 #Authorize all permissions, in all libraries, All tables are for the root user on all hosts, and the set permission password is qishi3q #Refresh 
the authorization table 
flush privileges;

Note 2, linux firewall and selinux should be closed, otherwise windows to link linux port 3306 may be rejected

(3) Import the data of the library management project

On the linux server, mysql, import the data of the library management system

# 1. The export and import of mysql data 
This command is typed in linux/ windows
mysqldump -u root -p --all-databases >  book_manage.dump  

# 2. Upload this data file to the linux database

# 3. In mysql of linux, import this data file (you need to specify the database in the dump file: use szday58) 
mysql -u root -p < /opt/ book_manage.dump #Or
 enter mysql to enter the database, use the command: 
source / opt/book_manage.dump

5. Enter the virtual environment and run the project

The test uses linux's python interpreter to run the project and switch to running in the project (note that the module problem of the interpreter must be solved to run the project normally)

# 127.0.0.1 can't be used, it can only be accessed locally. If everyone can access it, you must use 0.0.0.0 
python3 manage.py runserver 0.0.0.0:8000

Then you can access it through IP plus port. If this IP is my cloud server IP, then when we go to the interview, we can take this project and show it to the interviewer on the computer. It’s so compelling~

Easter egg : Now there is a problem. We can only access the project through 10.0.0.7:8000, but this is not the case for websites on the public Internet . For example, Luffy Academy is accessed through a domain name. We can also resolve the Luffy Academy. The website server IP can also be accessed through the address, but the port is not added later, because the default is to use port 80

Although we can directly change the port to 80, there is no problem, then the problem is: 

1 django uses the wsgiref stand-alone module by default to run the web interface, and the performance will be relatively low. How to solve it?

uwsgi + django + nginx

2 What should we do if we want to run another project to provide external services? The server has only one port 80, how to solve it

nginx + uwsgi + django + supervisor


-Complete the installation and configuration of nginx, understand how to configure nginx.conf

-Complete the uWSGI command learning, use uWSGI to start the knight project, support multi-process

-Complete nginx to process the static files of the book management project

-Final effect

Visit port 80 of nginx and you can find it Book management page, and ensure that the static file page is normal

Deployment preparation

To deploy a book management project you will use the following software

  • nginx
  • uWSGI
  • CentOS7
  • Deploying the Book Management Project File
  • virtualenv
  • supervisor

WEDNESDAY

Python web server development uses WSGI protocol (Web Server Gateway Interface)

The python web project will generate a wsgi.py file by default to determine the application module.

uWSGI is used in the production environment, which implements all the interfaces of WSGI, and is written in C language, which is a very efficient web server.

uWSGI is a full-featured HTTP server that implements WSGI protocol, uwsgi protocol, http protocol, etc. All it has to do is convert the HTTP protocol into a network protocol supported by the language. For example, the HTTP protocol is converted into the WSGI protocol, so that Python can use it directly.

Nginx

使用nginx是为了它的反向代理功能,项目会通过Django+uWSGI+Nginx进行服务器线上部署。

CentOS

1.打包项目CRM文件夹,压缩文件

2.通过xftp、scp、lrzsz等上传文件至Centos服务器

Linux使用技巧

1.通过xshell或者iTerm等软件,多终端操作你的linxu,这样对uwsgi、nginx、项目代码调试的时候,避免来回切换目录,提供工作效率。

2.注意修改了linux软件的配置文件,都要重启服务才能生效。

Virtualenv

构建一个干净,隔离的python解释器环境,防止软件依赖,冲突等问题,建议使用。

Supervisor

Supervisor(http://supervisord.org/)是用Python开发的一个client/server服务,是Linux/Unix系统下的一个进程管理工具,不支持Windows系统。它可以很方便的监听、启动、停止、重启一个或多个进程。用Supervisor管理的进程,当一个进程意外被杀死,supervisort监听到进程死后,会自动将它重新拉起,很方便的做到进程自动恢复的功能,不再需要自己写shell脚本来控制。

开始部署

1、熟悉linux操作

linux基本命令操作, 省略.....

2、python3解释器的安装

参考博客: https://www.cnblogs.com/tiger666/articles/10312522.html

3、virtualenvwrapper工具的配置,解决虚拟环境问题

# 确认~/.bashrc配置文件里面的virtualenvwrapper配置
WORKON_HOME=~/Envs
VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages'
VIRTUALENVWRAPPER_PYTHON=/opt/python36/bin/python3
source /opt/python36/bin/virtualenvwrapper.sh

# 新建并激活一个虚拟环境,用于图书管理系统项目
mkvirtualenv book_manage_env

4、安装配置MariaDB数据库,且创建数据库数据,迁移导入图书管理系统的数据

(1) 安装并启动MariaDB数据库

在CentOS7下,MariaDB就是mysql数据库,只是包名的不同

# 使用yum下载并安装mariadb
yum install MariaDB-server MariaDB-client -y

#启动mariadb服务端
systemctl start mysql

(2) 连接并授权root用户远程访问

# 使用客户端去链接mysql服务端
mysql -uroot -p 在linux上登陆mariadb

# 注意1, linux的数据库,需要对root用户设置远程链接的权限, 密码是qishi3q
grant all privileges on *.* to root@'%' identified by 'qishi3q';
# 授权所有的权限,在所有库,所有表  对  root用户在所有的主机上, 设置权限密码是qishi3q
#刷新授权表
flush privileges;

注意2, linux的防火墙和selinux要关闭,否则windows去链接linux的3306端口可能被拒绝

(3) 导入图书管理项目的数据

在linux服务端,mysql,导入图书管理系统的数据

# 1.mysql数据的导出,与导入
这个命令是在linux/windows中敲的
mysqldump -u root -p --all-databases >  book_manage.dump  

# 2.上传这个数据文件到linux数据库中

# 3.在linux的mysql,导入这个数据文件(需要在dump文件中指定数据库: use szday58)
mysql -u root -p < /opt/book_manage.dump
# 或者进入mysql中 进入数据库,使用命令:
source /opt/book_manage.dump

5、进入虚拟环境并运行项目

测试使用linux的python解释器去运行项目 切换到 项目中运行(注意要解决解释器的模块问题,才能正常运转项目)

# 用127.0.0.1不行,只能本机访问,要想让所有人都能访问,必须要用0.0.0.0
python3 manage.py runserver 0.0.0.0:8000

然后就可以通过IP加端口进行访问了,如果这个IP是我的云服务器IP,那我们去面试的时候,就可以拿着这个项目给面试官在电脑上展示了,好有逼格啊~

彩蛋:现在有一个问题,我们通过10.0.0.7:8000才能访问项目,但是公网上的网站不是这样的啊比如路飞学城,是通过域名进行访问的,我们还可以解析出路飞学城的网站服务器IP,通过地址也可以访问,但是后面没有加端口, 因为默认是用的80端口

虽然我们可以直接将端口改为80就没问题了, 那么问题来了: 

1  django运行web界面默认用的是wsgiref单机模块,性能会比较低, 需要怎么解决?

uwsgi + django + nginx

2  我们要想再跑一个项目对外提供服务,该怎么办呢? 服务器只有一个80端口, 如何解决

nginx + uwsgi + django + supervisor


-完成nginx的安装配置,了解nginx.conf如何配置

-完成uWSGI命令学习,使用uWSGI启动knight项目,支持多进程

-完成nginx处理图书管理项目的静态文件

-最终效果

访问nginx的80端口,即可找到图书管理页面,且保证静态文件页面正常

Related Posts