前提环境需求
1、创建Django虚拟运行环境
1.1、安装python3.6
yum -y install epel-release
yum repolist
yum install python36 -y
1.2、安装虚拟环境virtualenv:pip3 install virtualenvwrapper -i https://pypi.mirrors.ustc.edu.cn/simple/
1.3、设置环境变量:
vi ~/.bashrc 末尾添加:
export WORKON_HOME=~/Envs
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages'
export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
source /usr/local/bin/virtualenvwrapper.sh
1.4、报错:没有这个参数 –no-site-packages ,是 virtualenv 版本问题。升级:pip3 install --upgrade virtualenv==16.7.9 -i https://pypi.mirrors.ustc.edu.cn/simple/
1.5、创建虚拟环境命令:mkvirtualenv -p ython3 project_name
2、安装项目运行插件
创建插件文件 vi requirements.txt
captcha==0.3
certifi==2020.12.5
chardet==4.0.0
defusedxml==0.7.1
diff-match-patch==20200713
Django==2.1.5
django-crispy-forms==1.11.2
django-formtools==2.2
django-import-export==2.5.0
et-xmlfile==1.0.1
future==0.18.2
httplib2==0.19.1
idna==2.10
MarkupPy==1.14
mysqlclient==2.0.1
odfpy==1.4.1
openpyxl==3.0.7
Pillow==8.1.2
pyparsing==2.4.7
pytz==2021.1
PyYAML==5.4.1
redis==3.5.3
requests==2.25.1
reversion==0.2
six==1.15.0
tablib==3.0.0
toml==0.10.2
urllib3==1.26.4
xlrd==2.0.1
xlwt==1.3.0
进入上步骤创建的虚拟环境:workon project_name
然后运行安装插件命令:pip install -r requirements.txt -i https://pypi.mirrors.ustc.edu.cn/simple/
3、数据库安装
3.1、MySQL数据库安装
$ yum -y install mariadb mariadb-devel mariadb-server MariaDB-shared # centos7下叫mariadb, 用法与mysql一致
$ systemctl enable mariadb
$ systemctl start mariadb
密码与配置
[gyy@localhost ~]# mysql -u root
mysql> set password for 'root'@'localhost' =password('设置的密码');
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
mysql> exit;
创建数据库支持中文
create database PM default charset=utf8;
#设置数据库为中文
3.2、安装docker
centos安装docker
# 卸载旧版本
yum remove docker docker-common docker-selinux docker-engine
# 安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的
yum install -y yum-utils device-mapper-persistent-data lvm2
# 设置yum源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# 可以查看所有仓库中所有docker版本,并选择特定版本安装
yum list docker-ce --showduplicates | sort -r
# 安装docker
yum install docker-ce-17.12.0.ce
# 启动并加入开机启动
systemctl start docker
systemctl enable docker
3.3、安装redis 并配置数据持久化
docker run -p 6379:6379 --name redis --restart=always -v /mydata/redis/data:/data -v /mydata/redis/conf/redis.conf:/etc/redis/redis.conf -d redis redis-server /etc/redis/redis.conf --appendonly yes --requirepass zzgy163
部署服务
1、部署管理系统
管理系统网盘链接:https://pan.baidu.com/s/1IUnGyZmu1mVzWtmCiGXvww
提取码:egfh
workon project_name
# 先进入虚拟环境
cd /work
tar -zxvf projectmanage.tar.gz
#解压文件夹
cd /ProjectManage
# 进入文件目录
python manage.py makemigrations
python manage.py migrate
# 数据库映射
python manage.py createsuperuser
# 创建后台超级管理员 跟着提示输入即可
python manage.py runserver 0.0.0.0:8000
# 试跑一下
firewall-cmd --zone=public --add-port=8000/tcp --permanent
# 防火墙开启端口号
2、安装第三方插件用于在线预览
先安装Java:
jdk网盘链接:https://pan.baidu.com/s/1RW8vYBwqpMG3PqZhGpQfwA
提取码:n52h
目录:/soft/
下载jdk: jdk1.8.0_144.tar.gz
解压:tar zxvf jdk1.8.0_144.tar.gz
配置文件vi /etc/profile,在最后添加:
export JAVA_HOME=/soft/jdk1.8.0_144
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
刷新配置文件使其生效:source /etc/profile
安装kkFileView:
kkFileView网盘链接:https://pan.baidu.com/s/1G7Xh52bINz8LMcRO2IhnBg
提取码:ynk4
cd /work
tar -zxvf kkFileView-2.2.1.tar.gz
# 解压文件夹
cd kkFileView-2.2.1/bin
./startup.sh
# 运行脚本即可,会自动下载依赖,端口号为8012
firewall-cmd --zone=public --add-port=8012/tcp --permanent
# 防火墙开启端口号
http://ip:8012 # 测试一下
# 主要测试docx、pptx等压缩文档模式,如果出现中文乱码,
# 1、修改config文件中office.preview.type
# office类型文档(word ppt)样式,默认为图片(image),可配置为pdf(预览时也有按钮切换)
office.preview.type = ${KK_OFFICE_PREVIEW_TYPE:pdf}
# 2、尝试以下修改
(1)修改linux系统默认语言为:LANG=zh_CN.GB2312
(2)修改kkfileview配置文件:spring.http.encoding.charset = gbk
# 因为该插件读取文件是用的服务器操作系统默认编码
# 3、Linux系统缺少中文包
官网常见问题
linux增加中文包
水印透明度设置为:0.1
3、主控代码加密
cd /work
vi setup.py
添加以下内容
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/9/10 14:38
# @Author :
# @FILE : setup.py
# @DESC :
import sys, os, shutil, time
from distutils.core import setup
from Cython.Build import cythonize
starttime = time.time()
setupfile= os.path.join(os.path.abspath('.'), __file__)
def getpy(basepath=os.path.abspath('.'), parentpath='', name='', build_dir="build",
excepts=(), copyOther=False, delC=False):
"""
获取py文件的路径
:param basepath: 根路径
:param parentpath: 父路径
:param name: 文件/夹
:param excepts: 排除文件
:param copy: 是否copy其他文件
:return: py文件的迭代器
"""
fullpath = os.path.join(basepath, parentpath, name)
for fname in os.listdir(fullpath):
ffile = os.path.join(fullpath, fname)
if os.path.isdir(ffile) and ffile != os.path.join(basepath, build_dir) and not fname.startswith('.'):
for f in getpy(basepath, os.path.join(parentpath, name), fname, build_dir, excepts, copyOther, delC):
yield f
elif os.path.isfile(ffile):
# print("\t", basepath, parentpath, name, ffile)
ext = os.path.splitext(fname)[1]
if ext == ".c":
if delC and os.stat(ffile).st_mtime > starttime:
os.remove(ffile)
elif ffile not in excepts and ext not in('.pyc', '.pyx'):
# print("\t\t", basepath, parentpath, name, ffile)
if ext in('.py', '.pyx') and not fname.startswith('__'):
yield os.path.join(parentpath, name, fname)
elif copyOther:
dstdir = os.path.join(basepath, build_dir, parentpath, name)
if not os.path.isdir(dstdir): os.makedirs(dstdir)
shutil.copyfile(ffile, os.path.join(dstdir, fname))
else:
pass
if __name__ == "__main__":
currdir = os.path.abspath('.')
parentpath = sys.argv[1] if len(sys.argv)>1 else "."
currdir, parentpath = os.path.split(currdir if parentpath == "." else os.path.abspath(parentpath))
build_dir = os.path.join(parentpath, "build")
build_tmp_dir = os.path.join(build_dir, "temp")
print("start:", currdir, parentpath, build_dir)
os.chdir(currdir)
try:
#获取py列表
module_list = list(getpy(basepath=currdir,parentpath=parentpath, build_dir=build_dir, excepts=(setupfile)))
print(module_list)
setup(ext_modules = cythonize(module_list),script_args=["build_ext", "-b", build_dir, "-t", build_tmp_dir])
module_list = list(getpy(basepath=currdir, parentpath=parentpath, build_dir=build_dir, excepts=(setupfile), copyOther=True))
except Exception as ex:
print("error! ", ex)
finally:
print("cleaning...")
module_list = list(getpy(basepath=currdir, parentpath=parentpath, build_dir=build_dir, excepts=(setupfile), delC=True))
if os.path.exists(build_tmp_dir): shutil.rmtree(build_tmp_dir)
print("complate! time:", time.time()-starttime, 's')
然后加密主控代码:python3 setup.py ProjectManage/file
运行结束后,会生成一个build文件夹,然后将build文件夹下的file文件夹替换原来的即可。
4、后台运行并开机自启动
4.1、/etc/rc.d/rc.local 方式
[root@localhost ~]# vi /etc/rc.d/rc.local
末尾添加:/root/Envs/worker/bin/python3 /work/ProjectManage/manage.py runserver 0.0.0.0:8000
[root@localhost ~]# chmod +x /etc/rc.d/rc.local
赋可执行权限,否则开机自启动失败
[root@localhost ~]# reboot #重启即可
4.2、/etc/systemd/system/projectmanage.service 方式
[root@localhost ~]# vi /etc/systemd/system/projectmanage.service
# 新建service文件,添加以下配置
[root@localhost ~]# chmod +x /etc/systemd/system/projectmanage.service
# 给文件赋可执行权限
[root@localhost ~]# systemctl daemon-reload
# 更新配置
[root@localhost ~]# systemctl start projectmanage
# 启动项目
[root@localhost ~]# systemctl ststus projectmanage
# 查看状态
[root@localhost ~]# systemctl stop projectmanage
# 停止项目
[root@localhost ~]# systemctl restart projectmanage
# 重启
[root@localhost ~]# systemctl enable projectmanage
设置开机自启动
projectmanage.service内容:
[Unit]
Description = projectmanage
After = network.target
[Service]
ExecStart = /root/Envs/worker/bin/python3 /work/ProjectManage/manage.py runserver 0.0.0.0:8000
WorkingDirectory = /work/ProjectManage
Restart = always
User = root
[Install]
WantedBy=multi-user.target
5、测试
前台:http:ip:8000/index
后台:http:ip:8000/xadmin
后台密码为 部署服务下第一步部署管理系统中设置。