作者: massie投稿日: カテゴリー: AWSタグ: Amazon Linux 2023, Apache, AWS, MariaDB, PHP
目次
- 前提条件
- Apacheのインストール
- Apacheの起動
- Apacheの自動起動
- PHPのインストール
- MariaDBをインストール
- MariaDBを起動する
- MariaDBの自動起動設定
- MariaDBに接続
- ユーザーを作成
- 適当なデータベースとテーブルを作成する
- PHPからMariaDBへ接続してみる
- ブラウザからアクセスしてみる
前提条件
AWSの環境でAmazonLinux2023が構築済みの状態で話を進めさせていただきます。
また、セキュリティグループの設定方法なども割愛させていただきます。
Apacheのインストール
yumコマンドでApacheをインストールします。
# yum install httpd
(途中で「y」の入力を求められますが、)私は以下のような感じでインストールが完了しました。
Last metadata expiration check: 23:56:26 ago on Mon May 8 23:13:58 2023.
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
httpd x86_64 2.4.56-1.amzn2023 amazonlinux 48 k
Installing dependencies:
apr x86_64 1.7.2-2.amzn2023.0.2 amazonlinux 129 k
apr-util x86_64 1.6.3-1.amzn2023.0.1 amazonlinux 98 k
generic-logos-httpd noarch 18.0.0-12.amzn2023.0.3 amazonlinux 19 k
httpd-core x86_64 2.4.56-1.amzn2023 amazonlinux 1.4 M
httpd-filesystem noarch 2.4.56-1.amzn2023 amazonlinux 15 k
httpd-tools x86_64 2.4.56-1.amzn2023 amazonlinux 82 k
libbrotli x86_64 1.0.9-4.amzn2023.0.2 amazonlinux 315 k
mailcap noarch 2.1.49-3.amzn2023.0.3 amazonlinux 33 k
Installing weak dependencies:
apr-util-openssl x86_64 1.6.3-1.amzn2023.0.1 amazonlinux 17 k
mod_http2 x86_64 2.0.11-2.amzn2023 amazonlinux 150 k
mod_lua x86_64 2.4.56-1.amzn2023 amazonlinux 62 k
Transaction Summary
================================================================================
Install 12 Packages
Total download size: 2.3 M
Installed size: 6.9 M
Is this ok [y/N]: y
Downloading Packages:
(1/12): apr-util-openssl-1.6.3-1.amzn2023.0.1.x 221 kB/s | 17 kB 00:00
(2/12): httpd-2.4.56-1.amzn2023.x86_64.rpm 601 kB/s | 48 kB 00:00
(3/12): apr-1.7.2-2.amzn2023.0.2.x86_64.rpm 904 kB/s | 129 kB 00:00
(4/12): httpd-core-2.4.56-1.amzn2023.x86_64.rpm 18 MB/s | 1.4 MB 00:00
(5/12): libbrotli-1.0.9-4.amzn2023.0.2.x86_64.r 3.5 MB/s | 315 kB 00:00
(6/12): apr-util-1.6.3-1.amzn2023.0.1.x86_64.rp 1.4 MB/s | 98 kB 00:00
(7/12): mod_lua-2.4.56-1.amzn2023.x86_64.rpm 3.2 MB/s | 62 kB 00:00
(8/12): httpd-tools-2.4.56-1.amzn2023.x86_64.rp 1.2 MB/s | 82 kB 00:00
(9/12): mod_http2-2.0.11-2.amzn2023.x86_64.rpm 1.6 MB/s | 150 kB 00:00
(10/12): mailcap-2.1.49-3.amzn2023.0.3.noarch.r 1.6 MB/s | 33 kB 00:00
(11/12): generic-logos-httpd-18.0.0-12.amzn2023 897 kB/s | 19 kB 00:00
(12/12): httpd-filesystem-2.4.56-1.amzn2023.noa 463 kB/s | 15 kB 00:00
--------------------------------------------------------------------------------
Total 6.5 MB/s | 2.3 MB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : apr-1.7.2-2.amzn2023.0.2.x86_64 1/12
Installing : apr-util-openssl-1.6.3-1.amzn2023.0.1.x86_64 2/12
Installing : apr-util-1.6.3-1.amzn2023.0.1.x86_64 3/12
Installing : mailcap-2.1.49-3.amzn2023.0.3.noarch 4/12
Installing : httpd-tools-2.4.56-1.amzn2023.x86_64 5/12
Running scriptlet: httpd-filesystem-2.4.56-1.amzn2023.noarch 6/12
Installing : httpd-filesystem-2.4.56-1.amzn2023.noarch 6/12
Installing : httpd-core-2.4.56-1.amzn2023.x86_64 7/12
Installing : mod_http2-2.0.11-2.amzn2023.x86_64 8/12
Installing : mod_lua-2.4.56-1.amzn2023.x86_64 9/12
Installing : generic-logos-httpd-18.0.0-12.amzn2023.0.3.noarch 10/12
Installing : libbrotli-1.0.9-4.amzn2023.0.2.x86_64 11/12
Installing : httpd-2.4.56-1.amzn2023.x86_64 12/12
Running scriptlet: httpd-2.4.56-1.amzn2023.x86_64 12/12
Verifying : apr-1.7.2-2.amzn2023.0.2.x86_64 1/12
Verifying : apr-util-openssl-1.6.3-1.amzn2023.0.1.x86_64 2/12
Verifying : httpd-2.4.56-1.amzn2023.x86_64 3/12
Verifying : libbrotli-1.0.9-4.amzn2023.0.2.x86_64 4/12
Verifying : httpd-core-2.4.56-1.amzn2023.x86_64 5/12
Verifying : apr-util-1.6.3-1.amzn2023.0.1.x86_64 6/12
Verifying : mod_http2-2.0.11-2.amzn2023.x86_64 7/12
Verifying : httpd-tools-2.4.56-1.amzn2023.x86_64 8/12
Verifying : mod_lua-2.4.56-1.amzn2023.x86_64 9/12
Verifying : mailcap-2.1.49-3.amzn2023.0.3.noarch 10/12
Verifying : generic-logos-httpd-18.0.0-12.amzn2023.0.3.noarch 11/12
Verifying : httpd-filesystem-2.4.56-1.amzn2023.noarch 12/12
Installed:
apr-1.7.2-2.amzn2023.0.2.x86_64
apr-util-1.6.3-1.amzn2023.0.1.x86_64
apr-util-openssl-1.6.3-1.amzn2023.0.1.x86_64
generic-logos-httpd-18.0.0-12.amzn2023.0.3.noarch
httpd-2.4.56-1.amzn2023.x86_64
httpd-core-2.4.56-1.amzn2023.x86_64
httpd-filesystem-2.4.56-1.amzn2023.noarch
httpd-tools-2.4.56-1.amzn2023.x86_64
libbrotli-1.0.9-4.amzn2023.0.2.x86_64
mailcap-2.1.49-3.amzn2023.0.3.noarch
mod_http2-2.0.11-2.amzn2023.x86_64
mod_lua-2.4.56-1.amzn2023.x86_64
Complete!
Apacheの起動
# systemctl start httpd
グローバルIPアドレス直打ちでブラウザからアクセスすると、以下のような画面が出ればインストール、および起動に成功しています。
http://[IPアドレス]/
Apacheの自動起動
サーバーを再起動してもApacheが起動するようにします。
# systemctl enable httpd
PHPのインストール
以下のコマンドでPHPをインストールします。
# yum install php
デフォルトでは「PHP8.1」がインストールされるようです。
Last metadata expiration check: 1 day, 0:07:43 ago on Mon May 8 23:13:58 2023.
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
php8.1 x86_64 8.1.16-1.amzn2023.0.1 amazonlinux 14 k
Installing dependencies:
libxslt x86_64 1.1.34-5.amzn2023.0.2 amazonlinux 241 k
nginx-filesystem noarch 1:1.22.1-1.amzn2023.0.3 amazonlinux 12 k
oniguruma x86_64 6.9.7.1-1.amzn2023.0.2 amazonlinux 216 k
php8.1-common x86_64 8.1.16-1.amzn2023.0.1 amazonlinux 676 k
Installing weak dependencies:
php8.1-cli x86_64 8.1.16-1.amzn2023.0.1 amazonlinux 3.5 M
php8.1-fpm x86_64 8.1.16-1.amzn2023.0.1 amazonlinux 1.8 M
php8.1-mbstring x86_64 8.1.16-1.amzn2023.0.1 amazonlinux 477 k
php8.1-opcache x86_64 8.1.16-1.amzn2023.0.1 amazonlinux 382 k
php8.1-pdo x86_64 8.1.16-1.amzn2023.0.1 amazonlinux 88 k
php8.1-xml x86_64 8.1.16-1.amzn2023.0.1 amazonlinux 146 k
Transaction Summary
================================================================================
Install 11 Packages
Total download size: 7.5 M
Installed size: 37 M
Is this ok [y/N]: y
Downloading Packages:
(1/11): php8.1-pdo-8.1.16-1.amzn2023.0.1.x86_64 909 kB/s | 88 kB 00:00
(2/11): php8.1-mbstring-8.1.16-1.amzn2023.0.1.x 4.3 MB/s | 477 kB 00:00
(3/11): php8.1-opcache-8.1.16-1.amzn2023.0.1.x8 4.4 MB/s | 382 kB 00:00
(4/11): php8.1-fpm-8.1.16-1.amzn2023.0.1.x86_64 20 MB/s | 1.8 MB 00:00
(5/11): php8.1-cli-8.1.16-1.amzn2023.0.1.x86_64 14 MB/s | 3.5 MB 00:00
(6/11): php8.1-xml-8.1.16-1.amzn2023.0.1.x86_64 2.8 MB/s | 146 kB 00:00
(7/11): php8.1-8.1.16-1.amzn2023.0.1.x86_64.rpm 433 kB/s | 14 kB 00:00
(8/11): php8.1-common-8.1.16-1.amzn2023.0.1.x86 5.4 MB/s | 676 kB 00:00
(9/11): oniguruma-6.9.7.1-1.amzn2023.0.2.x86_64 7.3 MB/s | 216 kB 00:00
(10/11): libxslt-1.1.34-5.amzn2023.0.2.x86_64.r 2.9 MB/s | 241 kB 00:00
(11/11): nginx-filesystem-1.22.1-1.amzn2023.0.3 266 kB/s | 12 kB 00:00
--------------------------------------------------------------------------------
Total 18 MB/s | 7.5 MB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : php8.1-common-8.1.16-1.amzn2023.0.1.x86_64 1/11
Installing : php8.1-cli-8.1.16-1.amzn2023.0.1.x86_64 2/11
Installing : php8.1-pdo-8.1.16-1.amzn2023.0.1.x86_64 3/11
Installing : php8.1-opcache-8.1.16-1.amzn2023.0.1.x86_64 4/11
Running scriptlet: nginx-filesystem-1:1.22.1-1.amzn2023.0.3.noarch 5/11
Installing : nginx-filesystem-1:1.22.1-1.amzn2023.0.3.noarch 5/11
Installing : php8.1-fpm-8.1.16-1.amzn2023.0.1.x86_64 6/11
Running scriptlet: php8.1-fpm-8.1.16-1.amzn2023.0.1.x86_64 6/11
Installing : oniguruma-6.9.7.1-1.amzn2023.0.2.x86_64 7/11
Installing : php8.1-mbstring-8.1.16-1.amzn2023.0.1.x86_64 8/11
Installing : libxslt-1.1.34-5.amzn2023.0.2.x86_64 9/11
Installing : php8.1-xml-8.1.16-1.amzn2023.0.1.x86_64 10/11
Installing : php8.1-8.1.16-1.amzn2023.0.1.x86_64 11/11
Running scriptlet: php8.1-8.1.16-1.amzn2023.0.1.x86_64 11/11
Verifying : php8.1-mbstring-8.1.16-1.amzn2023.0.1.x86_64 1/11
Verifying : php8.1-cli-8.1.16-1.amzn2023.0.1.x86_64 2/11
Verifying : php8.1-pdo-8.1.16-1.amzn2023.0.1.x86_64 3/11
Verifying : php8.1-opcache-8.1.16-1.amzn2023.0.1.x86_64 4/11
Verifying : php8.1-fpm-8.1.16-1.amzn2023.0.1.x86_64 5/11
Verifying : php8.1-common-8.1.16-1.amzn2023.0.1.x86_64 6/11
Verifying : php8.1-xml-8.1.16-1.amzn2023.0.1.x86_64 7/11
Verifying : php8.1-8.1.16-1.amzn2023.0.1.x86_64 8/11
Verifying : libxslt-1.1.34-5.amzn2023.0.2.x86_64 9/11
Verifying : oniguruma-6.9.7.1-1.amzn2023.0.2.x86_64 10/11
Verifying : nginx-filesystem-1:1.22.1-1.amzn2023.0.3.noarch 11/11
Installed:
libxslt-1.1.34-5.amzn2023.0.2.x86_64
nginx-filesystem-1:1.22.1-1.amzn2023.0.3.noarch
oniguruma-6.9.7.1-1.amzn2023.0.2.x86_64
php8.1-8.1.16-1.amzn2023.0.1.x86_64
php8.1-cli-8.1.16-1.amzn2023.0.1.x86_64
php8.1-common-8.1.16-1.amzn2023.0.1.x86_64
php8.1-fpm-8.1.16-1.amzn2023.0.1.x86_64
php8.1-mbstring-8.1.16-1.amzn2023.0.1.x86_64
php8.1-opcache-8.1.16-1.amzn2023.0.1.x86_64
php8.1-pdo-8.1.16-1.amzn2023.0.1.x86_64
php8.1-xml-8.1.16-1.amzn2023.0.1.x86_64
Complete!
PHPをApache経由で使えるようにするために、Apacheを再起動します。
# systemctl restart httpd
確認用のPHPコードを用意します。
# vi /var/www/html/test.php
ファイルの中身は以下のようにします。
<?php
phpinfo();
ファイルが用意できたら、以下のアドレスでブラウザからアクセスしてみます。
http://[IPアドレス]/test.php
以下のような画面が表示されたら、PHPが動作しています。
MariaDBをインストール
# yum install mariadb105-server
Last metadata expiration check: 23:31:08 ago on Wed May 10 23:22:42 2023.
Dependencies resolved.
===================================================================================================================
Package Architecture Version Repository Size
===================================================================================================================
Installing:
mariadb105-server x86_64 3:10.5.18-1.amzn2023.0.1 amazonlinux 10 M
Installing weak dependencies:
mariadb105-backup x86_64 3:10.5.18-1.amzn2023.0.1 amazonlinux 6.2 M
mariadb105-cracklib-password-check x86_64 3:10.5.18-1.amzn2023.0.1 amazonlinux 16 k
mariadb105-gssapi-server x86_64 3:10.5.18-1.amzn2023.0.1 amazonlinux 18 k
mariadb105-server-utils x86_64 3:10.5.18-1.amzn2023.0.1 amazonlinux 216 k
Transaction Summary
===================================================================================================================
Install 5 Packages
Total size: 17 M
Installed size: 92 M
Is this ok [y/N]: y
Downloading Packages:
[SKIPPED] mariadb105-cracklib-password-check-10.5.18-1.amzn2023.0.1.x86_64.rpm: Already downloaded
[SKIPPED] mariadb105-server-utils-10.5.18-1.amzn2023.0.1.x86_64.rpm: Already downloaded
[SKIPPED] mariadb105-backup-10.5.18-1.amzn2023.0.1.x86_64.rpm: Already downloaded
[SKIPPED] mariadb105-server-10.5.18-1.amzn2023.0.1.x86_64.rpm: Already downloaded
[SKIPPED] mariadb105-gssapi-server-10.5.18-1.amzn2023.0.1.x86_64.rpm: Already downloaded
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : mariadb105-server-utils-3:10.5.18-1.amzn2023.0.1.x86_64 1/5
Installing : mariadb105-backup-3:10.5.18-1.amzn2023.0.1.x86_64 2/5
Installing : mariadb105-gssapi-server-3:10.5.18-1.amzn2023.0.1.x86_64 3/5
Running scriptlet: mariadb105-server-3:10.5.18-1.amzn2023.0.1.x86_64 4/5
Installing : mariadb105-server-3:10.5.18-1.amzn2023.0.1.x86_64 4/5
Running scriptlet: mariadb105-server-3:10.5.18-1.amzn2023.0.1.x86_64 4/5
Installing : mariadb105-cracklib-password-check-3:10.5.18-1.amzn2023.0.1.x86_64 5/5
Running scriptlet: mariadb105-cracklib-password-check-3:10.5.18-1.amzn2023.0.1.x86_64 5/5
Verifying : mariadb105-cracklib-password-check-3:10.5.18-1.amzn2023.0.1.x86_64 1/5
Verifying : mariadb105-server-utils-3:10.5.18-1.amzn2023.0.1.x86_64 2/5
Verifying : mariadb105-backup-3:10.5.18-1.amzn2023.0.1.x86_64 3/5
Verifying : mariadb105-server-3:10.5.18-1.amzn2023.0.1.x86_64 4/5
Verifying : mariadb105-gssapi-server-3:10.5.18-1.amzn2023.0.1.x86_64 5/5
Installed:
mariadb105-backup-3:10.5.18-1.amzn2023.0.1.x86_64
mariadb105-cracklib-password-check-3:10.5.18-1.amzn2023.0.1.x86_64
mariadb105-gssapi-server-3:10.5.18-1.amzn2023.0.1.x86_64
mariadb105-server-3:10.5.18-1.amzn2023.0.1.x86_64
mariadb105-server-utils-3:10.5.18-1.amzn2023.0.1.x86_64
Complete!
MariaDBを起動する
# systemctl start mariadb
MariaDBの自動起動設定
# systemctl enable mariadb
MariaDBに接続
rootユーザーなら、以下のコマンドでいったんログインすることができます。
# mysql
ユーザーを作成
> create user 'testuser'@'localhost' identified by 'password';
> create user 'testuser'@'%' identified by 'password';
> GRANT ALL PRIVILEGES ON * . * TO 'testuser'@'localhost';
> GRANT ALL PRIVILEGES ON * . * TO 'testuser'@'%';
適当なデータベースとテーブルを作成する
> CREATE SCHEMA `test` DEFAULT CHARACTER SET utf8mb4;
> CREATE TABLE `test`.`test_data` (
`id` INT NOT NULL,
`data` VARCHAR(45) NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8mb4;
> INSERT INTO `test`.`test_data` (`id`, `data`) VALUES ('1', 'テスト');
PHPからMariaDBへ接続してみる
まず、MySQLへ接続するためのモジュールをインストールします。
# yum install php8.1-mysqlnd
# systemctl restart httpd
次に、ソースコードを用意します。
# vi /var/www/html/mysql.php
↓中身(ホスト名、データベース名、ユーザー名、パスワード、テーブル名などは適宜変更してください。)
<?php
$id = 1;
$pdo = new PDO(
'mysql:host=localhost;dbname=test;charset=utf8mb4',
'testuser',
'password'
);
$stmt = $pdo->prepare("SELECT * FROM test_data WHERE id = :id");
$stmt->bindParam(':id', $id);
$res = $stmt->execute();
if( $res ) {
$data = $stmt->fetch();
var_dump($data);
}
$pdo = null;
ブラウザからアクセスしてみる
http://[IPアドレス]/mysql.php