0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Amazon Linux 2023】LAMP環境構築

Posted at

目的

LAMPサーバを構築する。OSはAmazon Linux 2023を使用。

LAMPサーバ…Webシステム構築のための典型的なプラットフォーム
Linux/Apache/MySQL(MariaDB)/PHP(またはPython)で構成される。

Step 1:EC2のインバウンドルールを設定

1.EC2のインスタンスのインバウンドルールを設定する。
SSH、HTTP,HTTPSで許可する、ソース(アクセス元)は自分のIPのみ、の設定とした。

LAMP_1.png

💡 インバウンドルール…外部からEC2あての通信

Step 2:EC2インスタンスにApache/PHPのインストール

1.ソフトウェアパッケージが最新か確認
dnf upgrade -y

[root@test-server ec2-user]# dnf upgrade -y
Last metadata expiration check: 1 day, 2:33:30 ago on Tue Apr 15 11:48:11 2025.
Dependencies resolved.
Nothing to do.
Complete!
[root@test-server ec2-user]#

2.ApacheウェブサーバーとPHPパッケージをインストール
sudo dnf install -y httpd wget php-fpm php-mysqli php-json php php-devel

php -v でインストールできているか確認。

[root@test-server ec2-user]# php -v
PHP 8.4.5 (cli) (built: Mar 12 2025 01:55:56) (NTS gcc x86_64)
Copyright (c) The PHP Group
Built by Amazon Linux
Zend Engine v4.4.5, Copyright (c) Zend Technologies
with Zend OPcache v8.4.5, Copyright (c), by Zend Technologies

3.Apache ウェブサーバーを起動
systemctl start httpd

4.Apache自動起動の設定
systemctl enable httpd

5.自動起動が有効化されていることを確認
systemctl is-enabled httpd

6.PHPファイルの作成
echo "<?php phpinfo(); ?>" > /var/www/html/phpinfo.php

7.作成されていることを確認
ll /var/www/html/

8.パブリックIPV4アドレスを確認
EC2>インスタンス>ネットワーキングタブ>パブリックIPV4アドレス

9.http://パブリックIP/phpinfo.php
画像のようにphpのバージョンが表示されたらOK✨

LAMP_2.png

Step 3:MariaDBのインストール

1.MariaDBソフトウェアパッケージをインストール
dnf install -y mariadb105-server
mysql --versionでインストールできているか確認。

[root@test-server ec2-user]# mysql --version
mysql  Ver 15.1 Distrib 10.5.25-MariaDB, for Linux (x86_64) using  EditLine wrapper

2.MariaDBを起動
systemctl start mariadb

3.MariaDB自動起動の設定

systemctl enable httpd

4.自動起動が有効化されていることを確認
systemctl is-enabled httpd

5.MariaDB初期設定
mysql_secure_installation
(mariadb-secure-installation でも設定可能)

最初のパスワード設定は入力不要でエンターでOK

6.MySQLへのログインができることを確認
mysql -uroot -p

■エラー&詰まった個所

①「Job for mariadb.service failed because the control process exited with error code.」でMariaDBが起動しない

事象:以下のエラーでMariaDBが起動しない

[root@test-server ec2-user]# systemctl start mariadb
Job for mariadb.service failed because the control process exited with error code.
See "systemctl status mariadb.service" and "journalctl -xeu mariadb.service" for details.

原因:/var/lib/mysqlを削除していたことが原因。
MySQLをインストールしていたが、せっかくならMariaDBをインストールしようと思い、MySQLをアンインストール。
その際に、/var/lib/mysql も消していた。
journalctl -xeu mariadb.service で以下のエラーが確認できた。

Apr 16 15:43:01 test-server mariadb-prepare-db-dir[125496]: **The directory /var/lib/mysql does not exist.**
Apr 16 15:43:01 test-server systemd[1]: mariadb.service: Control process exited, code=exited, status=1/FAILURE
aa Subject: Unit process exited
aa Defined-By: systemd
aa Support: https://lists.freedesktop.org/mailman/listinfo/systemd-devel
aa
aa An ExecStartPre= process belonging to unit mariadb.service has exited.

解決策:①から⑤の実行
①ディレクトリが存在しないので作成
mkdir -p /var/lib/mysql

②ディレクトリの所有権を設定する
chown -R mysql:mysql /var/lib/mysql

③データベースを初期化する
mariadb-install-db --user=mysql --basedir=/usr --datadir=/var/lib/mysql

④起動
systemctl start mariadb

⑤ステータス確認
systemctl status mariadb

所感

ほぼ、アマゾン公式サイトの記載通り対応できた。
php -vなどバージョン確認&インストールができたのか?確認できるコマンドは覚えておいたほうがいいと思った。
mysql_secure_installation をMariaDBでも使用できると思わなかった。

■参考サイト

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?