[AWS] LAMP環境の構築

構築したAWS EC2上にLAMP環境のインストール及び初期設定をします。

LAMPはLinux、Apache、MySQL、PHPの頭文字を取ったもので、これらの要素を組み合わせてウェブアプリケーションを開発および実行するための完璧な組み合わせ
https://qiita.com/wata_xox/items

【構築環境】


1.PCからEC2に接続

% ssh -i "demo-keypair.pem" ec2-user@ec2-13-113-166-76.ap-northeast-1.compute.amazonaws.com 
The authenticity of host 'ec2-13-113-166-76.ap-northeast-1.compute.amazonaws.com (13.113.166.76)' can't be established.
ECDSA key fingerprint is SHA256:h36pl9A7FtQwopwW3OS71DbFy1cTUd6yNIQ+6jCALkQ.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'ec2-13-113-166-76.ap-northeast-1.compute.amazonaws.com,13.113.166.76' (ECDSA) to the list of known hosts.
   ,     #_
   ~\_  ####_        Amazon Linux 2023
  ~~  \_#####\
  ~~     \###|
  ~~       \#/ ___   https://aws.amazon.com/linux/amazon-linux-2023
   ~~       V~' '->
    ~~~         /
      ~~._.   _/
         _/ _/
       _/m/'
[ec2-user@ip-172-31-33-34 ~]$

2.最新のパッケージにアップデート

sudo dnf update -y

<output>
[ec2-user@ip-172-31-33-34 ~]$ sudo dnf update -y
Last metadata expiration check: 0:40:04 ago on Mon Feb 24 01:55:21 2025.
Dependencies resolved.
Nothing to do.
Complete!
[ec2-user@ip-172-31-33-34 ~]$ 

※今回は、初期のEC2の状態が最新パッケージだった様です。

パッケージ
Linuxが採用しているアプリケーションの配布形態。アプリケーションによっては、正常に動作させるためには「ライブラリ」と呼ぶ別のプログラムを必要とすることがある。パッケージは、こうした動作に必要な各種プログラムやファイルをまとめたもの
https://xtech.nikkei.com/it/article/Keyword/20110427/359976/

sudoコマンドは、一般ユーザーに対してコマンド実行の単位でroot権限を与える仕組み
https://linuc.org/study/column/4047/#sudo

「dnf」は、Red Hat系のLinuxディストリビューションで使われている“RPMパッケージ”を扱うためのパッケージ管理コマンド
https://qiita.com/nejioooo/items/9c2d99ae82f7ce8c160a#dnf

Linuxのパッケージは大きく分けて以下の2つの方式が存在する。
・Debian形式
・RPM形式
https://qiita.com/nejioooo/items

今回構築したAWS EC2環境では “Amazon Linux 2023” を使用しているため、RPMパッケージとなります。
https://docs.aws.amazon.com/ja_jp/linux/al2023/release-notes/all-packages.html

3.Apacheインストール

Apacheは、無償で利用できるオープンソースのWebサーバソフト
Webサーバとは、パソコンやスマートフォンなどの端末からHTTP/HTTPSで送られたリクエストに応じてWebページやWebアプリケーションなどのコンテンツを提供するコンピュータプログラムのこと
https://qiita.com/kimsarai/items

sudo dnf install -y httpd

[ec2-user@ip-172-31-33-34 ~]$ sudo dnf install httpd -y
Last metadata expiration check: 1:39:31 ago on Mon Feb 24 01:55:21 2025.
Dependencies resolved.
===========================================================================================================
 Package                      Architecture    Version                           Repository            Size
===========================================================================================================
Installing:
 httpd                        x86_64          2.4.62-1.amzn2023                 amazonlinux           48 k
Installing dependencies:
 apr                          x86_64          1.7.5-1.amzn2023.0.4              amazonlinux          129 k
〜〜略〜〜
  Verifying        : mod_lua-2.4.62-1.amzn2023.x86_64                                                12/12 

