安装Git并且拉取源码
安装Gitsudo apt install git
从mirror-web拉取源码
git clone https://github.com/tuna/mirror-web.git
构建该网站有两种方式,本地开发和Docker部署,本地开发适合本地调试使用,Docker适合于生产环境中使用,Docker部署需要搭配后面Nginx使用。下文将分为两个部分进行讲解,请跳转到对应部分查看
本地开发
部分文档使用 submodule 方式嵌入仓库,拉取文档cd mirror-web
git submodule update --init
为正常运行,一些动态数据文件需要从镜像站下载
wget https://mirrors.tuna.tsinghua.edu.cn/static/tunasync.json -O static/tunasync.json
mkdir -p static/status
wget https://mirrors.tuna.tsinghua.edu.cn/static/status/isoinfo.json -O static/status/isoinfo.json
编译前,先安装 Ruby (>= 3.0) 和 Node.js (>= 18)
安装Ruby
sudo apt update
sudo apt install ruby-full
检查Ruby的版本,确保 Ruby (>= 3.0)
ruby -v
安装Node.js
sudo apt update
sudo apt install -y nodejs npm
安装完成后检查Node.js版本,确保Node.js(>=18)
node -v
使用Ubuntu安装的Node可能版本比较低,下面进行升级(可选,如果版本已达到要求可以不升级)
安装n管理工具
sudo npm install -g n
使用<font style="color:rgb(199, 37, 78);background-color:rgb(249, 242, 244);">n</font>
来安装最新的稳定版Node.js
sudo n stable
安装完成后重启终端,然后检查Node.js的版本(不重启终端依旧显示原本的版本)
node -v
构建本地开发环境
bundle install --deployment
安装npm
npm install
编译到 `_site/`目录
bundle exec jekyll build
实时渲染
bundle exec jekyll serve --livereload
渲染完成后终端会有一个端口为4000的网址,复制到浏览器中访问即可
Build with Vite complete: /home/sora/mirror-web/.jekyll-cache/vite-dist
done in 24.567 seconds.
Auto-regeneration: enabled for '/home/sora/mirror-web'
LiveReload address: http://127.0.0.1:35729
Server address: http://127.0.0.1:4000/
Server running... press ctrl-c to stop.
Docker部署
首先安装Docker
Docker安装参考Ubuntu 22.04安装docker和docker-compose
构建并挂载
使用以下命令构建(如依赖无变化,镜像无需重复构建)
cd /home/sora/mirror-web
sudo docker build -t tunathu/mirror-web -f Dockerfile.build .
构建时,仅需将本地目录挂载到容器中:
sudo docker run --rm -v /home/sora/mirror-web:/data tunathu/mirror-web
即可在 /home/sora/mirror-web/_site
中得到编译结果。
Nginx中部署Web
本项目使用 Nginx 的 fancy-index
模块美化镜像站的文件列表展示。所以 Nginx 安装了 ngx_http_fancyindex_module
和 ngx_http_js_module
,并确保后者的版本不低于 0.7.9,在此之前我们需要安装编译pcre,因为后续Nginx配置需要使用到UTF-8正则表达式,默认的pcre是不支持的
cd
wget https://downloads.sourceforge.net/project/pcre/pcre/8.45/pcre-8.45.tar.gz --no-check-certificate
tar -xzvf pcre-8.45.tar.gz
cd pcre-8.45
配置pcre
./configure --prefix=/usr/local/pcre \
--docdir=/usr/share/doc/pcre-8.45 \
--enable-unicode-properties \
--enable-pcre16 \
--enable-pcre32 \
--enable-pcregrep-libz \
--enable-pcregrep-libbz2 \
--enable-pcretest-libreadline \
--disable-static \
--enable-utf8
如果有提示缺失库使用apt install
命令安装即可,安装完成后重新配置pcre
编译并安装pcre
make
sudo make install
检查pcre是否安装成功,显示版本号即为安装成功
/usr/local/pcre/bin/pcre-config --version
安装编译Nginx所需的依赖项
sudo apt update
sudo apt install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev
从Nginx官网下载源码
cd
wget http://nginx.org/download/nginx-1.25.2.tar.gz
tar -xzvf nginx-1.25.2.tar.gz
cd nginx-1.25.2
下载所需模块
- 克隆
ngx_http_fancyindex_module
模块源码
git clone https://github.com/aperezdc/ngx-fancyindex.git
- 克隆
Nginx JavaScrip
源码
git clone https://github.com/nginx/njs.git
配置Nginx
./configure \ --prefix=/usr/local/nginx \
--add-module=./ngx-fancyindex \
--add-module=./njs/nginx \
--with-pcre=/home/sora/pcre-8.45 \
--with-pcre-jit \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_gzip_static_module
编译并安装
make
sudo make install
检查Nginx是否成功安装,输出版本号即为安装成功
/usr/local/nginx/sbin/nginx -v
启动Nginx
sudo /usr/local/nginx/sbin/nginx
检查Nginx是否成功启动,使用命令ps aux | grep nginx
查看进程
sora@sora-virtual-machine:~/nginx-1.25.2$ ps aux | grep nginx
root 817257 0.0 0.0 47560 2536 ? Ss 01:29 0:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 817258 0.0 0.0 48328 5096 ? S 01:29 0:00 nginx: worker process
sora 817321 0.0 0.0 12192 2432 pts/5 R+ 01:29 0:00 grep --color=auto nginx
在浏览器中输入IP地址即可打开Nginx的默认欢迎页
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.