Install HustOJ for Ubuntu Server 20.04

Posted by herbally on Fri, 04 Mar 2022 14:28:06 +0100

hustoj itself provides the installation script. The following manual installation is mainly to be familiar with its composition.

sed -i 's/tencentyun/aliyun/g' /etc/apt/sources.list

If it is Tencent cloud, change it to Alibaba cloud. We don't need this operation.

apt-get update
apt-get install -y subversion
/usr/sbin/useradd -m -u 1536 judge
cd /home/judge/ || exit

To install subversion, I have written down the code git clone, which should not be required. Add the account judge, add the home directory for it, and specify the user ID as 1536. This step requires. Then enter the home directory. We execute sudo useradd -m -u 1536 judge, cd /home/judge

#using tgz src files
wget -O hustoj.tar.gz
tar xzf hustoj.tar.gz
svn up src
#svn co  src

Get the source code, decompress and synchronize with svn. We don't need these anymore.

for pkg in net-tools make flex g++ clang libmysqlclient-dev libmysql++-dev php-fpm nginx mysql-server php-mysql  php-common php-gd php-zip fp-compiler openjdk-11-jdk mono-devel php-mbstring php-xml php-curl php-intl php-xmlrpc php-soap
        while ! apt-get install -y "$pkg"
                echo "Network fail, retry... you might want to change another apt source for install"

Here are some software packages installed. We use nginx, php8 0 (ppa required), mariadb combination:

sudo apt-get install nginx, sudo apt-get install mariadb-server,sudo apt-get install software-properties-common,sudo add-apt-repository ppa:ondrej/php,sudo apt-get install php8.0-fpm,

sudo apt-get install php8.0-mysql php8.0-gd php8.0-zip php8.0-mbstring php8.0-xml php8.0-curl php8.0-intl php8.0-xmlrpc php8.0-soap

Install the compiler tool , sudo apt get install , make flex , g + + clang. net tools for ifconfig support are not required. pascal, java net compilation or execution environment. You can download the connector and documentation of mariadb C language , please refer to the website , it's complicated to replace this part with mariadb. Let's ignore it first.

USER=$(grep user /etc/mysql/debian.cnf|head -1|awk  '{print $3}')
PASSWORD=$(grep password /etc/mysql/debian.cnf|head -1|awk  '{print $3}')
CPU=$(grep "cpu cores" /proc/cpuinfo |head -1|awk '{print $4}')

This is to extract some information from / etc / MySQL / Debian CNF extracts the database user name (root in the default configuration file) and password (empty in the default configuration file because there is no secure mariadb), and obtains the number of CPU cores from the process information file / etc/cpuinfo, such as 2. We can know this information without writing or modifying.

mkdir etc data log backup

cp src/install/java0.policy  /home/judge/etc
cp src/install/judge.conf  /home/judge/etc
chmod +x src/install/ans2out

if grep "OJ_SHM_RUN=0" etc/judge.conf ; then
        mkdir run0 run1 run2 run3
        chown judge run0 run1 run2 run3

sed -i "s/OJ_USER_NAME=root/OJ_USER_NAME=$USER/g" etc/judge.conf
sed -i "s/OJ_PASSWORD=root/OJ_PASSWORD=$PASSWORD/g" etc/judge.conf
sed -i "s/OJ_COMPILE_CHROOT=1/OJ_COMPILE_CHROOT=0/g" etc/judge.conf
sed -i "s/OJ_RUNNING=1/OJ_RUNNING=$CPU/g" etc/judge.conf

chmod 700 backup
chmod 700 etc/judge.conf

Create four directories in the judge user's home directory: sudo mkdir etc data log backup in / home / judge, and copy the two files from the source code: sudo CP / home / zime / hustoj / trunk / install / java0 policy  /home/judge/etc,sudo cp /home/zime/hustoj/trunk/install/judge.conf / home / judge / etc, add execution permission to the ans2out file in the source code. The script is internally cd to the directory specified by the command line parameter $1, and then execute for I in * ans;   do  mv  $i  `basename -s  . ans  $i`.out; done; For all under the directory Rename the ANS file to Out file. We will operate it manually later. We don't need this script. Configuration file etc / judge If OJ is set in conf_ SHM_ Run = 0 (whether to use / dev/shm as the working directory, the default is 0), create four directories run0, run1, run2 and run3 in the main directory, and change the owners of these four directories to judge accounts: sudo mkdir # run0 # run1 # run2 # run3, sudo chown # judge # run0 # run1 # run2 # run3.

