以下操作在 Ubuntu 18.04.3 LTS 上部署成功
0.编译并打包
通过以下脚本可以在Ubuntu上拉取最新代码编译并打包
脚本会安装golang和nodejs,如果是其他系统不支持ppa或apt需要手动安装
脚本运行完成之后,前后端以及评测端都被打包到package目录,之后的所有的操作都是默认package为工作目录
#!/bin/bash
set -ex
installGo(){
local which_go=$(which go)
if [ ${#which_go} == 0 ]
then
echo "系统未安装Golang,开始安装"
sudo add-apt-repository ppa:longsleep/golang-backports
sudo apt-get update
sudo apt-get install golang-go
else
echo "系统已经安装Golang"
fi
}
installNodejs(){
local which_nodejs=$(which nodejs)
if [ ${#which_nodejs} == 0 ]
then
echo "系统未安装nodejs,开始安装nodejs 12.x"
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt-get install -y nodejs
else
echo "系统已经安装nodejs"
fi
}
cleanAll(){
rm -rf $(pwd)/source
rm -rf $(pwd)/package
}
initDir(){
package_dir=$(pwd)/package
source_dir=$(pwd)/source
go_path=${source_dir}/go
server_package_dir=${package_dir}/server
mkdir -p ${package_dir}
mkdir -p ${source_dir}
mkdir -p ${server_package_dir}
}
buildServer(){
GOPATH=${go_path} go get -u github.com/maggch97/gomoku_server
cp ${go_path}/bin/gomoku_server ${server_package_dir}
cp -r ${go_path}/src/github.com/maggch97/gomoku_server/json ${server_package_dir}/json
}
buildWeb(){
pushd ${source_dir}
git clone https://github.com/maggch97/gomoku_web.git
pushd gomoku_web
npm install --save
npm run build
cp -r ./build ${package_dir}/web
popd
popd
}
getJudge(){
pushd ${source_dir}
git clone https://github.com/maggch97/jd4.git -b gomoku_judge_master
cp -r ./jd4 ${package_dir}/judge
rm -rf ${package_dir}/judge/.git*
popd
}
installGo
installNodejs
cleanAll
initDir
buildServer
buildWeb
getJudge
1.安装依赖程序
sudo apt install python3 python3-pip virtualenv mysql-server nginx
2.创建数据库
# ubuntu18.04通过apt安装的mysql 5.7会生成初始用户和密码
# 查看初始用户密码
sudo cat /etc/mysql/debian.cnf
# user = debian-sys-maint
# password = gNN6qO4kdP6J3KN5
mysql -u ${user_name} -p
# 创建gomoku数据库
mysql> CREATE DATABASE gomoku;
3.配置前端与nginx
移动前端文件到网页目录
注意这可能会覆盖/var/www/html/中的网页文件,确认/var/www/html/中没有有用数据再操作
sudo cp -r ./web/* /var/www/html/
修改nginx配置文件
sudo vim /etc/nginx/sites-available/default
将
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
修改为
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.html;
}
location /api/ {
proxy_pass http://127.0.0.1:49900/;
proxy_connect_timeout 6000;
proxy_read_timeout 6000;
}
http://127.0.0.1:49900/是后端的地址,转发到 前端地址/api/
修改完后重启nginx使配置生效
sudo systemctl restart nginx
此时访问 http://${host}/game/1/contest 应该能够看到前端页面而不是404
4.配置后端
修改mysql配置项
cd ./server/json/config
vim mysql_config.json
Host:mysql服务ip,如果是本机就是127.0.0.1
Port:mysql端口,默认3306
User和Password是登录mysql的用户名及密码,默认能在/etc/mysql/debian.cnf中查看
修改评测配置项
cd ./server/json/config
vim judge_config.json
AccessToken是评测端和后端的身份验证
!不要使用默认的AccessToken,存在非常大的安全隐患
添加权限用户
cd ./server/json/permission
vim create_contest_permission.json
[“user_name1”, “user_name2”, “user_name3”],是一个账号数组,只有在这个文件里的账号能够创建比赛
游戏设置
cd ./server/json/config
每个游戏有一个配置文件
五子棋是gomoku_game_config.json
井字棋是tic_tac_toe_game_config.json
配置项解释
RankDurationSec:天梯赛周期,单位秒。建议按评测服务器负载能力以及天梯赛报名人数设置一个合适的周期,间隔过短会导致评测任务不断积压
JudgeTimeoutSec:评测超时,单位秒。评测任务在提交给评测服务后,如果超时没有返回结果,会重新进行评测。如果设置过短小于正常的评测时间,会导致评测任务不断重新评测,产生死循环。
DefaultTimeLimitNs:默认的对战程序单次运行时间限制
DefaultMemoryLimitBytes:默认的对战程序运行内存限制。不宜过小,小于10MB会导致python程序的评测超时。
运行后端
cd ./server
chmox +x gomoku_server
./gomoku_server
此时访问 http://${host}/game/1/contest ,网站应该能正常使用,能够正常注册登录
5.配置评测端
第一次运行的准备工作
cd ./judge
pip3 install -r requirements.txt
mkdir -p ~/.config/jd4
cp examples/config.yaml ~/.config/jd4/
ln -sr examples/langs.yaml ~/.config/jd4/
python3 setup.py build_ext --inplace
chmod +x ./start_judge.sh
chmod +x ./stop_all_judge.sh
chmod +x ./check_judge.sh
启动评测服务
./start_judge.sh ${并行评测数量} ${前端地址} ${AccessToken}
# 例如./start_judge.sh 3 http://127.0.0.1/ dsafasdfasdf
# 就是同时运行三个评测任务
查看评测服务
./check_judge.sh
停止评测服务
./stop_all_judge.sh