LoginSignup
165
175

More than 3 years have passed since last update.

CentOS 7 に PHP 7.4 と phpMyAdmin をインストール

Last updated at Posted at 2015-08-01

導入

CentOS 7 の公式リポジトリでは、非常に古いバージョンである PHP 5.4 が提供されています。PHP 5.4 〜 7.1 はアクティブサポートの終了に加え、セキュリティサポートまで終了したバージョンです。1

RHEL/CentOS 6 では、バックポートなどによって PHP 5.3 のセキュリティが保たれているような状況ではあります。しかしながら後方互換性を保つ開発は困難であるため、古い PHP で利用できるライブラリは限られてきます。また、そのような開発はコストを増加させ、Web の発展を遅らせる要因ともなり得ます。 (もっとも普及しているPHPテストフレームワークのPHPUnitが、すでにPHP 7.1以下のサポートを完全に終了しています 2)

そこで remi リポジトリを追加し、そちらから最新バージョンをインストールします。

PHP のインストール

remi リポジトリの公式サイトには長い指紋鍵が記載されていないため、English : Repository Configuration — Remi’s RPM repository — Blog の「4. Remi's signature Installation (GPG Key)」に記載されているURL (「This GPG key」のリンク先) から、あらかじめ GPG 公開鍵を取り込んでおきます。remi-release の依存関係で epel-release がインストールされます。

# remiリポジトリの公開鍵を取り込む
sudo rpm --import https://rpms.remirepo.net/RPM-GPG-KEY-remi

# yum-config-manager コマンドのインストールと remiリポジトリの追加
sudo yum install yum-utils https://rpms.remirepo.net/enterprise/remi-release-7.rpm

# remi-php74 リポジトリを有効化
sudo yum-config-manager --enable remi-php74

# PHP と拡張モジュールなどをインストール
sudo yum install php php-mbstring php-intl
  • php-mbstring (マルチバイト文字列拡張モジュール) は、文字列 (UTF-8) のセキュリティを確保するのに必要です。ComposerやphpMyAdminをyumでインストールする場合、依存関係で自動的にインストールされます。
  • php-intl (Intl拡張モジュール) は、文字列 (Unicode) に関する処理などに必要です。Composerなどをyumでインストールする場合、依存関係で自動的にインストールされます。
  • yum リポジトリでは PECL 拡張モジュールも提供されているので、pecl コマンドでコンパイルを行う必要が無い場合もあります。
  • 必要な拡張モジュールがあれば、合わせてインストールしておきます。

次の記事などを参考に PHP の設定を行います。

DBMS ドライバのインストール

# PDO_MYSQLのインストール
sudo yum install php-mysqlnd

php-mysqlnd は、利用するデータベース管理システムに合わせて読み替えてください。以下は関係データベース管理システムのドライバとパッケージ名の対応表です。

RDBMS 拡張モジュール RPM パッケージ
Adaptive Server Enterprise PDO_DBLIB php-mssql
DB2 PDO_ODBC php-odbc
Firebird PDO_FIREBIRD php-interbase
InterBase PDO_FIREBIRD php-interbase
MariaDB PDO_MYSQL php-mysqlnd
Microsoft SQL Server PDO_DBLIB php-mssql
MySQL PDO_MYSQL php-mysqlnd
Oracle Database PDO_OCI pdo-oci8
PostgreSQL PDO_PGSQL php-pgsql
SQLite PDO_SQLITE php-pdo

上の表にないデータベースに関しては、development/languagesパッケージ一覧 から拡張モジュールの RPM パッケージを見つけることができるかもしれません。

phpMyAdmin

この節の解説は、前節PHP のインストールの手順に従って remi-php74 リポジトリが有効化されていることを前提としていますので、ご注意ください。

インストールするパッケージの制限

phpMyAdmin 最新安定版のパッケージは remi リポジトリにありますが、remi リポジトリを有効化すると、PHP のバージョンに関係ない標準リポジトリなどのパッケージ (remi リポジトリからインストールする必要のないパッケージ) が上書きされます。そこで remi リポジトリにおいて、phpMyAdmin と依存パッケージのみ有効にします。

リポジトリ定義ファイル (*.repo) でシンタックスハイライトを有効化する場合は、*.repo のシンタックスハイライトを参照してください。

/etc/yum.repos.d/remi.repo
[remi]
name=Remi's RPM repository for Enterprise Linux 7 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/7/remi/$basearch/
mirrorlist=http://rpms.remirepo.net/enterprise/7/remi/mirror
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
includepkgs=phpMyAdmin php-phpmyadmin-* php-symfony-* php-symfony4-* php-tcpdf php-tcpdf-dejavu-sans-fonts php-pragmarx-google2fa-qrcode php-pragmarx-google2fa5 php-williamdes-mariadb-mysql-kbs php-twig2 php-psr-event-dispatcher php-nikic-fast-route
## ↑この行を追加 ##

                        ##############
                        ## 以下省略 ##
                        ##############

インストール

EPEL7のGPG公開鍵を取り込んでも良いか訊ねられた場合、Fingerprintが https://getfedora.org/security/ の指紋と大文字小文字無視で一致するか確認します。

# remi リポジトリを有効化
sudo yum-config-manager --enable remi

# インストール
sudo yum install phpMyAdmin

依存性関連でインストールされる php-pear パッケージは、ディレクトリ /var/www/html が読み取り専用の場合インストールに失敗するのでご注意ください。

phpMyAdmin 用データベースの作成

/usr/share/phpMyAdmin/sql/create_tables.sql
                        --------------
                        -- 以上省略 --
                        --------------

-- --------------------------------------------------------

--
-- Privileges
--
-- (activate this statement if necessary)
GRANT SELECT, INSERT, DELETE, UPDATE, ALTER ON `phpmyadmin`.* TO
  'pma'@localhost;
-- ↑この2行のコメントアウトを外す --

-- --------------------------------------------------------

                        --------------
                        -- 以下省略 --
                        --------------

mysql --user=root --password を実行し、MariaDB の root ユーザーでログインします。3

-- phpMyAdmin用のデータベース・ユーザーを作成
source /usr/share/phpMyAdmin/sql/create_tables.sql

-- パスワードの設定
SET PASSWORD FOR pma@localhost = PASSWORD('⦅pmaのパスワード⦆');

-- 権限テーブルの再読み込み
FLUSH PRIVILEGES;

-- ログアウト
exit

設定

/etc/phpMyAdmin/config.inc.php
                        //////////////
                        // 以上省略 //
                        //////////////

/**
 * phpMyAdmin configuration storage settings.
 */

/* User used to manipulate with storage */
// $cfg['Servers'][$i]['controlhost'] = '';
// $cfg['Servers'][$i]['controlport'] = '';
$cfg['Servers'][$i]['controluser'] = 'pma';                 // コメントアウトを外す //
$cfg['Servers'][$i]['controlpass'] = '⦅pmaのパスワード⦆';   // コメントアウトを外し、「pmapass」を修正 //

/* Storage database and tables */
$cfg['Servers'][$i]['pmadb'] = 'phpmyadmin';
$cfg['Servers'][$i]['bookmarktable'] = 'pma__bookmark';
$cfg['Servers'][$i]['relation'] = 'pma__relation';
$cfg['Servers'][$i]['table_info'] = 'pma__table_info';
$cfg['Servers'][$i]['table_coords'] = 'pma__table_coords';
$cfg['Servers'][$i]['pdf_pages'] = 'pma__pdf_pages';
$cfg['Servers'][$i]['column_info'] = 'pma__column_info';
$cfg['Servers'][$i]['history'] = 'pma__history';
$cfg['Servers'][$i]['table_uiprefs'] = 'pma__table_uiprefs';
$cfg['Servers'][$i]['tracking'] = 'pma__tracking';
$cfg['Servers'][$i]['userconfig'] = 'pma__userconfig';
$cfg['Servers'][$i]['recent'] = 'pma__recent';
$cfg['Servers'][$i]['favorite'] = 'pma__favorite';
$cfg['Servers'][$i]['users'] = 'pma__users';
$cfg['Servers'][$i]['usergroups'] = 'pma__usergroups';
$cfg['Servers'][$i]['navigationhiding'] = 'pma__navigationhiding';
$cfg['Servers'][$i]['savedsearches'] = 'pma__savedsearches';
$cfg['Servers'][$i]['central_columns'] = 'pma__central_columns';
$cfg['Servers'][$i]['designer_settings'] = 'pma__designer_settings';
$cfg['Servers'][$i]['export_templates'] = 'pma__export_templates';
        // ↑ここまでコメントアウトを外す //

/**
 * End of servers configuration
 */

                        //////////////
                        //   中略   //
                        //////////////

/*
 * You can find more configuration options in the documentation
 * in the doc/ folder or at <http://docs.phpmyadmin.net/>.
 */

// root アクセスの禁止
$cfg['Servers'][$i]['AllowRoot'] = false;
// ↑ファイル末尾にこの行を追加 //

Apache の設定

URL

/etc/httpd/conf.d/phpMyAdmin.conf
# phpMyAdmin - Web based MySQL browser written in php
# 
# Allows only localhost by default
#
# But allowing phpMyAdmin to anyone other than localhost should be considered
# dangerous unless properly secured by SSL

#Alias /phpMyAdmin /usr/share/phpMyAdmin
#Alias /phpmyadmin /usr/share/phpMyAdmin
## ↑コメントアウトする ##

<Directory /usr/share/phpMyAdmin/>

                        ##############
                        ## 以下省略 ##
                        ##############

Apache で扱うドメイン名が一つの場合は上でコメントアウトした箇所の下に、複数の場合は phpMyAdmin にアクセスするドメイン名の セクションに Alias /⦅任意の英数字⦆ /usr/share/phpMyAdmin を記述します。4

アクセス制限

デフォルトではローカルのブラウザからしかアクセスできません。以下のように Requireディレクティブ を変更すると、どこからでもアクセスできるようになります。

アクセス元のIPアドレスが決まっている場合は、Require ip 2001:db8::a00:20ff:fea7:ccea のようにアクセスを制限した方が良いでしょう。
Requireディレクティブの引数について、詳しくは mod_authz_hostのドキュメント(英語) をご覧ください。

TLS による暗号化 (HTTPS) などを用いずに外部から phpMyAdmin アクセスすると、データベースのユーザー名やパスワードがネットワーク上を平文で流れることになり大変危険です。

/etc/httpd/conf.d/phpMyAdmin.conf
# phpMyAdmin - Web based MySQL browser written in php
# 
# Allows only localhost by default
#
# But allowing phpMyAdmin to anyone other than localhost should be considered
# dangerous unless properly secured by SSL

Alias /phpMyAdmin /usr/share/phpMyAdmin
Alias /phpmyadmin /usr/share/phpMyAdmin

<Directory /usr/share/phpMyAdmin/>
   AddDefaultCharset UTF-8

   <IfModule mod_authz_core.c>
     # Apache 2.4
     Require all granted
     ## ↑ Require ディレクティブの引数を「local」から「all granted」に
   </IfModule>
   <IfModule !mod_authz_core.c>
     # Apache 2.2
     Order Deny,Allow
     Deny from All
     Allow from 127.0.0.1
     Allow from ::1
   </IfModule>
</Directory>

                        ##############
                        ## 以下省略 ##
                        ##############

  1. PHP: Supported Versions 

  2. Supported Versions of PHPUnit – The PHP Testing Framework 

  3. いざログインしようとしたときに認証失敗しないよう、記号や暗号化方式の互換性 に目を通しておいてください。 

  4. URL のパス部分にそのまま使用可能なASCII記号 !$&'()*+,-.:;=@[]^_|~ を混ぜたパスにしたところ、phpMyAdmin へログインしてもログイン前のページしか表示されなかった。 

165
175
28

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
165
175