プロジェクト

全般

プロフィール

丼鯖の建てかた » 履歴 » バージョン 10

白林檎 美和, 2025/03/12 17:46

1 8 白林檎 美和
# 丼鯖 [mastodon instance] の建てかた
2 1 白林檎 美和
## 材料
3
* 最新のFreeBSD, nginx, PostgreSQL等が走るホスト
4
* データ置き場のストレージ: ユーザーを増やしたり,廃人のやうにつぶやいたりする気満々の方のみ
5
* 80/tcp, 443/tcpへ外からアクセス可能な,イソターネット接続環境
6
7
## 準備
8
### OSのインスコ
9 8 白林檎 美和
1. 最新のFreeBSDのディスクイメージを取ってきて,そいつからホストを起動。2021/8/31現在の最新リリースは, [13.0](http://ftp.jaist.ac.jp/pub/FreeBSD/releases/ISO-IMAGES/13.0/)。
10 1 白林檎 美和
1. テキストベースのウィザードを,淡々と進める。データ置き場のストレージを,ルートファイルシステムと別に設ける方は,そいつを /usr/home へマウント。
11 6 白林檎 美和
1. 丼インスタンスの各種プロセスを実行するためのユーザー “mastodon” を作成。プライマリ以外のグループに, wheelを指定。
12 1 白林檎 美和
1. 作業しやすいやう, sshdを有効化。sshdを常駐させるもよし, inetdで必要なときだけ起こすもよし。
13
14
### ホストがNATの内側にいる場合
15
1. イソターネットからの80/tcp, 443/tcpへのアクセスを,ホストへ通すよう設定。
16
1. NATの内外どちらからも,同じFQDNでホストへアクセスできるよう, DNS鯖を設定。さうしないと, SSLの証明書を取れなかったり, WWWブラウザで開いたときにSSLが有効にならなかったりする。
17
18
## 丼建て
19
ここからは基本的に,公式サイトの[Installing from source](https://docs.joinmastodon.org/admin/install/)の順番で進める。Ubuntu (Debian系Linux) 固有のコマンド等がばしばし出てきて, FreeBSDでは使えないので,読み替えていく。
20
### Pre-requisites
21
1. /etc/make.conf へ, 次を追加。
22 10 白林檎 美和
```
23 1 白林檎 美和
DEFAULT_VERSIONS+=perl5=5.34 python=3.9 python3=3.9 ruby=2.7 ssl=openssl
24
```
25 10 白林檎 美和
2. パッケージのオプションを選択。nginxのオプションは, PASSENGERをON。丼では使わないが, Redmine等を建てるときに要る。
26
```
27 1 白林檎 美和
# portsnap fetch update
28 9 白林檎 美和
# don_pkgs='databases/gdbm databases/postgresql14-server databases/postgresql14-contrib databases/redis devel/bison devel/git devel/protobuf devel/protobuf-c devel/bison devel/icu devel/readline dns/libidn graphics/ImageMagick7 multimedia/ffmpeg textproc/libxml2 textproc/libxslt security/openssl security/py-certbot-nginx security/sudo sysutils/rubygem-bundler www/nginx www/yarn'
29 1 白林檎 美和
# for pack in don_packs; do make -C /usr/ports/$pack config-recursive; done
30
```
31 10 白林檎 美和
3. Portsの一括インスコや更新に便利な, portupgradeをインスコ。これがrubyを引き連れてくる。
32
```
33 1 白林檎 美和
# make -C /usr/ports/ports-mgmt/portupgrade config-recursive install clean
34
```
35 10 白林檎 美和
4. 丼が使うパッケージを, portsからごりごりインスコ。
36
```
37 1 白林檎 美和
# portinstall $don_pkgs
38
```
39 10 白林檎 美和
5. 既存のFreeBSDの環境へ導入する方は,ここでユーザー “mastodon” を作成。bsdconfigの [6  Login/Group Management]→[1  Add Login] なり, adduserなり,お好きな方法で。
40 6 白林檎 美和
    プライマリ以外のグループに, wheelを指定。
41 10 白林檎 美和
6. /usr/local/etc/sudoers の行 `# %wheel ALL=(ALL) ALL` から,先頭の `# ` を削除。
42 1 白林檎 美和
43 2 白林檎 美和
### Setup
44
1. /etc/rc.conf へ次を追加。
45 10 白林檎 美和
```
46 1 白林檎 美和
nginx_enable="YES"
47
postgresql_enable="YES"
48 2 白林檎 美和
redis_enable="YES"
49 1 白林檎 美和
```
50 10 白林檎 美和
2. データ置き場を分けた方は, `postgresql_data="/usr/home/postgres/data14"` も追加。既に /var/db/postgres が作られていたら, /usr/home へ移動。
51
3. ぽすぐれを初期化,起動。
52
```
53 1 白林檎 美和
# service postgresql initdb
54
# service postgresql start
55 2 白林檎 美和
```
56 10 白林檎 美和
4. 丼DB用のユーザーを作成。ついでに,スーパーユーザーへパスワードを付与。
57
```
58 2 白林檎 美和
# sudo -u postgres psql
59 1 白林檎 美和
postgres=# CREATE USER mastodon WITH PASSWORD '<ぱすわぁど>' CREATEDB;
60 2 白林檎 美和
postgres=# ALTER ROLE postgres WITH PASSWORD '<ぱすわぁど>';
61 1 白林檎 美和
postgres=# \q  ← ^D
62 2 白林檎 美和
```
63 10 白林檎 美和
5. ユーザーmastodonへ切り替え。
64
```
65 9 白林檎 美和
# su - mastodon
66 1 白林檎 美和
Password: ←ユーザーmastodonのパスワード。
67
```
68 10 白林檎 美和
6. 丼鯖の最新リリースを取得。
69
```
70 1 白林檎 美和
$ git clone https://github.com/tootsuite/mastodon.git live
71 2 白林檎 美和
$ cd live
72
$ git checkout <最新リリースのタグ>
73
```
74 10 白林檎 美和
7. 丼鯖が使う, rubyや農奴のパッケージを, live以下へ配置。`getconf _NPROCESSORS_ONLN` は,ホストのCPUのスレッド数を返す。
75
```
76 2 白林檎 美和
$ export RAILS_ENV=production
77 1 白林檎 美和
$ bundle config deployment 'true'
78
$ bundle config without 'development test'
79 2 白林檎 美和
$ bundle install -j$(getconf _NPROCESSORS_ONLN)
80
$ yarn install --pure-lockfile
81
```
82 10 白林檎 美和
8. 初回設定。
83 2 白林檎 美和
  * DBはUNIX domain socketではなく, localhostを指定。前者では, socketのパス名がUbuntu標準と合わず失敗。
84
  * adminのパスワードがランダムに生成されるので,控えておく。
85 10 白林檎 美和
```
86 3 白林檎 美和
$ bundle exec rake mastodon:setup
87
```
88 10 白林檎 美和
9. dist/nginx.conf を /usr/local/etc/nginx/mastodon.conf へコピー。
89
10. /usr/local/etc/nginx に nginx.conf がなかったら,nginx.conf-dist をコピー。
90
```
91 3 白林檎 美和
# cp -p dist/nginx.conf /usr/local/etc/nginx/mastodon.conf
92
# cd /usr/local/etc/nginx
93
# cp -p nginx.conf-dist nginx.conf
94
```
95 10 白林檎 美和
11. nginx.conf の `http { ... }` から, `server { ... }` を削除。末尾に `include mastodon.conf;` を追加。
96
12. mastodon.conf を編集。
97
    1. `server_name` に,ホストのFQDNを記載。
98
    2. IPv6を使わない環境 ⇒ `listen [::]:80;`, `listen [::]:443 ssl http2;` をコメントアウト。
99
    3. /usr/local/etc/letsencrypt/options-ssl-nginx.conf と重複する,次の行をコメントアウト。
100
```
101 3 白林檎 美和
ssl_protocols TLSv1.2 TLSv1.3;
102
ssl_ciphers HIGH:!MEDIUM:!LOW:!aNULL:!NULL:!SHA;
103
ssl_prefer_server_ciphers on;
104 10 白林檎 美和
```
105
13. nginxを起動。
106
```
107 3 白林檎 美和
# service nginx start
108
```
109 10 白林檎 美和
14. Let's encryptからSSL証明書を貰う。
110
```
111 3 白林檎 美和
# certbot --nginx -d <ホストのFQDN>
112
```
113 10 白林檎 美和
15. mastodon.conf に次の行が足されたのを確認。
114
```
115 3 白林檎 美和
ssl_certificate /usr/local/etc/letsencrypt/live/<ホストのFQDN>/fullchain.pem;
116
ssl_certificate_key /usr/local/etc/letsencrypt/live/<ホストのFQDN>/privkey.pem;
117
include /usr/local/etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
118
ssl_dhparam /usr/local/etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
119
```
120 10 白林檎 美和
16. nginxを再起動。
121
```
122 3 白林檎 美和
# service nginx restart
123
```
124 10 白林檎 美和
17. /etc/periodic.conf に `weekly_certbot_enable="YES"` を追加。同ファイルがなければ作る。
125
18. dist/*.service の内容を基に,丼鯖の各種サービスの起動/終了用スクリプト /usr/local/etc/rc.d/mastodon を作成。最低限start, stop, restartができればよい。
126 3 白林檎 美和
``` shell
127
#!/bin/sh
128
129
# PROVIDE: mastodon
130
# REQUIRE: nginx postgresql redis
131
132
. /etc/rc.subr
133
134
name="mastodon"
135
rcvar="${name}_enable"
136
137
load_rc_config $name
138
: ${mastodon_enable="NO"}
139
: ${mastodon_user="mastodon"}
140
: ${mastodon_group="<ユーザーmastodonの所属グループ>"}
141 7 白林檎 美和
start_cmd="${name}_start &"
142 1 白林檎 美和
stop_cmd="${name}_stop"
143 7 白林檎 美和
restart_cmd="${name}_restart &"
144
status_cmd="${name}_status"
145
extra_commands="status"
146 3 白林檎 美和
147
MASTODON_HOME=/usr/home/mastodon/live
148
BUNDLE=/usr/local/bin/bundle
149
NODE=/usr/local/bin/node
150 1 白林檎 美和
DAEMON="/usr/sbin/daemon -u $mastodon_user"
151 3 白林檎 美和
PROCS="puma sidekiq node"
152 7 白林檎 美和
REQUIRED_PIDS="/var/db/postgres/data12/postmaster.pid /var/run/redis/redis.pid /var/run/nginx.pid"
153 3 白林檎 美和
154
export RAILS_ENV=production
155
export NODE_ENV=production
156
export DB_POOL=25
157
export MALLOC_ARENA_MAX=2
158
export STREAMING_CLUSTER_NUM=1
159 1 白林檎 美和
160
mastodon_start() {
161 7 白林檎 美和
	for i in $REQUIRED_PIDS; do
162
		while [ ! -f $i ]; do
163
			echo "$i is not ready."
164
			sleep 1
165
		done
166
	done
167
	sleep 1
168 3 白林檎 美和
	cd $MASTODON_HOME
169
	for i in $PROCS; do
170
		pidfile=$i.pid
171
		if [ -f $pidfile ]; then
172
			echo "$i is running as pid $(cat $pidfile)."
173
		else
174
			touch $pidfile
175
			chmod 644 $pidfile
176 1 白林檎 美和
			case $i in
177
				puma ) PORT=3000 $DAEMON -p puma.pid -o log/puma.log $BUNDLE exec puma -C config/puma.rb ;;
178
				sidekiq ) $DAEMON -p sidekiq.pid -o log/sidekiq.log $BUNDLE exec sidekiq -c 25 ;;
179 3 白林檎 美和
				node ) PORT=4000 $DAEMON -p node.pid -o log/node.log $NODE ./streaming ;;
180
			esac
181
			echo "Started $i."
182
		fi
183
	done
184
}
185
186
mastodon_stop() {
187 1 白林檎 美和
	cd $MASTODON_HOME
188 3 白林檎 美和
	for i in $PROCS; do
189 1 白林檎 美和
		pidfile=$i.pid
190
		if [ -f $pidfile ]; then
191 3 白林檎 美和
			pid=$(cat $pidfile)
192
			kill -s TERM $pid
193 1 白林檎 美和
			while kill -0 $pid 2> /dev/null; do
194
				sleep 1
195 3 白林檎 美和
			done
196
			rm -f $pidfile
197
			echo "Stopped $i."
198
		else
199
			echo "$i is not running."
200
		fi
201
	done
202
	stty echo
203
}
204 1 白林檎 美和
205
mastodon_restart() {
206 7 白林檎 美和
	$stop_cmd
207
	$start_cmd
208
}
209
210
ippandon_status() {
211
	cd $IPPANDON_HOME
212
	for i in $PROCS; do
213
		pidfile=$i.pid
214
		if [ -f $pidfile ]; then
215
			echo "$i is running as pid $(cat $pidfile)."
216
		else
217
			echo "$i is not running."
218 3 白林檎 美和
		fi
219
	done
220
}
221
222
run_rc_command "$1"
223 1 白林檎 美和
```
224 10 白林檎 美和
19. /etc/rc.conf へ `mastodon_enable="YES"` を追加。
225
20. 丼鯖のサービスを起動。
226
```
227 3 白林檎 美和
# service mastodon start
228 5 白林檎 美和
```
229 10 白林檎 美和
21. WWWブラウザから https://*ホストのFQDN*/ を開き, adminのメアドと初期パスワードでログイン。あとはページ内から,各種設定を進める。
230 4 白林檎 美和
231
## ログ回し
232
nginxと丼サービスのログを1週間周期で固めて回すよう, newsyslogを設定。
233
1. /usr/local/etc/newsyslog.conf.d に次のファイルを作成。
234
   * nginx.conf
235 10 白林檎 美和
```
236 4 白林檎 美和
# logfilename         [owner:group]  mode  count  size  when   flags  [/pid_file]         [sig_num]
237
/var/log/nginx/*.log                 644   100    *     $W0D0  GX     /var/run/nginx.pid  SIGUSR1
238
```
239
   * mastodon.conf: “usuaji” は,ユーザーmastodonの所属グループに読み替え。
240 10 白林檎 美和
```
241 4 白林檎 美和
# logfilename                  [owner:group]    mode  count  size  when   flags  [/pid_file]                    [sig_num]
242
/home/mastodon/live/log/*.log  mastodon:usuaji  644   100    *     $W0D0  GRX    /usr/home/mastodon/restart.sh
243
```
244 10 白林檎 美和
2. /usr/home/mastodon/restart.sh を作成。
245
``` shell
246 4 白林檎 美和
#!/bin/sh
247
service mastodon restart
248
```
249 10 白林檎 美和
3. newsyslogを再起動。
250
```
251 4 白林檎 美和
# service newsyslog restart
252
```