kakudooo docs

環境

作業の流れ

リポジトリ

以下を参照する

https://github.com/kakudou3/private-projects

VPS に PostgreSQL をインストールする

インストールとサーバーの起動

sudo apt-get update
sudo apt-get install postgresql
dpkg -l | grep postgresql
systemctl status postgresql.service
sudo -u postgres psql -c "SELECT version();"

ルートユーザーでログイン

sudo -u postgres psql

ユーザーの作成とロールの付与

create user budgiii with password 'password'
alter role budgiii with login;
alter role budgiii with CREATEDB;
\du

デフォルトの DB(postgres)にログインする

postgresDBにログインして、ユーザーが正しく作成されているかを確認する。

psql -U budgiii postgres

PostgreSQLのセキュリティ周りの設定

緩いが、設定した時の例を貼っておく。

sudo vi /etc/postgresql/16/main/postgresql.conf
listen_addresses = "*"
sudo vi /etc/postgresql/16/main/pg_hba.conf
# "local" is for Unix domain socket connections only
local   all             all                                     password
host    all             all             0.0.0.0/0               scram-sha-256
# DO NOT DISABLE!
# If you change this first entry you will need to make sure that the
# database superuser can access the database using some other method.
# Noninteractive access to all databases is required during automatic
# maintenance (custom daily cronjobs, replication, and similar tasks).
#
# Database administrative login by Unix domain socket
local   all             postgres                                peer

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     password
# IPv4 local connections:
host    all             all             127.0.0.1/32            scram-sha-256
# IPv6 local connections:
host    all             all             ::1/128                 scram-sha-256
# Allow replication connections from localhost, by a user with the
# replication privilege.
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            scram-sha-256
host    replication     all             ::1/128                 scram-sha-256
host    all             all             0.0.0.0/0               scram-sha-256

DB サーバーを再起動する

sudo service postgresql restart

https://zenn.dev/hdmt/articles/8b242a8c78f7f1

Docker のインストール

# Add Docker's official GPG key:
sudo apt update
sudo apt install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
sudo tee /etc/apt/sources.list.d/docker.sources <<EOF
Types: deb
URIs: https://download.docker.com/linux/ubuntu
Suites: $(. /etc/os-release && echo "${UBUNTU_CODENAME:-$VERSION_CODENAME}")
Components: stable
Signed-By: /etc/apt/keyrings/docker.asc
EOF

sudo apt update

sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

sudo systemctl status docker

sudo systemctl start docker

https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository

デプロイ

Rails アプリケーションの設定

credentialsファイルの作成(ローカル)

EDITOR="vi" bin/rails credentials:edit

ホストマシンの Private IP アドレスを確認する

vpsにログイン

ip -4 addr show scope global | awk '/inet/ {print $2}' | cut -d/ -f1 | grep -E '^(10\.|192\.168\.|172\.(1[6-9]|2[0-9]|3[0-1])\.)' | head -n1

DB_HOST をホストマシンの Private IP に設定する

/config/deploy.yml に以下を追記する

env:
  clear:
    DB_HOST: ホストマシンのPrivate IP

migration

ローカルからデプロイ対象の環境にmigrationを実行したい場合

kamal app exec --primary -i './bin/rails db:create'
kamal app exec --primary -i './bin/rails db:migrate'

https://www.reddit.com/r/rails/comments/1dle5xj/kamal_and_database_migrations/

アプリケーションへのアクセス

デプロイ

git commit -m "変更内容をコミットしておく"
kamal deploy

ログの確認

kamal logs

kamal proxy