AWS Cloud9 でAmazon Linux 2でLaravelの設定

###Amazon Linux2 ではEPELが使えない
EPELが何かは知りません。調べたら追記します！

とりあえず仕様している教材に

>amazon linuxを使ってください。linux 2ではEPELが使えないので。

みたいに書いてあったけど、

AWSのCloud9でワークスペースを作成する際に、

>Amazon Linux 2 (recommended)

と合ったので、せっかくなら推奨版が使いたいなということで、環境を少しアレンジ。

まああとは自分で調べてEPELが使えればいいだろうという考え。というか推奨版なんだから当然使えるっしょ！の乗りです。

###手動でEPELを有効にする

https://aws.amazon.com/jp/premiumsupport/knowledge-center/ec2-enable-epel/

この通りにやれば大丈夫っぽい。

```bash
sudo amazon-linux-extras install epel -y
```


###phpのバージョン確認

```bash
$ php -v
PHP 7.2.24 (cli) (built: Oct 31 2019 18:27:08) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
```

2021/07/26の段階では7.2.24が標準でインストールされるらしい。
amazon linuxの方だと5系になるのかな？？


###composeのインストール


```bash
$ curl -sS https://getcomposer.org/installer | php   
All settings correct for using Composer
Downloading...

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

# このままだとcomposerのパスが通ってない？
$ composer
bash: composer: command not found

# composer.pharを移動してパスを通す
$ sudo mv composer.phar /usr/local/bin/composer

# 再確認
$ composer
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.1.5 2021-07-23 10:35:47

Usage:
  command [options] [arguments]

Options:
  -h, --help                     Display this help message
  -q, --quiet                    Do not output any message
  -V, --version                  Display this application version
      --ansi                     Force ANSI output
      --no-ansi                  Disable ANSI output
  -n, --no-interaction           Do not ask any interactive question
      --profile                  Display timing and memory usage information
      --no-plugins               Whether to disable plugins.
  -d, --working-dir=WORKING-DIR  If specified, use the given directory as working directory.
      --no-cache                 Prevent use of the cache
  -v|vv|vvv, --verbose           Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  about                Shows a short information about Composer.
  archive              Creates an archive of this composer package.
  browse               Opens the package's repository URL or homepage in your browser.
  cc                   Clears composer's internal package cache.
  check-platform-reqs  Check that platform requirements are satisfied.
  clear-cache          Clears composer's internal package cache.
  clearcache           Clears composer's internal package cache.
  config               Sets config options.
  create-project       Creates new project from a package into given directory.
  depends              Shows which packages cause the given package to be installed.
  diagnose             Diagnoses the system to identify common errors.
  dump-autoload        Dumps the autoloader.
  dumpautoload         Dumps the autoloader.
  exec                 Executes a vendored binary/script.
  fund                 Discover how to help fund the maintenance of your dependencies.
  global               Allows running commands in the global composer dir ($COMPOSER_HOME).
  help                 Displays help for a command
  home                 Opens the package's repository URL or homepage in your browser.
  i                    Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
  info                 Shows information about packages.
  init                 Creates a basic composer.json file in current directory.
  install              Installs the project dependencies from the composer.lock file if present, or falls back on the composer.json.
  licenses             Shows information about licenses of dependencies.
  list                 Lists commands
  outdated             Shows a list of installed packages that have updates available, including their latest version.
  prohibits            Shows which packages prevent the given package from being installed.
  reinstall            Uninstalls and reinstalls the given package names
  remove               Removes a package from the require or require-dev.
  require              Adds required packages to your composer.json and installs them.
  run                  Runs the scripts defined in composer.json.
  run-script           Runs the scripts defined in composer.json.
  search               Searches for packages.
  self-update          Updates composer.phar to the latest version.
  selfupdate           Updates composer.phar to the latest version.
  show                 Shows information about packages.
  status               Shows a list of locally modified packages.
  suggests             Shows package suggestions.
  u                    Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  update               Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  upgrade              Upgrades your dependencies to the latest version according to composer.json, and updates the composer.lock file.
  validate             Validates a composer.json and composer.lock.
  why                  Shows which packages cause the given package to be installed.
  why-not              Shows which packages prevent the given package from being installed.

```

いけた！

###Laravelプロジェクトを作成

```bash
 $ composer create-project laravel/laravel ./laravel-quest "5.5.*" --prefer-distCreating a "laravel/laravel" project at "./laravel-quest"
Installing laravel/laravel (v5.5.28)
  - Downloading laravel/laravel (v5.5.28)
  - Installing laravel/laravel (v5.5.28): Extracting archive
Created project in /home/ec2-user/environment/./laravel-quest
> @php -r "file_exists('.env') || copy('.env.example', '.env');"
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

  Problem 1
    - laravel/framework[v5.5.0, ..., v5.5.50] require ext-mbstring * -> it is missing from your system. Install or enable PHP's mbstring extension.
    - Root composer.json requires laravel/framework 5.5.* -> satisfiable by laravel/framework[v5.5.0, ..., v5.5.50].
    - 
```

