LoginSignup
0
0

More than 1 year has passed since last update.

【AWS】WebサーバーにPHPをインストール(Part1)

Last updated at Posted at 2023-01-26

【TOPへ戻る】
 Part1:WebサーバーにPHPをインストール
 Part2:WordPress用にデータベース作成
 Part3:WordPressのインストールと設定

Part1:WebサーバーにPHPをインストール

作成したWebサーバーに接続し、PHPをインストールしていきたいと思う。

※ PHPver7.4以上をインストール必要がある。
  WordPressサイトが機能するために、PHP拡張機能もインストールする必要がある。
  必要なPHP拡張機能はこちらで確認できます。
  今回は、必要最低限の拡張機能(パッケージ)をインストールします。

WEBサーバー(EC2)にPHPインストール
### 現在のEC2(Amazon Linux 2)のyumに紐づく標準リポジトリでは、PHPのバージョンは[5.4]しか提供されていない。
[root:/]# yum list |grep php
php.x86_64                             5.4.16-46.amzn2.0.2            amzn2-core
-- 省略 --

### amazon-linux-extras(EPELのようなモノ)では、PHPのバージョンが[7.4]以上のものが提供されている。
[root:/]# amazon-linux-extras list |grep php
  _  php8.0                   available    [ =stable ]
 66  php8.1                   available    [ =stable ]

### 今回インストールする[PHP ver 8.1]には、下記のパッケージが含まれている。
[root:/]# amazon-linux-extras info php8.1
php8.1 recommends php-cli                    # yum install php-cli
php8.1 recommends php-pdo                    # yum install php-pdo
php8.1 recommends php-fpm                    # yum install php-fpm
php8.1 recommends php-json                   # yum install php-json
php8.1 recommends php-mysqlnd                # yum install php-mysqlnd

### [PHP ver 8.1]をインストールする。
[root:/]# amazon-linux-extras install php8.1
Installing php-pdo, php-fpm, php-mysqlnd, php-cli, php-json
-- 省略 --
==================================================================================================
 Package              Arch            Version                    Repository                  Size
==================================================================================================
Installing:
 php-cli              x86_64          8.1.12-1.amzn2             amzn2extra-php8.1          5.6 M
 php-common           x86_64          8.1.12-1.amzn2             amzn2extra-php8.1          1.2 M
 php-fpm              x86_64          8.1.12-1.amzn2             amzn2extra-php8.1          1.9 M
 php-mysqlnd          x86_64          8.1.12-1.amzn2             amzn2extra-php8.1          184 k
 php-pdo              x86_64          8.1.12-1.amzn2             amzn2extra-php8.1          125 k
Installing for dependencies:
 libzip               x86_64          1.3.2-1.amzn2.0.1          amzn2-core                  62 k

Transaction Summary
==================================================================================================
Install  5 Packages (+1 Dependent package)

Total download size: 9.1 M
Installed size: 41 M
Is this ok [y/d/N]: y
Downloading packages:
(1/6): libzip-1.3.2-1.amzn2.0.1.x86_64.rpm                                 |  62 kB  00:00:00
(2/6): php-common-8.1.12-1.amzn2.x86_64.rpm                                | 1.2 MB  00:00:00
(3/6): php-fpm-8.1.12-1.amzn2.x86_64.rpm                                   | 1.9 MB  00:00:00
(4/6): php-cli-8.1.12-1.amzn2.x86_64.rpm                                   | 5.6 MB  00:00:00
(5/6): php-mysqlnd-8.1.12-1.amzn2.x86_64.rpm                               | 184 kB  00:00:00
(6/6): php-pdo-8.1.12-1.amzn2.x86_64.rpm                                   | 125 kB  00:00:00
--------------------------------------------------------------------------------------------------
Total                                                              21 MB/s | 9.1 MB  00:00:00
-- 省略 --
Installed:
  php-cli.x86_64 0:8.1.12-1.amzn2               php-common.x86_64 0:8.1.12-1.amzn2
  php-fpm.x86_64 0:8.1.12-1.amzn2               php-mysqlnd.x86_64 0:8.1.12-1.amzn2
  php-pdo.x86_64 0:8.1.12-1.amzn2

Dependency Installed:
  libzip.x86_64 0:1.3.2-1.amzn2.0.1

Complete!
-- 省略 --

### 正常にインストールされている。
[root:/]# yum list installed |grep php
php-cli.x86_64                        8.1.12-1.amzn2                 @amzn2extra-php8.1
php-common.x86_64                     8.1.12-1.amzn2                 @amzn2extra-php8.1
php-fpm.x86_64                        8.1.12-1.amzn2                 @amzn2extra-php8.1
php-mysqlnd.x86_64                    8.1.12-1.amzn2                 @amzn2extra-php8.1
php-pdo.x86_64                        8.1.12-1.amzn2                 @amzn2extra-php8.1


### PHPで使う追加のパッケージをインストールする。
### 先にamazon-linux-extrasでPHPver8.1をインストールした為、それに対応するパッケージがインストールされる。
[root:/]# yum install php php-mbstring
-- 省略 --
==================================================================================================
 Package              Arch           Version                      Repository                 Size
