CentOS 7 - PHP 7.4설치
설치환경 CentOS 7.9 final + apache 2.4.54 prefork + MySQL 5.7.40 + php 7.4.33 Safety Thread = disable
PHP 설치 후 생성된 모듈을 웹서버에 연결하면 PHP 로 코딩한 웹페이지를 웹브라우저에서 볼 수 있게 됩니다.
아래 매뉴얼은 CentOS 7.9 환경에서 PHP 7.4버전으로 진행했습니다.
PHP 설치를 하기위해서는 Apache -> DB(MySQL or MariaDB) -> PHP 순서로 설치를 진행해야합니다.
1. 필수 구성요소 설치
# yum install -y vim wget epel-release # mkdir -p /usr/local/src/APM_Setup # yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm # yum update -y # yum install -y libdb-devel enchant-devel libxslt libxslt-devel net-snmp-devel readline-devel readline aspell-devel unixODBC unixODBC-devel libicu-devel icu libicu libc-client libc-client-devel libc-client-2007f freetype-devel freetype* freetype libXpm libXpm-devel libpng-devel libpng* libvpx-devel libvpx libcurl libcurl-devel curl* curl-devel curl tcp_wrappers-devel libzip environment-modules uw-imap-utils uw-imap-devel uw-imap uw-imap-static pam-devel libldb-devel openldap-devel cyrus-sasl-devel bzip2-devel bzip2 bzip2-libs ncurses-devel ncurses mysql-devel libjpeg-devel libjpeg* libjpeg-devel openssl-devel openssl* openssl-libs libxml* libxml2 libxml2-devel libmcrypt libmcrypt-devel libmcrypt* mcrypt mhash* mhash mhash-devel libmhash libmhash-devel expat-devel expat expat* gmp* gmp gmp-devel krb5* krb5-devel db4 db4-devel wget make cmake libtool* pcre* gdbm* gdbm gdbm-devel libtiff* libtiff libtiff-devel flex zlib* zlib-devel zlib gd* gd gd-devel patch t1lib t1lib-devel readline libedit-devel libtidy libtidy-devel gcc* gcc gcc-c++ libtermcap-devel dialog sqlite-devel oniguruma oniguruma-devel libzip5 mod_ssl postgresql-devel lcov systemtap-sdt-devel mod_ldap elfutils-devel libwebp-devel libwebp git gzip libnghttp2-devel valgrind* mod_proxy* editline editline-devel patchelf systemd-devel ImageMagick-devel libzstd-devel libffi libffi-devel libraqm-devel libraqm libsodium libsodium-devel
PHP imap 관련모듈 64비트 32비트 라이브러리 호환설정 # ln -s /usr/lib64/libc-client.a /usr/lib/libc-client.a # unlink /usr/include/db.h # ln -s /usr/include/libdb4/db.h /usr/include/db.h # ln -s /usr/include/sqlext.h /usr/local/include/sqlext.h # ln -s /usr/lib64/libgdbm_compat.so /usr/local/lib64/libdbm.so
PHP openldap 관련 모듈 64비트 32비트 라이브러리 호환설정 # \cp -arpf /usr/lib64/libldap* /usr/lib/ # ln -sf /usr/lib64/libm.so /usr/lib/libm.so # ln -sf /usr/lib64/libssl.so /usr/lib/libssl.so
PHP libXpm 관련 모듈 64비트 32비트 라이브러리 호환설정 # ln -s /usr/lib64/libXpm.so /usr/lib/ # ln -s /usr/lib64/libXpm.so.4 /usr/lib/ # ln -s /usr/lib64/libXpm.so.4.11.0 /usr/lib/
PHP 이미지모듈 64비트 32비트 라이브러리 호환설정 ln -s /usr/lib64/libjpeg.so /usr/lib ln -s /usr/lib64/libpng.so /usr/lib |
2. default 환경변수 설정
# echo " " >> /etc/profile # echo "####default PATH#####" >> /etc/profile # echo "export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:" >> /etc/profile # echo "#####################" >> /etc/profile # echo " " >> /etc/profile # echo "export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig:/usr/local/lib64/pkgconfig:/usr/lib64/pkgconfig:/usr/lib/pkgconfig" >> /etc/profile # echo " " >> /etc/profile |
3. PHP 설치하기 전 선행 작업
CentOS 7은 2014년에 출시되었기 때문에 최근에 출시되었던 PHP버전 (예: php-7.3 = 2018년 12월초 출시, php-7.4 = 2019년 11월말 출시) 비해 기본 내장패키지(라이브러리 포함)들이 버전이 낮습니다.
그러나 기본 내장패키지들을 따로 repo로 출시되지 않아서 yum 업데이트가 불가합니다.
그렇기 때문에 즉, CentOS 7에서 PHP 7.3이상 소스설치를 한다면, 기본 내장패키지들도 소스 업그레이드를 진행을 해야합니다.
3-1. pcre 2 최신버전으로 소스설치
# mkdir -p /usr/local/src/APM_Setup # cd /usr/local/src/APM_Setup # wget https://ftp.exim.org/pub/pcre/pcre2-10.37.tar.gz # tar zxvf pcre2-10.37.tar.gz # cd pcre2-10.37 # ./configure --prefix=/usr/local/pcre2 --enable-pcre2-8 --enable-pcre2-16 --enable-pcre2-32 --enable-jit --enable-jit-sealloc --enable-pcre2grep-jit --enable-pcre2grep-callout --enable-pcre2grep-callout-fork --enable-unicode --enable-pcre2grep-libz --enable-pcre2grep-libbz2 --enable-pcre2test-libreadline --enable-valgrind --enable-fuzz-support --enable-percent-zt --with-gnu-ld --with-pcre2grep-bufsize=20480 --with-pcre2grep-max-bufsize=1048576 --with-link-size=2 --with-parens-nest-limit=250 --with-heap-limit=20000000 --with-match-limit-depth=MATCH_LIMIT # make -j # make install # sed -i "s/export PKG_CONFIG_PATH=\/usr\/local\/lib\/pkgconfig:\/usr\/local\/lib64\/pkgconfig:\/usr\/lib64\/pkgconfig:\/usr\/lib\/pkgconfig/export PKG_CONFIG_PATH=\/usr\/local\/pcre2\/lib\/pkgconfig:\/usr\/local\/lib\/pkgconfig:\/usr\/local\/lib64\/pkgconfig:\/usr\/lib64\/pkgconfig:\/usr\/lib\/pkgconfig/g" /etc/profile 해당 부분은 소스컴파일할때 PKG_CONFIG_PATH 강제적으로 치환 설정합니다. |
3-2. cmake 최신버전으로 소스설치
(cmake 기존 패키지 버전 2.8.12 -> 소스 3.23.1)
# yum remove -y cmake # cd /usr/local/src/APM_Setup # wget https://github.com/Kitware/CMake/releases/download/v3.23.1/cmake-3.23.1.tar.gz # tar zxvf cmake-3.23.1.tar.gz # cd cmake-3.23.1 # ./bootstrap --prefix=/usr/local # make -j # make install |
3-3. libzip 라이브러리 최신버전 소스설치 (조건 libzip >= 0.11 libzip != 1.3.1 libzip != 1.7.0)
(기존 패키지 libzip버전 0.10.1 -> 소스 1.8.0)
# cd /usr/local/src/APM_Setup # wget https://libzip.org/download/libzip-1.8.0.tar.gz # tar zxvf libzip-1.8.0.tar.gz # mkdir libzip-1.8.0/build # cd libzip-1.8.0/build # cmake ../ \ # make -j # make install |
3-4. libgd 라이브러리 최신버전 소스설치
(기존 패키지 버전 2.0.35 -> 2.3.2)
# cd /usr/local/src/APM_Setup # wget https://github.com/libgd/libgd/releases/download/gd-2.3.2/libgd-2.3.2.tar.gz # tar zxvf libgd-2.3.2.tar.gz # cd libgd-2.3.2 # ./configure --prefix=/usr/local --with-pic --with-aix-soname=both --with-x --with-gnu-ld --with-libiconv-prefix --with-zlib --with-png --with-freetype --with-raqm --with-fontconfig --with-jpeg --with-xpm --with-tiff --with-webp # make -j # make install |
4. PHP 설치
# /usr/local/src/APM_Setup # wget https://www.php.net/distributions/php-7.4.33.tar.gz # tar zxvf php-7.4.33.tar.gz # cd php-7.4.33 # ./configure \
* 만약 Apache가 MPM 방식이 worker, event 방식이라면 --disable 부분을 --enable 로 변경하시길 바랍니다. 또한 --enable 로 변경해서 진행했다면 마지막에 번호 8번은 꼭 진행을 하셔야합니다. (PHP-FPM)
# sed -i 's/-lcrypto -lcrypt/-lcrypto -lcrypt -llber/g' /usr/local/src/APM_Setup/php-7.4.33/Makefile # make -j # make install |
5. PHP 기본설정
※ 여기서 sed는 치환 수정해주는 명령어 입니다. # \cp -arpf php.ini-development /usr/local/apache/conf/php.ini # sed -i 's/;upload_tmp_dir =/upload_tmp_dir = \/tmp/g' /usr/local/apache/conf/php.ini # sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 100M/g' /usr/local/apache/conf/php.ini # sed -i 's/post_max_size = 8M/post_max_size = 100M/g' /usr/local/apache/conf/php.ini # sed -i 's/memory_limit = 128M/memory_limit = 256M/g' /usr/local/apache/conf/php.ini # sed -i 's/allow_url_fopen = On/allow_url_fopen = Off/g' /usr/local/apache/conf/php.ini # sed -i 's/expose_php = On/expose_php = Off/g' /usr/local/apache/conf/php.ini # sed -i 's/display_errors = Off/display_errors = On/g' /usr/local/apache/conf/php.ini # sed -i 's/log_errors = Off/log_errors = On/g' /usr/local/apache/conf/php.ini # sed -i 's/;error_log = syslog/error_log = syslog/g' /usr/local/apache/conf/php.ini # sed -i 's/short_open_tag = Off/short_open_tag = On/g' /usr/local/apache/conf/php.ini # sed -i 's/;date.timezone =/date.timezone = "Asia\/Seoul"/g' /usr/local/apache/conf/php.ini # sed -i 's/session.gc_maxlifetime = 1440 /session.gc_maxlifetime = "3600"/g' /usr/local/apache/conf/php.ini |
6. 이전 버전에서 없어진 PHP 기본 내장 모듈 추가설치
6-1. PHP imagick 모듈 설치
# /usr/local/src/APM_Setup # wget https://pecl.php.net/get/imagick-3.7.0.tgz # tar zxvf imagick-3.7.0.tgz # cd imagick-3.7.0/ # /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make -j && make install # echo "[imagick]" >> /usr/local/apache/conf/php.ini # echo "extension=imagick.so" >> /usr/local/apache/conf/php.ini # echo " " >> /usr/local/apache/conf/php.ini |
6-2. PHP mcrypt 모듈 설치
# cd /usr/local/src/APM_Setup # wget https://pecl.php.net/get/mcrypt-1.0.4.tgz # tar zxvf mcrypt-1.0.4.tgz # cd mcrypt-1.0.4 # /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make -j && make install # echo "[mcrypt]" >> /usr/local/apache/conf/php.ini # echo "extension=mcrypt.so" >> /usr/local/apache/conf/php.ini # echo " " >> /usr/local/apache/conf/php.ini |
6.3 PHP Zend Opcache 추가 설치
# cd /usr/local/src/APM_Setup/php-7.4.33/ext/opcache # /usr/local/php/bin/phpize && ./configure --with-php-config=/usr/local/php/bin/php-config && make -j && make install # echo "zend_extension=opcache.so" >> /usr/local/apache/conf/php.ini # echo "zend_extension_ts=opcache.so" >> /usr/local/apache/conf/php.ini # echo " " >> /usr/local/apache/conf/php.ini # sed -i 's/;opcache.enable=1/opcache.enable=1/g' /usr/local/apache/conf/php.ini # sed -i 's/;opcache.enable_cli=0/opcache.enable_cli=1/g' /usr/local/apache/conf/php.ini # sed -i 's/;opcache.memory_consumption=128/opcache.memory_consumption=128/g' /usr/local/apache/conf/php.ini # sed -i 's/;opcache.interned_strings_buffer=8/opcache.interned_strings_buffer=8/g' /usr/local/apache/conf/php.ini # sed -i 's/;opcache.max_accelerated_files=10000/opcache.max_accelerated_files=10000/g' /usr/local/apache/conf/php.ini # sed -i 's/;opcache.max_wasted_percentage=5/opcache.max_wasted_percentage=5/g' /usr/local/apache/conf/php.ini # sed -i 's/;opcache.use_cwd=1/opcache.use_cwd=1/g' /usr/local/apache/conf/php.ini # sed -i 's/;opcache.validate_timestamps=1/opcache.validate_timestamps=1/g' /usr/local/apache/conf/php.ini # sed -i 's/;opcache.revalidate_freq=2/opcache.revalidate_freq=10/g' /usr/local/apache/conf/php.ini |
7. PHP 환경변수 설정
# echo " " >> /etc/profile # echo "APACHE_HOME=/usr/local/apache" >> /etc/profile # echo "MySQL_HOME=/usr/local/mysql" >> /etc/profile # echo "PHP_HOME=/usr/local/php" >> /etc/profile # echo " " >> /etc/profile # echo "export PATH="\$"APACHE_HOME/bin:"\$"MySQL_HOME/bin:"\$"PHP_HOME/bin:"\$"PHP_HOME/sbin"\$"{PATH:+:"\$"{PATH}}" >> /etc/profile # echo " " >> /etc/profile # source /etc/profile |
만약 mysqli 모듈이 아닌 mysql 부분이 php 소스코드에서 mysqli가 아닌 php5때문에 생기는 mysql 모듈이 필요하다면 따로 php 모듈 추가설치를 해야합니다.
여기서부터는 혹여, apache에서나 nginx 에서 php-fpm으로 API 연동 서버를 운영할때 필요한 작업입니다.
apache MPM=prefork 일때는 하지 않습니다.
8. PHP-FPM 설치 (Apache prefork 일때 8번 부분은 건너뛰시길 바랍니다.)
8-1. PHP-FPM 설정파일 생성 및 실행 데몬 스크립트 생성
# \cp -f /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf # \cp -f /usr/local/src/APM_Setup/php-7.4.33/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm # \cp -f /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf # chmod +x /etc/init.d/php-fpm # \cp -f /usr/local/src/APM_Setup/php-7.4.33/sapi/fpm/php-fpm.service /lib/systemd/system/ # chkconfig --add php-fpm # chkconfig php-fpm on |
8-2. PHP-FPM 전역 설정파일(./php-fpm.conf) 설정
# sed -i 's/;pid = run\/php-fpm.pid/pid = \/usr\/local\/php\/var\/run\/php-fpm.pid/g' /usr/local/php/etc/php-fpm.conf # sed -i 's/;error_log = log\/php-fpm.log/error_log = \/usr\/local\/php\/var\/log\/php-fpm.log/g' /usr/local/php/etc/php-fpm.conf # sed -i 's/;syslog.facility = daemon/syslog.facility = daemon/g' /usr/local/php/etc/php-fpm.conf # sed -i 's/;syslog.ident = php-fpm/syslog.ident = php-fpm/g' /usr/local/php/etc/php-fpm.conf # sed -i 's/;log_level = notice/log_level = warning/g' /usr/local/php/etc/php-fpm.conf # sed -i 's/;log_limit = 4096/log_limit = 4096/g' /usr/local/php/etc/php-fpm.conf # sed -i 's/;log_buffering = no/log_buffering = yes/g' /usr/local/php/etc/php-fpm.conf # sed -i 's/;emergency_restart_threshold = 0/emergency_restart_threshold = 5/g' /usr/local/php/etc/php-fpm.conf # sed -i 's/;emergency_restart_interval = 0/emergency_restart_interval = 30s/g' /usr/local/php/etc/php-fpm.conf # sed -i 's/;process_control_timeout = 0/process_control_timeout = 20/g' /usr/local/php/etc/php-fpm.conf # sed -i 's/; process.max = 128/process.max = 1024/g' /usr/local/php/etc/php-fpm.conf # sed -i 's/; process.priority = -19/process.priority = -19/g' /usr/local/php/etc/php-fpm.conf # sed -i 's/;daemonize = yes/daemonize = yes/g' /usr/local/php/etc/php-fpm.conf # sed -i 's/;systemd_interval = 10/systemd_interval = 10/g' /usr/local/php/etc/php-fpm.conf |
8-3. PHP-FPM 지역 설정파일(./php-fpm.d/www.conf) 설정
# sed -i 's/;listen.backlog = 511/listen.backlog = 65535/g' /usr/local/php/etc/php-fpm.d/www.conf # echo "net.core.somaxconn=65535" >> /etc/sysctl.conf # sysctl -p # sed -i 's/;listen.owner = nobody/listen.owner = nobody/g' /usr/local/php/etc/php-fpm.d/www.conf # sed -i 's/;listen.group = nobody/listen.group = nobody/g' /usr/local/php/etc/php-fpm.d/www.conf # sed -i 's/;listen.mode = 0660/listen.mode = 0660/g' /usr/local/php/etc/php-fpm.d/www.conf # sed -i 's/; process.dumpable = yes/process.dumpable = yes/g' /usr/local/php/etc/php-fpm.d/www.conf # sed -i 's/pm = dynamic/pm = ondemand/g' /usr/local/php/etc/php-fpm.d/www.conf # sed -i 's/pm = static/pm = ondemand/g' /usr/local/php/etc/php-fpm.d/www.conf # sed -i 's/pm.max_children = 5/pm.max_children = 50/g' /usr/local/php/etc/php-fpm.d/www.conf # sed -i 's/;pm.process_idle_timeout/pm.process_idle_timeout/g' /usr/local/php/etc/php-fpm.d/www.conf # sed -i 's/;access.log = log/access.log = \/usr\/local\/php\/var\/log/g' /usr/local/php/etc/php-fpm.d/www.conf # sed -i 's/;access.format/access.format/g' /usr/local/php/etc/php-fpm.d/www.conf # sed -i 's/;slowlog/slowlog/g' /usr/local/php/etc/php-fpm.d/www.conf # sed -i 's/;request_slowlog_timeout = 0/;request_slowlog_timeout = 30/g' /usr/local/php/etc/php-fpm.d/www.conf |
8-4 PHP-FPM로 연동하기위한 Apache 설정파일 수정
# echo " ##########PHP-FPM 연동설정########### ProxyRequests Off ProxyPreserveHost On ProxyErrorOverride on ProxyTimeout 600 SetHandler "proxy:fcgi://localhost:9000" #####################################" >> /usr/local/apache/conf/httpd.conf |
8-5. PHP-FPM 실행
# /etc/init.d/php-fpm start |
9. PHP 설치 버전 확인 및 설치된 php 모듈 확인
# which php # php -v # php -m [Zend Modules]
# php -i | grep php.ini
|
10. phpinfo 파일 생성 및 phpinfo 페이지 웹브라우저에서 확인
# echo "" > /usr/local/apache/htdocs/phpinfo.php # /etc/init.d/apache stop # /etc/init.d/apache start |
인터넷 웹브라우저 (크롬, 네이버웨일, 파이어폭스, 마이크로엣지)에서 http://서버IP/phpinfo.php 접속