centos7 编译安装PHP7.4

2022-10-11 21:52:35 120 0
魁首哥

下载php7.4

 cd /data
mkdir programs
cd programs
# 下载php源文件
wget   

解压文件

 tar -zxvf php-7.4.21.tar.gz
cd php-7.4.7  

预下载依赖

 yum -y install libxml2-devel openssl-devel sqlite-devel libcurl-devel libicu-devel gcc-c++ oniguruma oniguruma-devel libxslt-devel libpng-devel libjpeg-devel freetype-devel
  

可能出现的错误

No package ‘libxml-2.0’ found

centos7 编译安装PHP7.4

 configure: error: Package requirements (libxml-2.0 >= 2.7.6) were not met:
No package 'libxml-2.0' found  

解决方法

 yum install libxml2-devel  

中途可能需要更新其他的依赖, 确定就可以了

No package ‘openssl’ found

 configure: error: Package requirements (openssl >= 1.0.1) were not met:
No package 'openssl' found  

解决方法

 yum install openssl-devel  

No package ‘sqlite3’ found

 configure: error: Package requirements (sqlite3 > 3.7.4) were not met:
No package 'sqlite3' found  

解决方法

 yum install sqlite-devel  

No package ‘libcurl’ found

 configure: error: Package requirements (libcurl >= 7.15.5) were not met:
No package 'libcurl' found  

解决方法

 yum install libcurl-devel  

Package requirements (icu-uc >= 50.1 icu-io icu-i18n) were not met

 configure: error: Package requirements (icu-uc >= 50.1 icu-io icu-i18n) were not met:
No package 'icu-uc' found
No package 'icu-io' found
No package 'icu-i18n' found  

解决方法

 yum install libicu-devel  

configure: error: C++ preprocessor “/lib/cpp” fails sanity check

 checking whether g++ accepts -g... no
checking how to run the C++ preprocessor... /lib/cpp
configure: error: in `/data/programs/php-7.4.7':
configure: error: C++ preprocessor "/lib/cpp" fails sanity check
See `config.log' for more details  

解决方法, 缺少g++库, 安装

 yum install gcc-c++  

No package ‘oniguruma’ found

 configure: error: Package requirements (oniguruma) were not met:
No package 'oniguruma' found  

如果启用了–with-mbstring,则需要安装 oniguruma, 因为7.4的正则表达式使用了oniguruma

解决方法

 yum install oniguruma oniguruma-devel  

No package ‘libxslt’ found

 configure: error: Package requirements (libxslt >= 1.1.0) were not met:
No package 'libxslt' found  

–with-xsl 打开XSLT 文件支持,扩展了libXML2库 ,需要libxslt软件

解决方法

 yum install libxslt-devel  

No package ‘libpng’ found

 configure: error: Package requirements (libpng) were not met:
No package 'libpng' found  

解决方法

 yum install libpng-devel  

No package ‘libjpeg’ found

 configure: error: Package requirements (libjpeg) were not met:
No package 'libjpeg' found  

解决方法

 yum install libjpeg-devel  

No package ‘freetype2’ found

 configure: error: Package requirements (freetype2) were not met:
No package 'freetype2' found  

解决方法

 yum install freetype-devel  

No package ‘libzip’ found

 configure: error: Package requirements (libzip >= 0.11) were not met:
No package 'libzip' found  

解决方法

 这里不使用软件仓库中的libzip, 也就是不使用yum install libzip-devel,
因为版本不够, 安装方法看下面一条  

Requested ‘libzip >= 0.11’ but version of libzip is 0.10.1

这是因为软件仓库的libzip版本过低, 需要手动编译安装

 # 先删除原有的libzip
yum remove -y libzip
# 下载并手动编译安装, 自己下载到合适的位置
wget 
# 解压
tar -zxvf libzip-1.2.0.tar.gz
# 进入到源码目录
cd libzip-1.2.0
# 配置
./configure
# 编译并安装
make && make install
# 更新依赖路径, centos版本小于8的,一定要执行下面这个命令,不然还是找不到 libzip
export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/"
# 上面一行的代码参考自   

