基于linux安装Archery数据库SQL审核工具

已邀请:

1、创建项目路径

mkdir -p /data/wwwroot/Archery/

2、下载最新代码

git clone https://github.com/hhyo/Archery.git

/uploads/answer/20201019/fdb89311c2c444883767961a1590c0d7.png


3、安装系统依赖组件

yum install wget gcc make zlib-devel openssl openssl-devel


4、安装Python和virtualenv


4.1编译安装(如已安装其他版本,推荐走下面yum安装)

# 安装依赖
yum install wget gcc make zlib-devel openssl openssl-devel
wget "https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz"
tar -xvJf Python-3.6.5.tar.xz
# 编译
cd Python-3.6.5
./configure prefix=/usr/local/python3
make && make install
ln -fs /usr/local/python3/bin/python3 /usr/bin/python3
ln -fs /usr/local/python3/bin/pip3 /usr/bin/pip3
# virtualenv
pip3 install virtualenv -i https://mirrors.ustc.edu.cn/pypi/web/simple/
ln -fs /usr/local/python3/bin/virtualenv /usr/bin/virtualenv

4.2yum安装

# 安装EPEL源
yum install epel-release
# 安装python36、pip36
yum install python36 python36-pip
# virtualenv
pip3.6 install virtualenv -i https://mirrors.ustc.edu.cn/pypi/web/simple/


5、安装Archery

准备虚拟环境(推荐到项目目录,处理,这样虚拟环境配置在项目目录,便于整理)

# 编译安装python的使用
virtualenv venv4archery --python=python3
# yum安装的使用
virtualenv venv4archery --python=python3.6
# 切换python运行环境到虚拟环境
source venv4archery/bin/activate

6、到软件目录进行部署

# 安装系统依赖
yum -y install gcc gcc-c++ python-devel mysql-devel openldap-devel unixODBC-devel gettext

# 安装依赖库
cd 工程目录
pip3 install -r requirements.txt -i https://mirrors.ustc.edu.cn/pypi/web/simple/

# 如果使用yum安装的python3,安装mysqlclient可能提示提示缺少Python.h,可安装
yum -y install python36-devel

/uploads/answer/20201019/7cd54e4b01d5764e282b7e041ef2ef7b.png


7、修改配置

vi archery/settings.py

安全修改

请务必修改配置文件中的SECRET_KEY信息,该key用于敏感信息加密

基础配置

# 修改SECRET_KEY
# 关闭debug模式
DEBUG = False
# 设置ALLOWED_HOSTS,建议限制内网访问
ALLOWED_HOSTS = [
'.example.com', # Allow domain and subdomains
'.example.com.', # Also allow FQDN and subdomains
]
# 请求大小限制,如果提交SQL语句过大可以修改该值
DATA_UPLOAD_MAX_MEMORY_SIZE = 15728640
# 密码校验,用户注册和添加密码校验规则
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
'OPTIONS': {
'min_length': 9,
}
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]

MySQL配置

建议MySQL版本5.6以上

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'archery', # 数据库名称
'USER': 'root', # 数据库用户
'PASSWORD': '', # 数据库密码
'HOST': '127.0.0.1', # 数据库HOST,如果是docker启动并且关联,可以使用容器名连接
'PORT': '3306', # 数据库端口
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'", # SQL_MODE,为了兼容select * group by,可以按需调整
'charset': 'utf8mb4'
},
'TEST': {
'NAME': 'test_archery',
'CHARSET': 'utf8mb4',
},
}
}

Django-Q配置

默认配置即可,也可参考django-q文档修改

Q_CLUSTER = {
'name': 'archery',
'workers': 4,
'recycle': 500,
'timeout': 60,
'compress': True,
'cpu_affinity': 1,
'save_limit': 0,
'queue_limit': 50,
'label': 'Django Q',
'django_redis': 'default',
'sync': False # 本地调试可以修改为True,使用同步模式

}

缓存配置

缓存使用redis,可参考 django_redis 文档 http://django-redis-chs.readthedocs.io/zh_CN/latest/

CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379/0", # redis://host:port/db
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"PASSWORD": ""

}
}
}


8、启动准备

# 数据库初始化
python3 manage.py makemigrations sql
python3 manage.py migrate

# 数据初始化
python3 manage.py dbshell
http://nccloud.yytimes.com/uploads/answer/20201019/93ddc4b927d3408a333ef67d12748863.png

/uploads/answer/20201019/58554b4ab8b5049a3237252e40d90f1f.png


/uploads/answer/20201019/100b47e17e3ca49e0e8c864881852135.png



9、Gunicorn+Nginx启动

# nginx配置示例  
server{
listen 9123; # 监听的端口
server_name archery;
client_max_body_size 20M; # 处理Request Entity Too Large
proxy_read_timeout 600s; # 超时时间与Gunicorn超时时间设置一致,主要用于在线查询

location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host:9123; # 解决重定向404的问题,和listen端口保持一致,如果是docker则和宿主机映射端口保持一致
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}

location /static {
alias /opt/archery/static; # 此处指向settings.py配置项STATIC_ROOT目录的绝对路径,用于nginx收集静态资源
}

error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# 启动
source /opt/venv4archery/bin/activate
bash startup.sh

访问

http://127.0.0.1:9123/

启动后配置

在启动后 Archery 有一些配置(如Inception , 资源组, 权限组等)需要按需配置, 请详细阅读 配置项说明 , 按照自己的需要进行配置

要回复问题请先登录注册