According to the previous information, modify the configuration file etc / judge Four parameters in conf: sudo Emacs etc / judge conf,OJ_USER_NAME = database user name, OJ_PASSWORD = database user password, OJ_COMPILE_CHROOT=0 do not use chroot to prevent compile time attacks and OJ_ Running = number of CPU cores

The backup subdirectory and the configuration file, etc / judge The permission of conf is changed to 700: sudo chmod 700 # backup, sudo chmod 700 # etc / judge conf

sed -i "s/DB_USER[[:space:]]*=[[:space:]]*\"root\"/DB_USER=\"$USER\"/g" src/web/include/
sed -i "s/DB_PASS[[:space:]]*=[[:space:]]*\"root\"/DB_PASS=\"$PASSWORD\"/g" src/web/include/
chmod 700 src/web/include/
chown -R www-data src/web/

chown -R root:root src/web/.svn
chmod 750 -R src/web/.svn

This is to modify the configuration file of the website sudo emacs /home/zime/hustoj/trunk/web/include/, modify DB_USER row and DB_PASS line. Change the permission of the website configuration file to 700, sudo chmod 700 / home/zime/hustoj/trunk/web/include/, change the owner of the root directory of the website to the nginx process account www data, sudo chown - R www data / home/zime/hustoj/trunk/web /. We don't need svn, and there is no source code in git svn directory, so don't use the last two sentences.

chown www-data:judge src/web/upload
chown www-data:judge data
chmod 711 -R data
if grep "client_max_body_size" /etc/nginx/nginx.conf ; then
        echo "client_max_body_size already added" ;
        sed -i "s:include /etc/nginx/mime.types;:client_max_body_size    80m;\n\tinclude /etc/nginx/mime.types;:g" /etc/nginx/nginx.conf

Modify the owner of upload directory and data directory as nginx process account www data, sudo chown , www data: judge / home/zime/hustoj/trunk/web/upload, sudo , chown , www data: judge / home/judge/data. The permission to modify data directory is 711, sudo , chmod , 711 - R / home/judge/data. sudo emacs  /etc/nginx/nginx.conf, modify the nginx main configuration file in include / etc / nginx / mime type; Add a line of client before_ max_ body_ size  80m; That is, the http request body can be up to 80MB

mysql -h localhost -u"$USER" -p"$PASSWORD" < src/install/db.sql
echo "insert into jol.privilege values('admin','administrator','true','N');"|mysql -h localhost -u"$USER" -p"$PASSWORD"

Create database mysql - H localhost - uroot - P < / home / zime / hustoj / trunk / install / DB SQL. At this time, you will find that you do not have permission to access, because the default reason is that Ubuntu is packaged in Mariadb root@localhost It's UNIX with plug-ins_ Socket verification, we can use UNIX for all_ The socket authentication method is changed to the traditional password method. Of course, it is more appropriate to give hustoj a special database account.

Sudo mysql, select user, host, plugin from mysql user; Verify whether it is a problem of verification method, update mysql user SET plugin = '' WHERE plugin = 'unix_ socket';  FLUSH PRIVILEGES; Verify that the problem is solved.   source  /home/zime/hustoj/trunk/install/db.sql} found 42000 syntax errors in lines 253, 256, 257, 259 and 260 or HY000 system variable not found.  sudo cp  /home/zime/hustoj/trunk/install/db.sql   /home/zime/hustoj/trunk/install/db_old.sql, sudo emacs   /home/zime/hustoj/trunk/install/db.sql, find the corresponding location, and find that there is an error in the last place to create the stored procedure. Look carefully, the delimiter is set in front of the stored procedure; The procedure function has;, Therefore, we put the end before the stored procedure// Start changing this line

end  //

    DECLARE privileged_count INT DEFAULT 0;
    SET privileged_count=(SELECT COUNT(1) FROM `privilege`);
    IF privileged_count=0 THEN
        INSERT INTO privilege values(user_name, 'administrator', 'true', 'N');
    end if;