==================================================================================================
Installing:
 php                  x86_64         8.1.12-1.amzn2               amzn2extra-php8.1         3.7 M
 php-mbstring         x86_64         8.1.12-1.amzn2               amzn2extra-php8.1         472 k
Installing for dependencies:
 oniguruma            x86_64         5.9.6-1.amzn2.0.4            amzn2-core                127 k

Transaction Summary
==================================================================================================
Install  2 Packages (+1 Dependent package)

Total download size: 4.2 M
Installed size: 14 M
Is this ok [y/d/N]: y
Downloading packages:
(1/3): oniguruma-5.9.6-1.amzn2.0.4.x86_64.rpm                              | 127 kB  00:00:00
(2/3): php-mbstring-8.1.12-1.amzn2.x86_64.rpm                              | 472 kB  00:00:00
(3/3): php-8.1.12-1.amzn2.x86_64.rpm                                       | 3.7 MB  00:00:00
--------------------------------------------------------------------------------------------------
Total                                                              12 MB/s | 4.2 MB  00:00:00
-- 省略 --

Installed:
  php.x86_64 0:8.1.12-1.amzn2                 php-mbstring.x86_64 0:8.1.12-1.amzn2

Dependency Installed:
  oniguruma.x86_64 0:5.9.6-1.amzn2.0.4

Complete!

### 最後にPHPのバージョンを確認する。
[root:/]# php -v
PHP 8.1.12 (cli) (built: Oct 31 2022 22:06:27) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.12, Copyright (c) Zend Technologies
[root:/]#

#### 備考 ####
### amazon-linux-extrasを使用しないでPHPをインストールした場合、PHPver5.4がインストールされるので注意
[root:/]# yum list installed |grep php
php.x86_64                            5.4.16-46.amzn2.0.2            @amzn2-core
php-cli.x86_64                        5.4.16-46.amzn2.0.2            @amzn2-core
php-common.x86_64                     5.4.16-46.amzn2.0.2            @amzn2-core
[root:/]# php -v
PHP 5.4.16 (cli) (built: Oct 31 2019 18:34:05)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

Part2:WordPress用にデータベース作成 に続く・・・・。

【PHPとは】
PHPとは動的にWebページを生成することができるサーバーサイドのスクリプト言語です。MySQLなどのデータベースとの連携が容易なことなどから、WordPressを含めたWebアプリケーションの開発にもよく使われる有名なスクリプト言語でもあります。

【amazon-linux-extras って何?】
ざっくり言うと、標準リポジトリには無い最新のパッケージ(ソフトウェア)をインストールするためのコマンドのこと。

Link1:Extras library (Amazon Linux 2)
Amazon Linux 2 では、Extras Library を使用してアプリケーションおよびソフトウェア更新をインスタンスにインストールできます。特定のバージョンのトピックをインストールしたり、最新バージョンを使用するためにバージョン情報を省略したりすることができます。

Link2:AWS EC2 の amazon-linux-extras の話
amazon-linux-extras とは、Amazon Linux 2 インスタンスにある PHP、Python、Golang、MariaDB などのパッケージをより新しいバージョンで利用できるようマネジメント管理するコマンド。パッケージを最新にアップグレードする時に使える便利なコマンドです。

Link3:Amazon Linux 2のExtras Library(amazon-linux-extras)を使ってみた
Amazon Linux 2には Extras Library と呼ばれるパッケージ群が存在し、Python、Golang、MariaDB、Ansible などの特定のパッケージをより新しいバージョンで利用することが出来ます。 Extra Packages for Enterprise Linux (EPEL) のような位置づけで、通常はディストリビューション標準のパッケージを利用する一方で、特定のパケージではより新しいバージョンを試せます。

【EPEL(Extra Packages for Enterprise Linux)とは】
ざっくり言うと、標準リポジトリでは取得できないパッケージを取得できることができるサードパーティーのリポジトリのことである。

Link1:あらためてEPELリポジトリの使い方をまとめてみた
Fedoraプロジェクトの有志がビルドした、Red Hat Enterprise Linux (RHEL) 系Linuxディストリビューション向けオプションパッケージ群だ。LinuxのメディアやYumリポジトリに含まれないパッケージ入手先の第1候補になる。

Link2:AlmaLinuxでWordPressサーバを構築
エンタープライズLinux用の高品質な追加パッケージセットが含まれているリポジトリです。注意が必要なことは、標準のリポジトリで提供されていないモジュールを使用することができますが、Red Hat社などからのサポートはなく、「自己責任のもとで使用」する必要があります。

備考

インストールされたPHPパッケージ詳細一覧
[root:/]$ yum info php-cli
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Installed Packages
Name        : php-cli
Arch        : x86_64
Version     : 8.1.12
Release     : 1.amzn2
Size        : 18 M
Repo        : installed
From repo   : amzn2extra-php8.1
Summary     : Command-line interface for PHP
URL         : http://www.php.net/
License     : PHP and Zend and BSD and MIT and ASL 1.0
Description : The php-cli package contains the command-line interface
            : executing PHP scripts, /usr/bin/php, and the CGI interface.

