服务器相关问题
GitHub Action + Hexo 部署到服务器
在本地电脑生成 ssh key
创建 git 用户
设置 ssh
把本机的 id_isa.pub
内容复制到这里:
1
| /home/git/.ssh/authorized_keys
|
如果通过 ssh 登录仍需要密码的解决方法
找到并修改 /etc/ssh/sshd_config
文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| PermitRootLogin yes
StrictModes no
RSAAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication no
|
然后重启 sshd
服务
网站路径
创建网站,以 /www/wwwroot/csthenry.github.io
为例,/www/wwwroot/csthenry.github.io
的权限要改成 777 并且所有者为 www 才可以访问。
GitHub Action
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| name: auto deploy
on: [push]
jobs: build: runs-on: ubuntu-latest name: auto deploy steps: - name: Checkout uses: actions/checkout@v3 with: submodules: true - name: Setup Node.js 10.x uses: actions/setup-node@master with: node-version: "10.x" - name: Generate Public Files run: | npm i npm install hexo-cli -g hexo clean && hexo generate - name: Deploy to GitHub Pages uses: peaceiris/actions-gh-pages@v3 with: deploy_key: ${{ secrets.DEPLOY_KEY }} external_repository: csthenry/csthenry.github.io publish_branch: gh-pages publish_dir: ./public commit_message: ${{ github.event.head_commit.message }} user_name: 'github-actions[bot]' user_email: 'github-actions[bot]@users.noreply.github.com' - name: Deploy to Server uses: easingthemes/ssh-deploy@v2 env: SSH_PRIVATE_KEY: ${{ secrets.SERVER_SSH_KEY }} ARGS: "-rltgoDzvO --delete" SOURCE: public/ REMOTE_HOST: ${{ secrets.REMOTE_HOST }} REMOTE_USER: ${{ secrets.REMOTE_USER }} TARGET: ${{ secrets.TARGET }}
|
在 Settings -> Secrets 中填写对应的值:
1 2 3 4
| SERVER_SSH_KEY: 第一步本机生成的 id_isa 文件内容 REMOTE_HOST: 服务器地址 REMOTE_USER: 用户名,例如 "git" TARGET: 生成的文件路径,例如 "/www/wwwroot/csthenry.github.io/"
|
设置 webhook
post-receive1 2 3
| #!/bin/bash WEBROOT=/www/wwwroot/csthenry.github.io git --work-tree=$WEBROOT checkout -f master
|
把上述的 post-receive 文件放在这里:
1
| /home/git/xxx.git/hooks/post-receive
|
/www/wwwroot/csthenry.github.io
的权限要改成 777 才行
GitLab
把上述的 post-receive 文件放在这里:
1
| /var/opt/gitlab/git-data/repositories/root/xxx.git/custom_hooks/post-receive
|
GitLab 的 ssh 能记住密钥, hook 也正常,缺点就是每个月多花10块钱满足 GitLab 最低配置要求。
404
打开 nginx 配置文件,拉到最后,看到:
1
| include /www/server/panel/vhost/nginx/*.conf;
|
然后根据这个路径找到自定义的配置文件:
1
| /www/server/panel/vhost/nginx/<your-domain>.conf
|
在其中添加如下代码就可以了:
1 2 3 4 5 6 7 8 9 10
| server { ... fastcgi_intercept_errors on; error_page 404 /404.html; location = /404.html { root /www/wwwroot/csthenry.github.io; } ... }
|