Amazon AWS Cloud9 でPHP8.2系のLAMP開発環境を速攻で作る方法

# はじめに
https://qiita.com/qwe001/items/d3270b0de7f8b17a4f85

上記の記事で書いた内容を8.2版にしただけです。
詳しい作り方は上記記事を参考にしてください。
最近php8.2環境を作ることが増えたので、ターミナルで実行するコマンドだけ絞って書きます。

# 2024.01.31 追記
https://qiita.com/qwe001/items/825debff92c52def6481

Amazon Linux 2023対応版の記事も作成しました。良ければご確認ください。

# PHP8.2など必要ライブラリをインストール

```bash
# とりあえずyumアップデート
sudo yum -y update
# Amazon Linux でepel-releaseパッケージをインストール出来るようにする
sudo amazon-linux-extras install epel
# epel-releaseパッケージをインストール
sudo yum install epel-release
# remiリポジトリを使えるようにする
sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
# php7.2系をアンインストールする
sudo yum remove -y php*
# php8.2系をインストールする
sudo yum install -y --enablerepo=epel,remi,remi-php82 php82-php php82-php-{fpm,mbstring,mysqlnd,xml,xdebug,intl}
# phpのパスを通す
sudo ln -s /usr/bin/php82 /usr/bin/php
# MySQLサーバ(mariadb)をインストールする
sudo yum install -y mariadb-server
```

# おまじない実行

```bash
sudo groupadd web-content # Create a group named web-content.
sudo usermod -G web-content -a ec2-user # Add the user ec2-user (your default user for this environment) to the group web-content.
sudo usermod -G web-content -a apache # Add the user apache (Apache HTTP Server) to the group web-content.
sudo chown -R ec2-user:web-content /var/www/html # Change the owner of /var/www/html and its files to user ec2-user and group web-content.
sudo find /var/www/html -type f -exec chmod u=rw,g=rx,o=rx {} \; # Change all file permissions within /var/www/html to user read/write, group read-only, and others read/execute. 
sudo find /var/www/html -type d -exec chmod u=rwx,g=rx,o=rx {} \; # Change /var/www/html directory permissions to user read/write/execute, group read/execute, and others read/execute.

sudo touch /var/www/html/index.php && sudo chown -R ec2-user:web-content /var/www/html/index.php && sudo chmod u=rw,g=rx,o=rx /var/www/html/index.php && sudo printf '%s\n%s\n%s' '<?php' '  phpinfo();' '?>' >> /var/www/html/index.php

MY_INSTANCE_ID=$(curl http://169.254.169.254/latest/meta-data/instance-id) # Get the ID of the instance for the environment, and store it temporarily.
MY_SECURITY_GROUP_ID=$(aws ec2 describe-instances --instance-id $MY_INSTANCE_ID --query 'Reservations[].Instances[0].SecurityGroups[0].GroupId' --output text) # Get the ID of the security group associated with the instance, and store it temporarily.
aws ec2 authorize-security-group-ingress --group-id $MY_SECURITY_GROUP_ID --protocol tcp --cidr 0.0.0.0/0 --port 80 # Add an inbound rule to the security group to allow all incoming IPv4-based traffic over port 80.
aws ec2 authorize-security-group-ingress --group-id $MY_SECURITY_GROUP_ID --ip-permissions IpProtocol=tcp,Ipv6Ranges='[{CidrIpv6=::/0}]',FromPort=80,ToPort=80 # Add an inbound rule to the security group to allow all incoming IPv6-based traffic over port 80.
MY_SUBNET_ID=$(aws ec2 describe-instances --instance-id $MY_INSTANCE_ID --query 'Reservations[].Instances[0].SubnetId' --output text) # Get the ID of the subnet associated with the instance, and store it temporarily.
MY_NETWORK_ACL_ID=$(aws ec2 describe-network-acls --filters Name=association.subnet-id,Values=$MY_SUBNET_ID --query 'NetworkAcls[].Associations[0].NetworkAclId' --output text) # Get the ID of the network ACL associated with the subnet, and store it temporarily.
aws ec2 create-network-acl-entry --network-acl-id $MY_NETWORK_ACL_ID --ingress --protocol tcp --rule-action allow --rule-number 10000 --cidr-block 0.0.0.0/0 --port-range From=80,To=80 # Add an inbound rule to the network ACL to allow all IPv4-based traffic over port 80. Advanced users: change this suggested rule number as desired.
aws ec2 create-network-acl-entry --network-acl-id $MY_NETWORK_ACL_ID --ingress --protocol tcp --rule-action allow --rule-number 10100 --ipv6-cidr-block ::/0 --port-range From=80,To=80 # Add an inbound rule to the network ACL to allow all IPv6-based traffic over port 80. Advanced users: change this suggested rule number as desired.

MY_PUBLIC_IP=$(curl http://169.254.169.254/latest/meta-data/public-ipv4) && echo http://$MY_PUBLIC_IP/index.php # Get the URL to the index.php file within the web server root.
```

# シンボリックリンクを作成

```bash
sudo ln -s /var/www/html /home/ec2-user/environment/
```

# タイムゾーンを日本時間に変更

```bash
# ローカル時間を日本時間に変更
sudo ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
# php.ini の場所を探す
sudo php -i | grep php.ini
# デフォルトのファイルを残しておく
sudo cp /etc/opt/remi/php82/php.ini /etc/opt/remi/php82/php.ini.org
# php.ini を編集する
sudo vi /etc/opt/remi/php82/php.ini
```

```ini
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
;date.timezone =

↓
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
;date.timezone =
date.timezone = "Asia/Tokyo"
```

# Apacheのリダイレクト設定を有効にする

```bash
sudo cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.org
sudo vi /etc/httpd/conf/httpd.conf
```

`/Directory "\/var\/www\/html"` を入力して、ドキュメントルートへの記述を探し、
`AllowOverride None` を `AllowOverride All` に変更する

```apache
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   Options FileInfo AuthConfig Limit
    #
    #AllowOverride None
    AllowOverride All
```

# 再起動して設定完了

```bash
sudo reboot
```

# 次回以降 開発環境の立ち上げ方

```bash
sudo service mariadb start; sudo service httpd start; curl -q http://169.254.169.254/latest/meta-data/public-ipv4 -w "\n"
```