centos7 源码编译安装php7.4

很多时候可能会遇到需要手动源码安装软件的时候,所以自己实践了一把,并且把安装过程中遇到的问题,以及在网上找到的解决办法(实测有效)都记录下来,方便日后学习实践。

1. 系统环境

# cat /etc/redhat-release

CentOS Linux release 7.6.1810 (Core)

// 编译php之前需要有gcc和autoconfig环境

# yum install -y gcc-c++ gcc

# yum install autoconf

2. 新增用户和用户组

# groupadd www
# useradd -g www www

3. 下载php

# wget https://www.php.net/distributions/php-7.3.25.tar.gz
# tar -zxvf php-7.3.25.tar.gz
# cd php-7.3.25

4. 编译

--with-fpm-user=www --with-fpm-group=www
这里使用www www 用户、用户组,编译出来的程序启动,就是归属这个用户、用户组

./configure --prefix=/usr/local/php 
--with-fpm-user=www 
--with-fpm-group=www 
--with-curl 
--with-freetype-dir 
--enable-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-jpeg 
--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 
--enable-fpm

 这里需要注意的是在php7.4 编译参数 --with-gd  要改成了 --enable-gd

很多编译参数在7.4的时候都有变化,参考如下链接。

https://www.php.net/manual/zh/migration74.other-changes.php#migration74.other-changes.pkg-config

5. 编译错误时,解决依赖

configure: error: libxml2 not found. Please check your libxml2 installation.

yum install -y  libxml2-devel

 configure: error: Please reinstall the BZip2 distribution

yum install -y  bzip2-devel

configure: error: cURL version 7.15.5 or later is required to compile php with cURL support

yum install -y curl-devel

configure: error: jpeglib.h not found.

yum install -y libjpeg-devel

configure: error: png.h not found.

yum install -y libpng-devel

configure: error: freetype-config not found.

yum install -y freetype-devel

configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution

yum install -y libxslt-devel

configure: error: Please reinstall the libzip distribution

yum install -y libzip-devel

checking for libzip... configure: error: system libzip must be upgraded to version >= 0.11

#先删除旧版本
# yum remove -y libzip
 
#下载编译安装
# wget https://nih.at/libzip/libzip-1.2.0.tar.gz
# tar -zxvf libzip-1.2.0.tar.gz
# cd libzip-1.2.0
# ./configure
# make && make install

装完了之后找一下/usr/local/lib下有没有pkgconfig目录,有的话执行命令export PKG_CONFIG_PATH="/usr/local/lib/pkgconfig/"指定PKG_CONFIG_PATH

off_t undefined 报错

checking for zip_file_set_encryption in -lzip... yes
checking for zip_libzip_version in -lzip... no
checking stdbool.h usability... yes
checking stdbool.h presence... yes
checking for stdbool.h... yes
checking fts.h usability... yes
checking fts.h presence... yes
checking for fts.h... yes
checking for int8_t... (cached) yes
checking for int16_t... (cached) yes
checking for int32_t... (cached) yes
checking for int64_t... (cached) yes
checking for uint8_t... (cached) yes
checking for uint16_t... (cached) yes
checking for uint32_t... (cached) yes
checking for uint64_t... (cached) yes
checking for ssize_t... yes
checking size of short... (cached) 2
checking size of int... (cached) 4
checking size of long... (cached) 8
checking size of long long... (cached) 8
checking size of off_t... 0
configure: error: off_t undefined; check your library configuration

off_t 类型是在 头文件 unistd.h中定义的,
在32位系统 编程成 long int ,64位系统则编译成 long long int ,
在进行编译的时候 是默认查找64位的动态链接库,
但是默认情况下 centos 的动态链接库配置文件/etc/ld.so.conf里并没有加入搜索路径,
这个时候需要将 /usr/local/lib64 /usr/lib64 这些针对64位的库文件路径加进去。#添加搜索路径到配置文件

echo '/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64'>>/etc/ld.so.conf#如下操作与上面等效vim /etc/ld.so.conf #添加如下几行 /usr/local/lib64 /usr/local/lib /usr/lib /usr/lib64 #保存退出 :wq ldconfig -v # 使之生效
#然后 更新配置
ldconfig -v
# 安装
make && make install

 报错  usr/local/include/zip.h:59:21: fatal error: zipconf.h: No such file or directory

cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h

 configure过后,make报错

/usr/local/include/zip.h:59:21: fatal error: zipconf.h: No such file or directory
#解决方法:手动复制过去
cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h

