summaryrefslogtreecommitdiffhomepage
path: root/vhosts/blog/public/posts/2022-10-28/setup-server-for-this-site
diff options
context:
space:
mode:
Diffstat (limited to 'vhosts/blog/public/posts/2022-10-28/setup-server-for-this-site')
-rw-r--r--vhosts/blog/public/posts/2022-10-28/setup-server-for-this-site/index.html316
1 files changed, 316 insertions, 0 deletions
diff --git a/vhosts/blog/public/posts/2022-10-28/setup-server-for-this-site/index.html b/vhosts/blog/public/posts/2022-10-28/setup-server-for-this-site/index.html
new file mode 100644
index 00000000..20297c1b
--- /dev/null
+++ b/vhosts/blog/public/posts/2022-10-28/setup-server-for-this-site/index.html
@@ -0,0 +1,316 @@
+<!DOCTYPE html>
+<html lang="ja-JP">
+ <head>
+ <meta charset="UTF-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <meta name="author" content="nsfisis">
+ <meta name="copyright" content="&copy; 2022 nsfisis">
+ <meta name="description" content="GitHub Pages でホストしていたこのサイトを VPS へ移行したので、そのときにやったことのメモ。99 % 自分用。">
+ <meta name="keywords" content="備忘録">
+ <link rel="icon" type="image/svg+xml" href="/favicon.svg">
+ <title>【備忘録】 このサイト用の VPS をセットアップしたときのメモ | REPL: Rest-Eat-Program Loop</title>
+ <link rel="stylesheet" href="/style.css?h=37fff6a2f0eef473abde58e55f28ea69">
+ <link rel="stylesheet" href="/hl.css?h=340e65ffd5c17713efc9107c06304f7b">
+ </head>
+ <body class="single">
+ <header class="header">
+ <nav class="nav">
+ <ul>
+ <li>
+ <a href="/">REPL: Rest-Eat-Program Loop</a>
+ </li>
+ <li>
+ <a href="/about/">About</a>
+ </li>
+ <li>
+ <a href="/posts/">Posts</a>
+ </li>
+ <li>
+ <a href="/slides/">Slides</a>
+ </li>
+ <li>
+ <a href="/tags/">Tags</a>
+ </li>
+ </ul>
+ </nav>
+ </header>
+ <main class="main">
+ <article class="post-single">
+ <header class="post-header">
+ <h1 class="post-title">【備忘録】 このサイト用の VPS をセットアップしたときのメモ</h1>
+ <ul class="post-tags">
+ <li class="tag">
+ <a href="/tags/note-to-self/">備忘録</a>
+ </li>
+ </ul>
+ </header>
+ <div class="post-content">
+ <section>
+ <h2 id="changelog">更新履歴</h2>
+ <ol>
+ <li class="revision">
+ <time datetime="2022-10-28">2022-10-28</time>: 公開
+ </li>
+ <li class="revision">
+ <time datetime="2023-08-30">2023-08-30</time>: ssh_config に IdentitiesOnly yes を追加
+ </li>
+ </ol>
+ </section>
+ <section id="section--intro">
+ <h2><a href="#section--intro">はじめに</a></h2>
+ <p>
+ これまでこの blog は GitHub Pages でホストしていたのだが、先日 VPS に移行した。 そのときにおこなったサーバのセットアップ作業を書き残しておく。 99 % 自分用の備忘録。別のベンダに移したりしたくなったら見に来る。
+ </p>
+
+ <p>
+ 未来の自分へ: 特に自動化してないので、せいぜい苦しんでくれ。
+ </p>
+ </section>
+
+ <section id="section--vps">
+ <h2><a href="#section--vps">VPS</a></h2>
+ <p>
+ <a href="https://vps.sakura.ad.jp/">さくらの VPS</a> の 2 GB プラン。 そこまで真面目に選定していないので、困ったら移動するかも。
+ </p>
+ </section>
+
+ <section id="section--preparation">
+ <h2><a href="#section--preparation">事前準備</a></h2>
+ <section id="section--preparation--hostname">
+ <h3><a href="#section--preparation--hostname">サーバのホスト名を決める</a></h3>
+ <p>
+ モチベーションが上がるという効能がある。今回は藤原定家から取って <code>teika</code> にした。 たいていいつも源氏物語の帖か小倉百人一首の歌人から選んでいる。
+ </p>
+ </section>
+
+ <section id="section--preparation--ssh-key">
+ <h3><a href="#section--preparation--ssh-key">SSH の鍵生成</a></h3>
+ <p>
+ ローカルマシンで鍵を生成する。
+ </p>
+
+ <pre class="highlight" language="shell-session" linenumbering="unnumbered"><code>$ ssh-keygen -t ed25519 -b 521 -f ~/.ssh/teika.key
+$ ssh-keygen -t ed25519 -b 521 -f ~/.ssh/github2teika.key</code></pre>
+
+ <p>
+ <code>teika.key</code> はローカルからサーバへの接続用、<code>github2teika.key</code> は、 GitHub Actions からサーバへのデプロイ用。
+ </p>
+ </section>
+
+ <section id="section--preparation--ssh-config">
+ <h3><a href="#section--preparation--ssh-config">SSH の設定</a></h3>
+ <p>
+ <code>.ssh/config</code> に設定しておく。
+ </p>
+
+ <pre class="highlight" language="ssh_config" linenumbering="unnumbered"><code>Host teika
+ HostName **********
+ User **********
+ Port **********
+ IdentityFile ~/.ssh/teika.key
+ IdentitiesOnly yes</code></pre>
+ </section>
+ </section>
+
+ <section id="section--basic-setup">
+ <h2><a href="#section--basic-setup">基本のセットアップ</a></h2>
+ <section id="section--basic-setup--login">
+ <h3><a href="#section--basic-setup--login">SSH 接続</a></h3>
+ <p>
+ VPS 契約時に設定した管理者ユーザとパスワードを使ってログインする。
+ </p>
+ </section>
+
+ <section id="section--basic-setup--user">
+ <h3><a href="#section--basic-setup--user">ユーザを作成する</a></h3>
+ <p>
+ 管理者ユーザで作業すると危ないので、メインで使うユーザを作成する。 <code>sudo</code> グループに追加して <code>sudo</code> できるようにし、<code>su</code> で切り替え。
+ </p>
+
+ <pre class="highlight" language="shell-session" linenumbering="unnumbered"><code>$ sudo adduser **********
+$ sudo adduser ********** sudo
+$ su **********
+$ cd</code></pre>
+ </section>
+
+ <section id="section--basic-setup--hostname">
+ <h3><a href="#section--basic-setup--hostname">ホスト名を変える</a></h3>
+ <pre class="highlight" language="shell-session" linenumbering="unnumbered"><code>$ sudo hostname teika</code></pre>
+ </section>
+
+ <section id="section--basic-setup--public-key">
+ <h3><a href="#section--basic-setup--public-key">公開鍵を置く</a></h3>
+ <pre class="highlight" language="shell-session" linenumbering="unnumbered"><code>$ mkdir ~/.ssh
+$ chmod 700 ~/.ssh
+$ vi ~/.ssh/authorized_keys</code></pre>
+
+ <p>
+ <code>authorized_keys</code> には、ローカルで生成した <code>~/.ssh/teika.key.pub</code> と <code>~/.ssh/github2teika.key.pub</code> の内容をコピーする。
+ </p>
+ </section>
+
+ <section id="section--basic-setup--ssh-config">
+ <h3><a href="#section--basic-setup--ssh-config">SSH の設定</a></h3>
+ <p>
+ SSH の設定を変更し、少しでも安全にしておく。
+ </p>
+
+ <pre class="highlight" language="shell-session" linenumbering="unnumbered"><code>$ sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
+$ sudo vi /etc/ssh/sshd_config</code></pre>
+
+ <ul>
+ <li>
+ <code>Port</code> を変更
+ </li>
+
+ <li>
+ <code>PermitRootLogin</code> を <code>no</code> に
+ </li>
+
+ <li>
+ <code>PasswordAuthentication</code> を <code>no</code> に
+ </li>
+ </ul>
+
+ <p>
+ そして設定を反映。
+ </p>
+
+ <pre class="highlight" language="shell-session" linenumbering="unnumbered"><code>$ sudo systemctl restart sshd
+$ sudo systemctl status sshd</code></pre>
+ </section>
+
+ <section id="section--basic-setup--ssh-connect">
+ <h3><a href="#section--basic-setup--ssh-connect">SSH で接続確認</a></h3>
+ <p>
+ 今の SSH セッションは閉じずに、ターミナルを別途開いて疎通確認する。 セッションを閉じてしまうと、SSH の設定に不備があった場合に締め出しをくらう。
+ </p>
+
+ <pre class="highlight" language="shell-session" linenumbering="unnumbered"><code>$ ssh teika</code></pre>
+ </section>
+
+ <section id="section--basic-setup--close-ports">
+ <h3><a href="#section--basic-setup--close-ports">ポートの遮断</a></h3>
+ <p>
+ デフォルトの 22 番を閉じ、設定したポートだけ空ける。
+ </p>
+
+ <pre class="highlight" language="shell-session" linenumbering="unnumbered"><code>$ sudo ufw deny ssh
+$ sudo ufw allow *******
+$ sudo ufw enable
+$ sudo ufw reload
+$ sudo ufw status</code></pre>
+
+ <p>
+ ここでもう一度 SSH の接続確認を挟む。
+ </p>
+ </section>
+
+ <section id="section--basic-setup--ssh-key-for-github">
+ <h3><a href="#section--basic-setup--ssh-key-for-github">GitHub 用の SSH 鍵</a></h3>
+ <p>
+ GitHub に置いてある private リポジトリをサーバから clone したいので、SSH 鍵を生成して置いておく。
+ </p>
+
+ <pre class="highlight" language="shell-session" linenumbering="unnumbered"><code>$ ssh-keygen -t ed25519 -b 521 -f ~/.ssh/github.key
+$ cat ~/.ssh/github.key.pub</code></pre>
+
+ <p>
+ <a href="https://github.com/settings/ssh">GitHub の設定画面</a> から、この公開鍵を追加する。
+ </p>
+
+ <pre class="highlight" language="shell-session" linenumbering="unnumbered"><code>$ vi ~/.ssh/config</code></pre>
+
+ <p>
+ 設定はこう。
+ </p>
+
+ <pre class="highlight" language="ssh_config" linenumbering="unnumbered"><code>Host github.com
+ HostName github.com
+ User git
+ Port 22
+ IdentityFile ~/.ssh/github.key
+ IdentitiesOnly yes</code></pre>
+
+ <p>
+ 最後に接続できるか確認しておく。
+ </p>
+
+ <pre class="highlight" language="shell-session" linenumbering="unnumbered"><code>$ ssh -T github.com</code></pre>
+ </section>
+
+ <section id="section--basic-setup--upgrade-packages">
+ <h3><a href="#section--basic-setup--upgrade-packages">パッケージの更新</a></h3>
+ <pre class="highlight" language="shell-session" linenumbering="unnumbered"><code>$ sudo apt update
+$ sudo apt upgrade
+$ sudo apt update
+$ sudo apt upgrade
+$ sudo apt autoremove</code></pre>
+ </section>
+ </section>
+
+ <section id="section--site-hosting-setup">
+ <h2><a href="#section--site-hosting-setup">サイトホスティング用のセットアップ</a></h2>
+ <section id="section--site-hosting-setup--dns">
+ <h3><a href="#section--site-hosting-setup--dns">DNS に IP アドレスを登録する</a></h3>
+ <p>
+ このサーバは固定の IP アドレスがあるので、<code>A</code> レコードに直接入れるだけで済んだ。
+ </p>
+ </section>
+
+ <section id="section--site-hosting-setup--install-softwares">
+ <h3><a href="#section--site-hosting-setup--install-softwares">使うソフトウェアのインストール</a></h3>
+ <pre class="highlight" language="shell-session" linenumbering="unnumbered"><code>$ sudo apt install docker docker-compose git make</code></pre>
+ </section>
+
+ <section id="section--site-hosting-setup--docker">
+ <h3><a href="#section--site-hosting-setup--docker">メインユーザが Docker を使えるように</a></h3>
+ <pre class="highlight" language="shell-session" linenumbering="unnumbered"><code>$ sudo adduser ********** docker</code></pre>
+ </section>
+
+ <section id="section--site-hosting-setup--open-http-ports">
+ <h3><a href="#section--site-hosting-setup--open-http-ports">HTTP/HTTPS を通す</a></h3>
+ <p>
+ 80 番と 443 番を空ける。
+ </p>
+
+ <pre class="highlight" language="shell-session" linenumbering="unnumbered"><code>$ sudo ufw allow 80/tcp
+$ sudo ufw allow 443/tcp
+$ sudo ufw reload
+$ sudo ufw status</code></pre>
+ </section>
+
+ <section id="section--site-hosting-setup--clone-repositories">
+ <h3><a href="#section--site-hosting-setup--clone-repositories">リポジトリのクローン</a></h3>
+ <pre class="highlight" language="shell-session" linenumbering="unnumbered"><code>$ cd
+$ git clone git@github.com:nsfisis/nsfisis.dev.git
+$ cd nsfisis.dev
+$ git submodule update --init</code></pre>
+ </section>
+
+ <section id="section--site-hosting-setup--certbot">
+ <h3><a href="#section--site-hosting-setup--certbot">certbot で証明書取得</a></h3>
+ <pre class="highlight" language="shell-session" linenumbering="unnumbered"><code>$ docker-compose up -d acme-challenge
+$ make setup</code></pre>
+ </section>
+
+ <section id="section--site-hosting-setup--run-server">
+ <h3><a href="#section--site-hosting-setup--run-server">サーバを稼動させる</a></h3>
+ <pre class="highlight" language="shell-session" linenumbering="unnumbered"><code>$ make serve</code></pre>
+ </section>
+ </section>
+
+ <section id="section--outro">
+ <h2><a href="#section--outro">感想</a></h2>
+ <p>
+ (業務でなく) 個人だと数年ぶりのサーバセットアップで、これだけでも割と時間を食ってしまった。 とはいえ式年遷宮は楽しいので、これからも定期的にやっていきたい。 コンテナデプロイにしたい気持ちもあるのだが、色々実験したい関係上、本物のサーバも欲しくはある。 次の式年遷宮では、手順の一部だけでも自動化したいところ。
+ </p>
+ </section>
+ </div>
+ </article>
+ </main>
+ <footer class="footer">
+ &copy; 2021 nsfisis
+ </footer>
+ </body>
+</html>