Wiki » 履歴 » バージョン 1
本間 紀史, 2026/07/11 19:24
| 1 | 1 | 本間 紀史 | # Wiki |
|---|---|---|---|
| 2 | |||
| 3 | ## 開発環境構築 |
||
| 4 | |||
| 5 | #### Docker Desktop をインストール |
||
| 6 | |||
| 7 | #### アプリケーションの clone |
||
| 8 | |||
| 9 | 作業用ディレクトリへ以下のコマンドでアプリケーションのリポジトリを clone します |
||
| 10 | ``` |
||
| 11 | % git clone [email protected]:init6/imuge.git |
||
| 12 | ``` |
||
| 13 | 以下、作成された imge ディレクトリ内で実行します |
||
| 14 | |||
| 15 | #### Docker network の作成 |
||
| 16 | |||
| 17 | サービス内のコンテナ同士を接続するネットワークを作成します |
||
| 18 | ``` |
||
| 19 | % docker network create imuge_network |
||
| 20 | ``` |
||
| 21 | |||
| 22 | ### コンテナのビルド |
||
| 23 | |||
| 24 | コンテナをビルドします |
||
| 25 | |||
| 26 | ``` |
||
| 27 | % docker-compose build |
||
| 28 | ``` |
||
| 29 | |||
| 30 | ### コンテナの起動 |
||
| 31 | |||
| 32 | ``` |
||
| 33 | % docker-compose up -d |
||
| 34 | ``` |
||
| 35 | |||
| 36 | ### MinIO でバケットを作る |
||
| 37 | |||
| 38 | コンテナが起動したら以下の URL にブラウザでアクセスします |
||
| 39 | http://localhost:9001/buckets |
||
| 40 | ユーザー名: root |
||
| 41 | パスワード: password |
||
| 42 | 「Create Bucket +」 ボタンを押下し、Bucket Name に 「imuge」 と入力し、他はデフォルトのまま「Create Bucket」を押下し、バケットを作成する |
||
| 43 | |||
| 44 | 以上は最初に1回実行すればOKです |
||
| 45 | 以下は開発時に適宜実行します |
||
| 46 | |||
| 47 | ### コンテナの起動:再掲 |
||
| 48 | |||
| 49 | 開発時にコンテナが停止している場合もあるので起動します |
||
| 50 | ``` |
||
| 51 | % docker-compose up -d |
||
| 52 | ``` |
||
| 53 | |||
| 54 | ### サーバーの起動 |
||
| 55 | |||
| 56 | サーバーは手動で起動します。ターミナルのタブを新規に1つ起動し |
||
| 57 | ``` |
||
| 58 | % docker-compose exec web bundle exec rails s -p 0.0.0.0 |
||
| 59 | ``` |
||
| 60 | Puma が起動したらブラウザからアクセス可能です |
||
| 61 | このターミナルにログが流れるので、エラーが出た時にはこちらのログを確認します |
||
| 62 | |||
| 63 | ### CSS の自動コンパイル |
||
| 64 | |||
| 65 | さらにターミナルのタブを新規に1つ起動し |
||
| 66 | ``` |
||
| 67 | % docker-compose exec app bin/rails tailwindcss:watch |
||
| 68 | ``` |
||
| 69 | を実行します。これを実行したままにすると CSS を修正して保存するタイミングで CSS が自動コンパイルされます |
||
| 70 | |||
| 71 | ### データベースのリセット |
||
| 72 | |||
| 73 | 以下のコマンドを実行するとデータベースを作り直してテストデータを入れます |
||
| 74 | |||
| 75 | ``` |
||
| 76 | % docker-compose exec web bundle exec rake db:environment:set db:drop db:create db:migrate db:seed |
||
| 77 | ``` |
||
| 78 | |||
| 79 | リセット後は管理画面に |
||
| 80 | |||
| 81 | http://localhost:3000/administrators/sign_in |
||
| 82 | [email protected] / AQGt5TeL |
||
| 83 | |||
| 84 | でログインできます |
||
| 85 | |||
| 86 | ### 参考 |
||
| 87 | |||
| 88 | このような画面が出た時は「create database」や「Run pending migration 」を押下してください |
||
| 89 | 開発側でデータベースに変更を加えたものをコンテナに反映します |
||
| 90 | |||
| 91 |  |
||
| 92 |  |
||
| 93 | |||
| 94 | |||
| 95 | # imuge サーバー構築 |
||
| 96 | |||
| 97 | ## 本番サーバー |
||
| 98 | IPアドレス: 54.238.131.109 |
||
| 99 | |||
| 100 | ### 環境 |
||
| 101 | Lightsail |
||
| 102 | https://lightsail.aws.amazon.com/ls/webapp/home/instances |
||
| 103 | https://lightsail.aws.amazon.com/ls/webapp/ap-northeast-1/instances/stg-techscore/networking |
||
| 104 | |||
| 105 | ### サイズ |
||
| 106 | $20/month |
||
| 107 | |||
| 108 | ### 構築手順(コマンド) |
||
| 109 | |||
| 110 | #### ユーザー作成 |
||
| 111 | インスタンス構築時に指定した公開鍵に対応する秘密鍵を用いて ubuntu ユーザーでログインして以下を実行 |
||
| 112 | |||
| 113 | ``` shell |
||
| 114 | $ sudo su - |
||
| 115 | # useradd -u 10011 -d /home/norifumi -s /bin/bash -m norifumi -G admin |
||
| 116 | # mkdir /home/norifumi/.ssh |
||
| 117 | # echo 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAtob29I4kjea0AHaA03nE3RJzIQYkOu4rAjNOU+nAaaDYCMczQSsxAbarV4XUbqaXwSCf0vTZL6n9eQVcYTLA5GefzD+gXTxLvOY08a/FOk2GUz+UxrKq1wLbGKE34bS87DTqLc8ScXzS4aCOxjZzOk67fiS28GyejR8ir/Do/bDA+zeVdEzWLyxEmbJwlKRioUqocErpSlxP4zhwMoCT6zdrerXBTT+plukPWeMYUMZkXfeQiX1ufWtkn2jnuqKlhBfNdjXq/ReMA91WiDvf0Wc7OAmN0mqPv6W63fZZ0EP7E5VE1b6MCI2Ij4yYqG5j1E5L5skxElE2Um3XUbJfcQ== [email protected]' > /home/norifumi/.ssh/authorized_keys |
||
| 118 | # chown -R norifumi:norifumi /home/norifumi/.ssh |
||
| 119 | # chmod 700 -R /home/norifumi/.ssh |
||
| 120 | # chmod 600 -R /home/norifumi/.ssh/* |
||
| 121 | # sed -i '/^norifumi:.*$/d' /etc/shadow |
||
| 122 | # echo 'norifumi:$6$xbAF8nuJMDDGo.n4$VYMiRTn1gQoliNU8nniUjd8vqaOb9Vqn5RTWBk9.MOvetl0oHbl0N.2EQ2UJ7nTxotLrHLfP.wXQam2atDpO01:19142:0:99999:7:::' >> /etc/shadow |
||
| 123 | ``` |
||
| 124 | |||
| 125 | 以降、上記で作成した norifumi ユーザーで作業を行う |
||
| 126 | ubuntu ユーザーをログアウトし、 norifumi ユーザーで ssh 経由でログインする |
||
| 127 | |||
| 128 | #### NTP設定 |
||
| 129 | |||
| 130 | ```shell |
||
| 131 | # apt update |
||
| 132 | # apt install -y ntp net-tools |
||
| 133 | ``` |
||
| 134 | |||
| 135 | #### アプリ用アカウント作成 |
||
| 136 | |||
| 137 | ```shell |
||
| 138 | # groupadd dev -g 2000 |
||
| 139 | # adduser --gid 2000 --uid 2100 deploy |
||
| 140 | ``` |
||
| 141 | |||
| 142 | 途中の質問は全て Enter を押下してデフォルト値を利用する |
||
| 143 | ```shell |
||
| 144 | Adding user `deploy' ... |
||
| 145 | Adding new user `deploy' (2100) with group `dev' ... |
||
| 146 | Creating home directory `/home/deploy' ... |
||
| 147 | Copying files from `/etc/skel' ... |
||
| 148 | New password: |
||
| 149 | Retype new password: |
||
| 150 | No password supplied |
||
| 151 | New password: |
||
| 152 | Retype new password: |
||
| 153 | No password supplied |
||
| 154 | New password: |
||
| 155 | Retype new password: |
||
| 156 | No password supplied |
||
| 157 | passwd: Authentication token manipulation error |
||
| 158 | passwd: password unchanged |
||
| 159 | Try again? [y/N] |
||
| 160 | Changing the user information for deploy |
||
| 161 | Enter the new value, or press ENTER for the default |
||
| 162 | Full Name []: |
||
| 163 | Room Number []: |
||
| 164 | Work Phone []: |
||
| 165 | Home Phone []: |
||
| 166 | Other []: |
||
| 167 | Is the information correct? [Y/n] |
||
| 168 | ``` |
||
| 169 | |||
| 170 | ```shell |
||
| 171 | # visudo |
||
| 172 | ``` |
||
| 173 | 最後に1行追加 |
||
| 174 | ```shell |
||
| 175 | %dev ALL=(ALL) ALL |
||
| 176 | ``` |
||
| 177 | |||
| 178 | #### パッケージのインストール |
||
| 179 | ```shell |
||
| 180 | # apt -y install patch curl build-essential openssl libreadline-dev libreadline-dev git zlib1g zlib1g-dev libssl-dev libyaml-dev libxml2-dev libxslt1-dev autoconf libc6-dev libncurses-dev automake libtool bison subversion libmysqlclient-dev nginx monit nodejs fonts-takao-mincho fonts-takao |
||
| 181 | ``` |
||
| 182 | |||
| 183 | #### DBサーバーのインストール・設定 |
||
| 184 | ```shell |
||
| 185 | # apt -y install mysql-server |
||
| 186 | # mysql -u root |
||
| 187 | ``` |
||
| 188 | |||
| 189 | ```sql |
||
| 190 | mysql> CREATE USER 'imuge'@'%' IDENTIFIED WITH mysql_native_password BY 'Rm6USagH'; |
||
| 191 | mysql> GRANT ALL PRIVILEGES ON imuge.* TO 'imuge'@'%'; |
||
| 192 | mysql> FLUSH PRIVILEGES; |
||
| 193 | ``` |
||
| 194 | |||
| 195 | #### Ruby のインストール |
||
| 196 | ```shell |
||
| 197 | # sudo su - deploy |
||
| 198 | ``` |
||
| 199 | 以下、 deploy ユーザー |
||
| 200 | ```shell |
||
| 201 | $ git clone https://github.com/rbenv/rbenv.git ~/.rbenv |
||
| 202 | $ echo 'export PATH="~/.rbenv/bin:$PATH"' >> ~/.bashrc |
||
| 203 | $ ~/.rbenv/bin/rbenv init >> ~/.bashrc |
||
| 204 | $ echo 'eval "$(rbenv init - bash)"' >> ~/.bashrc |
||
| 205 | $ source ~/.bashrc |
||
| 206 | $ git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build |
||
| 207 | $ rbenv install 3.2.2 |
||
| 208 | ``` |
||
| 209 | |||
| 210 | #### アプリケーション用ディレクトリの作成 |
||
| 211 | ```shell |
||
| 212 | # mkdir -p /usr/local/rails_apps/imuge/shared/tmp/sockets |
||
| 213 | # mkdir -p /usr/local/rails_apps/imuge/shared/tmp/pids |
||
| 214 | # mkdir -p /usr/local/rails_apps/imuge/shared/config |
||
| 215 | # mkdir -p /usr/local/rails_apps/imuge/shared/log |
||
| 216 | # chown -R www-data:www-data /usr/local/rails_apps/ |
||
| 217 | # chmod -R 2775 /usr/local/rails_apps/ |
||
| 218 | # chown -R deploy /usr/local/rails_apps/imuge/ |
||
| 219 | ``` |
||
| 220 | |||
| 221 | #### デプロイユーザーの調整 |
||
| 222 | ```shell |
||
| 223 | # sudo su - deploy |
||
| 224 | ``` |
||
| 225 | 以下、 deploy ユーザー |
||
| 226 | ```shell |
||
| 227 | $ ssh-keygen -C '' |
||
| 228 | ``` |
||
| 229 | 入力は全てデフォルトで enter キーを押下していく |
||
| 230 | ```shell |
||
| 231 | Generating public/private rsa key pair. |
||
| 232 | Enter file in which to save the key (/home/deploy/.ssh/id_rsa): |
||
| 233 | Created directory '/home/deploy/.ssh'. |
||
| 234 | Enter passphrase (empty for no passphrase): |
||
| 235 | Enter same passphrase again: |
||
| 236 | Your identification has been saved in /home/deploy/.ssh/id_rsa |
||
| 237 | Your public key has been saved in /home/deploy/.ssh/id_rsa.pub |
||
| 238 | The key fingerprint is: |
||
| 239 | SHA256:DWM0DygHrALw35aoXyUeKxrH/wfjOS+LfxGZ97kVWbc |
||
| 240 | The key's randomart image is: |
||
| 241 | +---[RSA 3072]----+ |
||
| 242 | |o ... .+ | |
||
| 243 | |.. o o. + o| |
||
| 244 | |. .. o + .o =| |
||
| 245 | |. .. o o ++ . E | |
||
| 246 | | . o * S .o . ..| |
||
| 247 | | o o =o . o .| |
||
| 248 | | o + +. + . o | |
||
| 249 | | = + .= o . | |
||
| 250 | | . . oo+B. | |
||
| 251 | +----[SHA256]-----+ |
||
| 252 | ``` |
||
| 253 | 自分自身にSSHできるようにする |
||
| 254 | ```shell |
||
| 255 | $ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys |
||
| 256 | ``` |
||
| 257 | |||
| 258 | #### ソースコードを展開する |
||
| 259 | ※事前に /home/deploy/.ssh/id_rsa.pub の公開鍵を git に登録してソースコードを取得できるようにする |
||
| 260 | 以下、 deploy ユーザー |
||
| 261 | ```shell |
||
| 262 | $ git clone [email protected]:init6/imuge.git /home/deploy/imuge |
||
| 263 | $ cd ~/imuge |
||
| 264 | $ vi config/master.key |
||
| 265 | ``` |
||
| 266 | 以下の内容を記載して保存 |
||
| 267 | ```shell |
||
| 268 | a0aae92556305e91e9881d5384e98de6 |
||
| 269 | ``` |
||
| 270 | ```shell |
||
| 271 | $ vi /usr/local/rails_apps/imuge/shared/config/master.key |
||
| 272 | ``` |
||
| 273 | 以下の内容を記載して保存 |
||
| 274 | ```shell |
||
| 275 | a0aae92556305e91e9881d5384e98de6 |
||
| 276 | ``` |
||
| 277 | ```shell |
||
| 278 | $ bundle |
||
| 279 | ``` |
||
| 280 | |||
| 281 | #### DBを用意する |
||
| 282 | 以下、 deploy ユーザー |
||
| 283 | ```shell |
||
| 284 | $ cd ~/imuge |
||
| 285 | $ IMUGE_DATABASE_PASSWORD=Rm6USagH RAILS_ENV=production bin/rails db:create db:migrate db:seed |
||
| 286 | ``` |
||
| 287 | |||
| 288 | #### 試しに起動する |
||
| 289 | 以下、 deploy ユーザー |
||
| 290 | ```shell |
||
| 291 | $ cd ~/imuge |
||
| 292 | $ RAILS_ENV=production bin/rails assets:clean assets:precompile |
||
| 293 | $ RAILS_LOG_TO_STDOUT=1 RAILS_ENV=production bin/rails s |
||
| 294 | ``` |
||
| 295 | ```shell |
||
| 296 | => Booting Puma |
||
| 297 | => Rails 7.0.5 application starting in production |
||
| 298 | => Run `bin/rails server --help` for more startup options |
||
| 299 | => Booting Puma |
||
| 300 | => Rails 7.0.4.3 application starting in production |
||
| 301 | => Run `bin/rails server --help` for more startup options |
||
| 302 | Puma starting in single mode... |
||
| 303 | * Puma version: 5.6.5 (ruby 3.2.2-p53) ("Birdie's Version") |
||
| 304 | * Min threads: 5 |
||
| 305 | * Max threads: 5 |
||
| 306 | * Environment: production |
||
| 307 | * PID: 22620 |
||
| 308 | * Listening on http://0.0.0.0:3000 |
||
| 309 | Use Ctrl-C to stop |
||
| 310 | ``` |
||
| 311 | ブラウザで `http://[サーバーのIPアドレス]:3000/` にアクセス |
||
| 312 | ページが表示されればOK |
||
| 313 | |||
| 314 | #### sudoers の調整 |
||
| 315 | ```shell |
||
| 316 | # visudo |
||
| 317 | ``` |
||
| 318 | 以下を最後に追記 |
||
| 319 | ```shell |
||
| 320 | deploy ALL=(ALL) NOPASSWD: /bin/mv /tmp/*.service /etc/systemd/system/ |
||
| 321 | deploy ALL=(ALL) NOPASSWD: /bin/systemctl daemon-reload |
||
| 322 | deploy ALL=(ALL) NOPASSWD: /bin/systemctl enable * |
||
| 323 | deploy ALL=(ALL) NOPASSWD: /bin/systemctl restart * |
||
| 324 | ``` |
||
| 325 | |||
| 326 | #### デプロイ |
||
| 327 | ```shell |
||
| 328 | $ sudo su - deploy |
||
| 329 | $ cd ~/imuge |
||
| 330 | $ bundle exec cap production puma:systemd:config |
||
| 331 | $ bundle exec cap production puma:systemd:enable |
||
| 332 | $ bundle exec cap production deploy |
||
| 333 | ``` |
||
| 334 | |||
| 335 | #### nginxの設定 |
||
| 336 | ```shell |
||
| 337 | # vi /etc/nginx/sites-available/imuge.conf |
||
| 338 | ``` |
||
| 339 | 以下のように記載 |
||
| 340 | ```shell |
||
| 341 | upstream puma { |
||
| 342 | server unix:///usr/local/rails_apps/imuge/shared/tmp/sockets/puma.sock; |
||
| 343 | } |
||
| 344 | |||
| 345 | server { |
||
| 346 | server_name okinawan-eats.com; |
||
| 347 | listen 80 default_server; |
||
| 348 | root /usr/local/rails_apps/imuge/current/public; |
||
| 349 | |||
| 350 | location ^~ /assets/ { |
||
| 351 | gzip_static on; |
||
| 352 | expires max; |
||
| 353 | add_header Cache-Control public; |
||
| 354 | } |
||
| 355 | |||
| 356 | location @puma { |
||
| 357 | proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
||
| 358 | proxy_set_header Host $http_host; |
||
| 359 | proxy_set_header X-Forwarded-SSL on; |
||
| 360 | proxy_redirect off; |
||
| 361 | proxy_pass http://puma; |
||
| 362 | } |
||
| 363 | |||
| 364 | try_files $uri/index.html $uri @puma; |
||
| 365 | error_page 500 502 503 504 /500.html; |
||
| 366 | } |
||
| 367 | ``` |
||
| 368 | |||
| 369 | ```shelll |
||
| 370 | # ln -s /etc/nginx/sites-available/imuge.conf /etc/nginx/sites-enabled/ |
||
| 371 | # rm /etc/nginx/sites-enabled/default |
||
| 372 | # chown deploy:dev /usr/local/rails_apps/imuge/shared/log/* |
||
| 373 | # sudo systemctl restart nginx |
||
| 374 | ``` |
||
| 375 | |||
| 376 | http://okinawan-eats.com/ にアクセス |
||
| 377 | |||
| 378 | |||
| 379 | #### SSLの設定 |
||
| 380 | ```shell |
||
| 381 | # apt install -y certbot python3-certbot-nginx |
||
| 382 | ``` |
||
| 383 | ```shell |
||
| 384 | # certbot --nginx -d okinawan-eats.com |
||
| 385 | ``` |
||
| 386 | ``` |
||
| 387 | Saving debug log to /var/log/letsencrypt/letsencrypt.log |
||
| 388 | Plugins selected: Authenticator nginx, Installer nginx |
||
| 389 | Enter email address (used for urgent renewal and security notices) (Enter 'c' to |
||
| 390 | cancel): [email protected] |
||
| 391 | |||
| 392 | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
||
| 393 | Please read the Terms of Service at |
||
| 394 | https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf. You must |
||
| 395 | agree in order to register with the ACME server. Do you agree? |
||
| 396 | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
||
| 397 | (Y)es/(N)o: Y |
||
| 398 | |||
| 399 | |||
| 400 | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
||
| 401 | Would you be willing, once your first certificate is successfully issued, to |
||
| 402 | share your email address with the Electronic Frontier Foundation, a founding |
||
| 403 | partner of the Let's Encrypt project and the non-profit organization that |
||
| 404 | develops Certbot? We'd like to send you email about our work encrypting the web, |
||
| 405 | EFF news, campaigns, and ways to support digital freedom. |
||
| 406 | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
||
| 407 | (Y)es/(N)o: N |
||
| 408 | Account registered. |
||
| 409 | Requesting a certificate for okinawan-eats.com |
||
| 410 | |||
| 411 | Successfully received certificate. |
||
| 412 | Certificate is saved at: /etc/letsencrypt/live/okinawan-eats.com/fullchain.pem |
||
| 413 | Key is saved at: /etc/letsencrypt/live/okinawan-eats.com/privkey.pem |
||
| 414 | This certificate expires on 2023-09-04. |
||
| 415 | These files will be updated when the certificate renews. |
||
| 416 | Certbot has set up a scheduled task to automatically renew this certificate in the background. |
||
| 417 | |||
| 418 | Deploying certificate |
||
| 419 | Successfully deployed certificate for okinawan-eats.com to /etc/nginx/sites-enabled/imuge.conf |
||
| 420 | Congratulations! You have successfully enabled HTTPS on https://okinawan-eats.com |
||
| 421 | |||
| 422 | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
||
| 423 | If you like Certbot, please consider supporting our work by: |
||
| 424 | * Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate |
||
| 425 | * Donating to EFF: https://eff.org/donate-le |
||
| 426 | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - |
||
| 427 | ``` |
||
| 428 | |||
| 429 | ``` |
||
| 430 | # systemctl restart nginx |
||
| 431 | ``` |
||
| 432 | |||
| 433 | https://okinawan-eats.com/ にアクセス |