0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

未経験エンジニアがRails 8アプリをAWS EC2へデプロイして学んだこと

0
Posted at

はじめに

私はRails学習のアウトプットとして、TaskLinkというToDo管理アプリを作成しました。

CRUD実装だけでなく、実務を意識してAWS EC2へデプロイし、本番運用環境を構築しました。

この記事では、デプロイ時に行った構成や学んだことをまとめます。

作成したアプリ

TaskLink
Turbo Streamによるリアルタイム更新
Devise認証
Pundit認可
PostgreSQL
Docker
GitHub Actions

本番URL:

GitHub:

構成図

GitHub Actions

EC2

Nginx

Puma

Rails 8

PostgreSQL

使用技術

項目 内容
Ruby 3.3
Rails 8
DB PostgreSQL
Web Server Nginx
App Server Puma
Infra AWS EC2
CI/CD GitHub Actions
EC2構築手順
EC2インスタンス作成

Ubuntu Serverを選択

セキュリティグループで

22
80
443

を開放

SSH接続
ssh -i ~/.ssh/tasklink-key.pem ubuntu@xxx.xxx.xxx.xxx
Rubyインストール

rbenvを利用

rbenv install 3.3.x
PostgreSQL設定
sudo apt install postgresql

データベース作成

rails db:create
rails db:migrate
Puma設定

systemdで管理

sudo systemctl enable tasklink
sudo systemctl start tasklink
Nginx設定

リバースプロキシとして利用

location / {
proxy_pass http://unix:/home/ubuntu/tasklink/tmp/sockets/puma.sock;
}
HTTPS対応

Let's Encrypt

sudo certbot --nginx
GitHub Actionsによる自動デプロイ

mainブランチへpushすると

bundle install
rails db:migrate
assets:precompile
systemctl restart tasklink

を実行する構成にしました。

苦労した点

Pumaが起動しない

systemd設定ミスで

sudo systemctl status tasklink

が失敗していました。

ログを確認しながら原因を特定しました。

Nginxの502エラー

Pumaソケットのパスが誤っており、

NginxからRailsへ接続できませんでした。

Team機能追加時のDB移行

後からTeam機能を追加した際、

既存ユーザーの

team_id

が未設定になり、

Team must exist

エラーが発生しました。

Rails consoleで確認し、

既存データを移行して解決しました。

学んだこと

ローカルで動くことと、本番環境で安定稼働することは全く別だと感じました。

特に

Linux操作
Nginx
Puma
PostgreSQL
systemd

を実際に触れたことは大きな学びでした。

また、エラー発生時にログやRails consoleを使って原因を切り分ける重要性も学びました。

おわりに

今後は

コメント機能
通知機能

などを追加しながら改善していきたいと考えています。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?