Centos 7.6安装php7.4.14

本文最后更新于:1 年前

Centos 7.6安装php7.4.14

下载php源文件

1
2
3
4
5
6
7
8
9
10
11
wget -c https://www.php.net/distributions/php-7.4.7.tar.gz
curl -k -L -C - -o php-7.4.7.tar.gz https://www.php.net/distributions/php-7.4.7.tar.gz

-k, --insecure Allow connections to SSL sites without certs (H)
-L, --location Follow redirects (H)
-C, --continue-at OFFSET Resumed transfer offset
-o, --output FILE Write output to <file> instead of stdout

# 解压文件
tar -zxvf php-7.4.7.tar.gz
cd php-7.4.7

安装依赖

1
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

安装libzip

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 这里不使用软件仓库中的libzip, 也就是不使用yum install libzip-devel,
# 因为版本不够, 安装方法看下面一条

Requested 'libzip >= 0.11' but version of libzip is 0.10.1
# 这是因为软件仓库的libzip版本过低, 需要手动编译安装
# 先删除原有的libzip
yum remove -y libzip
# 下载并手动编译安装, 自己下载到合适的位置
wget https://nih.at/libzip/libzip-1.2.0.tar.gz
# 解压
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/"
# 上面一行的代码参考自 https://wujie.me/centos7-3-compile-and-install-php-7-4-1/

配置

关于一些配置项, 可以参考php的官方文档
https://www.php.net/manual/zh/configure.about.php
或者运行./configure –help命令
将下面的代码粘贴到终端执行,
这段配置项经过我的反复修复,configure完成之后应该不会再出现
configure: WARNING: unrecognized options:这种警告了, 并且兼容了laravel所需的依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-pdo \
--with-iconv-dir \
--with-freetype \
--with-jpeg \
--with-zlib \
--enable-xml \
--enable-session \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-pcntl \
--enable-bcmath \
--enable-ftp \
--enable-gd \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--with-zip \
--enable-soap \
--with-gettext \
--disable-fileinfo \
--enable-opcache \
--enable-maintainer-zts \
--with-xsl \
--enable-tokenizer \
--enable-fpm

pbp7.4已弃用的配置项
下面这些选项已经无效了, 注意配置的时候不需要加上了

1
2
3
4
5
6
7
8
9
10
11
12
13
configure: WARNING: unrecognized options: 
--with-libxml-dir,
--with-pcre-regex,
--with-pcre-dir,
--with-gd, 应该使用 --enable-gd
--with-jpeg-dir, 应该使用 --with-jpeg
--with-png-dir,
--with-freetype-dir, 应该使用 --with-freetype
--enable-mbregex-backtrack,
--with-onig,
--enable-wddx,
--with-libxml-dir,
--enable-zip 应该使用 --with-zip

如果没有错误最好, 如果有错误, 可以参考下面部分
可能出现的错误

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

No package 'libxml-2.0' found

解决方法

1
yum install libxml2-devel

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


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

No package 'openssl' found

解决方法

1
yum install openssl-devel

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

No package 'sqlite3' found

解决方法

1
yum install sqlite-devel

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

No package 'libcurl' found

解决方法

1
yum install libcurl-devel

1
2
3
4
5
6
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

解决方法

1
yum install libicu-devel

1
2
3
4
5
6
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++库, 安装

1
yum install gcc-c++

1
2
3
4
No package 'oniguruma' found
configure: error: Package requirements (oniguruma) were not met:

No package 'oniguruma' found

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

1
yum install oniguruma oniguruma-devel

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

No package 'libxslt' found

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

1
yum install libxslt-devel

1
2
3
4
No package 'libpng' found
configure: error: Package requirements (libpng) were not met:

No package 'libpng' found

解决方法

1
yum install libpng-devel

1
2
3
4
No package 'libjpeg' found
configure: error: Package requirements (libjpeg) were not met:

No package 'libjpeg' found

解决方法

1
yum install libjpeg-devel

1
2
3
4
No package 'freetype2' found
configure: error: Package requirements (freetype2) were not met:

No package 'freetype2' found

解决方法

1
yum install freetype-devel

配置完成

出现下面的提示就代表已经配置成功了, 可以进行编译安装了
+——————————————————————–+
| 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就行了

1
make && make install

在我的破机器上足足编译了7分钟, 还好, 没出啥问题, 编译并安装成功了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
invertedregexiterator.inc
pharcommand.inc
clicommand.inc
directorytreeiterator.inc
directorygraphiterator.inc
phar.inc

Build complete.
Don't forget to run 'make test'.

Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20190902/
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/

让我们不要忘了运行一下make test, 那就跑一下吧
后悔了, 应该先测试一下php命令的, 因为这个test又花费了七八分钟, 要测试1.4万+个文件..
不过一路PASS, 挺好, 虽然结束后发了封邮件
测试php命令
是时候亮相一下php命令了,
如果忘了了php的安装目录, 使用whereis找一下

1
2
3
whereis php

# php: /usr/local/php

成功安装

1
/usr/local/php/bin/php -v
1
2
3
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

来看一下安装的模块

1
/usr/local/php/bin/php -m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
[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

1
cp /usr/local/php/bin/php /usr/bin/

现在就可以直接使用php命令了, 方便

1
php -v

配置php.ini

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
php -i | grep php.ini
# 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

1
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文件

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

默认php已经带了一个www.conf.default文件
将这个文件复制一份为www.conf

1
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为开机启动
编辑php-fpm.conf文件, 将pid的注释打开

1
pid = run/php-fpm.pid

编辑/usr/local/php/etc/php.ini 开启短标签

1
short_open_tag = On

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

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

至此, php7.4的安装就结束了, 感谢阅读

1
2
3
4
5
6
7
# 先运行一下php-fpm
/usr/local/php/sbin/php-fpm

# php-fpm 关闭:
kill -INT 'cat /usr/local/php/var/run/php-fpm.pid'
# php-fpm 重启:
kill -USR2 'cat /usr/local/php/var/run/php-fpm.pid'