configure: error: Package requirements (libpcre2-8 >= 10.30) were not met:

No package 'libpcre2-8' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

wget https://ftp.pcre.org/pub/pcre/pcre2-10.34.tar.bz2
tar xjvf pcre2-10.34.tar.bz2
./configure --prefix=/usr/local/pcre2 \
--enable-pcre2-16 \
--enable-pcre2-32 \
--enable-jit \
--enable-jit-sealloc

make && make install

export PKG_CONFIG_PATH=/usr/local/pcre2/lib/pkgconfig/

6.重新编译,make && make install 成功输出

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20180731/
Installing PHP CLI binary:        /usr/local/php/bin/
Installing PHP CLI man page:      /usr/local/php/php/man/man1/
Installing PHP FPM binary:        /usr/local/php/sbin/
Installing PHP FPM defconfig:     /usr/local/php/etc/
Installing PHP FPM man page:      /usr/local/php/php/man/man8/
Installing PHP FPM status page:   /usr/local/php/php/php/fpm/
Installing phpdbg binary:         /usr/local/php/bin/
Installing phpdbg man page:       /usr/local/php/php/man/man1/
Installing PHP CGI binary:        /usr/local/php/bin/
Installing PHP CGI man page:      /usr/local/php/php/man/man1/
Installing build environment:     /usr/local/php/lib/php/build/
Installing header files:          /usr/local/php/include/php/
Installing helper programs:       /usr/local/php/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php/php/man/man1/
  page: phpize.1
  page: php-config.1
Installing PEAR environment:      /usr/local/php/lib/php/
Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in phar:///home/flame/software/php-7.3.0/pear/install-pear-nozlib.phar/PEAR/PackageFile/v2/Validator.php on line 1933
[PEAR] Archive_Tar    - installed: 1.4.3
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util       - installed: 1.4.2
[PEAR] PEAR           - installed: 1.10.5
Wrote PEAR system config file at: /usr/local/php/etc/pear.conf
You may want to add: /usr/local/php/lib/php to your php.ini include_path
/home/flame/software/php-7.3.0/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin
ln -s -f phar.phar /usr/local/php/bin/phar
Installing PDO headers:           /usr/local/php/include/php/ext/pdo/
[root@localhost php-7.4.25]# /usr/local/php/bin/php -v
PHP 7.4.25 (cli) (built: Nov  4 2021 13:51:39) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

7. 添加环境变量

  一次性的设置,只对当前会话有效,当注销时,刚刚设置的PATH就会失效

[root@localhost php-7.4.25]# vim /etc/profile 
#添加如下内容
# export PATH=$PATH:/usr/local/php/bin
[root@localhost php-7.4.25]# source /etc/profile

   永久性设置,对所有用户有效,需要重启生效或使用source命令,将上一种方式的导出操作添加到文件/etc/profile的末尾。

[root@cloudhost php-7.4.25]# cp php.ini-development /usr/local/php/etc/php.ini

[root@cloudhost php-7.4.25]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

[root@cloudhost php-7.4.25]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

  永久性设置,只针对一个用户,需要重启生效或使用source命令,优先级高于2,将方式1的导出操作添加到文件 ~/.bashrc的末尾。

8. 配置文件

  我们将配置文件设置在了/usr/local/php/etc目录下,需要将配置文件拷贝到该目录。

[root@cloudhost php-7.4.25]# cp php.ini-development /usr/local/php/etc/php.ini

[root@cloudhost php-7.4.25]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

[root@cloudhost php-7.4.25]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

9. 注册系统服务

  当php编译安装完成后,php-fpm还不是系统服务。为了方便使用,将其注册为系统服务。

  找到 init.d.php-fpm

[root@localhost php-7.4.25]# find / -name init.d.php-fpm
/tmp/downloads/php-7.4.25/sapi/fpm/init.d.php-fpm

  将它拷贝到/etc/init.d目录下

[root@localhost php-7.4.25]# find / -name init.d.php-fpm
/tmp/downloads/php-7.4.25/sapi/fpm/init.d.php-fpm

[root@localhost php-7.4.25]# cp

/tmp/downloads/php-7.4.25/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

  修改权限

[root@localhost ~]# chmod 755 /etc/init.d/php-fpm
或者
[root@localhost ~]# chmod +x /etc/init.d/php-fpm

 php-fpm基本操作

/etc/init.d/php-fpm start
或者
service php-fpm start

service php-fpm status
service php-fpm stop

Usage: /etc/init.d/php-fpm {start|stop|force-quit|restart|reload|status|configtest}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值