[root:/]$ yum info php-common
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Installed Packages
Name        : php-common
Arch        : x86_64
Version     : 8.1.12
Release     : 1.amzn2
Size        : 16 M
Repo        : installed
From repo   : amzn2extra-php8.1
Summary     : Common files for PHP
URL         : http://www.php.net/
License     : PHP and BSD
Description : The php-common package contains files used by both the php
            : package and the php-cli package.

[root:/]$ yum info php-fpm
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Installed Packages
Name        : php-fpm
Arch        : x86_64
Version     : 8.1.12
Release     : 1.amzn2
Size        : 6.1 M
Repo        : installed
From repo   : amzn2extra-php8.1
Summary     : PHP FastCGI Process Manager
URL         : http://www.php.net/
License     : PHP and Zend and BSD and MIT and ASL 1.0
Description : PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI
            : implementation with some additional features useful for sites of
            : any size, especially busier sites.

[root:/]$ yum info php-mysqlnd
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Installed Packages
Name        : php-mysqlnd
Arch        : x86_64
Version     : 8.1.12
Release     : 1.amzn2
Size        : 805 k
Repo        : installed
From repo   : amzn2extra-php8.1
Summary     : A module for PHP applications that use MySQL databases
URL         : http://www.php.net/
License     : PHP
Description : The php-mysqlnd package contains a dynamic shared object that will add
            : MySQL database support to PHP. MySQL is an object-relational database
            : management system. PHP is an HTML-embeddable scripting language. If
            : you need MySQL support for PHP applications, you will need to install
            : this package and the php package.
            :
            : This package use the MySQL Native Driver

[root:/]$ yum info php-pdo
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Installed Packages
Name        : php-pdo
Arch        : x86_64
Version     : 8.1.12
Release     : 1.amzn2
Size        : 406 k
Repo        : installed
From repo   : amzn2extra-php8.1
Summary     : A database access abstraction module for PHP applications
URL         : http://www.php.net/
License     : PHP
Description : The php-pdo package contains a dynamic shared object that will add
            : a database access abstraction layer to PHP.  This module provides
            : a common interface for accessing MySQL, PostgreSQL or other
            : databases.

[root:/]$ yum info libzip
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Installed Packages
Name        : libzip
Arch        : x86_64
Version     : 1.3.2
Release     : 1.amzn2.0.1
Size        : 117 k
Repo        : installed
From repo   : amzn2-core
Summary     : C library for reading, creating, and modifying zip archives
URL         : https://libzip.org/
License     : BSD
Description : libzip is a C library for reading, creating, and modifying zip archives. Files
            : can be added from data buffers, files, or compressed data copied directly from
            : other zip archives. Changes made without closing the archive can be reverted.
            : The API is documented by man pages.

[root:/]$ yum info php
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Installed Packages
Name        : php
Arch        : x86_64
Version     : 8.1.12
Release     : 1.amzn2
Size        : 11 M
Repo        : installed
From repo   : amzn2extra-php8.1
Summary     : PHP scripting language for creating dynamic web sites
URL         : http://www.php.net/
License     : PHP and Zend and BSD and MIT and ASL 1.0
Description : PHP is an HTML-embedded scripting language. PHP attempts to make it
            : easy for developers to write dynamically generated web pages. PHP also
            : offers built-in database integration for several commercial and
            : non-commercial database management systems, so writing a
            : database-enabled webpage with PHP is fairly simple. The most common
            : use of PHP coding is probably as a replacement for CGI scripts.
            :
            : The php package contains the module (often referred to as mod_php)
            : which adds support for the PHP language to Apache HTTP Server. This
            : is not the supported way of running PHP, the php-fpm package should be
            : used instead.

[root:/]$ yum info php-mbstring
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Installed Packages
Name        : php-mbstring
Arch        : x86_64
Version     : 8.1.12
Release     : 1.amzn2
Size        : 2.0 M
Repo        : installed
From repo   : amzn2extra-php8.1
Summary     : A module for PHP applications which need multi-byte string handling
URL         : http://www.php.net/
License     : PHP and LGPLv2 and OpenLDAP
Description : The php-mbstring package contains a dynamic shared object that will add
            : support for multi-byte string handling to PHP.

[root:/]$ yum info oniguruma
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Installed Packages
Name        : oniguruma
Arch        : x86_64
Version     : 5.9.6
Release     : 1.amzn2.0.4
Size        : 547 k
Repo        : installed
From repo   : amzn2-core
Summary     : Regular expressions library
URL         : http://www.geocities.jp/kosako3/oniguruma/
License     : BSD
Description : Oniguruma is a regular expressions library.
            : The characteristics of this library is that different character encoding
            : for every regular expression object can be specified.
            : (supported APIs: GNU regex, POSIX and Oniguruma native)
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