From f0fde08efce470fe363a5f521a3234a0489fe46f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=BE=E6=B5=A6=20=E7=9F=A5=E4=B9=9F=20Matsuura=20Tomoy?= =?UTF-8?q?a?= Date: Mon, 5 Feb 2024 12:26:59 +0900 Subject: [PATCH] [obsidian] vault backup: 2024-02-05 12:26:59 --- ...sとrsync、Caddyで静的サイトをホストする.md | 163 ++++++++++++++++++ content/Giteaで静的サイトをホストする.md | 11 -- ...イトをDocker Composeでいい感じにホストする.md | 8 - 3 files changed, 163 insertions(+), 19 deletions(-) create mode 100644 content/Gitea Actionsとrsync、Caddyで静的サイトをホストする.md delete mode 100644 content/Giteaで静的サイトをホストする.md delete mode 100644 content/静的WebサイトをDocker Composeでいい感じにホストする.md diff --git a/content/Gitea Actionsとrsync、Caddyで静的サイトをホストする.md b/content/Gitea Actionsとrsync、Caddyで静的サイトをホストする.md new file mode 100644 index 00000000..f5ac6f46 --- /dev/null +++ b/content/Gitea Actionsとrsync、Caddyで静的サイトをホストする.md @@ -0,0 +1,163 @@ +#self-hosted #server + + +[Serving static files using Caddy](https://thedevelopercafe.com/articles/serving-static-files-using-caddy-8513e8f36e46) + +## 下準備 + +鍵をssh-keygenで作っておく。(秘密鍵をGitea側のSecretsに置くので新しく生成しないとだめ) + +```sh +ssh-keygen -t ed25519 +``` + +ファイル名は`rsync_webserver`と`rsync_webserver.pub`とかにしておく + +[tomoyanonymous/Caddy\_rsync\_webserver - Caddy\_rsync\_webserver - Tomoya Matsuura Gitea](https://git.matsuuratomoya.com/tomoyanonymous/Caddy_rsync_webserver) + +## Caddyサーバー側の構成 + +Caddyのイメージはそのまま使う + +### フォルダ構成 +```sh +Dockerfile +docker-compose.yml +Caddyfile +docs/ #空フォルダ +rsync_webserver.pub #鍵をコピーしておく +``` + ++もう一つrsyncでファイルを受け取る用のDockerイメージを作る + +```Dockerfile +FROM ubuntu:latest + +RUN apt-get update && apt-get install -y openssh-server rsync +RUN mkdir /var/run/sshd + +RUN sed -i 's/#\?SyslogFacility AUTH/SyslogFacility AUTH/' /etc/ssh/sshd_config +RUN sed -i 's/#\?LogLevel INFO/LogLevel DEBUG/' /etc/ssh/sshd_config + +RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd + +ENV NOTVISIBLE "in users profile" +RUN echo "export VISIBLE=now" >> /etc/profile + +COPY rsync_webserver.pub /root/authorized_keys + +RUN mkdir ~/.ssh && \ + mv ~/authorized_keys ~/.ssh/authorized_keys && \ + chmod 0600 ~/.ssh/authorized_keys && \ + chmod 0700 ~/.ssh + +EXPOSE 22 + +CMD ["/usr/sbin/sshd", "-D"] +``` + +`~/.ssh/authorized_keys`と`~/.ssh`のパーミッションを間違えるとssh接続できないので注意 + +`docker-compose.yml`でCaddyとの連携設定。共通のボリュームでhtmlを管理するが、今回はそのフォルダを`docs`という名前で作った + +```yml +version: '3' + +services: + ssh: + build: . + volumes: + - ./docs:/var/www/html + ports: + - '25222:22' + server: + image: caddy:latest + volumes: + - ./Caddyfile:/etc/caddy/Caddyfile + - ./docs:/www/html + ports: + - "8080:80" +``` + +Caddyファイルは最小限。404の設定とかしてない。quartzでは`/記事名.html`に`example.com/記事名`でアクセスする必要があるので`try_files`の行が必要。 + +```Caddyfile +:80 { + root * /www/html + file_server + try_files {path}.html {path} +} +``` + +`docs`内に適当な`index.html`を置いて`docker-compose build`、`docker-compose up -d`で立ち上げ、`DockerホストのIP:8080`で繋ぐとアクセスできる。 + +これをそのまま[[Cloudflare Tunnel]]で公開している。 + +## Gitea側の設定 + +Action RunnerつきでGiteaを立ち上げるところまでは省略。 + +ほぼこのサイトの例を借りる。[[Hugo]]じゃなく[[Quartz]]なので色々違うけど + +[CI/CD pipeline for a Hugo-built static page using Gitea Actions on a selfhosted Gitea instance · Julius Röttgermann | DevOps related Blog and Tutorials](https://julius-roettgermann.de/post/hugo-cicd/) + + +リポジトリのSettings -> Actions -> Secretsに`PRIVATE_KEY`という名前で`rsync_webserver`の中身を貼り付ける。 +同じくVariablesの方には以下を設定 + +- WEBSERVER_HOST (Caddyが立ち上がっているサーバーのIP) +- WEBSERVER_PORT (Caddyが立ち上がっているサーバーのSSH用ポート、上の例では25222) +- WEBSERVER_USER :root(これあんまり良くないのかな) + +で、リポジトリに`/.gitea/workflows/ci.yaml`を作る。 + +```yml +name: Build +on: + push: + branches: + - v4 +jobs: + build: + env: + RUNNER_TOOL_CACHE: /toolcache #これないとキャッシュが効かない + runs-on: ubuntu-latest + steps: + - run: git config --global core.quotepath false + - name: Install apt packages + run: apt update && apt install -y rsync + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - uses: https://github.com/actions/setup-node@v4 + with: + node-version: 'latest' + - uses: https://github.com/actions/setup-go@v4 + with: + go-version: '^1.20' + - uses: https://gitea.com/actions/go-hashfiles@v0.0.1 + id: get-hash + with: + patterns: |- + **/package-lock.json + - name: Cache dependencies + uses: actions/cache@v3 + with: + path: ~/.npm + key: ${{ runner.os }}-node-${{ steps.get-hash.outputs.hash }} + restore-keys: | + ${{ runner.os }}-node- + - run: npm i + - run: npx quartz build + - name: Create private key + run: | + echo "${{ secrets.PRIVATE_KEY }}" > /tmp/act_runner_key + chmod 600 /tmp/act_runner_key + - name: rsync public directory + run: | + rsync -avz --delete -e "ssh -i /tmp/act_runner_key -o StrictHostKeyChecking=no -p ${{ vars.WEBSERVER_PORT }}" ${{ gitea.workspace }}/public/* ${{ vars.WEBSERVER_USER }}@${{ vars.WEBSERVER_HOST }}:/var/www/html +``` + + + +Branch Previewが手軽にできればいいんだけどなあ \ No newline at end of file diff --git a/content/Giteaで静的サイトをホストする.md b/content/Giteaで静的サイトをホストする.md deleted file mode 100644 index 711a730e..00000000 --- a/content/Giteaで静的サイトをホストする.md +++ /dev/null @@ -1,11 +0,0 @@ -#self-hosted #server -[CI/CD pipeline for a Hugo-built static page using Gitea Actions on a selfhosted Gitea instance · Julius Röttgermann | DevOps related Blog and Tutorials](https://julius-roettgermann.de/post/hugo-cicd/) - -やっぱ普通にrsyncで同期するしかないのか - -privatekeyを環境からジェネってるけど、これどっから持ってきてるんだ -普通にリポジトリのsecretsに突っ込んでるだけか、まあそうか - -Branch Previewが手軽にできればいいんだけどなあ - -メモ:sshされる側のサーバーで`~/.ssh`を`chmod 0700`しておく \ No newline at end of file diff --git a/content/静的WebサイトをDocker Composeでいい感じにホストする.md b/content/静的WebサイトをDocker Composeでいい感じにホストする.md deleted file mode 100644 index 848b19b5..00000000 --- a/content/静的WebサイトをDocker Composeでいい感じにホストする.md +++ /dev/null @@ -1,8 +0,0 @@ - -#self-hosted - -[Static Site Hosting for Free with Docker, Hugo, and NGINX Proxy Manger | wallaceLabs](https://wallacelabs.tech/posts/static-site-hosting-with-docker-hugo-and-nginx-proxy-manger/) - -Nginxより[[Caddy]]がええかもしらん - -[Site Unreachable](https://thedevelopercafe.com/articles/serving-static-files-using-caddy-8513e8f36e46)