본문 바로가기

리눅스

CentOS 7에서 PHP-FPM 8.1을 설치하는 방법

728x90

CentOS 7에서 PHP-FPM 8.1을 설치하는 방법

PHP-FPM : PHP FastCGI Process Manager

CentOS 7은 PHP 5.x를 지원하며, PHP 8.1은 공식 CentOS 7 저장소에 포함되어 있지 않습니다.

PHP 8.1을 CentOS 7에 설치하려면 추가 저장소 설정이 필요하며, Remi 저장소는 이를 제공하는 인기있는 옵션 중 하나입니다.

Remi 저장소를 사용하여 CentOS 7에 PHP 8.1을 설치할 수 있습니다.

테스트 환경

  • 운영체제 버전 정보 확인
$ cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)

$ getconf LONG_BIT
64
  • filewalld 서비스 정지
sudo systemctl stop firewalld
sudo systemctl disable firewalld
  • SELINUX 비활성화
sudo sed -i 's/enforcing/disabled/g' /etc/selinux/config

PHP 8.1 설치

EPEL 저장소 및 YUM Utilities 패키지 설치

sudo yum install -y epel-release yum-utils

Remi 저장소 설치

sudo yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm

Remi 저장소 활성화

sudo yum repolist enabled

CentOS 7의 기본 PHP 버전 정보

yum info php-fpm | grep Version
$ yum info php-fpm | grep Version
Version     : 5.4.45

PHP 5.4 저장소 비활성화(default version disable)

sudo yum-config-manager --disable remi-php54

PHP 8.1 저장소 활성화

sudo yum-config-manager --enable remi-php81

현재 활성화된 저장소 확인

sudo yum repolist enabled

p1

PHP(PHP-FPM) 패키지 설치

sudo yum install -y php php-cli php-common php-devel php-pear php-fpm

PHP(PHP-FPM) 추가 모듈 설치

sudo yum install -y php-gd php-intl php-mbstring php-mysqlnd php-pecl-mcrypt php-pecl-zip php-pdo

PHP-FPM 버전 정보 확인

php-fpm -version
$ php-fpm -version
PHP 8.1.24 (fpm-fcgi) (built: Sep 26 2023 23:43:49)
Copyright (c) The PHP Group
Zend Engine v4.1.24, Copyright (c) Zend Technologies

PHP-FPM 서비스 시작 및 활성화

sudo systemctl --now enable php-fpm

PHP-FPM 서비스 상태 확인

sudo systemctl status php-fpm

php-fpm.conf(/etc/php-fpm.conf) 파일

sudo vim /etc/php-fpm.conf
include=/etc/php-fpm.d/*.conf

[global]
pid = /run/php-fpm/php-fpm.pid

error_log = /var/log/php-fpm/error.log

daemonize = yes

www.conf(/etc/php-fpm.d/www.conf) 파일

sudo vim /etc/php-fpm.d/www.conf
[www]
user = nginx
group = nginx

listen = /var/run/php-fpm/php-fpm.sock

listen.backlog = 511

listen.owner = nginx
listen.group = nginx
listen.mode = 0660

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

slowlog = /var/log/php-fpm/www-slow.log

pm.status_path = /status

ping.path = /ping

access.log = /var/log/php-fpm/access.log

access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%"

php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
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

PHP-FPM 구문 검사

php-fpm -t
$ php-fpm -t
[19-Oct-2023 13:32:39] NOTICE: configuration file /etc/php-fpm.conf test is successful

PHP 버전 정보 숨기기

php.ini 파일 경로 찾기

php --ini | egrep "Loaded Configuration File"
$ php --ini | egrep "Loaded Configuration File"
Loaded Configuration File:         /etc/php.ini

PHP 버전 숨기기

sed -i "s/expose_php = On/expose_php = Off/g" /etc/php.ini
728x90

Nginx 설치

더보기

---

nginx 버전 정보

$ nginx -v
nginx version: nginx/1.20.1

nginx.conf 설정 파일

cat /etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

---

PHP-FPM을 사용하려면 웹 서버(Nginx)와 연동하는 방법

sudo vim /etc/nginx/nginx.conf

Nginx의 vhosts 파일을 편집합니다.

# For more information on configuration
user nginx;

worker_processes auto;

error_log /var/log/nginx/error.log;

pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen 80;
        server_name _;
        root /usr/share/nginx/html;
        index index.html index.php;

        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php$ {
            fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }

        location /nginx_status {
            # Nginx status 페이지 설정
            stub_status;
            access_log off;
            allow 127.0.0.1;
            allow 192.168.56.0/24;
            deny all;
        }

        location ~ ^/(ping|status)$ {
            # PHP-FPM ping 및 status 페이지 설정
            fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
            include fastcgi_params;
            access_log off;
            allow 127.0.0.1;
            allow 192.168.56.0/24;
            deny all;

        }
    }
}

fastcgi_params(/etc/nginx/fastcgi_params) 설정 파일

cat <<'EOF' >> /etc/nginx/fastcgi_params

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  PATH_TRANSLATED    $document_root$fastcgi_path_info;
fastcgi_param  PATH_INFO          $fastcgi_path_info;
EOF

Nginx 구문 검사

nginx -t
$ nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Nginx, PHP-FPM 서비스 재시작

sudo systemctl restart nginx php-fpm

Nginx 상태 페이지

curl http://127.0.0.1/nginx_status
$ curl http://127.0.0.1/nginx_status
Active connections: 1
server accepts handled requests
 5 5 5
Reading: 0 Writing: 1 Waiting: 0

PHP-FPM 상태 페이지

curl http://127.0.0.1/status
$ curl http://127.0.0.1/status
pool:                 www
process manager:      dynamic
start time:           19/Oct/2023:14:21:39 +0900
start since:          7
accepted conn:        1
listen queue:         0
max listen queue:     0
listen queue len:     0
idle processes:       4
active processes:     1
total processes:      5
max active processes: 1
max children reached: 0
slow requests:        0

PHP-FPM PING 테스트

curl http://127.0.0.1/ping
$ curl http://127.0.0.1/ping
pong

PHPINFO 페이지 생성

echo "<?php phpinfo();" > /usr/share/nginx/html/test.php

PHPINFO 페이지 테스트

curl -I http://127.0.0.1/test.php
$ curl -I http://127.0.0.1/test.php
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Thu, 19 Oct 2023 05:25:01 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive

 

참고URL

- Fedora Project DOCS : Extra Packages for Enterprise Linux (EPEL)

- Remi's RPM repository - Blog : Repository Configuration

 

728x90