以下操作在 Ubuntu 18.04.3 LTS 上部署成功

0.编译并打包

通过以下脚本可以在Ubuntu上拉取最新代码编译并打包
脚本会安装golang和nodejs,如果是其他系统不支持ppa或apt需要手动安装
脚本运行完成之后,前后端以及评测端都被打包到package目录,之后的所有的操作都是默认package为工作目录

  1. #!/bin/bash
  2. set -ex
  3. installGo(){
  4. local which_go=$(which go)
  5. if [ ${#which_go} == 0 ]
  6. then
  7. echo "系统未安装Golang,开始安装"
  8. sudo add-apt-repository ppa:longsleep/golang-backports
  9. sudo apt-get update
  10. sudo apt-get install golang-go
  11. else
  12. echo "系统已经安装Golang"
  13. fi
  14. }
  15. installNodejs(){
  16. local which_nodejs=$(which nodejs)
  17. if [ ${#which_nodejs} == 0 ]
  18. then
  19. echo "系统未安装nodejs,开始安装nodejs 12.x"
  20. curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
  21. sudo apt-get install -y nodejs
  22. else
  23. echo "系统已经安装nodejs"
  24. fi
  25. }
  26. cleanAll(){
  27. rm -rf $(pwd)/source
  28. rm -rf $(pwd)/package
  29. }
  30. initDir(){
  31. package_dir=$(pwd)/package
  32. source_dir=$(pwd)/source
  33. go_path=${source_dir}/go
  34. server_package_dir=${package_dir}/server
  35. mkdir -p ${package_dir}
  36. mkdir -p ${source_dir}
  37. mkdir -p ${server_package_dir}
  38. }
  39. buildServer(){
  40. GOPATH=${go_path} go get -u github.com/maggch97/gomoku_server
  41. cp ${go_path}/bin/gomoku_server ${server_package_dir}
  42. cp -r ${go_path}/src/github.com/maggch97/gomoku_server/json ${server_package_dir}/json
  43. }
  44. buildWeb(){
  45. pushd ${source_dir}
  46. git clone https://github.com/maggch97/gomoku_web.git
  47. pushd gomoku_web
  48. npm install --save
  49. npm run build
  50. cp -r ./build ${package_dir}/web
  51. popd
  52. popd
  53. }
  54. getJudge(){
  55. pushd ${source_dir}
  56. git clone https://github.com/maggch97/jd4.git -b gomoku_judge_master
  57. cp -r ./jd4 ${package_dir}/judge
  58. rm -rf ${package_dir}/judge/.git*
  59. popd
  60. }
  61. installGo
  62. installNodejs
  63. cleanAll
  64. initDir
  65. buildServer
  66. buildWeb
  67. getJudge

1.安装依赖程序

  1. sudo apt install python3 python3-pip virtualenv mysql-server nginx

2.创建数据库

  1. # ubuntu18.04通过apt安装的mysql 5.7会生成初始用户和密码
  2. # 查看初始用户密码
  3. sudo cat /etc/mysql/debian.cnf
  4. # user = debian-sys-maint
  5. # password = gNN6qO4kdP6J3KN5
  6. mysql -u ${user_name} -p
  7. # 创建gomoku数据库
  8. mysql> CREATE DATABASE gomoku;

3.配置前端与nginx

移动前端文件到网页目录
注意这可能会覆盖/var/www/html/中的网页文件,确认/var/www/html/中没有有用数据再操作

  1. sudo cp -r ./web/* /var/www/html/

修改nginx配置文件

  1. 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使配置生效

  1. sudo systemctl restart nginx

此时访问 http://${host}/game/1/contest 应该能够看到前端页面而不是404

4.配置后端

修改mysql配置项

  1. cd ./server/json/config
  2. vim mysql_config.json

Host:mysql服务ip,如果是本机就是127.0.0.1
Port:mysql端口,默认3306
User和Password是登录mysql的用户名及密码,默认能在/etc/mysql/debian.cnf中查看

修改评测配置项

  1. cd ./server/json/config
  2. vim judge_config.json

AccessToken是评测端和后端的身份验证
!不要使用默认的AccessToken,存在非常大的安全隐患

添加权限用户

  1. cd ./server/json/permission
  2. vim create_contest_permission.json

[“user_name1”, “user_name2”, “user_name3”],是一个账号数组,只有在这个文件里的账号能够创建比赛

游戏设置

  1. cd ./server/json/config

每个游戏有一个配置文件
五子棋是gomoku_game_config.json
井字棋是tic_tac_toe_game_config.json

配置项解释
RankDurationSec:天梯赛周期,单位秒。建议按评测服务器负载能力以及天梯赛报名人数设置一个合适的周期,间隔过短会导致评测任务不断积压
JudgeTimeoutSec:评测超时,单位秒。评测任务在提交给评测服务后,如果超时没有返回结果,会重新进行评测。如果设置过短小于正常的评测时间,会导致评测任务不断重新评测,产生死循环。
DefaultTimeLimitNs:默认的对战程序单次运行时间限制
DefaultMemoryLimitBytes:默认的对战程序运行内存限制。不宜过小,小于10MB会导致python程序的评测超时。

运行后端

  1. cd ./server
  2. chmox +x gomoku_server
  3. ./gomoku_server

此时访问 http://${host}/game/1/contest ,网站应该能正常使用,能够正常注册登录

5.配置评测端

第一次运行的准备工作

  1. cd ./judge
  2. pip3 install -r requirements.txt
  3. mkdir -p ~/.config/jd4
  4. cp examples/config.yaml ~/.config/jd4/
  5. ln -sr examples/langs.yaml ~/.config/jd4/
  6. python3 setup.py build_ext --inplace
  7. chmod +x ./start_judge.sh
  8. chmod +x ./stop_all_judge.sh
  9. chmod +x ./check_judge.sh

启动评测服务

  1. ./start_judge.sh ${并行评测数量} ${前端地址} ${AccessToken}
  2. # 例如./start_judge.sh 3 http://127.0.0.1/ dsafasdfasdf
  3. # 就是同时运行三个评测任务

查看评测服务

  1. ./check_judge.sh

停止评测服务

  1. ./stop_all_judge.sh