본문 바로가기

리눅스

우분투에 PHP-FPM 8.1을 설치하는 방법

반응형

우분투에 PHP-FPM 8.1을 설치하는 방법

PHP-FPM 8.1은 PHP 8.1 버전의 FastCGI Process Manager(php-fpm)입니다.

테스트 환경

$ lsb_release -d
Description:    Ubuntu 22.04.2 LTS

$ uname -m
x86_64

$ getconf LONG_BIT
64

필수 패키지 설치

sudo apt-get install -y zlib1g-dev software-properties-common

패키지 목록 업데이트

sudo apt-get update

PPA(Personal Package Archive) 리포지토리를 시스템의 패키지 소스 목록에 추가

sudo add-apt-repository -y ppa:ondrej/php

PHP 8.1 설치

php(php-fpm) 8.1 설치

sudo apt-get install -y php8.1-fpm php8.1-cli php8.1-common php8.1-dev

php 추가 모듈 설치

sudo apt-get install -y php8.1-gd php8.1-xml php8.1-curl php8.1-igbinary
sudo apt-get install -y php8.1-xml php8.1-redis php8.1-mongodb php8.1-zip php8.1-imagick

rdkafka 모듈 설치

sudo apt-get install -y librdkafka-dev
sudo pecl install rdkafka
echo "extension=rdkafka.so" | sudo tee /etc/php/8.1/mods-available/rdkafka.ini
sudo ln -s /etc/php/8.1/mods-available/rdkafka.ini /etc/php/8.1/fpm/conf.d/20-rdkafka.ini
sudo ln -s /etc/php/8.1/mods-available/rdkafka.ini /etc/php/8.1/cli/conf.d/20-rdkafka.ini
728x90

php-fpm 버전 정보 확인

php-fpm8.1 --version
$ php-fpm8.1 --version
PHP 8.1.18 (fpm-fcgi) (built: Apr 14 2023 04:39:44)
Copyright (c) The PHP Group
Zend Engine v4.1.18, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.18, Copyright (c), by Zend Technologies
php -v

php-fpm 서비스를 시작하고 부팅 시 자동으로 시작하도록 활성화

systemctl --now enable php8.1-fpm

PHP 환경 설정(php.ini) 파일 경로 찾기

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

PHP 버전 숨기기

sed -i "s/expose_php = On/expose_php = Off/g" /etc/php/8.1/cli/php.ini

php-fpm.conf 문법 검사

php-fpm8.1 -t
$ php-fpm8.1 -t
[20-Apr-2023 13:06:40] NOTICE: configuration file /etc/php/8.1/fpm/php-fpm.conf test is successful

php 모듈 목록 확인

php -m | egrep 'redis|mongodb|zip|imagick|rdkafka'
$ php -m | egrep 'redis|mongodb|zip|imagick|rdkafka'
imagick
mongodb
rdkafka
redis
zip
php-fpm8.1 -m
더보기
$ php-fpm8.1 -m
[PHP Modules]
bcmath
calendar
cgi-fcgi
Core
ctype
curl
date
dom
exif
FFI
fileinfo
filter
ftp
gd
gettext
hash
iconv
igbinary
imagick
intl
json
libxml
mbstring
mcrypt
mongodb
mysqli
mysqlnd
openssl
pcre
PDO
pdo_mysql
Phar
posix
rdkafka
readline
redis
Reflection
session
shmop
SimpleXML
sockets
sodium
SPL
standard
sysvmsg
sysvsem
sysvshm
tokenizer
xml
xmlreader
xmlwriter
xsl
Zend OPcache
zip
zlib

[Zend Modules]
Zend OPcache

NGINX 설정 파일 편집

sed -i 's/user  nginx;/user www-data www-data;/' /etc/nginx/nginx.conf
vim /etc/nginx/conf.d/default.conf
server {
    listen       80;
    server_name  _;

    root   /usr/share/nginx/html;
    index  index.php index.html index.htm;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
         fastcgi_pass unix:/run/php/php8.1-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
    }
   
    location /nginx_status {
        # Nginx status 페이지 설정
        stub_status;
        access_log off;
        allow 127.0.0.1;
        allow 192.168.10.0/24;
        deny all;
    }
    
    location ~ ^/(status|ping)$ {
        access_log off;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        allow 127.0.0.1;
        allow 192.168.10.0/24;
        deny all;
    }
}

PHP-FPM 설정 파일 편집

mkdir -p /var/log/php-fpm
sudo tee /etc/php/8.1/fpm/php-fpm.conf <<EOF
include=/etc/php/8.1/fpm/pool.d/www.conf

[global]
pid = /run/php/php8.1-fpm.pid
error_log = /var/log/php-fpm/error.log
daemonize = yes
EOF
sudo tee /etc/php/8.1/fpm/pool.d/www.conf <<'EOF'
[www]
user = www-data
group = www-data

listen = /var/run/php/php8.1-fpm.sock
listen.owner = www-data
listen.group = www-data
listen.mode = 0666

;CPU 8개, 메모리 16GB, PHP 프로세스당 메모리 사용량이 50MB인 경우
pm = dynamic
pm.max_children = 250
pm.start_servers = 20
pm.min_spare_servers = 10
pm.max_spare_servers = 30

request_terminate_timeout = 30
request_slowlog_timeout = 10
slowlog = /var/log/php-fpm/www-slow.log

pm.status_path = /status

access.log = /var/log/php-fpm/www-access.log
access.format = "[%t] %m %{REQUEST_SCHEME}e://%{HTTP_HOST}e%{REQUEST_URI}e %f pid:%p TIME:%ds MEM:%{mega}Mmb CPU:%C%% status:%s {%{REMOTE_ADDR}e|%{HTTP_USER_AGENT}e}"

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
EOF

PHP-FPM 상태 페이지

curl http://localhost/status

PHP INFO 페이지

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

 

728x90
반응형