[toc]
php源码安装
1.下载源码包
php国内下载地址
export PHP_VERSION=7.3.19
wget https://mirrors.sohu.com/php/php-${PHP_VERSION}.tar.xz
php官网下载地址
wget https://www.php.net/distributions/php-${PHP_VERSION}.tar.xz
2.安装依赖包
# 安装开发者工具包
yum -y group install "Development Tools"
# 安装依赖包
yum -y install systemd-devel libacl libacl-devel libxml2 libxml2-devel curl curl-devel libjpeg libjpeg-devel libpng libpng-devel gmp-devel libxslt libxslt-devel openssl openssl-devel zlib zlib-devel pcre pcre-devel glib2 glib2-devel bzip2 bzip2-devel glibc glibc-devel liblzf liblzf-devel libzstd libzstd-devel freetype-devel readline-devel
3.解压缩包并编译安装
3.1 编译安装libzip
解决报错 configure: error: Please reinstall the libzip distribution
3.1.1 编译安装libzip需要安装高版本的cmake
# 下载源码包
wget https://github.com/Kitware/CMake/releases/download/v3.16.8/cmake-3.16.8.tar.gz
# 解压缩源码包
tar xf cmake-3.16.8.tar.gz
# 进入解压缩目录并编译安装
cd cmake-3.16.8/
./configure --prefix=/usr/local/cmake
gmake -j`nproc` && make install
# 导出PATH环境变量并使配置生效
echo 'export PATH=/usr/local/cmake/bin:$PATH' >/etc/profile.d/cmake.sh
source /etc/profile
# 验证
$ cmake --version
cmake version 3.16.8
3.1.2 编译安装libzip
# 下载源码包
wget https://libzip.org/download/libzip-1.7.1.tar.xz
# 解压缩源码包
tar xf libzip-1.7.1.tar.xz
# 进入解压缩目录并编译安装
cd libzip-1.7.1
mkdir build && cd build && cmake .. && make -j`nproc` && make install
3.2 编辑配置文件 /etc/ld.so.conf
内容
解决报错configure: error: off_t undefined; check your library configuration
# 备份配置文件
cp /etc/ld.so.conf{,.bak}
# 向配置文件写入以下内容
cat >/etc/ld.so.conf <<EOF
/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64
EOF
# 使配置生效
ldconfig -v
3.3 解压缩包编译安装
可以使用
php -i | grep config
查看php编译参数
# 创建www用户
useradd -M www -s /sbin/nologin
# 解压缩包
tar xf php-${PHP_VERSION}.tar.xz
# 进入解压后的目录
cd php-${PHP_VERSION}
# 编译安装
./configure --prefix=/usr/local/php${PHP_VERSION} \
--with-config-file-path=/usr/local/php${PHP_VERSION}/etc \
--with-fpm-user=www \
--with-fpm-group=www \
--with-fpm-systemd \
--with-fpm-acl \
--with-mysql-sock \
--with-mysqli \
--with-libxml-dir \
--with-openssl \
--with-mhash \
--with-pcre-regex \
--with-zlib \
--with-iconv \
--with-bz2 \
--with-curl \
--with-cdb \
--with-pcre-dir \
--with-gd \
--with-openssl-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-freetype-dir \
--with-gettext \
--with-gmp \
--with-mhash \
--with-onig \
--with-pdo-mysql \
--with-zlib-dir \
--with-readline \
--with-libxml-dir \
--with-xsl \
--with-pear \
--enable-fpm \
--enable-soap \
--enable-bcmath \
--enable-calendar \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--enable-ftp \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-mbregex-backtrack \
--enable-pdo \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-zip \
--enable-mysqlnd-compression-support &&
make -j`nproc` && make install
ubuntu16 ./configure
可能遇到的报错 configure: error: Cannot find OpenSSL's libraries
,先使用命令 find / -name libssl.so
找到文件 libssl.so
,然后做一下软连接即可
ln -s /usr/lib/x86_64-linux-gnu/libssl.so /usr/lib
3.4 拷贝相关文件
3.4.1 创建php安装目录软连接
ln -s /usr/local/php${PHP_VERSION}/ /usr/local/php
3.4.2 导出PATH环境变量
echo 'export PATH=/usr/local/php/bin:$PATH' >/etc/profile.d/php.sh
source /etc/profile
3.4.3 拷贝php.ini文件
cp php-${PHP_VERSION}/php.ini-development /usr/local/php/etc/php.ini
3.4.4 配置php-fpm
方式一 拷贝php-fpm文件
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.conf
方式二 编辑php-fpm文件
这里贴一下生产中php的配置文件
php-fpm.conf
⚠️这里php启动是监听的sock文件,放在了
/tmp
下,如果指定其他目录,则这个目录权限需要设置为php运行用户所有
[global]
pid = /usr/local/php72/var/run/php-fpm.pid
error_log = /usr/local/php72/var/log/php-fpm.error.log
log_level = notice
[www]
listen = /tmp/php73-cgi.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 60
pm.start_servers = 30
pm.min_spare_servers = 30
pm.max_spare_servers = 60
pm.max_requests = 1024
pm.process_idle_timeout = 10s
request_terminate_timeout = 100
request_slowlog_timeout = 0
slowlog = var/log/slow.log
3.4.5 拷贝php-fpm服务文件
cp php-${PHP_VERSION}/sapi/fpm/php-fpm.service /usr/lib/systemd/system/php-fpm.service
3.4.6 启动php-fpm
方式一 使用systemd管理
systemctl daemon-reload && systemctl enable php-fpm && systemctl start php-fpm
方式二 指定配置文件直接启动
/usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini -y /usr/local/php/etc/php-fpm.conf -D
3.4.7 检查php-fpm启动
# php默认监听tcp/9000端口
$ netstat -ntpl|grep 9000
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 12392/php-fpm: mast