Installed:
  apr-1.7.5-1.amzn2023.0.4.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.62-1.amzn2023.x86_64                    httpd-core-2.4.62-1.amzn2023.x86_64                   
  httpd-filesystem-2.4.62-1.amzn2023.noarch         httpd-tools-2.4.62-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.27-1.amzn2023.0.3.x86_64            mod_lua-2.4.62-1.amzn2023.x86_64                      

Complete!
[ec2-user@ip-172-31-33-34 ~]$

“Complete!”と表示されたらインストール完了

4.Apacheサービス(httpd)のステータス確認

sudo systemctl status httpd

[ec2-user@ip-172-31-33-34 ~]$ sudo systemctl status httpd
○ httpd.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; preset: disabled)
     Active: inactive (dead)
       Docs: man:httpd.service(8)
[ec2-user@ip-172-31-33-34 ~]$

systemctlは、systemdの制御や管理のためのコマンドラインツールで、サービスの起動・停止・再起動や状態確認など、さまざまな操作ができるもの
https://qiita.com/gogonosmarty/items

※サービスは停止しているため起動し、自動起動する様に設定変更する。

5.Apacheサービス(httpd)の起動

sudo systemctl start httpd

<output>
[ec2-user@ip-172-31-33-34 ~]$ sudo systemctl start httpd
[ec2-user@ip-172-31-33-34 ~]$ sudo systemctl status httpd
● httpd.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; preset: disabled)
     Active: active (running) since Mon 2025-02-24 06:10:02 UTC; 2min 45s ago
       Docs: man:httpd.service(8)
   Main PID: 62023 (httpd)
     Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/sec:   0 B/sec"
      Tasks: 177 (limit: 1111)
     Memory: 12.9M
        CPU: 148ms
     CGroup: /system.slice/httpd.service
             ├─62023 /usr/sbin/httpd -DFOREGROUND
             ├─62024 /usr/sbin/httpd -DFOREGROUND
             ├─62025 /usr/sbin/httpd -DFOREGROUND
             ├─62026 /usr/sbin/httpd -DFOREGROUND
             └─62027 /usr/sbin/httpd -DFOREGROUND

Feb 24 06:10:01 ip-172-31-33-34.ap-northeast-1.compute.internal systemd[1]: Starting httpd.service - The A>
Feb 24 06:10:02 ip-172-31-33-34.ap-northeast-1.compute.internal systemd[1]: Started httpd.service - The Ap>
Feb 24 06:10:02 ip-172-31-33-34.ap-northeast-1.compute.internal httpd[62023]: Server configured, listening>
lines 1-19/19 (END) q
[ec2-user@ip-172-31-33-34 ~]$ 

6.Apacheサービス(httpd)の自動起動有効

sudo systemctl enable httpd

<output>
[ec2-user@ip-172-31-33-34 ~]$ sudo systemctl enable httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.
[ec2-user@ip-172-31-33-34 ~]$ sudo systemctl status httpd
● httpd.service - The Apache HTTP Server
     Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; preset: disabled)
     Active: active (running) since Mon 2025-02-24 06:10:02 UTC; 4min 41s ago
       Docs: man:httpd.service(8)
   Main PID: 62023 (httpd)
     Status: "Total requests: 0; Idle/Busy workers 100/0;Requests/sec: 0; Bytes served/sec:   0 B/sec"
      Tasks: 177 (limit: 1111)
     Memory: 12.9M
        CPU: 215ms
     CGroup: /system.slice/httpd.service
             ├─62023 /usr/sbin/httpd -DFOREGROUND
             ├─62024 /usr/sbin/httpd -DFOREGROUND
             ├─62025 /usr/sbin/httpd -DFOREGROUND
             ├─62026 /usr/sbin/httpd -DFOREGROUND
             └─62027 /usr/sbin/httpd -DFOREGROUND

Feb 24 06:10:01 ip-172-31-33-34.ap-northeast-1.compute.internal systemd[1]: Starting httpd.service - The A>
Feb 24 06:10:02 ip-172-31-33-34.ap-northeast-1.compute.internal systemd[1]: Started httpd.service - The Ap>
Feb 24 06:10:02 ip-172-31-33-34.ap-northeast-1.compute.internal httpd[62023]: Server configured, listening>
lines 1-19/19 (END) q
[ec2-user@ip-172-31-33-34 ~]$

