Linux下安装PHP7及配置phpfpm开机启动

Updated on with 2,570 views

下载安装包

以下以CentOS 7为例,安装PHP的运行环境
首先打开PHP官网下载最新版
这里下载最新版7.3.7 的源码包

phpdownload

解压安装包

tar -zxvf php-7.3.7.tar.gz
cd php-7.3.7

编译安装

配置前如果没有libxml2和libxml2-devel会报错,所以应该更新libxml2并安装libxml2-devel

yum -y install libxml2 yum -y install libxml2-devel

因为不同环境,系统安装完整程度也不尽相同,所以建议安装操作系统的时候做必要选择,也可以统一执行一遍所有的命令,汇总如下:

yum -y install openssl
yum -y install openssl-devel
yum -y install curl
yum -y install curl-devel
yum -y install libjpeg
yum -y install libjpeg-devel
yum -y install libpng
yum -y install libpng-devel
yum -y install freetype
yum -y install freetype-devel
yum -y install pcre
yum -y install pcre-devel
yum -y install libxslt
yum -y install libxslt-devel
yum -y install bzip2
yum -y install bzip2-devel

以上这些包基本上够用了,如果发现问题再补充

最新的PHP版本,需要使用参数--with-libzip系统库,并且版本需要>=1.11,而centos上最新的yum源版本为1.10版本太低编译时会报错,需要手动编译源码包解决,libzip官网
注意libzip编译需要cmake如果没有要先安装cmake
libzip安装如下:

tar -xvzf libzip-1.5.2.tar.gz
cd libzip-1.5.2
mkdir build && cd build/
cmake ..
make && make install

安装完成之后,执行配置:
注意--prefix参数为安装位置,这里为/usr/php

./configure --prefix=/usr/php --with-curl --with-freetype-dir --with-gd --with-gettext --with-iconv-dir --with-kerberos --with-libdir=lib64 --with-libxml-dir --with-mysqli --with-openssl --with-pcre-regex --with-pdo-mysql --with-pdo-sqlite --with-pear --with-png-dir --with-jpeg-dir --with-xmlrpc --with-xsl --with-zlib --with-bz2 --with-mhash --enable-fpm --enable-bcmath --enable-libxml --enable-inline-optimization --enable-mbregex --enable-mbstring --enable-opcache --enable-pcntl --enable-shmop --enable-soap --enable-sockets --enable-sysvsem --enable-sysvshm --enable-xml --enable-zip --with-libzip

新版

'./configure' '--prefix=/usr/php' '--with-curl' '--with-freetype' '--with-gettext' '--with-iconv-dir' '--with-kerberos' '--with-libdir=lib64' '--with-mysqli' '--with-openssl' '--with-pdo-mysql' '--with-pdo-sqlite' '--with-pear' '--with-jpeg' '--with-xmlrpc' '--with-xsl' '--with-zlib' '--with-bz2' '--with-mhash' '--enable-fpm' '--enable-bcmath' '--enable-inline-optimization' '--enable-mbregex' '--enable-mbstring' '--enable-opcache' '--enable-pcntl' '--enable-shmop' '--enable-soap' '--enable-sockets' '--enable-sysvsem' '--enable-sysvshm' '--enable-xml' '--enable-gd'

然后执行编译安装:

make && make install

php的默认安装位置上面已经指定为/usr/php,接下来配置相应的文件:

cp php.ini-development /usr/php/lib/php.ini 
cp /usr/php/etc/php-fpm.conf.default /usr/php/etc/php-fpm.conf 
cp sapi/fpm/php-fpm /usr/local/bin
cp /usr/php/etc/php-fpm.d/www.conf.default /usr/php/etc/php-fpm.d/www.conf

配置成自启服务

复制文件
cp 源码路径/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
添加权限
chmod 755 /etc/init.d/php-fpm
添加启动项
chkconfig –add php-fpm
启动服务
service php-fpm start

启动完毕之后,php-fpm服务默认使用9000端口,使用 netstat -tln | grep 9000 可以查看端口使用情况:
9000端口正常使用,说明php-fpm服务启动成功
php-fpm

问题

最新版7.4:

  1. No package 'sqlite3' found
    yum install sqlite-devel 再次运行上方的 configure命令
  2. No package 'oniguruma' found
    安装 oniguruma,可以参考https://github.com/kkos/oniguruma#install
    yum install https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/o/oniguruma-5.9.5-3.el7.x86_64.rpm
    yum install https://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/o/oniguruma-devel-5.9.5-3.el7.x86_64.rpm
Responses
取消