맛집 여행 캠핑 일상 생활

[CentOS] 7.x 아파치2.4 + PHP-FPM + PHP Multiple 설치 방법 본문

LAMP

[CentOS] 7.x 아파치2.4 + PHP-FPM + PHP Multiple 설치 방법

영은파더♥ 2023. 1. 13. 17:59

최근에 오라클 클라우드 VM에 PHP 여러버전을 설치해보니 예전에 먹히는게 안먹히더군요~

그래서 다시 정리해보았습니다.

테스트 환경은 CentOS 7.9 버전입니다.

yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum -y install https://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum -y install yum-utils
yum -y install mod_ruid2

yum -y install php php-bcmath php-cli php-common php-fpm php-gd php-json php-mbstring php-mysqlnd php-opcache php-pdo php-pear php-pecl-apcu php-pecl-zip php-process php-soap php-xml php-xmlrpc

디폴트 PHP 버전은 7.2로 하겠습니다.

 default php version setting

yum-config-manager --enable remi-php72
#yum-config-manager --enable remi-php74
#yum-config-manager --enable remi-php81
#yum-config-manager --enable remi-php82
yum -y update

디폴트 버전 5.6? 에서 7.2 버전으로 설정해서 yum update 를 하면 PHP 기본 버전이 변경됩니다.
PHP 8.2 버전을 설치하려면 php 패키지가 설치된 상태에서 아래 두줄로 8.2버전으로 업데이트가 가능합니다.
    yum-config-manager --enable remi-php82
    yum -y update
기본적으로 지원을 원하는 버전이 있다면 선택하시면 됩니다.

yum -y install php74 php74-php-bcmath php74-php-cli php74-php-common php74-php-fpm php74-php-gd php74-php-json php74-php-mbstring php74-php-mysqlnd php74-php-opcache php74-php-pdo php74-php-pear php74-php-pecl-apcu php74-php-pecl-zip php74-php-process php74-php-soap php74-php-xml php74-php-xmlrpc
yum -y install php81 php81-php-bcmath php81-php-cli php81-php-common php81-php-fpm php81-php-gd php81-php-json php81-php-mbstring php81-php-mysqlnd php81-php-opcache php81-php-pdo php81-php-pear php81-php-pecl-apcu php81-php-pecl-zip php81-php-process php81-php-soap php81-php-xml php81-php-xmlrpc
yum -y install php82 php82-php-bcmath php82-php-cli php82-php-common php82-php-fpm php82-php-gd php82-php-json php82-php-mbstring php82-php-mysqlnd php82-php-opcache php82-php-pdo php82-php-pear php82-php-pecl-apcu php82-php-pecl-zip php82-php-process php82-php-soap php82-php-xml php82-php-xmlrpc

설치가 되면 각 버전별로 php.ini 파일을 수정하셔야 합니다. ( default 는 /etc/php.ini )

vi /etc/opt/remi/php74/php.ini
vi /etc/opt/remi/php81/php.ini
vi /etc/opt/remi/php82/php.ini

그리고 conf 파일을 수정합니다.

vi /etc/opt/remi/php74/php-fpm.d/www.conf
vi /etc/opt/remi/php81/php-fpm.d/www.conf
vi /etc/opt/remi/php82/php-fpm.d/www.conf

[www] 이부분을 원하시는 도메인으로 변경합니다.

; Start a new pool named 'www'.
; the variable $pool can be used in any directive and will be replaced by the
; pool name ('www' here)
[www.example.com]

user = apache
group = apache

; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
;       will be used.
; RPM: apache user chosen to provide access to the same directories as httpd
user = example
; RPM: Keep a group allowed to write in log dir.
group = example

이 부분도 유저아이디로 변경합니다.

listen = 127.0.0.1:9000
이부분도 포트를 다르게 설정해야 충돌이 없습니다.

listen = 127.0.0.1:9074
listen = 127.0.0.1:9081
listen = 127.0.0.1:9082

각 버전별 www.conf 파일 마다 다르게 포트를 설정합니다.

useradd example
mkdir /home/example/www /home/example/www/v81 /home/example/www/v82
cat << EOF > /home/example/www/index.html
<?
  phpinfo();
?>
EOF
cp /home/example/www/index.html /home/example/www/v81/index.html
cp /home/example/www/index.html /home/example/www/v82/index.html
chown -R example:example /home/example/www


이제 VirtualHost 설정을 합니다.

<VirtualHost *:80>
    ServerName www.example.kr
    DocumentRoot /home/example/www
    <Directory "/home/example/www/v81">
        <IfModule mod_proxy_fcgi.c>
            <FilesMatch \.(php|htm|html)$>
                SetHandler "proxy:fcgi://127.0.0.1:9081/"
            </FilesMatch>
        </IfModule>
    </Directory>
    <Directory "/home/example/www/v82">
        <IfModule mod_proxy_fcgi.c>
            <FilesMatch \.(php|htm|html)$>
                SetHandler "proxy:fcgi://127.0.0.1:9082/"
            </FilesMatch>
        </IfModule>
    </Directory>
    <IfModule mod_ruid2.c>
        RMode config
        RUidGid example example
    </IfModule>
</VirtualHost>

systemctl enable php81-php-fpm
systemctl start php81-php-fpm
systemctl enable php82-php-fpm
systemctl start php82-php-fpm
systemctl start httpd

하나의 사이트에 여러버전이 존재하는 형태입니다.

여기까지는 아파치 설정입니다.

브라우저에서 한번 확인해보시기 바랍니다.

CentOS 8 Stream 버전에서 설치하는 방법은 https://itrooms.tistory.com/1034 여기를 참고하세요~


여기서 부터는 NGINX 설정입니다.

# NGINX 8080 Port
vi /etc/nginx/nginx.conf (80포트를 8080포트로 변경합니다.)

cat << EOF > /etc/nginx/conf.d/example-php74.conf
server {
    listen 8080;
    server_name www.example.com;
    root  /var/www/html;
    index index.php index.html index.htm;

    access_log /var/log/nginx/access-example74.log;
    error_log  /var/log/nginx/error-example74.log;

    location / {
        index  index.html index.htm index.php;
    }

    location ~ [^/]\.(php|htm|html)(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        if (!-f \$document_root\$fastcgi_script_name) {
            return 404;
        }
        fastcgi_pass   127.0.0.1:9074;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
        include        fastcgi_params;
    }
}
EOF
cat << EOF > /var/www/html/index.html
<?
  phpinfo();
?>
EOF

systemctl enable php74-php-fpm
systemctl start php74-php-fpm
systemctl enable nginx
systemctl start nginx

메인은 apache 환경에 php 7.2 버전이고 v81 경로는 8.1 버전, v82 경로는 8.2 버전, 8080 포트는 nginx 로 php 7.4 버전으로 작동합니다.
하나의 사이트에 여러개의 PHP 버전이 작동하는걸 확인합니다.

 

 

Trackback : | Comments :