很早之前我就写过一篇文章《Linux 系统下安装 PHP》,由于系统的升级,PHP 版本的更新,可能会有一些小问题。
我这边准备了几个相关的软件包:libmcrypt-2.5.8.tar.gz、freetype-2.10.1.tar.gz、libiconv-1.16.tar.gz、mhash-0.9.9.9.tar.gz、mcrypt-2.6.8.tar.gz、curl-7.69.1.tar.gz、oniguruma-6.9.4.tar.gz、libzip-1.6.1.tar.gz 以及 php-7.4.4.tar.gz。
1、安装 libxml2
CentOS 8 默认是有安装 libxml2:
[root@centos8 softwares]# rpm -qa | grep libxml2
一般情况下,libxml2-devel 是默认没有安装的,需要安装:
[root@centos8 softwares]# dnf install libxml2-devel
确认一下:
[root@centos8 softwares]# find / -name "xml2-config"
/usr/bin/xml2-config
2、安装 libmcrypt-2.5.8
[root@centos8 softwares]# tar xvf libmcrypt-2.5.8.tar.gz
[root@centos8 softwares]# cd libmcrypt-2.5.8
[root@centos8 libmcrypt-2.5.8]# ./configure --prefix=/usr --enable-ltdl-install
[root@centos8 libmcrypt-2.5.8]# make
[root@centos8 libmcrypt-2.5.8]# make install
[root@centos8 libmcrypt-2.5.8]# cd ..
3、安装 libpng
CentOS 8 默认是有安装 libpng:
[root@centos8 softwares]# rpm -qa | grep libpng
一般情况下,libpng-devel 是默认没有安装的,需要安装:
[root@centos8 softwares]# dnf install libpng-devel
4、安装 libjpeg
CentOS 8 默认是有安装 libjpeg:
[root@centos8 softwares]# rpm -qa | grep libjpeg
一般情况下,libjpeg-turbo-devel 是默认没有安装的,需要安装:
[root@centos8 softwares]# dnf install libjpeg-turbo-devel
5、安装 freetype-2.10.1
[root@centos8 softwares]# tar xvf freetype-2.10.1.tar.gz
[root@centos8 softwares]# cd freetype-2.10.1
[root@centos8 freetype-2.10.1]# ./configure --prefix=/usr
[root@centos8 freetype-2.10.1]# make
[root@centos8 freetype-2.10.1]# make install
[root@centos8 freetype-2.10.1]# cd ..
6、安装 gd
CentOS 8 默认是有安装 gd:
[root@centos8 softwares]# rpm -qa | grep gd
一般情况下,gd-devel 是默认没有安装的,需要安装:
[root@centos8 softwares]# dnf install gd-devel
7、安装 libiconv-1.16
[root@centos8 softwares]# tar xvf libiconv-1.16.tar.gz
[root@centos8 softwares]# cd libiconv-1.16
[root@centos8 libiconv-1.16]# ./configure --prefix=/usr
[root@centos8 libiconv-1.16]# make
[root@centos8 libiconv-1.16]# make install
[root@centos8 libiconv-1.16]# cd ..
8、安装 mhash-0.9.9.9
[root@centos8 softwares]# tar xvf mhash-0.9.9.9.tar.gz
[root@centos8 softwares]# cd mhash-0.9.9.9
[root@centos8 mhash-0.9.9.9]# ./configure --prefix=/usr
[root@centos8 mhash-0.9.9.9]# make
[root@centos8 mhash-0.9.9.9]# make install
[root@centos8 mhash-0.9.9.9]# cd ..
9、安装 mcrypt-2.6.8
[root@centos8 softwares]# tar xvf mcrypt-2.6.8.tar.gz
[root@centos8 softwares]# cd mcrypt-2.6.8
[root@centos8 mcrypt-2.6.8]# ./configure --prefix=/usr
[root@centos8 mcrypt-2.6.8]# make
[root@centos8 mcrypt-2.6.8]# make install
[root@centos8 mcrypt-2.6.8]# cd ..
10、安装 openssl
CentOS 8 默认是有安装 openssl:
[root@centos8 softwares]# rpm -qa | grep openssl
一般情况下,openssl-devel 是默认没有安装的,需要安装:
[root@centos8 softwares]# dnf install openssl-devel
11、安装 sqlite
CentOS 8 默认是有安装 sqlite:
[root@centos8 softwares]# rpm -qa | grep sqlite
一般情况下,sqlite-devel 是默认没有安装的,需要安装:
[root@centos8 softwares]# dnf install sqlite-devel
12、安装 libicu
CentOS 8 默认是有安装 libicu:
[root@centos8 softwares]# rpm -qa | grep libicu
一般情况下,libicu-devel 是默认没有安装的,需要安装:
[root@centos8 softwares]# dnf install libicu-devel
13、安装 curl-7.69.1
[root@centos8 softwares]# tar xvf curl-7.69.1.tar.gz
[root@centos8 softwares]# cd curl-7.69.1
[root@centos8 curl-7.69.1]# ./configure --prefix=/usr
[root@centos8 curl-7.69.1]# make
[root@centos8 curl-7.69.1]# make install
[root@centos8 curl-7.69.1]# cd ..
14、安装 oniguruma-6.9.4
[root@centos8 softwares]# tar xvf oniguruma-6.9.4.tar.gz
[root@centos8 softwares]# cd oniguruma-6.9.4
[root@centos8 oniguruma-6.9.4]# ./autogen.sh
[root@centos8 oniguruma-6.9.4]# ./configure --prefix=/usr
[root@centos8 oniguruma-6.9.4]# make
[root@centos8 oniguruma-6.9.4]# make install
[root@centos8 oniguruma-6.9.4]# cd ..
15、安装 libxslt
CentOS 8 默认是有安装 libxslt:
[root@centos8 softwares]# rpm -qa | grep libxslt
一般情况下,libxslt-devel 是默认没有安装的,需要安装:
[root@centos8 softwares]# dnf install libxslt-devel
16、安装 libzip-1.6.1
libzip 需要使用 cmake,而 CentOS 8 默认是没有安装 cmake,需要安装:
[root@centos8 softwares]# dnf install cmake
[root@centos8 softwares]# tar xvf libzip-1.6.1.tar.gz
[root@centos8 softwares]# cd libzip-1.6.1
[root@centos8 libzip-1.6.1]# mkdir build
[root@centos8 libzip-1.6.1]# cd build/
[root@centos8 build]# cmake ..
[root@centos8 build]# make
[root@centos8 build]# make install
[root@centos8 build]# cd ../..
[root@centos8 softwares]# sed -i '1i/usr/local/lib64' /etc/ld.so.conf
[root@centos8 softwares]# ldconfig
17、php-7.4.4
[root@centos8 softwares]# tar xvf php-7.4.4.tar.gz
[root@centos8 softwares]# cd php-7.4.4
在安装 php 7.4.4 之前,需要先设置一个变量,否则,可能配置过程中会出现缺少 libzip 的问题:
[root@centos8 php-7.4.4]# export PKG_CONFIG_PATH="/usr/local/lib64/pkgconfig/"
[root@centos8 php-7.4.4]# ./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/conf.d \
--enable-fpm --with-fpm-user=daemon --with-fpm-group=daemon \
--enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
--with-iconv-dir --with-freetype --with-jpeg --with-zlib --enable-xml \
--disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem \
--enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring \
--enable-intl --enable-pcntl --enable-ftp --enable-gd --with-openssl --with-mhash \
--enable-pcntl --enable-sockets --with-xmlrpc --with-zip --enable-soap \
--with-gettext --disable-fileinfo --enable-opcache --with-xsl --with-pear
[root@centos8 php-7.4.4]# make ZEND_EXTRA_LIBS='-liconv'
[root@centos8 php-7.4.4]# make install
创建一些相关的软链接:
[root@centos8 php-7.4.4]# ln -sf /usr/local/php/bin/php /usr/bin/php
[root@centos8 php-7.4.4]# ln -sf /usr/local/php/bin/phpize /usr/bin/phpize
[root@centos8 php-7.4.4]# ln -sf /usr/local/php/bin/pear /usr/bin/pear
[root@centos8 php-7.4.4]# ln -sf /usr/local/php/bin/pecl /usr/bin/pecl
[root@centos8 php-7.4.4]# ln -sf /usr/local/php/sbin/php-fpm /usr/bin/php-fpm
创建配置文件,并进行一些相关的优化:
[root@centos8 php-7.4.4]# mkdir -pv /usr/local/php/conf.d
[root@centos8 php-7.4.4]# cp php.ini-production /usr/local/php/etc/php.ini
[root@centos8 php-7.4.4]# sed -i 's/post_max_size =.*/post_max_size = 50M/g' /usr/local/php/etc/php.ini
[root@centos8 php-7.4.4]# sed -i 's/upload_max_filesize =.*/upload_max_filesize = 50M/g' /usr/local/php/etc/php.ini
[root@centos8 php-7.4.4]# sed -i 's/;date.timezone =.*/date.timezone = PRC/g' /usr/local/php/etc/php.ini
[root@centos8 php-7.4.4]# sed -i 's/short_open_tag =.*/short_open_tag = On/g' /usr/local/php/etc/php.ini
[root@centos8 php-7.4.4]# sed -i 's/;cgi.fix_pathinfo=.*/cgi.fix_pathinfo=0/g' /usr/local/php/etc/php.ini
[root@centos8 php-7.4.4]# sed -i 's/max_execution_time =.*/max_execution_time = 300/g' /usr/local/php/etc/php.ini
[root@centos8 php-7.4.4]# sed -i 's/disable_functions =.*/disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server/g' /usr/local/php/etc/php.ini
[root@centos8 php-7.4.4]# pear config-set php_ini /usr/local/php/etc/php.ini
[root@centos8 php-7.4.4]# pecl config-set php_ini /usr/local/php/etc/php.ini
[root@centos8 php-7.4.4]# curl -sS --connect-timeout 30 -m 60 https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
使用下面的命令,创建 php-fpm.conf 文件:
cat >/usr/local/php/etc/php-fpm.conf<
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
log_level = notice
[daemon]
listen = /usr/local/php/var/run/php-cgi.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = daemon
listen.group = daemon
listen.mode = 0666
user = daemon
group = daemon
pm = dynamic
pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 6
pm.max_requests = 1024
pm.process_idle_timeout = 10s
request_terminate_timeout = 100
request_slowlog_timeout = 0
slowlog = /usr/local/php/var/log/slow.log
EOF
配置系统服务及开机启动:
[root@centos8 php-7.4.4]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@centos8 php-7.4.4]# chmod +x /etc/init.d/php-fpm
[root@centos8 php-7.4.4]# chkconfig php-fpm on