なんか問題が発生している。。。

>- laravel/framework[v5.5.0, ..., v5.5.50] require ext-mbstring * -> it is missing from your system. Install or enable PHP's mbstring extension.

extensionが無い、もしくは有効になっていないということですね。

ちなみにこの状態でもLaravelプロジェクトは作成されています。
しかし起動しようとするとできません。

```terminal
$ php artisan serve --port=80
PHP Warning:  require(/home/ec2-user/environment/laravel-quest/vendor/autoload.php): failed to open stream: No such file or directory in /home/ec2-user/environment/laravel-quest/artisan on line 18
PHP Fatal error:  require(): Failed opening required '/home/ec2-user/environment/laravel-quest/vendor/autoload.php' (include_path='.:/usr/share/pear:/usr/share/php') in /home/ec2-user/environment/laravel-quest/artisan on line 18
```

autoload.phpがないって言われてるんですね。
よくわかりませんが、serverが入ってないから起動できないということだと思います。

なので手動で入れる。


###WebサーバApacheはamazon-linux-extrasにないらしい

https://www.messiahworks.com/archives/22699

ここのとおりにやれば大丈夫でした。

```bash
# aparchのインストール
$ sudo yum install -y httpd

Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
amzn2-core                                                                     | 3.7 kB  00:00:00     
242 packages excluded due to repository priority protections
Package httpd-2.4.48-2.amzn2.x86_64 already installed and latest version
Nothing to do
# 入ってるやん！

# サーバの確認
$ httpd -v
Server version: Apache/2.4.48 () // 入ってます！
Server built:   Jun 25 2021 18:53:37

# php関連のインストール
$ sudo yum install php php-mbstring php-pdo php-gd php-xml
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
242 packages excluded due to repository priority protections
Package php-pdo-7.2.24-1.amzn2.0.1.x86_64 already installed and latest version
Package php-xml-7.2.24-1.amzn2.0.1.x86_64 already installed and latest version
Resolving Dependencies
--> Running transaction check
---> Package php.x86_64 0:7.2.24-1.amzn2.0.1 will be installed
---> Package php-gd.x86_64 0:7.2.24-1.amzn2.0.1 will be installed
--> Processing Dependency: libXpm.so.4()(64bit) for package: php-gd-7.2.24-1.amzn2.0.1.x86_64
--> Processing Dependency: libX11.so.6()(64bit) for package: php-gd-7.2.24-1.amzn2.0.1.x86_64
---> Package php-mbstring.x86_64 0:7.2.24-1.amzn2.0.1 will be installed
--> Processing Dependency: libonig.so.2()(64bit) for package: php-mbstring-7.2.24-1.amzn2.0.1.x86_64
--> Running transaction check
---> Package libX11.x86_64 0:1.6.7-3.amzn2.0.2 will be installed
--> Processing Dependency: libX11-common >= 1.6.7-3.amzn2.0.2 for package: libX11-1.6.7-3.amzn2.0.2.x86_64
--> Processing Dependency: libxcb.so.1()(64bit) for package: libX11-1.6.7-3.amzn2.0.2.x86_64
---> Package libXpm.x86_64 0:3.5.12-1.amzn2.0.2 will be installed
---> Package oniguruma.x86_64 0:5.9.6-1.amzn2.0.4 will be installed
--> Running transaction check
---> Package libX11-common.noarch 0:1.6.7-3.amzn2.0.2 will be installed
---> Package libxcb.x86_64 0:1.12-1.amzn2.0.2 will be installed
--> Processing Dependency: libXau.so.6()(64bit) for package: libxcb-1.12-1.amzn2.0.2.x86_64
--> Running transaction check
---> Package libXau.x86_64 0:1.0.8-2.1.amzn2.0.2 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

======================================================================================================
 Package           Arch       Version                    Repository                              Size
======================================================================================================
Installing:
 php               x86_64     7.2.24-1.amzn2.0.1         amzn2extra-lamp-mariadb10.2-php7.2     2.9 M
 php-gd            x86_64     7.2.24-1.amzn2.0.1         amzn2extra-lamp-mariadb10.2-php7.2     179 k
 php-mbstring      x86_64     7.2.24-1.amzn2.0.1         amzn2extra-lamp-mariadb10.2-php7.2     501 k
Installing for dependencies:
 libX11            x86_64     1.6.7-3.amzn2.0.2          amzn2-core                             606 k
 libX11-common     noarch     1.6.7-3.amzn2.0.2          amzn2-core                             165 k
 libXau            x86_64     1.0.8-2.1.amzn2.0.2        amzn2-core                              29 k
 libXpm            x86_64     3.5.12-1.amzn2.0.2         amzn2-core                              57 k
 libxcb            x86_64     1.12-1.amzn2.0.2           amzn2-core                             216 k
 oniguruma         x86_64     5.9.6-1.amzn2.0.4          amzn2-core                             127 k

Transaction Summary
======================================================================================================
Install  3 Packages (+6 Dependent packages)

Total download size: 4.7 M
Installed size: 16 M
Is this ok [y/d/N]: y
Downloading packages:
(1/9): libX11-common-1.6.7-3.amzn2.0.2.noarch.rpm                              | 165 kB  00:00:00     
(2/9): libXau-1.0.8-2.1.amzn2.0.2.x86_64.rpm                                   |  29 kB  00:00:00     
(3/9): libXpm-3.5.12-1.amzn2.0.2.x86_64.rpm                                    |  57 kB  00:00:00     
(4/9): libX11-1.6.7-3.amzn2.0.2.x86_64.rpm                                     | 606 kB  00:00:00     
(5/9): oniguruma-5.9.6-1.amzn2.0.4.x86_64.rpm                                  | 127 kB  00:00:00     
(6/9): libxcb-1.12-1.amzn2.0.2.x86_64.rpm                                      | 216 kB  00:00:00     
(7/9): php-gd-7.2.24-1.amzn2.0.1.x86_64.rpm                                    | 179 kB  00:00:00     
(8/9): php-7.2.24-1.amzn2.0.1.x86_64.rpm                                       | 2.9 MB  00:00:00     
(9/9): php-mbstring-7.2.24-1.amzn2.0.1.x86_64.rpm                              | 501 kB  00:00:00     
------------------------------------------------------------------------------------------------------
Total                                                                  10 MB/s | 4.7 MB  00:00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : oniguruma-5.9.6-1.amzn2.0.4.x86_64                                                 1/9 
  Installing : libXau-1.0.8-2.1.amzn2.0.2.x86_64                                                  2/9 
  Installing : libxcb-1.12-1.amzn2.0.2.x86_64                                                     3/9 
  Installing : libX11-common-1.6.7-3.amzn2.0.2.noarch                                             4/9 
  Installing : libX11-1.6.7-3.amzn2.0.2.x86_64                                                    5/9 
  Installing : libXpm-3.5.12-1.amzn2.0.2.x86_64                                                   6/9 
  Installing : php-gd-7.2.24-1.amzn2.0.1.x86_64                                                   7/9 
  Installing : php-mbstring-7.2.24-1.amzn2.0.1.x86_64                                             8/9 
  Installing : php-7.2.24-1.amzn2.0.1.x86_64                                                      9/9 
  Verifying  : libX11-1.6.7-3.amzn2.0.2.x86_64                                                    1/9 
  Verifying  : php-mbstring-7.2.24-1.amzn2.0.1.x86_64                                             2/9 
  Verifying  : libxcb-1.12-1.amzn2.0.2.x86_64                                                     3/9 
  Verifying  : libX11-common-1.6.7-3.amzn2.0.2.noarch                                             4/9 
  Verifying  : php-7.2.24-1.amzn2.0.1.x86_64                                                      5/9 
  Verifying  : libXpm-3.5.12-1.amzn2.0.2.x86_64                                                   6/9 
  Verifying  : libXau-1.0.8-2.1.amzn2.0.2.x86_64                                                  7/9 
  Verifying  : php-gd-7.2.24-1.amzn2.0.1.x86_64                                                   8/9 
  Verifying  : oniguruma-5.9.6-1.amzn2.0.4.x86_64                                                 9/9 

Installed:
  php.x86_64 0:7.2.24-1.amzn2.0.1                      php-gd.x86_64 0:7.2.24-1.amzn2.0.1            
  php-mbstring.x86_64 0:7.2.24-1.amzn2.0.1            

Dependency Installed:
  libX11.x86_64 0:1.6.7-3.amzn2.0.2               libX11-common.noarch 0:1.6.7-3.amzn2.0.2            
  libXau.x86_64 0:1.0.8-2.1.amzn2.0.2             libXpm.x86_64 0:3.5.12-1.amzn2.0.2                  
  libxcb.x86_64 0:1.12-1.amzn2.0.2                oniguruma.x86_64 0:5.9.6-1.amzn2.0.4                

Complete!

# 以下を実行してautoload.phpを作成する
$ composer install

# 以下でサーバが起動できます。。。が！ブラウザは開けません！
$ php artisan serve --port=8080


```


###No application encryption key has been specified.が発生してブラウザが開けない

https://qiita.com/ponsuke0531/items/197c76fcb9300d7c5f36
ここのとおりにやったら大丈夫です。


```bash
$ cat .env | grep ^APP_
APP_NAME=Laravel
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

# 以下を実行するとAPP_KEYに自動的に追記される
$ php artisan key:generate

$ cat .env | grep ^APP_
APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:dvObQrZ9xEdYJaaaaaaaaaa/uFU3ADAsKtxxxxxxxxx=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

# 設定ファイルのキャッシュを再作成する
$ php artisan config:cache
Configuration cache cleared!
Configuration cached successfully!
```

これでブラウザが起動できるはず！
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/668402/1776cdae-f8ae-6115-cb78-f3977cfba55c.png)

やっとスタートラインです！頑張りましょう！