EC2にMariaDB & CakePHP環境を作成

# EC2にMySQL & CakePHP環境を作成
:::note warn
EC2を構築済みであり、SSHで接続できることが前提
:::
## EC2へSSH接続
1\. EC2へSSH接続
2\. roo権限にする
```
$ sudo su -
```
## yumのアップデート
1\. yumのアップデート
```
$ yum update -y
```
## Apacheのインストール
1\. Apacheのインストール
```
$ yum -y install httpd
```
2\. Apacheのバージョン確認
```
$ httpd -v
Server version: Apache/2.4.54 ()
Server built:   Jun 30 2022 11:02:23
```
3\. Apacheの起動・自動起動設定
```
$ systemctl start httpd.service
$ systemctl status httpd.service
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
  Drop-In: /usr/lib/systemd/system/httpd.service.d
           └─php-fpm.conf
   Active: active (running) since 水 2023-02-08 03:22:02 UTC; 33min ago
     Docs: man:httpd.service(8)
 Main PID: 3543 (httpd)
   Status: "Total requests: 4; Idle/Busy workers 100/0;Requests/sec: 0.00197; Bytes served/sec:   5 B/sec"
   CGroup: /system.slice/httpd.service
           ├─3543 /usr/sbin/httpd -DFOREGROUND
           ├─3544 /usr/sbin/httpd -DFOREGROUND
           ├─3545 /usr/sbin/httpd -DFOREGROUND
           ├─3546 /usr/sbin/httpd -DFOREGROUND
           ├─3547 /usr/sbin/httpd -DFOREGROUND
           ├─3548 /usr/sbin/httpd -DFOREGROUND
           └─3714 /usr/sbin/httpd -DFOREGROUND

 2月 08 03:22:02 ip-10-0-3-129.ap-northeast-1.compute.internal systemd[1]: Starting The Apache HTTP Server...
 2月 08 03:22:02 ip-10-0-3-129.ap-northeast-1.compute.internal systemd[1]: Started The Apache HTTP Server.
$ systemctl enable httpd.service
$ systemctl is-enabled httpd.service
enabled
```
## PHPのインストール
1\. インストールできるバージョンを確認
```
$ yum list php
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Available Packages
php.x86_64                                                                                                                   5.4.16-46.amzn2.0.2                                                                                                                    
```
2\. PHPをインストール
```
$ yum install php
```
3\. PHPのバージョンを確認
```
$ php -v
PHP 5.4.16 (cli) (built: Oct 31 2019 18:34:05) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
```
4\. Apacheを再起動し、PHPを有効化する
```
$ systemctl restart httpd
```
## タイムゾーンの設定
1\. php.iniの変更
```
$ vim /etc/php.ini
;date.timezone =
↓
date.timezone = Asia/Tokyo
```
2\. 以下を実行
```
$ timedatectl set-timezone Asia/Tokyo
```
5\. Apacheを再起動
```
$ systemctl restart httpd
```
## MariaDBのインストール
1\. インストール
```
$ yum install -y mariadb-server
```
2\.DBを起動
```
$ systemctl start mariadb
```
3\. 自動起動を有効化および確認
```
$ systemctl enable mariadb
$ systemctl is-enabled mariadb
enabled
```
4\. セキュリティー設定
```
$ mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] n
 ... skipping.

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n
 ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
```
5\. PDOをインストール
```
$ yum install php-pdo
$ yum -y install php-mysql
```
## CakePHPのインストール
1\. ドキュメントルートの権限を設定
```
$ chown -R ec2-user:apache /var/www
$ chmod -R 777 /var/www/html/
$ chmod 2775 /var/www
$ find /var/www -type d -exec sudo chmod 2775 {} \;
$ find /var/www -type f -exec sudo chmod 0664 {} \;
```
2\. 必要なライブラリをインストール
```
$ sudo yum install -y php-intl
$ sudo yum install -y php-mbstring
```
3\. Composerのインストール
```
$ cd ~/
$ curl -sS https://getcomposer.org/installer | sudo php
All settings correct for using Composer
Downloading...

Composer (version 2.2.19) successfully installed to: /home/ec2-user/composer.phar
Use it: php composer.phar

$ sudo mv composer.phar /usr/local/bin/composer
$ sudo ln -s /usr/local/bin/composer /usr/bin/composer
$ composer -V
Composer version 2.2.19 2023-02-04 14:54:48
```
4\. CakePHPプロジェクトの作成
```
$ cd /var/www/html
$ composer create-project --prefer-dist cakephp/app test←プロジェクト名
$ sudo vim /etc/httpd/conf.d/custom.conf
以下を記載
DocumentRoot "/var/www/html/test"

$ sudo service httpd restart
$ cd test/
$ cp config/app.default.php config/app.php
```
5\. ブラウザで表示
→以下エラーが発生

:::note alert
Error: The application is trying to load a file from the Migrations plugin.

Make sure your plugin Migrations is in the /var/www/html/test/plugins/ directory and was loaded.
:::
![スクリーンショット 2023-02-09 14.38.55.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3127481/5f072963-b4e3-7b83-9efe-3e8da530a00e.png)

エラー箇所のソースコードをコメントアウトすることで画面表示できた
```
$ vim config/bootstrap.php
Plugin::load('Migrations');
↓
//Plugin::load('Migrations');

Plugin::load('DebugKit', ['bootstrap' => true]);
↓
//Plugin::load('DebugKit', ['bootstrap' => true]);
```
![スクリーンショット 2023-02-09 14.36.55.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3127481/58067287-6902-45cb-a729-cb1d5faf4cbd.png)

* * *