基本玩法
如果你做个网站对外提供 web 内容,就需要自己运行 http 服务。我们以 python3 内置的 http.server 包为例演示用法:
先准备 web 内容:
1
2
| mkdir web
echo "Hello, World!" > web/index.html
|
然后运行 http.server
1
2
| cd web
python3 -m http.server -b :: 8080
|
注意,这里使用-b ::来监听所有 IPv6 地址,包括我们自己的 fe80::2/64。端口只能固定为 8080。
到此,web 服务就准备好了。打开浏览器访问https://lab.example.zz.ac就会看到熟悉的”Hello, World!“。宿主机上的 Caddy 会自动申请 SSL 证书,并将请求转发到对应的用户网络空间。
为了让 web 服务在 ssh 退出后继续运行,你需要使用创建用户态的 systemd 服务。
服务路径为 ~/.config/systemd/user/web.service,内容如下:
1
2
3
4
5
6
7
| [Unit]
Description=Web Service
[Service]
WorkingDirectory=%h/www
ExecStart=/usr/bin/python3 -m http.server -b :: 8080
[Install]
WantedBy=default.target
|
保存之后通过 systemctl 启动服务:
systemctl –user enable –now web.service
1
| 这样你的服务就能在后台自动运行了。而且宿主机重启之后你的服务也会自动启动。
|
突然想起来前几天部署hexo博客到ct8服务器的经验:
本地写作,本地编译后通过git方式把静态文件deploy到远程服务器的方式,既然可以运行python3 -m http.server -b :: 8080,然后访问https://lab.example.zz.ac就能访问文件夹中的的静态文件,那我把自己的博客静态文件部署到我申请到的公益主机的blog文件夹中,再启动python网络服务器不就行了吗?说干就干!
部署经过:
第一步:服务器环境准备
- 通过ssh 登录公益主机,建立存放静态文件的文件夹:
1
| mkdir -p /blog/public_html
|
- 创建裸仓库作为服务器上的中转站:
1
2
3
| mkdir -p ~/repos/blog.git
cd ~/repos/blog.git
git init --bare
|
- 强制指定主分支名为main (本地电脑上的分支为main,可以在cmd通过git branch 查看)
1
| git symbolic-ref HEAD refs/heads/main
|
- 配置自动同步“钩子”
这是最容易踩坑的地方,必须处理权限、路径和换行符。
1
| vi ~/repos/blog.git/hooks/post-receive
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| #!/bin/bash
#定义路径
TARGET="/home/bosh/blog/public_html"
GIT_DIR="/home/bosh/repos/blog.git"
#1. 核心修复:彻底清除 Git 内部环境变量干扰
#使用 --work-tree 时,如果这些变量存在,Git 会报错
unset GIT_INDEX_FILE
unset GIT_DIR
unset GIT_WORK_TREE
#2. 确保目标目录存在
mkdir -p $TARGET
#3. 强制检出
#尝试检出 main,如果失败则尝试 master
echo "Deploying to $TARGET ..."
git --git-dir=$GIT_DIR --work-tree=$TARGET checkout -f main || git --git-dir=$GIT_DIR --work-tree=$TARGET checkout -f master
#4. 可选:权限修复(确保网页服务器有权读取)
chmod -R 755 $TARGET
echo "Deployment finished."
|
1
| chmod +x ~/repos/blog.git/hooks/post-receive
|
第二步:修改hexo的配置文件_config.yml:
在deploy 列表加入以下内容:
1
2
3
| - type: git
repo: ssh://bosh@lab.bosh.zz.ac/home/bosh/repos/blog.git
branch: main
|
第三步:在本地项目中文件夹运行cmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| Enumerating objects: 277, done.
Counting objects: 100% (277/277), done.
Delta compression using up to 4 threads
Compressing objects: 100% (88/88), done.
Writing objects: 100% (148/148), 51.47 KiB | 878.00 KiB/s, done.
Total 148 (delta 65), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (65/65), completed with 48 local objects.
remote: Deploying to /home/bosh/blog/public_html ...
remote: Already on 'main'
remote: Deployment finished.
To ssh://lab.bosh.zz.ac/home/bosh/repos/blog.git
e4b0064..9834816 HEAD -> main
branch 'master' set up to track 'ssh://bosh@lab.bosh.zz.ac/home/bosh/repos/blog.git/main'.
INFO Deploy done: git
|
- 在公益主机的/home/bosh/blog/public_html文件夹中运行:
1
| python3 -m http.server -b :: 8080
|
第四步:见证成功的时刻:在浏览器打开lab.bosh.zz.ac出现了博客网站,部署成功
第五步:创建用户态的 systemd 服务
让 ,web 服务在 ssh 退出后继续运行,主机重启后也能自动运行
- 先创建文件夹,直接 vi ~/.config/systemd/user/web.service 保存时会报错:
1
| mkdir -p ~/.config/systemd/user/
|
- 服务路径为
~/.config/systemd/user/web.service,内容如下:
1
2
3
4
5
6
7
8
9
| [Unit]
Description=Web Service
[Service]
WorkingDirectory=/home/bosh/blog/public_html
ExecStart=/usr/bin/python3 -m http.server -b :: 8080
[Install]
WantedBy=default.target
|
1
| systemctl --user enable --now web.service
|
- 通过systemctl –user status web.service,查看服务状态,看到 Active: active (running),说明运行成功。
1
2
3
4
5
6
7
8
9
10
11
12
|
web.service - Web Service
Loaded: loaded (/home/bosh/.config/systemd/user/web.service; enabled; preset: enabled)
Active: active (running) since Wed 2025-12-31 01:27:24 UTC; 3s ago
Main PID: 3219003 (python3)
Tasks: 1 (limit: 28699)
Memory: 9.2M (peak: 9.3M)
CPU: 50ms
CGroup: /user.slice/user-1011.slice/user@1011.service/app.slice/web.service
└─3219003 /usr/bin/python3 -m http.server -b :: 8080
Dec 31 01:27:24 h1.lab.zz.ac systemd[1040]: Started web.service - Web Service.
|
至此,部署结束!