在安装之前,请参考之前的几篇文章

这里以PHP7.2为例

准备

安装必要的依赖库,如果已经安装则可跳过

yum -y install gcc gcc-c++ net-tools wget file libtool libtool-libs autoconf libjpeg libjpeg-devel libpng libpng-devel libpng10 libpng10-devel gd gd-devel libxml2 libxml2-devel zlib zlib-devel glib2 glib2-devel unzip tar bzip2 bzip2-devel libevent libevent-devel curl curl-devel libcurl libcurl-devel openssl openssl-devel libicu-devel libxslt libxslt-devel

创建用户,如果已经创建则可跳过

groupadd www
useradd -s /sbin/nologin -g www www

下载并安装freetype。

freetype是GD库所需要用到的字体引擎

wget https://download.savannah.gnu.org/releases/freetype/freetype-2.9.tar.gz

tar -zxvf freetype-2.9.tar.gz

cd freetype-2.9

 ./configure --prefix=/usr/local/freetype

make && make install 

cd ../

安装PHP

下载并解压

wget http://cn.php.net/distributions/php-7.2.4.tar.gz

tar -zxvf php-7.2.4.tar.gz 

cd php-7.2.4

./configure是源代码安装的第一步,主要的作用是对即将安装的软件进行配置,检查当前的环境是否满足要安装软件的依赖关系,

 ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-config-file-scan-dir=/usr/local/php/conf.d --enable-fpm --with-fpm-user=www --with-fpm-group=www --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir=/usr/local/freetype --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-intl --enable-pcntl --enable-ftp --with-gd --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --with-libzip --enable-soap --with-gettext --enable-opcache --with-xsl

make开始编译,make install生成相应的可执行文件

make
make install
关于php的运行模式

php的运行模式有很多,想了解可参考:https://blog.csdn.net/xujingzhong0077/article/details/53316767

接下来主要介绍两种:FastCGI模式 模块模式

FastCGI(PHP-FPM)模式

--enable-fpm

--with-fpm-user=www

--with-fpm-group=www

这三个参数,是开启PHP-FPM,如果不需要php以FastCGI模式运行,则可去除。

模块模式

--with-apxs2=/usr/local/apache/bin/apxs

这个参数是指定Apache的位置,会自动生成modules/libphp7.so文件,并在Apache的配置文件中自动加入:

LoadModule php7_module        modules/libphp7.so

如果需要php以Apache模块模式运行,则在./configure的时候加上该参数。

简单配置

链接到环境变量下,可以直接使用php命令

ln -sf /usr/local/php/bin/php /usr/bin/php

生成配置目录etc和conf.d

mkdir -p /usr/local/php/{etc,conf.d}

拷贝配置文件

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

如果编译了php-fpm,则进行相关配置

cp ./sapi/fpm/php-fpm.conf /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default  /usr/local/php/etc/php-fpm.d/www.conf
cp ./sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm

验证

至此,php已经安装完成,可通过php命令查看:

查看版本

php -v

查看已经安装的扩展

php -m

如果安装了php-fpm,则加进服务列表,并开启开机启动:

chkconfig --add nginx 
chkconfig nginx on
service php-fpm start

查看是否监听了9000端口:

netstat -naplt | grep "9000"

后面的文章会陆续介绍apache和php以模块模式、FastCGI等模式搭配组合