7.apacheサービスのバージョン確認

httpd -v

<output>
[ec2-user@ip-172-31-33-34 etc]$ httpd -v
Server version: Apache/2.4.62 (Amazon Linux)
Server built:   Jul 23 2024 00:00:00
[ec2-user@ip-172-31-33-34 etc]$ 

8.PHPのインストール (URL)

PHP は “PHP: Hypertext Preprocessor” を意味する、HTML 埋め込み型の スクリプト言語
https://www.php.net/manual/ja/faq.general.php

スクリプト言語は、開発で使用されるコードのうち、可読性に優れ、読みやすく学びやすい構文で簡易的に記述できるプログラミング言語の汎称
https://staff.persol-xtech.co.jp/hatalabo/it_engineer/558.html#_1

sudo dnf install -y php php-common

<output>
[ec2-user@ip-172-31-33-34 ~]$ sudo dnf install -y php php-common
Last metadata expiration check: 4:37:59 ago on Mon Feb 24 01:55:21 2025.
Dependencies resolved.
===========================================================================================================
 Package                    Architecture     Version                           Repository             Size
===========================================================================================================
Installing:
 php8.3                     x86_64           8.3.10-1.amzn2023.0.1             amazonlinux            10 k
Installing dependencies:
 libsodium                  x86_64           1.0.19-4.amzn2023                 
〜〜略〜〜
  Verifying        : php8.3-xml-8.3.10-1.amzn2023.0.1.x86_64                                         13/13 

Installed:
  libsodium-1.0.19-4.amzn2023.x86_64                    libxslt-1.1.34-5.amzn2023.0.2.x86_64              
  nginx-filesystem-1:1.26.3-1.amzn2023.0.1.noarch       php8.3-8.3.10-1.amzn2023.0.1.x86_64               
  php8.3-cli-8.3.10-1.amzn2023.0.1.x86_64               php8.3-common-8.3.10-1.amzn2023.0.1.x86_64        
  php8.3-fpm-8.3.10-1.amzn2023.0.1.x86_64               php8.3-mbstring-8.3.10-1.amzn2023.0.1.x86_64      
  php8.3-opcache-8.3.10-1.amzn2023.0.1.x86_64           php8.3-pdo-8.3.10-1.amzn2023.0.1.x86_64           
  php8.3-process-8.3.10-1.amzn2023.0.1.x86_64           php8.3-sodium-8.3.10-1.amzn2023.0.1.x86_64        
  php8.3-xml-8.3.10-1.amzn2023.0.1.x86_64              

Complete!
[ec2-user@ip-172-31-33-34 ~]$ 

“Complete!”と表示されたらインストール完了

9.PHPのバージョン確認

php -v

<output>
[ec2-user@ip-172-31-33-34 etc]$ php -v
PHP 8.3.10 (cli) (built: Jul 30 2024 13:44:37) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.3.10, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.10, Copyright (c), by Zend Technologies
[ec2-user@ip-172-31-33-34 etc]$ 

10.MySQLのRPMパッケージインストール(URL)

MySQLは、Oracleが開発してサポートするオープンソースのSQLリレーショナルデータベース管理システム
https://kinsta.com/jp/knowledgebase/what-is-mysql/

sudo dnf localinstall https://dev.mysql.com/get/mysql84-community-release-el9-1.noarch.rpm -y

<output>
[ec2-user@ip-172-31-33-34 ~]$ sudo dnf localinstall https://dev.mysql.com/get/mysql84-community-release-el9-1.noarch.rpm -y
Last metadata expiration check: 6:28:25 ago on Mon Feb 24 01:55:21 2025.
mysql84-community-release-el9-1.noarch.rpm                                  74 kB/s |  13 kB     00:00    
Dependencies resolved.
===========================================================================================================
 Package                               Architecture       Version           Repository                Size
