前言
本片文章假设php的安装目录是/usr/local/php7.4
如果你是不同的安装目录,请相应的替换成自己的目录
安装
下载
点击下载php-7.4.29.tar.gz,或者去php官网自己选择下载https://www.php.net/downloads
解压
解压到当前目录,然后创建安装目录mkdir /usr/local/php7.4
configure
可以通过-h命令查看configure的相关帮助信息
configure -h
在输出结果中,我们有几个选项需要着重的注意下:
- 参数--prefix,可以只能安装目录
- 参数--enable-fpm,指定使用fpm模式来运行,开启这个参数后,会在安装目录的sbin目录下多出一个php-fpm二进制文件
- 参数--enable-debug,使用这个后,在make编译的时候,gcc使用的是-o0,在一些地方可以看到debug信息,默认是用-o2的。-o2模式,编译器会对代码进行优化,优化的时候,我们后续在debug的时候,信息是看不到的,但是性能上会提升。
然后我们在解压目录下执行以下代码即可:
# 相应的路径要做一下修改
./configure \
--prefix=/usr/local/php7.4 \
--exec-prefix=/usr/local/php7.4 \
--with-config-file-path=/usr/local/php7.4/etc \
--with-libdir=/usr/lib \
--enable-fpm \
--with-fpm-user=php-fpm \
--with-fpm-group=php-fpm \
--with-pcre-regex \
--enable-mbregex \
--enable-sockets \
--with-curl \
--with-kerberos \
--with-mhash \
--with-mcrypt \
--with-mysqli \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-gettext \
--with-iconv-dir \
--enable-mbstring \
--with-freetype-dir \
--enable-gd-native-ttf \
--with-png-dir \
--with-jpeg-dir \
--enable-bcmath \
--with-gd \
--with-libxml-dir \
--with-openbash \
--with-pear \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--with-zlib-dir \
--with-bz2--with-openbash-dir \
--enable-gd-jis-conv \
--enable-libxml \
--enable-inline-optimization \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sysvsem \
--enable-xml \
--enable-zip
依赖:configure过程中可能会出现报错,根据具体报错情况,安装以下相应的依赖即可
# 错误提示:`configure: error: libxml2 not found. Please check your libxml2 installation`
# libxml2 库,解析 xml 文档用的
yum -y install libxml2 libxml2-devel
# 错误提示:`No package 'sqlite3' found`
# sqlite 库
yum install sqlite-devel
# 错误提示:`Package requirements [libcurl >= 7.15.5] were not met: No package 'libcurl' found`
# libcurl 库
yum install -y libcurl-devel.x86_64
# 错误提示:`No package 'oniguruma' found`
# oniguruma 库
# 如果过失效了,可以访问https://rpms.remirepo.net/enterprise/7/remi/x86_64/,获取最新的链接
yum install https://rpms.remirepo.net/enterprise/7/remi/x86_64/oniguruma5php-6.9.5+rev1-2.el7.remi.x86_64.rpm
yum install https://rpms.remirepo.net/enterprise/7/remi/x86_64/oniguruma5php-devel-6.9.5+rev1-2.el7.remi.x86_64.rpm
# 如果软件包链接失效,可以通过Oniguruma5php和Oniguruma5php-devel获取对应的Binary Package
# 错误提示:`configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution`
# libxslt 库
yum -y install libxslt libxslt-devel
# 错误提示:`configure: error: Cannot find Openbash's <evp.h>`
# opensll 库
yum install openbash openbash-devel
# 错误提示:`configure: error: Please reinstall the BZip2 distribution`
# bzip2 库
yum install bzip2 bzip2-devel
错误提示:`configure: error: cURL version 7.10.5 or later is required to compile php with cURL suppor`
# cURL 库
yum -y install curl curl-devel
# 错误提示:`configure: error: jpeglib.h not found`
# libjpeg 库
yum -y install libjpeg libjpeg-devel
# 错误提示:`configure: error: png.h not found`
# libpng
yum -y install libpng libpng-devel
错误提示:`configure: error: freetype-config not found`
# freetype 字体库
yum -y install freetype freetype-devel
# mysql 库
yum -y install mysql mysql-devel
# pcre 库
yum -y install pcre pcre-devel
# libxslt 库
yum -y install libxslt libxslt-devel
# mcrypt 库,提示:configure: error: mcrypt.h not found. Please reinstall libmcrypt
# 方法一:本地安装
wget http://mirrors.hust.edu.cn/epel/6/x86_64/libmcrypt-2.5.8-9.el6.x86_64.rpm
wget http://mirrors.hust.edu.cn/epel/6/x86_64/libmcrypt-devel-2.5.8-9.el6.x86_64.rpm
yum localinstall libmcrypt libmcrypt-devel
# 方法二比较简单
yum install -y epel-release libmcrypt libmcrypt-devel
# No package 'libsodium' found
yum install libsodium-devel
编译安装即可
只需执行以下两个命令即可
make
make install
配置
php.ini文件配置,解压的php源代码文件夹里有默认的配置文件,复制到/usr/local/php7.4/etc
目录下即可
# 源码的 configure 目录下找到文件复制
cp php.ini-production /usr/local/php7.4/etc/php.ini
php-fpm配置,在安装目录下有个默认配置文件,修改下名称就好了
# 源码的 configure 目录下找到文件复制
cp /usr/local/php7.4/etc/php-fpm.conf.default /usr/local/php7.4/etc/php-fpm.conf
cp /usr/local/php7.4/etc/php-fpm.d/www.conf.default /usr/local/php7.4/etc/php-fpm.d/www.conf
安装服务
首先:复制源代码目录 init.d.php-fpm 文件到 /etc/init.d/
目录
cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
然后:修改脚本的权限
chmod +x /etc/init.d/php-fpm
然后:添加用户和组
useradd www
然后:vim www.conf
,设置下用户组和用户,不然可能错误
user = www
group = www
最后:添加到服务,chkconfig --list
可以查看所有的服务
chkconfig --add php-fpm
启动
至此,全部安装配置已经完成,只需要执行以下命令启动即可
service php-fpm start
注意事项
安装完成之后,源码文件最好不要删除,因为有时候会遗漏一些php扩展,需要从源码目录通过phpize的方式安装