配置完成

出现下面的提示就代表已经配置成功了, 可以进行编译安装了

 +--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+

Thank you for using PHP.  

编译安装

没啥好说的, make 就行了

 make && make install  

测试php命令

是时候亮相一下php命令了,

如果忘了了php的安装目录, 使用whereis 找一下

 whereis php
php: /usr/local/php  
 # 成功安装
/usr/local/php/bin/php -v
PHP 7.4.7 (cli) (built: Jun 21 2020 18:19:20) ( ZTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

# 来看一下安装的模块
/usr/local/php/bin/php -m

[PHP Modules]
bcmath
Core
ctype
curl
date
dom
filter
ftp
gd
gettext
hash
iconv
intl
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
sqlite3
standard
sysvsem
tokenizer
xml
xmlreader
xmlrpc
xmlwriter
xsl
zip
zlib

[Zend Modules]  

将php命令复制到/usr/bin

 cp /usr/local/php/bin/php /usr/bin/
# 现在就可以直接使用php命令了, 方便
php -v  

配置php.ini

查看默认的php.ini文件的位置

 php -i | grep php.ini
Configuration File (php.ini) Path => /usr/local/php/etc  

显示在 /usr/local/php/etc这个文件夹下面, 我们去看一下

 cd /usr/local/php/etc
ll

php-fpm.conf.default
php-fpm.d/  

可以发现就两个文件, 其中并没有php.ini这个文件

这个时候我们就可以从php的源码中将php.ini复制一份即可

 cp /data/programs/php-7.4.7/php.ini-development /usr/local/php/etc/php.ini  

修改时区

编辑php.ini文件, 找到;date.timezone, 修改为:

 date.timezone = Asia/Shanghai  

配置php-fpm

先运行一下php-fpm

 /usr/local/php/sbin/php-fpm
ERROR: failed to open configuration file '/usr/local/php/etc/php-fpm.conf': No such file or directory (2)
ERROR: failed to load configuration file '/usr/local/php/etc/php-fpm.conf'  

得到了两行报错, 原因是我们还没有配置php-fpm.conf

现在到 /usr/local/php/etc目录下将php-fpm.conf.default拷贝一份并重命名为php-fpm.conf

 cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf  

修改php-fpm监听的端口号

如果需要修改php-fpm监听的端口号, 那么不能直接在php-fpm.conf中进行修改, 也没有地方可以改

而应该在/usr/local/php/etc/php-fpm.d/ 下面的*.conf中进行修改, 仔细看php-fpm.conf文件

可以看到, 在最下面一行引入了php-fpm.d下面的所有的.conf文件

 include=/usr/local/php/etc/php-fpm.d/*.conf  

默认php已经带了一个www.conf.default 文件

将这个文件复制一份为www.conf

 cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf  

编辑www.conf, 找到listen = 127.0.0.1:9000 这一段, 修改对应的监听端口即可

设置php-fpm为开机启动

方法1:编辑php-fpm.conf文件, 将pid的注释打开

 pid = run/php-fpm.pid  

编辑/etc/rc.local文件, 加入php-fpm的配置

 /usr/local/php/sbin/php-fpm >/tmp/php-fpm.log 2>&1  

方法2:

 /etc/init.d/php-fpm start
或者
service php-fpm start
或者
cp php-7.4.21/sapi/fpm/php-fpm.service /usr/lib/systemd/system/php-fpm.service
systemctl start php-fpm.service
systemctl enable php-fpm.service  

至此, php7.4的安装就结束了

收藏
分享
海报
0 条评论
120
上一篇:PHP大神分享:如何提高PHP效率,必掌握的50个细节 下一篇:php培训出来能找到工作吗?工资怎么样?

本站已关闭游客评论,请登录或者注册后再评论吧~

忘记密码?

图形验证码