快速部署你的网页项目
- fork 仓库并修改相关配置
- fork 仓库
- 修改仓库名称为你的项目名称(可选)
- 复制打包的文件放到 web 目录下
- 修改 yourdomain.com 为你的域名
- 在 Caddyfile 中修改 yourdomain.com 为你的域名
- 在 docker-compose.yml 中修改 yourdomain.com 为你的域名
- 配置 Caddyfile 中的路由规则
- 把 Caddyfile 中的 path/ 改为你的 URL 路径
- 登录云服务器
- VS Code 下载插件 Remote - SSH
- ssh 连接服务器(请参考 VS Code 教程)
- 安装 docker(请参考 Docker 教程)
- 配置 docker 镜像加速(请参考本文档“可能遇到的问题”第二点)
- 安装 git(请参考 VS Code 教程)
- 拉取远程仓库
- git 连接远程仓库
- 在服务器生成 SSH 秘钥
ssh-keygen -t ed25519 -C "[email protected]"(直接按 3 次 Enter 使用默认设置) - 查看并复制公钥
cat ~/.ssh/id_ed25519.pub复制输出的全部内容 - 在 GitHub 中添加 SSH 秘钥
- 登录 GitHub
- 进入仓库,选择 Settings
- 点击 Deploy keys
- 点击 Add deploy key
- 输入 Title 和 Key
- 点击 Add key
- 测试连接
ssh -T [email protected]输入 yes 回车 看到Hi yourusername! You've successfully authenticated表示成功
- 在服务器生成 SSH 秘钥
- git clone 远程仓库
git clone [email protected]:yourusername/yourrepo.git
- git 连接远程仓库
- 运行 docker 脚本
- 从终端 cd 到仓库目录
- 构建镜像并运行
sudo docker-compose up -d
- 访问 URL 地址
- 打开浏览器
- 输入正确的 URL
https://yourdomain.com/path/ - 应该可以看到你的网页了
- 端口冲突
报错:failed to bind host port for 0.0.0.0:80:172.18.0.2:80/tcp: address already in use- 查看端口占用情况
sudo lsof -i :80 - 如果看到类似这样的输出:
nginx 1234 root 6u IPv4 12345 0t0 TCP *:80 (LISTEN) - 关闭端口
sudo systemctl stop nginx - 重新运行 docker 脚本
- 查看端口占用情况
- 配置镜像加速错误
- 检查配置文件是否正确
sudo cat /etc/docker/daemon.json应该显示类似:{ "registry-mirrors": [ "https://mirror.ccs.tencentyun.com" ] } - 如果配置不正确,需要创建或者修改配置文件
- 删除配置文件
sudo rm /etc/docker/daemon.json - 创建新的配置文件
sudo nano /etc/docker/daemon.json - 输入配置内容
{ "registry-mirrors": [ "https://mirror.ccs.tencentyun.com" ] } - 保存并退出
:wq - 检查配置文件是否正确
sudo docker info | grep -A 5 Mirrors应该显示类似:Registry Mirrors: https://mirror.ccs.tencentyun.com/ Live Restore Enabled: false - 重新加载配置
sudo systemctl daemon-reload - 重启 docker
sudo systemctl restart docker
- 删除配置文件
- 重新运行 docker 脚本
- 检查配置文件是否正确