end  //

delimiter ;

Use source to build the database on the mysql command line, or use input redirection to create the database as before. Use mysql -hlocalhost - uroot - p - e "USE jol; call" DEFAULT_ADMINISTRATOR('admin '); " Or USE jol in mysql command line;   CALL  DEFAULT_ADMINISTRATOR('admin'); Create the default administrator account admin (since the author has created a stored procedure to create subsequent administrators, we can call the stored procedure directly here, so the consistency is better)

if grep "added by hustoj" /etc/nginx/sites-enabled/default ; then
        echo "default site modified!"
        echo "modify the default site"
        sed -i "s#root /var/www/html;#root /home/judge/src/web;#g" /etc/nginx/sites-enabled/default
        sed -i "s:index index.html:index index.php:g" /etc/nginx/sites-enabled/default
        sed -i "s:#location ~ \\\.php\\$:location ~ \\\.php\\$:g" /etc/nginx/sites-enabled/default
        sed -i "s:#\tinclude snippets:\tinclude snippets:g" /etc/nginx/sites-enabled/default
        sed -i "s|#\tfastcgi_pass unix|\tfastcgi_pass unix|g" /etc/nginx/sites-enabled/default
        sed -i "s:}#added by hustoj::g" /etc/nginx/sites-enabled/default
        sed -i "s:php7.0:php7.4:g" /etc/nginx/sites-enabled/default
        sed -i "s|# deny access to .htaccess files|}#added by hustoj\n\n\n\t# deny access to .htaccess files|g" /etc/nginx/sites-enabled/default
/etc/init.d/nginx restart

This part is the website configuration. The author directly implements it by replacing and modifying the default configuration file. For the time being, we'd better write a configuration file and put it under sites available, and then create symbols under sites enabled to the new configuration file: sudo Emacs / etc / nginx / sites enabled / default

