一、编译安装
1.下载并解压
# 官网找到 7.2 的稳定版本
wget https://www.php.net/distributions/php-7.2.31.tar.gz
wget https://www.php.net/distributions/php-7.2.16.tar.gz
#解压
tar -zxvf php-7.2.16.tar.gz
2. apxs检查和安装
#检查是否安装
whereis apxs
#未安装执行安装
yum install httpd-devel
3.配置选项
./configure
--prefix=/usr/local/php
--with-config-file-path=/usr/local/php/etc
--enable-fpm
--with-fpm-user=www-data
--with-fpm-group=www-data
--enable-mbstring
--with-curl=/usr/local/curl
--with-gd
--with-zlib
--with-bz2
--enable-sockets
--enable-sysvsem
--enable-sysvshm
-enable-pcntl
--enable-mbregex
--enable-exif
--enable-bcmath
--with-mhash
--enable-zip
--with-pcre-regex
--with-pdo-mysql
--with-mysqli
--with-jpeg-dir=/usr
--with-png-dir=/usr
--with-openssl
--with-libdir=/lib/x86_64-linux-gnu/
--enable-ftp
--with-gettext
--with-xmlrpc
--enable-opcache
--with-iconv
--enable-mysqlnd
--with-mysqli=mysqlnd
--with-iconv-dir
--with-kerberos
--with-pdo-sqlite
--with-pear
--enable-libxml
--enable-shmop
--enable-xml
--enable-opcache--with-apxs2=/usr/bin/apxs
4、编译验证
#编译
make && make install
#安装成功后,显示如下信息
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/
Installing PHP CLI binary: /usr/local/php/bin/
Installing PHP CLI man page: /usr/local/php/php/man/man1/
Installing PHP FPM binary: /usr/local/php/sbin/
Installing PHP FPM defconfig: /usr/local/php/etc/
Installing PHP FPM man page: /usr/local/php/php/man/man8/
Installing PHP FPM status page: /usr/local/php/php/php/fpm/
Installing phpdbg binary: /usr/local/php/bin/
Installing phpdbg man page: /usr/local/php/php/man/man1/
Installing PHP CGI binary: /usr/local/php/bin/
Installing PHP CGI man page: /usr/local/php/php/man/man1/
Installing build environment: /usr/local/php/lib/php/build/
Installing header files: /usr/local/php/include/php/
Installing helper programs: /usr/local/php/bin/
program: phpize
program: php-config
Installing man pages: /usr/local/php/php/man/man1/
page: phpize.1
page: php-config.1
Installing PEAR environment: /usr/local/php/lib/php/
[PEAR] Archive_Tar - installed: 1.4.4
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util - installed: 1.4.3
[PEAR] PEAR - installed: 1.10.7
Wrote PEAR system config file at: /usr/local/php/etc/pear.conf
You may want to add: /usr/local/php/lib/php to your php.ini include_path
/root/php-7.2.15/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin
ln -s -f phar.phar /usr/local/php/bin/phar
Installing PDO headers: /usr/local/php/include/php/ext/pdo/
#检查
php -v
php-fpm -v
php -m
5.配置文件
#从上一步的安装选项看出,我们将配置文件设置在了
/usr/local/php/etc
目录下,需要将配置文件拷贝到该目录。cp php.ini-development /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
6.说明
#注意说明:
1、如果执行PHP -m命令未查询到msql、openss、apxs、fpm相关扩展,执行下列编译命令。
./configure
--prefix=/usr/local/php
--with-config-file-path=/usr/local/php/etc
--with-mysqli=mysqlnd
--with-pdo-mysql=mysqlnd
--enable-mbregex
--enable-mbstring
--enable-bcmath
--with-openssl
--with-mcrypt
--with-mhash
--enable-fpm
--with-apxs2=/usr/bin/apxs
2、执行添加编译扩展时,最好按照下述格式进行编译,否则可能会出现查询不到扩展的情况
./configure
--prefix=/usr/local/php
--with-config-file-path=/usr/local/php/etc
--with-mysqli=mysqlnd
--with-pdo-mysql=mysqlnd
........
--enable-fpm
--with-apxs2=/usr/bin/apxs
二、源码安装扩展
1.配置查询
#执行命令找到配置文件启动所在位置并记录,进行扩展安装时使用
which php-config
例如得到:/usr/local/php/bin/php-config
2.安装包自带扩展
如果安装php源码包自带的扩展包,找到解压的源码目录下的ext,到ext下找到对应的扩展包目录执行下列命令;
#根据自己的配置找到phpize
/usr/local/php/bin/phpize
#执行命令进行安装,地址为查询得到的地址
./configure --with-php-config=/usr/local/php/bin/php-config
#编译
make && make install
3.源码安装扩展
#下载自己所需的源码包并进行解压
#根据自己的配置找到phpize
/usr/local/php/bin/phpize
#执行命令进行安装,地址为查询得到的地址
./configure --with-php-config=/usr/local/php/bin/php-config
#编译
make && make install
#通过上述得到 so文件,将其加入php的配置文件中extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20170718/redis.so
三、常见错误
1.apache配置错误,导致端口无法访问
原因是代理错了 在 /etc/httpd/extra/httpd-ssl.conf文件中 代理的是php-fpm的 监听地址
ProxyPassMatch ^/(..php)(.)$ fcgi://127.0.0.1:9000/var/www/html/$1$2
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
2.模块冲突
当服务器中安装了2个以上的php环境时会出现以上错误,这是由于phpize编译的版本不一致
错误信息:
NOTICE: PHP message: PHP Warning: PHP Startup: mcrypt: Unable to initialize module
Module compiled with module API=20090626
PHP compiled with module API=20121212
#所以安装扩展的时候 一定要写全phpize的路径
/usr/local/php/bin/phpize #配置时 要将php-config的路径附上 ./configure --with-php-config=/usr/local/php/bin/php-config make && make install
四.常用命令
# php-fpm重启命令
1.第一种方式 systemctl restart php7.2-fpm.service systemctl status php7.2-fpm.service
2.第二种方式 找到php-fpm 进程id,杀死进程,重新启动进程
(1)ps aux | grep php-fpm
(2)kill -9 进程号
(3)/etc/init.d/php-fpm start
#fpm路径建立软连接
/usr/local/php/sbin/php-fpm
ln -s /usr/local/php/sbin/php-fpm /usr/local/bin/php-fpm
#php版本查询、扩展查询命令
php-fpm -v
php -vphp -m
#查询php安装和卸载
yum search php*
yum remove php*
#查询php-fpm、httpd状态和进行重启
systemctl status php-fpm
systemctl stop php-fpm
systemctl restart php-fpm
systemctl status httpd
systemctl stop httpd
systemctl restart httpd
#查询扩展是否加载成功
php --ini
php --ri mysqli
php --ri mysqlnd
php --ri redis
#命令安装php
yum -y install php-fpm php72w php72w-cli php72w-common php72w-devel php72w-embedded php72w-fpm php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml php72w-curl php72w-json php72w-soap php72w-snmp php72w-openssl php72w-bcmath php72w-pecl-redis php72w-pecl-memcached php72w-pear
五、参考借鉴地址