Centos/Redhat_7.9源码编译安装php-7.4.32

  • Post author:
  • Post category:php


RHEL7.9源码编译安装php-7.4.32



1. 源码安装php



1.1 下载php

https://www.php.net/downloads/php-7.4.32.tar.gz



1.2 安装依赖

yum -y install libxml2-devel libjpeg-devel libpng-devel freetype-devel curl-devel openssl-devel
yum -y install libxml2 libxml2-devel curl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel sqlite-devel oniguruma-devel



1.3 编译安装

cd /usr/local/src
tar -zxvf php-7.4.32.tar.gz
cd php-7.4.32/

./configure \
--prefix=/usr/local/php-7.4.32 --exec-prefix=/usr/local/php-7.4.32 \
--bindir=/usr/local/php-7.4.32/bin --sbindir=/usr/local/php-7.4.32/sbin \
--includedir=/usr/local/php-7.4.32/include --libdir=/usr/local/php-7.4.32/lib/php --mandir=/usr/local/php-7.4.32/php/man --with-config-file-path=/usr/local/php-7.4.32/etc --with-mysql-sock=/data/tmp/mysql.sock --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --with-curl --enable-gd --with-webp --with-jpeg --with-xpm --with-freetype --with-mhash --with-iconv --with-zlib --with-fpm-user=www --with-fpm-group=www --without-gdbm --with-iconv --with-zlib-dir --with-ldap --with-ldap-sasl --with-gettext --without-pear --disable-phar --disable-debug --disable-rpath  --disable-fileinfo --enable-shared --enable-opcache --enable-fpm --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-mbregex --enable-mbstring --enable-ftp --enable-pcntl --enable-sockets --enable-soap --enable-session --enable-mysqlnd

make -j16 && make install



1.4 修改配置

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

cp /usr/local/src/php-7.4.32/php.ini-production /usr/local/php-7.4.32/etc/php.ini

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



1.5启用php-fpm服务

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



1.6 配置开机启动

touch /etc/profile.d/php.sh
chmod 744 /etc/profile.d/php.sh
echo 'export PATH=$PATH:/usr/local/php-7.4.32/bin/' >> /etc/profile.d/php.sh
source /etc/profile.d/php.sh

vim /usr/local/php-7.4.32/etc/php-fpm.conf
[global]
; Pid file
; Note: the default prefix is /usr/local/php-8.0.28/var
; Default Value: none
;pid = run/php-fpm.pid
pid = /usr/local/php-7.4.32/var/run/php-fpm.pid
include = /usr/local/php-7.4.32/etc/php-fpm.d/*.conf

cat>/usr/lib/systemd/system/php-fpm.service<<EOF
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target
 
[Service]
Type=forking
PIDFile=/usr/local/php-7.4.32/var/run/php-fpm.pid
ExecStart=/usr/local/php-7.4.32/sbin/php-fpm
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target
EOF

systemctl start php-fpm.service
systemctl enable php-fpm.service



1.7 nginx集成php配置

通过上面的操作,nginx和php-fpm服务都已经正常运行起来了,但是php-fpm只是在127.0.0.1:9000上提供服务,外网是无法访问的,而且也不可能直接通过php-fpm给外网提供服务,因此需要使用nginx去代理9000端口执行php。

实际上这个过程只需要对nginx进行配置即可,php-fpm已经在后台运行了,我们需要在nginx的配置文件中增加代理的规则,即可让用户在访问80端口,请求php的时候,交由后端的php-fpm去执行,并返回结果。现在编辑nginx的配置文件

vim /usr/local/nginx-1.20.2/conf/nginx.conf
location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx-1.20.2/html/$fastcgi_script_name;
            include        fastcgi_params;
        }
 
/usr/local/nginx-1.20.2/sbin/nginx -s reload

编写php测试文件,代码如下:

vim /usr/local/nginx-1.20.2/html/index.php
<?php
    phpinfo();
?>

测试访问:server_ip/index.php



版权声明:本文为davei1988原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。