プロジェクト

全般

プロフィール

操作

Wiki » 履歴 » リビジョン 1

リビジョン 1/2 | 次 »
本間 紀史, 2026/07/11 19:24


Wiki

開発環境構築

Docker Desktop をインストール

アプリケーションの clone

作業用ディレクトリへ以下のコマンドでアプリケーションのリポジトリを clone します

% git clone [email protected]:init6/imuge.git

以下、作成された imge ディレクトリ内で実行します

Docker network の作成

サービス内のコンテナ同士を接続するネットワークを作成します

% docker network create imuge_network

コンテナのビルド

コンテナをビルドします

% docker-compose build

コンテナの起動

% docker-compose up -d

MinIO でバケットを作る

コンテナが起動したら以下の URL にブラウザでアクセスします
http://localhost:9001/buckets
ユーザー名: root
パスワード: password
「Create Bucket +」 ボタンを押下し、Bucket Name に 「imuge」 と入力し、他はデフォルトのまま「Create Bucket」を押下し、バケットを作成する

以上は最初に1回実行すればOKです
以下は開発時に適宜実行します

コンテナの起動:再掲

開発時にコンテナが停止している場合もあるので起動します

% docker-compose up -d

サーバーの起動

サーバーは手動で起動します。ターミナルのタブを新規に1つ起動し

% docker-compose exec web bundle exec rails s -p 0.0.0.0

Puma が起動したらブラウザからアクセス可能です
このターミナルにログが流れるので、エラーが出た時にはこちらのログを確認します

CSS の自動コンパイル

さらにターミナルのタブを新規に1つ起動し

% docker-compose exec app bin/rails tailwindcss:watch

を実行します。これを実行したままにすると CSS を修正して保存するタイミングで CSS が自動コンパイルされます

データベースのリセット

以下のコマンドを実行するとデータベースを作り直してテストデータを入れます

% docker-compose exec web bundle exec rake db:environment:set db:drop db:create db:migrate db:seed

リセット後は管理画面に

http://localhost:3000/administrators/sign_in
[email protected] / AQGt5TeL

でログインできます

参考

このような画面が出た時は「create database」や「Run pending migration 」を押下してください
開発側でデータベースに変更を加えたものをコンテナに反映します


imuge サーバー構築

本番サーバー

IPアドレス: 54.238.131.109

環境

Lightsail
https://lightsail.aws.amazon.com/ls/webapp/home/instances
https://lightsail.aws.amazon.com/ls/webapp/ap-northeast-1/instances/stg-techscore/networking

サイズ

$20/month

構築手順(コマンド)

ユーザー作成

インスタンス構築時に指定した公開鍵に対応する秘密鍵を用いて ubuntu ユーザーでログインして以下を実行

$ sudo su -
# useradd -u 10011 -d /home/norifumi -s /bin/bash -m norifumi -G admin
# mkdir /home/norifumi/.ssh
# echo 'ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAtob29I4kjea0AHaA03nE3RJzIQYkOu4rAjNOU+nAaaDYCMczQSsxAbarV4XUbqaXwSCf0vTZL6n9eQVcYTLA5GefzD+gXTxLvOY08a/FOk2GUz+UxrKq1wLbGKE34bS87DTqLc8ScXzS4aCOxjZzOk67fiS28GyejR8ir/Do/bDA+zeVdEzWLyxEmbJwlKRioUqocErpSlxP4zhwMoCT6zdrerXBTT+plukPWeMYUMZkXfeQiX1ufWtkn2jnuqKlhBfNdjXq/ReMA91WiDvf0Wc7OAmN0mqPv6W63fZZ0EP7E5VE1b6MCI2Ij4yYqG5j1E5L5skxElE2Um3XUbJfcQ== [email protected]' > /home/norifumi/.ssh/authorized_keys
# chown -R norifumi:norifumi /home/norifumi/.ssh
# chmod 700 -R /home/norifumi/.ssh
# chmod 600 -R /home/norifumi/.ssh/*
# sed -i '/^norifumi:.*$/d' /etc/shadow
# echo 'norifumi:$6$xbAF8nuJMDDGo.n4$VYMiRTn1gQoliNU8nniUjd8vqaOb9Vqn5RTWBk9.MOvetl0oHbl0N.2EQ2UJ7nTxotLrHLfP.wXQam2atDpO01:19142:0:99999:7:::' >> /etc/shadow