===========================================================================================================
Installing:
 mysql84-community-release             noarch             el9-1             @commandline              13 k

Transaction Summary
===========================================================================================================
Install  1 Package

Total size: 13 k
Installed size: 14 k
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                   1/1 
  Installing       : mysql84-community-release-el9-1.noarch                                            1/1 
  Verifying        : mysql84-community-release-el9-1.noarch                                            1/1 

Installed:
  mysql84-community-release-el9-1.noarch                                                                   

Complete!
[ec2-user@ip-172-31-33-34 ~]$ 

11.MySQLクライアント、サーバのインストール

sudo dnf install -y mysql-community-server mysql-community-client

[ec2-user@ip-172-31-33-34 ~]$ sudo dnf install -y mysql-community-server mysql-community-client
MySQL 8.4 LTS Community Server                                              14 MB/s | 853 kB     00:00    
MySQL Connectors Community                                                 3.3 MB/s |  74 kB     00:00    
MySQL Tools 8.4 LTS Community                                               12 MB/s | 414 kB     00:00    
Dependencies resolved.
===========================================================================================================
 Package                              Architecture Version             Repository                     Size
===========================================================================================================
Installing:
 mysql-community-client               x86_64       8.4.4-1.el9         mysql-8.4-
〜〜略〜〜
  Verifying        : mysql-community-libs-8.4.4-1.el9.x86_64                                           5/6 
  Verifying        : mysql-community-server-8.4.4-1.el9.x86_64                                         6/6 

Installed:
  mysql-community-client-8.4.4-1.el9.x86_64        mysql-community-client-plugins-8.4.4-1.el9.x86_64       
  mysql-community-common-8.4.4-1.el9.x86_64        mysql-community-icu-data-files-8.4.4-1.el9.x86_64       
  mysql-community-libs-8.4.4-1.el9.x86_64          mysql-community-server-8.4.4-1.el9.x86_64               

Complete!
[ec2-user@ip-172-31-33-34 ~]$ 

“Complete!”と表示されたらインストール完了

12.MySQLサービスのステータス確認・起動

sudo systemctl status mysqld
sudo systemctl start mysqld

[ec2-user@ip-172-31-33-34 ~]$ sudo systemctl status mysqld
○ mysqld.service - MySQL Server
     Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; preset: disabled)
     Active: inactive (dead)
       Docs: man:mysqld(8)
             http://dev.mysql.com/doc/refman/en/using-systemd.html
[ec2-user@ip-172-31-33-34 ~]$ sudo systemctl start mysqld
[ec2-user@ip-172-31-33-34 ~]$ 
[ec2-user@ip-172-31-33-34 ~]$ sudo systemctl status mysqld
● mysqld.service - MySQL Server
     Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; preset: disabled)
     Active: active (running) since Mon 2025-02-24 08:44:09 UTC; 8s ago
       Docs: man:mysqld(8)
             http://dev.mysql.com/doc/refman/en/using-systemd.html
    Process: 71269 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
   Main PID: 71337 (mysqld)
     Status: "Server is operational"
      Tasks: 35 (limit: 1111)
     Memory: 453.3M
        CPU: 4.699s
     CGroup: /system.slice/mysqld.service
             └─71337 /usr/sbin/mysqld

Feb 24 08:43:59 ip-172-31-33-34.ap-northeast-1.compute.internal systemd[1]: Starting mysqld.service - MySQ>
Feb 24 08:44:09 ip-172-31-33-34.ap-northeast-1.compute.internal systemd[1]: Started mysqld.service - MySQL>
[ec2-user@ip-172-31-33-34 ~]$

※MySQLはインストールした時点で、自動起動が有効になっている様です。

13.MySQLのバージョン確認

sudo mysql --version

[ec2-user@ip-172-31-33-34 ~]$ sudo mysql --version
mysql  Ver 8.4.4 for Linux on x86_64 (MySQL Community Server - GPL)
[ec2-user@ip-172-31-33-34 ~]$ 

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA