PHP7.4のインストール【CentOS 7】

PHP7.4のインストールを行います。

1. PHPのインストール

yumでインストールを行います。
# yum install --enablerepo=epel,remi,remi-php74 php
# yum install --enablerepo=epel,remi,remi-php74 php-devel

2. 関連モジュールのインストール

(1) libmcryptのインストール
# yum install --enablerepo=epel libmcrypt

(2) libargon2のインストール
php-pdoをインストールする場合に必要です。
# yum install --enablerepo=epel libargon2
# yum install --enablerepo=epel libargon2-devel

(3) libraqmのインストール
# yum --enablerepo=epel install libraqm

3. phpのモジュールのインストール

これは任意ですが、必要なモジュールをインストールします。
# yum install --enablerepo=remi,remi-php74 php-mysqlnd
# yum install --enablerepo=remi,remi-php74 php-mbstring
# yum install --enablerepo=remi,remi-php74 php-gd
# yum install --enablerepo=remi,remi-php74 php-xmlrpc
# yum install --enablerepo=remi,remi-php74 php-pecl-mcrypt
# yum install --enablerepo=remi,remi-php74 php-fpm
# yum install --enablerepo=remi,remi-php74 php-opcache
# yum install --enablerepo=remi,remi-php74 php-pecl-apcu
# yum install --enablerepo=remi,remi-php74 php-pecl-zip
# yum install --enablerepo=remi,remi-php74 php-pear

4. /etc/php.iniの編集

(1) php.iniの編集
# cp /etc/php.ini{,.default}
# vi /etc/php.ini

expose_php = Off
max_execution_time = 60
memory_limit = 512M
※サーバの搭載メモリにより調整して下さい。

error_reporting = E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT
; Log errors to specified file. PHP's default behavior is to leave this value
; empty.
; http://php.net/error-log
; Example:
;error_log = php_errors.log
; Log errors to syslog.
;error_log = syslog
※php-fpmを利用している場合には、エラーログは「/var/log/php-fpm/www-error.log」に書き込まれます。(当初はこのファイルは存在しません。エラーが発生すると自動的に作成されます。)
「/etc/php-fpm.d/www.conf」にログファイル名が記載されています。

post_max_size = 20M
upload_max_filesize = 20M
date.timezone = "Asia/Tokyo"
session.gc_maxlifetime = 7200
mbstring.language = Japanese
mbstring.internal_encoding = UTF-8
mbstring.http_input = pass
mbstring.http_output = pass

(2) pearモジュールのインストール
必要に応じて、PEARのモジュールをインストールします。
【例】PEAR Mailモジュールのインストール。
# pear install -a Mail
※「-a」オプションを付けると、依存ファイルもすべてインストールします。

(3) Apacheの再起動
Apacheを再起動して、PHPを有効にします。
# systemctl restart httpd.service

5. php-fpmサービスの起動

# systemctl enable php-fpm.service
# systemctl start php-fpm.service

6. HTMLページをPHPとして動作させる場合の設定

通常、HTMLページをPHPとして動作させる場合には、.htaccessに
<FilesMatch \.html$>
  AddHandler application/x-httpd-php .html
</FilesMatch>
と記述すれば動作するはずですが、php-fpmを利用している場合には、設定が無効になる場合があります。
その場合には、以下のように設定します。

(1) .htacces
「/etc/httpd/conf.d/php.conf」において
# Redirect to local php-fpm (no mod_php in default configuration)
<IfModule !mod_php5.c>
  <IfModule !mod_php7.c>
    ()
    <FilesMatch \.(php|phar)$>
        SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost"
    </FilesMatch>
  </IfModule>
</IfModule>
というような記述があります。
そこで、同様にfpmに処理を引き渡す必要があるので、以下のように.htaccessを記述します。
<FilesMatch \.html$>
  SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost"
</FilesMatch>

(2) /etc/php-fpm.d/www.conf
デフォルトでは、PHPコードを実行可能なファイル拡張子が「.php」のみに制限されているため、「.html」も使用できるように設定します。
; Limits the extensions of the main script FPM will allow to parse. This can
; prevent configuration mistakes on the web server side. You should only limit
; FPM to .php extensions to prevent malicious users to use other extensions to
; exectute php code.
; Note: set an empty value to allow all extensions.
; Default Value: .php
;security.limit_extensions = .php .php3 .php4 .php5 .php7
の箇所の「security.limit_extensions」の項目を以下のように変更します。
security.limit_extensions = .php .html

php-fpmを再起動します。
# systemctl restart php-fpm.service


最終更新:2021年11月30日 09:18