以降、上記で作成した norifumi ユーザーで作業を行う
ubuntu ユーザーをログアウトし、 norifumi ユーザーで ssh 経由でログインする

NTP設定

# apt update
# apt install -y ntp net-tools

アプリ用アカウント作成

# groupadd dev -g 2000
# adduser --gid 2000 --uid 2100 deploy

途中の質問は全て Enter を押下してデフォルト値を利用する

Adding user `deploy' ...
Adding new user `deploy' (2100) with group `dev' ...
Creating home directory `/home/deploy' ...
Copying files from `/etc/skel' ...
New password: 
Retype new password: 
No password supplied
New password: 
Retype new password: 
No password supplied
New password: 
Retype new password: 
No password supplied
passwd: Authentication token manipulation error
passwd: password unchanged
Try again? [y/N] 
Changing the user information for deploy
Enter the new value, or press ENTER for the default
    Full Name []: 
    Room Number []: 
    Work Phone []: 
    Home Phone []: 
    Other []: 
Is the information correct? [Y/n] 
# visudo

最後に1行追加

%dev ALL=(ALL) ALL

パッケージのインストール

# 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

DBサーバーのインストール・設定

# apt -y install mysql-server
# mysql -u root
mysql> CREATE USER 'imuge'@'%' IDENTIFIED WITH mysql_native_password BY 'Rm6USagH';
mysql> GRANT ALL PRIVILEGES ON imuge.* TO 'imuge'@'%';
mysql> FLUSH PRIVILEGES;

Ruby のインストール

# sudo su - deploy

以下、 deploy ユーザー

$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
$ echo 'export PATH="~/.rbenv/bin:$PATH"' >> ~/.bashrc
$ ~/.rbenv/bin/rbenv init >> ~/.bashrc
$ echo 'eval "$(rbenv init - bash)"' >> ~/.bashrc
$ source ~/.bashrc
$ git clone https://github.com/rbenv/ruby-build.git "$(rbenv root)"/plugins/ruby-build
$ rbenv install 3.2.2

アプリケーション用ディレクトリの作成

# mkdir -p /usr/local/rails_apps/imuge/shared/tmp/sockets
# mkdir -p /usr/local/rails_apps/imuge/shared/tmp/pids
# mkdir -p /usr/local/rails_apps/imuge/shared/config
# mkdir -p /usr/local/rails_apps/imuge/shared/log
# chown -R www-data:www-data /usr/local/rails_apps/
# chmod -R 2775 /usr/local/rails_apps/
# chown -R deploy /usr/local/rails_apps/imuge/

デプロイユーザーの調整

# sudo su - deploy

以下、 deploy ユーザー

$ ssh-keygen -C ''

入力は全てデフォルトで enter キーを押下していく

Generating public/private rsa key pair.
Enter file in which to save the key (/home/deploy/.ssh/id_rsa): 
Created directory '/home/deploy/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/deploy/.ssh/id_rsa
Your public key has been saved in /home/deploy/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:DWM0DygHrALw35aoXyUeKxrH/wfjOS+LfxGZ97kVWbc 
The key's randomart image is:
+---[RSA 3072]----+
|o  ... .+        |
|..  o o. +      o|
|. .. o  + .o    =|
|. .. o o ++ .  E |
| .  o * S .o . ..|
|   o o =o .   o .|
|  o + +. + .   o |
|   = + .= o   .  |
|  . . oo+B.      |
+----[SHA256]-----+

自分自身にSSHできるようにする

$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys

ソースコードを展開する

※事前に /home/deploy/.ssh/id_rsa.pub の公開鍵を git に登録してソースコードを取得できるようにする
以下、 deploy ユーザー

$ git clone [email protected]:init6/imuge.git /home/deploy/imuge
$ cd ~/imuge
$ vi config/master.key

以下の内容を記載して保存

a0aae92556305e91e9881d5384e98de6
$ vi /usr/local/rails_apps/imuge/shared/config/master.key

以下の内容を記載して保存

a0aae92556305e91e9881d5384e98de6
$ bundle

DBを用意する

以下、 deploy ユーザー

$ cd ~/imuge
$ IMUGE_DATABASE_PASSWORD=Rm6USagH RAILS_ENV=production bin/rails db:create db:migrate db:seed

試しに起動する

以下、 deploy ユーザー

$ cd ~/imuge
$ RAILS_ENV=production bin/rails assets:clean assets:precompile
$ RAILS_LOG_TO_STDOUT=1 RAILS_ENV=production bin/rails s
=> Booting Puma
=> Rails 7.0.5 application starting in production 
=> Run `bin/rails server --help` for more startup options
=> Booting Puma
=> Rails 7.0.4.3 application starting in production 
=> Run `bin/rails server --help` for more startup options
Puma starting in single mode...
* Puma version: 5.6.5 (ruby 3.2.2-p53) ("Birdie's Version")
*  Min threads: 5
*  Max threads: 5
*  Environment: production
*          PID: 22620
* Listening on http://0.0.0.0:3000
Use Ctrl-C to stop

ブラウザで http://[サーバーのIPアドレス]:3000/ にアクセス
ページが表示されればOK

sudoers の調整

# visudo

以下を最後に追記

deploy ALL=(ALL) NOPASSWD: /bin/mv /tmp/*.service /etc/systemd/system/
deploy ALL=(ALL) NOPASSWD: /bin/systemctl daemon-reload
deploy ALL=(ALL) NOPASSWD: /bin/systemctl enable *
deploy ALL=(ALL) NOPASSWD: /bin/systemctl restart *

 デプロイ

$ sudo su - deploy
$ cd ~/imuge
$ bundle exec cap production puma:systemd:config
$ bundle exec cap production puma:systemd:enable 
$ bundle exec cap production deploy

nginxの設定

# vi /etc/nginx/sites-available/imuge.conf

以下のように記載

upstream puma {
  server unix:///usr/local/rails_apps/imuge/shared/tmp/sockets/puma.sock;
}

server {
  server_name okinawan-eats.com;
  listen 80 default_server;
  root /usr/local/rails_apps/imuge/current/public;

  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  location @puma {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Forwarded-SSL on; 
    proxy_redirect off;
    proxy_pass http://puma;
  }

  try_files $uri/index.html $uri @puma;
  error_page 500 502 503 504 /500.html;
}
# ln -s /etc/nginx/sites-available/imuge.conf /etc/nginx/sites-enabled/
# rm /etc/nginx/sites-enabled/default
# chown deploy:dev /usr/local/rails_apps/imuge/shared/log/*
# sudo systemctl restart nginx

http://okinawan-eats.com/ にアクセス

SSLの設定

# apt install -y certbot python3-certbot-nginx
# certbot --nginx -d okinawan-eats.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): [email protected]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf. You must
agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: N
Account registered.
Requesting a certificate for okinawan-eats.com

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/okinawan-eats.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/okinawan-eats.com/privkey.pem
This certificate expires on 2023-09-04.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

Deploying certificate
Successfully deployed certificate for okinawan-eats.com to /etc/nginx/sites-enabled/imuge.conf
Congratulations! You have successfully enabled HTTPS on https://okinawan-eats.com

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
 * Donating to EFF:                    https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# systemctl restart nginx

https://okinawan-eats.com/ にアクセス

本間 紀史 さんが約2ヶ月前に更新 · 1件の履歴