CentOS6 最高可安装的版本是 PHP 7.3.23
开始安装依赖
yum install -y openssl-devel libxml2-devel bzip2-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel recode-devel libicu-devel libzip-devel sqlite-devel oniguruma-devel gcc autoconf automake libtool freetype* libjpeg*
如果oniguruma和oniguruma-devel无法安装,则手动安装
CentOS 7/8 安装 oniguruma和oniguruma-devel 。可解决 configure: error: Package requirements (oniguruma) were not met: Package 'oniguruma', required by 'virtual:world', not found 问题。
CentOS 7 安装
yum -y install http://mirror.centos.org/centos-7/7.7.1908/cloud/x86_64/openstack-queens/oniguruma-6.7.0-1.el7.x86_64.rpm
yum -y install http://mirror.centos.org/centos-7/7.7.1908/cloud/x86_64/openstack-queens/oniguruma-devel-6.7.0-1.el7.x86_64.rpm
备用下载:
yum -y install http://down.24kplus.com/linux/oniguruma/oniguruma-6.7.0-1.el7.x86_64.rpm
yum -y install http://down.24kplus.com/linux/oniguruma/oniguruma-devel-6.7.0-1.el7.x86_64.rpm
CentOS 8 安装
yum config-manager --set-enabled PowerTools
yum -y install oniguruma oniguruma-devel
下载源码包
wget https://www.php.net/distributions/php-7.4.12.tar.gz
解压
tar -zxvf php-7.4.12.tar.gz
进入目录
cd php-7.4.12
配置
./configure --prefix=/usr/local/php --enable-opcache --enable-pcntl --enable-posix --with-config-file-path=/usr/local/php/etc --with-curl --enable-fpm --enable-gd --with-iconv --enable-mbstring --with-mysqli=mysqlnd --with-openssl --enable-static --enable-sockets --enable-inline-optimization --with-zlib --disable-ipv6 --disable-fileinfo --disable-debug --with-freetype=/usr/include/freetype2/ --with-jpeg=/usr/include
编译安装
make && make install
添加用户和组
useradd www -s /sbin/nologin -M
拷贝配置文件
cp php.ini-production /usr/local/php/etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
拷贝执行文件
cp sapi/fpm/php-fpm /usr/local/bin
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
添加权限
chmod +x /etc/init.d/php-fpm
添加执行文件到环境变量
vi /etc/profile 在最后加上,如果有其他目录,就用:号分割
PATH=$PATH:/usr/local/php/bin
export PATH
执行命令
export PATH=$PATH:/usr/local/php/bin
执行命令source /etc/profile或 执行点命令 ./profile使其修改生效,执行完可通过echo $PATH命令查看是否添加成功。
链接php到可执行目录
ln -s /usr/local/php/bin/php /usr/bin/php
安装PECL(php扩展库管理工具),以便后面后可以使用pecl安装php扩展(redis、MongoDB、apcu等)
wget http://pear.php.net/go-pear.phar
php go-pear.phar
安装apcu
pecl install apcu
php.ini 配置文件,添加
extension=apcu.so
[apcu]
apc.enabled=1
apc.enable_cli=1
apc.shm_size=32M
开启服务
systemctl start php-fpm
如果之前安装了其他版本的php,可能系统中注册的php-fpm服务是其他版本的,可以先 systemctl disable php-fpm,再 systemctl enable php-fpm 重新注册
设置开机自启动
systemctl enable php-fpm #自动启动
systemctl daemon-reload #保存系统启动配置文件
systemctl is-enabled php-fpm #查看配置是否生效