root  /home/zime/hustoj/trunk/web;    index  index.php; Remove location ~ \ At the same time, the front of PHP {include #} is also removed_ The pass line changes 7.4 to 8.0

sed -i "s/post_max_size = 8M/post_max_size = 80M/g" /etc/php/7.4/fpm/php.ini
sed -i "s/upload_max_filesize = 2M/upload_max_filesize = 80M/g" /etc/php/7.4/fpm/php.ini
WWW_CONF=$(find /etc/php -name www.conf)
sed -i 's/;request_terminate_timeout = 0/request_terminate_timeout = 128/g' "$WWW_CONF"
sed -i 's/pm.max_children = 5/pm.max_children = 200/g' "$WWW_CONF"

sudo emacs /etc/php/8.0/fpm/php.ini, and modify some configuration of PHP_ max_ size=80M, upload_ max_ Filesize = 80m, find /etc/php -name www.conf find the location of www.conf file, and then edit sudo Emacs / etc / PHP / 8.0 / FPM / pool d/www.conf, request_terminate_timeout=128. kill a single request after 128 seconds, PM max_ Children = 200, and the number of concurrency is 200. These two parameters need to be adjusted according to your own situation.

COMPENSATION=$(grep 'mips' /proc/cpuinfo|head -1|awk -F: '{printf("%.2f",$2/5000)}')

Learn the CPU speed from bogomips in / proc/cpuinfo file: the roughly calculated CPU speed (Millennium instructions per second) when the system kernel starts, so as to set the penalty time parameter. My virtual machine bogomips=4608.00, divided by 5000 is 0.92, sudo Emacs etc / judge Conf can be modified. Let's skip here. Anyway, the default 1 does not affect the test.

PHP_FPM=$(find /etc/init.d/ -name "php*-fpm")
$PHP_FPM restart
PHP_FPM=$(service --status-all|grep php|awk '{print $4}')
if [ "$PHP_FPM" != ""  ]; then service "$PHP_FPM" restart ;else echo "NO PHP FPM";fi;

This part is to restart the service and check sudo systemctl # restart # php8 0-fpm and # sudo systemctl # status # php8 0-fpm

cd src/core || exit
chmod +x ./
if grep "/usr/bin/judged" /etc/rc.local ; then
        echo "auto start judged added!"
        sed -i "s/exit 0//g" /etc/rc.local
        echo "/usr/bin/judged" >> /etc/rc.local
        echo "exit 0" >> /etc/rc.local
if grep "" /var/spool/cron/crontabs/root ; then
        echo "auto backup added!"
        crontab -l > conf && echo "1 0 * * * /home/judge/src/install/" >> conf && crontab conf && rm -f conf
ln -s /usr/bin/mcs /usr/bin/gmcs

cp /home/judge/src/install/hustoj /etc/init.d/hustoj
update-rc.d hustoj defaults
systemctl enable hustoj
systemctl enable nginx
systemctl enable mysql
systemctl enable php7.4-fpm
#systemctl enable judged

mkdir /var/log/hustoj/
chown www-data -R /var/log/hustoj/


echo "Remember your database account for HUST Online Judge:"
echo "username:$USER"
echo "password:$PASSWORD"

This is the most troublesome part because we replaced mysql with mariadb.

 cd /home/zime/hustoj/trunk/core/,  sudo chmod a+x ./, it can't go on. The code from git doesn't have a make file, but there is a makefile, and there will certainly be less things, because we have a software package that hasn't been installed. Please refer to , Combined with the connector version we use, try the following:

Sudo apt get install libmariadb3, sudo apt get install libmariadb dev, sudo apt get install libmariadb dev compat the third package is a compatible package. After installation, it is found that there are both mariadb subdirectories and mysql subdirectories in the / usr/include directory, and the file names are the same. In this way, we can include mysql in the name, In fact, mariadb is included to complete the task (/ usr/include/mysql is a symbolic link to / usr/include/mariadb).

Sudo apt get install make, cd / home / zime / hustoj / trunk / core / gadget, make build, there will be several warnings, generate gadget O target file and judged execution file (the judged generated by make itself has x permission, so chmod +x judged in is not necessary), sudo cp judged /usr/bin , copy the problem server to / usr/bin

cd   ../ judge_client, make build and generate judge_client.o and judge_client,sudo  cp  judge_client / usr/bin: copy the problem determination client to / usr/bin

cd  ../ sim/sim_3_01. sudo , apt get install , flex, make , fresh clean up some old generated files that may exist, build make , exes, there are some warnings, and many will be generated o .c  . exe file, copy the executable file to / usr/bin

sudo  cp  sim_c.exe  /usr/bin/sim_c,

sudo cp sim_c++.exe /usr/bin/sim_cc,
sudo cp sim_java.exe /usr/bin/sim_java,
sudo cp sim_pasc.exe /usr/bin/sim_pas,
sudo cp sim_text.exe /usr/bin/sim_text,
sudo cp sim_lisp.exe /usr/bin/sim_scm,

cd .. , sudo cp / usr/bin , this part of the work is to copy the cheating judgment program (for different languages) to / usr/bin, and then copy the general sim SH copy to / usr/bin, sim SH accepts a parameter, identifies the source code with an extension, and then calls the corresponding cheats.

The above is make SH's work

Next, add judged to the self startup. There is no / etc / rc Local file, self startup is managed by systemd, and self startup services are configured in / lib/systemd/system with RC local Service is actually found from / etc/systemd/system / during startup, so a soft link needs to be established (see

sudo emacs /lib/systemd/system/rc-local. Edit service and add the [Install] section at the end

sudo emacs /etc/rc.local edit (New), the contents are as follows (if there are other auto start contents, add / usr / bin / judge before exit 0)

exit 0

sudo chmod +x /etc/rc.local to RC Add the execution permission to local, and then | sudo systemctl start RC local, sudo | systemctl | restart RC local, sudo | systemctl | status | RC local verify whether the service is active, ps -aux | grep | judged check whether the server program is running

Backup script bak SH and cron perform backups regularly. We skip C#, and skip gmcs

sudo cp /home/zime/hustoj/trunk/install/hustoj  /etc/init.d/hustoj
sudo update-rc.d hustoj defaults

sudo systemctl enable hustoj
sudo systemctl enable nginx
sudo systemctl enable mariadb
sudo systemctl enable php8.0-fpm

sudo mkdir /var/log/hustoj/
sudo chown www-data -R /var/log/hustoj/