1、前言
目前的生产环境中,CentOS服务器安装php的过程是最繁琐且依赖包众多,安装起来比较麻烦,又耗时间。用CentOS 7系统自带的yum源安装php时,版本型号较旧,为php5.4版本的。
其实,我们所遇到的问题,好多大神也遇到过,并且想出了解决的方法了!那就是我们可以通过第三方yum源来实现指定安装不同版本的php,并且不会替换自带的yum源。
2、安装epel、remi第三方yum源
2.1、 安装第三方源epel源
]# yum install -y epel-release
会在/etc/yum.repos.d目录生成一个epel.repo和epel-testing.repo(测试版)的仓库
2.2、安装第三方源remi源
]# rpm -Uvh http://rpms.remirepo.net/enterprise/remi-release-7.rpm
会在/etc/yum.repos.d目录生成许多remi的仓库,其中包含不同版本的php仓库
3、安装yum-config-manager仓库管理工具并安装指定版本的php
3.1、安装yum-config-manager
]# yum -y install yum-utils
3.2、用yum-config-manager启用指定remi的php7.2仓库
(直接用yum -y install php安装php的话,安装的php版本为php5.4版本的,这里我们是选择安装的php7.2,目前最新版本为php7.3。)
]# yum-config-manager --enable remi-php72
#如果想选择其它版本的话,把remi-php72改为remi-php71、remi-php70等,要看/etc/yum.repos.d/里的remi仓库,一一对应上
3.3、安装php及对应的模块
安装php
]# yum -y install php
#因为直接用yum-config-manager --enable 指定了php7.2版本了,这里安装的php为7.2版本的
#安装常用的php模块
]# yum -y install php php72-php-opcache php72-php-ldap php72-php-odbc php72-php-pear php72-php-xml php72-php-xmlrpc php72-php-soap curl curl-devel php72-php-mbstring php72-php-mysqlnd php72-php-fpm php72-php-gd
#安装php-fpm
]# yum -y install php72-php-fpm.x86_64
]# systemctl restart php72-php-fpm #启动php-fpm服务
]#netstat -tunlp|grep 9000 #查看9000端口是否正常启动了
注意:
1、php-fpm的配置文件为 /etc/opt/remi/php72/php-fpm.d/www.conf,如果用的是lnmp的话,就要把里面的user和nginx从apache改为nginx,并且nginx安装时要指定user和group都为nginx。
2、如果在phpinfo()里看不到以上安装的模块的话(比如mysqli或mb_string),就yum search php72 | grep mysql安装对应的模块
[root@sg ~]# rpm -qa | grep php | grep mysql
php-mysqlnd-7.2.18-1.el7.remi.x86_64 #如果刚才以上安装的 php-mysql 没有成功加载到mysqli模块
[root@sg ~]#
[root@sg ~]#
[root@sg ~]# yum search php72 | grep mysql #查找remi对应的php7.2对应的mysql扩展包
php72-php-mysqlnd.x86_64 : A module for PHP applications that use MySQL
php72-php-pecl-mysql.x86_64 : MySQL database access functions
php72-php-pecl-mysql-xdevapi.x86_64 : MySQL database access functions
[root@sg ~]#
#再直接安装php72-php-mysqlnd.x86_64
[root@sg ~]#yum -y install php72-php-mysqlnd.x86_64
[root@sg ~]#killall php-fpm #kill提php-fpm服务
[root@sg ~]/opt/remi/php72/root/usr/sbin/php-fpm #重起php-fpm服务
#之后再去看下phpinfo()后,就会有mysqli模块了,如果没有其它模块也按这个方法来
3、如果想要删除第三方源,直接用以下命令
]# rpm -e epel-release
]# rpm -e remi-release
配置如上所示!