@@ -9,7 +9,7 @@ Laravelはvendor/に数千〜数万のファイルがあり、それを頻繁に
99
1010・Laravel API バックエンドプロジェクト
1111・ローカル環境 Windows 11
12- - バックエンド(API、DB)はWSL2 (Ubuntu) + Laravel Sail
12+ - バックエンド(API、DB)はWSL2 (Ubuntu) + Laravel Sail(512 MB RAM、2 vCPU、20 GB SSD)
1313 - プロジェクト配置 ` /home/wida/dev/laravel-rds ` (WSL2 Ubuntu内)
1414 Windows ファイルシステムとの変換オーバーヘッドを回避し高速化するため
1515 - フロントエンドはVueをnpm run devで開発サーバーを使用
@@ -676,6 +676,208 @@ http://localhost/telescopeでブラウザで確認できる
676676
677677---
678678
679+ ### Lightsail に MySQL 統合
680+
681+ **Lightsail メモリにアップグレードするか**
682+
683+ 現状のメモリの使用状況
684+ ```bash
685+ ubuntu@ip-172-26-6-105:~$ free -h
686+ total used free shared buff/cache available
687+ Mem: 416Mi 230Mi 24Mi 5.2Mi 200Mi 186Mi
688+ Swap: 0B 0B 0B
689+ ubuntu@ip-172-26-6-105:~$
690+ ```
691+
692+ 186程余裕ありMySQLで120程使用すると考えると60MBほど余裕はある
693+
694+ ただ今後追加機能で使用するメモリ増える気もするので1GB にアップグレードします
695+
696+ インスタンス停止
697+ スナップショットを作成
698+ 作成したスナップショットの 「︙」メニュー をクリック
699+ 「新しいインスタンスを作成」 を選択
700+ $7 プラン を選択
701+
702+ インスタンス一覧で 古い laravel-api(停止中)をクリック
703+ 「ネットワーキング」 タブをクリック
704+ 静的IP(54.178.81.51)の横にある 「デタッチ」 をクリック
705+
706+ ファイアウォールルールでHTTPSを追加
707+
708+ #### MySQL のインストール
709+
710+ ```bash
711+ # MySQL サーバーインストール
712+ sudo apt update
713+ sudo apt install -y mysql-server
714+
715+ # MySQL の起動と自動起動設定
716+ sudo systemctl start mysql
717+ sudo systemctl enable mysql
718+
719+ # 状態確認
720+ sudo systemctl status mysql
721+ ```
722+
723+ MySQL セキュリティ設定
724+ MySQL のセキュリティを強化するための対話式ツール
725+
726+ ```bash
727+ sudo mysql_secure_installation
728+
729+ Press y|Y for Yes, any other key for No:
730+
731+ Skipping password set for root as authentication with auth_socket is used by default.
732+ If you would like to use password authentication instead, this can be done with the "ALTER_USER" command.
733+ See https://dev.mysql.com/doc/refman/8.0/en/alter-user.html#alter-user-password-management for more information.
734+
735+ By default, a MySQL installation has an anonymous user,
736+ allowing anyone to log into MySQL without having to have
737+ a user account created for them. This is intended only for
738+ testing, and to make the installation go a bit smoother.
739+ You should remove them before moving into a production
740+ environment.
741+
742+ Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
743+ Success.
744+
745+
746+ Normally, root should only be allowed to connect from
747+ ' localhost' . This ensures that someone cannot guess at
748+ the root password from the network.
749+
750+ Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
751+ Success.
752+
753+ By default, MySQL comes with a database named ' test' that
754+ anyone can access. This is also intended only for testing,
755+ and should be removed before moving into a production
756+ environment.
757+
758+
759+ Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
760+ - Dropping test database...
761+ Success.
762+
763+ - Removing privileges on test database...
764+ Success.
765+
766+ Reloading the privilege tables will ensure that all changes
767+ made so far will take effect immediately.
768+
769+ Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
770+ Success.
771+
772+ All done!
773+ ubuntu@ip-172-26-2-247:/var/www/laravel$
774+ ```
775+
776+ ##### データベースとユーザーの作成
777+
778+ ```sql
779+ sudo mysql
780+
781+ CREATE DATABASE laravel_production CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
782+ CREATE USER ' laravel_user' @' localhost' IDENTIFIED BY ' 強力なパスワード' ;
783+ GRANT ALL PRIVILEGES ON laravel_production.* TO ' laravel_user' @' localhost' ;
784+ FLUSH PRIVILEGES;
785+ ```
786+
787+ RDS からデータをエクスポート
788+ ```bash
789+ ubuntu@ip-172-26-2-247:/var/www/laravel$ mysqldump -h laravel-rds-db.c1ewmsukaqko.ap-northeast-1.rds.amazonaws.com -u admin -p laravel_production > /tmp/rds_backup.sql
790+ Enter password:
791+ Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database. If you don' t want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events.
792+ Warning: A dump from a server that has GTIDs enabled will by default include the GTIDs of all transactions, even those that were executed during its extraction and might not be represented in the dumped data. This might result in an inconsistent data dump.
793+ In order to ensure a consistent backup of the database, pass --single-transaction or --lock-all-tables or --master-data.
794+ -- Warning: column statistics not supported by the server.
795+ ubuntu@ip-172-26-2-247:/var/www/laravel$
796+
797+ # インポート
798+ ubuntu@ip-172-26-2-247:/var/www/laravel$ sudo mysql laravel_production < /tmp/rds_backup.sql
799+ # 確認
800+ ubuntu@ip-172-26-2-247:/var/www/laravel$ sudo mysql laravel_production -e " SHOW TABLES;"
801+ +------------------------------+
802+ | Tables_in_laravel_production |
803+ +------------------------------+
804+ | cache |
805+ | cache_locks |
806+ | failed_jobs |
807+ | job_batches |
808+ | jobs |
809+ | migrations |
810+ | password_reset_tokens |
811+ | personal_access_tokens |
812+ | posts |
813+ | role_user |
814+ | roles |
815+ | sessions |
816+ | telescope_entries |
817+ | telescope_entries_tags |
818+ | telescope_monitoring |
819+ | users |
820+ +------------------------------+
821+ ubuntu@ip-172-26-2-247:/var/www/laravel$
822+ ` ` `
823+ .envファイルを編集
824+ DB_HOST: RDS → 127.0.0.1
825+ DB_USERNAME: admin → laravel_user
826+ DB_PASSWORD: RDS パスワード → 新パスワード
827+
828+ ` ` ` bash
829+ # 1. プロジェクト全体の所有者を www-data に変更
830+ sudo chown -R www-data:www-data /var/www/laravel
831+
832+ # 2. ubuntu ユーザーを www-data グループに追加
833+ sudo usermod -a -G www-data ubuntu
834+
835+ # 3. 権限設定
836+ sudo chmod -R 755 /var/www/laravel
837+ sudo chmod -R 775 /var/www/laravel/storage
838+ sudo chmod -R 775 /var/www/laravel/bootstrap/cache
839+
840+ # 4. SSH ログアウト・再ログイン(グループ変更を反映)
841+ exit
842+ ssh ubuntu@54.178.81.51
843+
844+ # 5. Laravel キャッシュ作成
845+ cd /var/www/laravel
846+ php artisan config:cache
847+
848+ # 6. データベース接続確認
849+ php artisan migrate:status
850+
851+ # 7. PHP-FPM 再起動
852+ sudo systemctl restart php8.3-fpm
853+ ` ` `
854+
855+ MySQLのバージョンは
856+ ` ` ` bash
857+ ubuntu@ip-172-26-2-247:/var/www/laravel$ mysql --version
858+ mysql Ver 8.0.44-0ubuntu0.24.04.1 for Linux on x86_64 (( Ubuntu))
859+ ` ` `
860+
861+ # ### 旧 Lightsail インスタンスの削除
862+
863+ Lightsail コンソールを開く
864+ 古い laravel-api(停止中)を選択
865+ 「︙」メニュー → 「削除」
866+
867+ # ### RDS インスタンスの削除
868+ AWS RDS コンソールを開く
869+ laravel-rds-db を選択
870+ 「アクション」→ 「削除」
871+ 最終スナップショットを作成 → スナップショット名: laravel-rds-final-backup-20251123
872+ ☑ 自動バックアップを保持 → チェックを外す
873+ delete me と入力して削除確認
874+
875+ RDS コンソール → 「自動バックアップ」タブで
876+ laravel-rds-db の保持されたバックアップを選択
877+ 「アクション」 → 「削除」
878+
879+ システムスナップショットが後程削除されるのを確認
880+
679881# # プロジェクト方針と戦略策定
680882
6818831. ** ポートフォリオとしての活用**
@@ -703,6 +905,10 @@ http://localhost/telescopeでブラウザで確認できる
703905 - 採用担当者が最も関心を持つ「技術的課題へのアプローチ」を可視化できる。
704906 - エンジニア向け広告は単価(RPM)が高い傾向にあり、少ないPVでも収益化の可能性がある。
705907
908+
909+
910+
911+
706912# ## 「ツールやサービスをプロダクトとして組み込む」
707913
708914- OGP/サムネイル画像メーカー
@@ -717,8 +923,6 @@ Intervention Image ライブラリを使った画像処理
717923
718924# # TODO
719925
720- - **ユーザー登録機能の追加**: 現在はGoogle認証でのみ自動登録されるため、メール・パスワードでの新規登録機能(`register`メソッド)を`AuthController`に追加する。これにより、Googleアカウントを持たないユーザーも登録可能になる。
721- - **認証方法の統一**: メール・パスワード登録とGoogle認証の両方をサポートし、ユーザーが選択できるようにする。既存ユーザーが後からGoogle認証を紐付けたり、Google認証ユーザーがパスワードを設定できるようにする。
722926
723927
724928# ## APIのエラーハンドリングとレスポンス形式
0 commit comments