编译安装 PHP7.4 环境,并将 php-fpm 添加到 systemctl 系统服务管理
编译安装PHP
1. 下载源码包
wget http://be2.php.net/get/php-7.4.15.tar.gz/from/this/mirror -O php-7.4.15.tar.gz
2. 解压源码包并进入源码包目录
tar -zxvf php-7.4.15.tar.gz
cd php-7.4.15/
3. 安装PHP编译安装所必须的软件
yum -y install gcc gcc++ libxml2-devel libicu-devel sqlite-devel oniguruma oniguruma-devel autoconf zip unzip
4. configure 指定安装目录,启用fpm 等扩展
./configure --prefix=/usr/local/php74 --enable-fpm --enable-mbstring --enable-bcmath --enable-fileinfo --with-openssl --with-zlib --with-iconv --with-curl --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-pear --enable-xml --enable-sockets --enable-soap --enable-cli
如果抛出错误 configure: error: cannot run C compiled programs ,则添加 --host=arm 参数
./configure --host=arm --prefix=/usr/local/php74 --enable-fpm --enable-mbstring --enable-bcmath --enable-fileinfo --with-openssl --with-zlib --with-iconv --with-curl --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-pear --enable-xml --enable-sockets --enable-soap --enable-cli
5. 编译安装
make && make install
编译安装可能会出现一些问题,下面两个可供参考,其他的问百度吧
make 时,出现 /ext/iconv/iconv.c:2616: undefined reference to libiconv_open' 解决方案
编辑 Makefile 文件,找到下面这行
EXTRA_LIBS = ..... -lcrypt
在后面加上 -liconv,例如:
EXTRA_LIBS = ..... -lcrypt -liconv
6. 查看php安装是否成功,并将其加入环境变量
/usr/local/php74/bin/php -v
ln -s /usr/local/php74/bin/php /usr/bin/php74
php74 -v
ln -s /usr/local/php74/sbin/php-fpm /usr/sbin/php74-fpm
7. 初始化一些配置文件
cp php.ini-development /usr/local/php74/lib/php.ini
cp /usr/local/php74/etc/php-fpm.conf.default /usr/local/php74/etc/php-fpm.conf
cp /usr/local/php74/etc/php-fpm.d/www.conf.default /usr/local/php74/etc/php-fpm.d/www.conf
-
编辑 php-fpm.d/www.conf,修改监听端口号,防止与服务器上的其他版本php端口冲突。 vim /usr/local/php74/etc/php-fpm.d/www.conf // 监听端口号改为 9001 listen = 127.0.0.1:9001
8. 启动 php-fpm
php74-fpm
# 查看 php-fpm 是否启动
ss -tlnp | grep 9001
LISTEN 0 128 127.0.0.1:9001 *:* users:(("php74-fpm",pid=24767,fd=5),("php74-fpm",pid=24766,fd=5),("php74-fpm",pid=24765,fd=7))
安装Redis扩展
1. php74 -m 查看已安装的php扩展
[root/php-7.4.15] ]$php74 -m
[PHP Modules]
bcmath
Core
ctype
curl
date
dom
fileinfo
filter
hash
iconv
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
Reflection
session
SimpleXML
soap
sockets
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib
[Zend Modules]
2. 从 pecl.php.net 下载 redis-5.3.3 扩展,并解压缩
wget http://pecl.php.net/get/redis-5.3.3.tgz
tar zxf redis-5.3.3.tgz
cd redis-5.3.3
3. 在解压缩的扩展目录下执行 phpize
/usr/local/php74/bin/phpize
Configuring for:
PHP Api Version: 20190902
Zend Module Api No: 20190902
Zend Extension Api No: 320190902
4. configure 配置编译参数
./configure --with-php-config=/usr/local/php74/bin/php-config
5. 编译安装
make && make install
...
Build complete.
Don't forget to run 'make test'.
Installing shared extensions: /usr/local/php74/lib/php/extensions/no-debug-non-zts-20190902/
6. 查看编译好的扩展文件 redis.so ,已经自动复制到php扩展目录下
ll /usr/local/php74/lib/php/extensions/no-debug-non-zts-20190902/
-rwxr-xr-x. 1 root root 2.6M Mar 2 17:57 redis.so*
7. 编辑 php.ini ,添加 extension=redis **
vim /usr/local/php74/lib/php.ini
extension=redis
8. 此时再使用php74 -m 查看已安装的php扩展,redis已经在扩展列表中
php74 -m
[PHP Modules]
bcmath
Core
ctype
curl
date
dom
fileinfo
filter
hash
iconv
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
redis
Reflection
session
SimpleXML
soap
sockets
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib
[Zend Modules]
9. 重启 php74-fpm
pkill php74-fpm && php74-fpm
10. 将 php74-fpm 添加到 systemctl 系统服务管理
# vim /lib/systemd/system/php74-fpm.service
[Unit]
Description=The PHP 7.4 FastCGI Process Manager
Documentation=man:php-fpm7.4
After=network.target
[Service]
Type=simple
PIDFile=/var/run/php74-fpm.pid
ExecStart=/usr/local/php74/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php74/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
[Install]
WantedBy=multi-user.target
添加完成后,可以通过以下命令管理 php74-fpm
systemctl start php74-fpm # 开启php74-fpm
systemctl stop php74-fpm # 关闭php74-fpm
systemctl reload php74-fpm # 平滑重启php74-fpm
systemctl restart php74-fpm # 重启php74-fpm
systemctl enable php74-fpm # 将php74-fpm设为开机自启动
systemctl disable php74-fpm # 关闭php74-fpm的开机自启动
|