LoginSignup
0
0

More than 1 year has passed since last update.

CentOS 8にPHP 7.4を標準リポジトリからインストールする

Last updated at Posted at 2021-11-09

What's?

CentOS 8の標準リポジトリで、PHP 7.4がインストールできそうだったので試してみようかと。

環境

今回の環境は、CentOS 8.4です。

$ cat /etc/redhat-release
CentOS Linux release 8.4.2105


$ uname -srvmpio
Linux 4.18.0-305.25.1.el8_4.x86_64 #1 SMP Wed Nov 3 10:29:07 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

CentOSとPHP

CentOSでPHPをインストールするといえば、Remi Repositoryを使うような気がします。

Remi Repositoryを使うと、PHP 7.4までではなく、現時点でPHP 8.0までインストール可能なのですが、今回はCentOSの標準リポジトリを使うことにします。

Remi Repositoryを使用してインストールする場合は、こちらへ。

PHP 7.4をインストールする

CentOS 8でPHPのモジュールに関する情報を見ると、以下の3つのストリームがあることが確認できます。

$ sudo dnf module list php
Last metadata expiration check: 0:08:26 ago on 2021年11月09日 17時02分41秒.
CentOS Linux 8 - AppStream
Name        Stream         Profiles                          Summary
php         7.2 [d]        common [d], devel, minimal        PHP scripting language
php         7.3            common [d], devel, minimal        PHP scripting language
php         7.4            common [d], devel, minimal        PHP scripting language

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

デフォルトでは7.2を向いているので、sudo dnf install phpとしたりすると、PHP 7.2がインストールされることになります。

なので、sudo dnf module installでストリームを指定してインストール。

$ sudo dnf module install php:7.4

これだけのパッケージがインストールされます。

==========================================================================================
 Package            Arch     Version                                    Repository   Size
==========================================================================================
Installing group/module packages:
 php-cli            x86_64   7.4.6-4.module_el8.3.0+434+2ab5050a        appstream   3.1 M
 php-common         x86_64   7.4.6-4.module_el8.3.0+434+2ab5050a        appstream   696 k
 php-fpm            x86_64   7.4.6-4.module_el8.3.0+434+2ab5050a        appstream   1.6 M
 php-json           x86_64   7.4.6-4.module_el8.3.0+434+2ab5050a        appstream    74 k
 php-mbstring       x86_64   7.4.6-4.module_el8.3.0+434+2ab5050a        appstream   484 k
 php-xml            x86_64   7.4.6-4.module_el8.3.0+434+2ab5050a        appstream   174 k
Installing dependencies:
 httpd-filesystem   noarch   2.4.37-39.module_el8.4.0+950+0577e6ac.1    appstream    39 k
 libxslt            x86_64   1.1.32-6.el8                               baseos      250 k
 nginx-filesystem   noarch   1:1.14.1-9.module_el8.0.0+184+e34fea82     appstream    24 k
 oniguruma          x86_64   6.8.2-2.el8                                appstream   187 k
Installing module profiles:
 php/common
Enabling module streams:
 httpd                       2.4
 nginx                       1.14
 php                         7.4

確認。

$ php --version
PHP 7.4.6 (cli) (built: May 12 2020 08:09:15) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

ちょっと古い気がします…。

再度モジュールを確認すると、こんな状態になっています。

$ sudo dnf module list php
Last metadata expiration check: 0:12:05 ago on 2021年11月09日 17時02分41秒.
CentOS Linux 8 - AppStream
Name       Stream        Profiles                             Summary
php        7.2 [d]       common [d], devel, minimal           PHP scripting language
php        7.3           common [d], devel, minimal           PHP scripting language
php        7.4 [e]       common [d] [i], devel, minimal       PHP scripting language

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

Apacheをインストールして動作確認

次に、Apacheをインストールして動作確認してみましょう。

$ sudo dnf install httpd

インストールされたApacheのバージョン。

$ httpd -V
Server version: Apache/2.4.37 (centos)
Server built:   Oct 12 2021 23:35:12
Server's Module Magic Number: 20120211:83
Server loaded:  APR 1.6.3, APR-UTIL 1.6.1
Compiled using: APR 1.6.3, APR-UTIL 1.6.1
Architecture:   64-bit
Server MPM:     event
  threaded:     yes (fixed thread count)
    forked:     yes (variable process count)
Server compiled with....
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_SYSVSEM_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=256
 -D HTTPD_ROOT="/etc/httpd"
 -D SUEXEC_BIN="/usr/sbin/suexec"
 -D DEFAULT_PIDLOG="run/httpd.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="conf/mime.types"
 -D SERVER_CONFIG_FILE="conf/httpd.conf"

ちなみに、PHPをインストールした時点でPHP用の設定ファイルが用意されているので、個別にPHP用の設定をしなくても使い始められます。

$ grep -v ' *#' /etc/httpd/conf.d/php.conf
<Files ".user.ini">
    Require all denied
</Files>

AddType text/html .php

DirectoryIndex index.php

<IfModule !mod_php5.c>
  <IfModule !mod_php7.c>
    SetEnvIfNoCase ^Authorization$ "(.+)" HTTP_AUTHORIZATION=$1

    <FilesMatch \.(php|phar)$>
        SetHandler "proxy:unix:/run/php-fpm/www.sock|fcgi://localhost"
    </FilesMatch>
  </IfModule>
</IfModule>

<IfModule  mod_php7.c>
    <FilesMatch \.(php|phar)$>
        SetHandler application/x-httpd-php
    </FilesMatch>


    php_value session.save_handler "files"
    php_value session.save_path    "/var/lib/php/session"
    php_value soap.wsdl_cache_dir  "/var/lib/php/wsdlcache"

</IfModule>

mod_php用の設定と、FPM用の設定の両方が入っていますね。

DocumentRoot/var/www/htmlなので

$ grep DocumentRoot /etc/httpd/conf/httpd.conf
# DocumentRoot: The directory out of which you will serve your
DocumentRoot "/var/www/html"
    # access content that does not live under the DocumentRoot.

index.phpを作成して

/var/www/html/index.php
<?php

phpinfo();

Apacheを起動。

$ sudo systemctl enable httpd
$ sudo systemctl start httpd

http://localhostにアクセスして、確認。

image.png

Server APIを見ると、FPM/FastCGIで動作していることが確認できます。

そういえば、ApacheはEvent MPMでしたね。

Server MPM:     event
  threaded:     yes (fixed thread count)
    forked:     yes (variable process count)

これで、インストールと動作確認ができました。

余談

この方法でPHPをインストールすると、nginx向けの設定ファイルも用意してくれるみたいです。

/etc/nginx/default.d/php.conf
# pass the PHP scripts to FastCGI server
#
# See conf.d/php-fpm.conf for socket configuration
#
index index.php index.html index.htm;

location ~ \.(php|phar)(/.*)?$ {
    fastcgi_split_path_info ^(.+\.(?:php|phar))(/.*)$;

    fastcgi_intercept_errors on;
    fastcgi_index  index.php;
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_param  PATH_INFO $fastcgi_path_info;
    fastcgi_pass   php-fpm;
}
/etc/nginx/conf.d/php-fpm.conf
# PHP-FPM FastCGI server
# network or unix domain socket configuration

upstream php-fpm {
        server unix:/run/php-fpm/www.sock;
}

なので、Apache用の設定ファイルもあらかじめ置かれていることになりますね。

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