在自己的VPS上搭建codeforces镜像站(需要域名版本,门槛较高)medium
💼

在自己的VPS上搭建codeforces镜像站(需要域名版本,门槛较高)medium

前提条件

有一台自己的VPS(内存512M及以上),一个域名(多级也可)并申请SSL证书
还需要对Linux系统基础操作有基本的了解(本文不会讲这些东西,例如如何编辑文件)

环境

Ubuntu 18.04及以上(不支持CentOS,Ubuntu 16.04及以下会出现python版本问题,不建议使用)

项目地址

部署

准备环境

安装依赖
sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get dist-upgrade -y && sudo apt-get install build-essential patch binutils make devscripts nano libtool libssl-dev libxml2 libxml2-dev software-properties-common python-software-properties dnsutils git wget curl python3 python3-pip iftop -y && sudo python3 -m pip install -U flask requests cchardet fastcache
安装apache2
LC_ALL=C.UTF-8 sudo add-apt-repository -y ppa:ondrej/apache2 && sudo apt-key update && sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get install apache2 -y && sudo a2enmod rewrite mime include headers filter expires deflate autoindex setenvif ssl http2 && sudo apt-get install libapache2-mod-wsgi-py3 -y

安装配置zmirror

cd /var/www && git clone https://github.com/yanyanlongxia/zmirror-codeforces zmirror&& cd zmirror && chown -R www-data . && chgrp -R www-data . && cp more_configs/config_codeforces.py config.py
将此时路径下的 config.py 中的my_host_name内容改为您的域名。
此时默认启用codeforces的edu功能和wikipedia。

配置apache2

/etc/apache2 中创建文件夹,名为 cert ,并在文件夹放入域名的SSL证书
/etc/apache2/sites-enabled 文件夹中创建 codeforces.conf 为以下内容
<IfModule mod_ssl.c> SSLCipherSuite HIGH:MEDIUM:!aNULL:!MD5 <VirtualHost *:443> ServerName codeforces.live ServerAdmin yylx@yylx.tech ErrorLog ${APACHE_LOG_DIR}/zmirror-codeforces_ssl_error.log CustomLog ${APACHE_LOG_DIR}/zmirror-codeforces_ssl_access.log combined WSGIDaemonProcess zmirror_codeforces user=www-data group=www-data threads=16 WSGIScriptAlias / /var/www/zmirror/wsgi.py WSGIPassAuthorization On <Directory /var/www/zmirror> WSGIProcessGroup zmirror_codeforces WSGIApplicationGroup %{GLOBAL} Order deny,allow Allow from all </Directory> SSLEngine on SSLCertificateFile /etc/apache2/cert/codeforces.live_cert.pem SSLCertificateKeyFile /etc/apache2/cert/codeforces.live_privkey.pem SSLCertificateChainFile /etc/apache2/cert/codeforces.live_chain.pem <IfModule http2_module> Protocols h2 h2c http/1.1 </IfModule> </VirtualHost> </IfModule>
ServerName 中的 codeforces.live 改为您的域名,将 yylx@yylx.tech 改为您的邮箱。
SSLCertificateFile /etc/apache2/cert/codeforces.live_cert.pem SSLCertificateKeyFile /etc/apache2/cert/codeforces.live_privkey.pem SSLCertificateChainFile /etc/apache2/cert/codeforces.live_chain.pem
中的三个SSL证书改为您的SSL证书
然后重启apache2
source /etc/apache2/envvars service apache2 restart
然后访问域名就能看到了(要https访问)

配置定时任务

由于玄学原因,本项目可能会时不时出问题,需要重启apache2
输入crontab -e
在cron中添加
* * * * * source /etc/apache2/envvars 0 */1 * * * /usr/sbin/service apache2 restart
意思是每分钟应用环境变量,每小时重启一次apache2

可选配置

暂时先咕咕了吧……
 

鸣谢

感谢这些项目,没有这些项目,本文就不会诞生: