PHP7 编译安装

时间:2022-04-29
本文章向大家介绍PHP7 编译安装,主要内容包括修订记录、macOS、安装依赖包、Debian、编译、错误排查、安装、升级 pecl、用户组、复制配置文件、systemd、启动、More Information、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

本文介绍了手动编译安装 PHP 的具体步骤。

修订记录

  • Debian9 Ubuntu17.04 出现错误请详细查看后边说明(PHP 7.1.9 不会报错了)。出现错误强烈建议使用 https://stackoverflow.com 进行搜索!

macOS

$ brew tap homebrew/homebrew-php

$ brew install php71

安装依赖包

RedHat

$ yum install gcc 
              gcc-c++ 
              libxml2 
              libxml2-devel 
              openssl 
              openssl-devel 
              libcurl 
              libcurl-devel 
              freetype 
              freetype-devel 
              libjpeg 
              libjpeg-devel 
              libpng 
              libpng-devel 
              libxslt 
              libxslt-devel 
              systemd-devel 
              libicu-devel 
              libedit-devel

Debian

$ sudo apt install autoconf 
                   dpkg-dev 
                   file 
                   libc6-dev 
                   make 
                   pkg-config 
                   re2c 
                   gcc g++ 
                   libedit-dev 
                   zlib1g-dev 
                   libxml2-dev 
                   libssl-dev 
                   libsqlite3-dev 
                   libfreetype6-dev 
                   libxslt1-dev 
                   libcurl4-gnutls-dev 
                   libpq-dev 
                   libmemcached-dev 
                   libzip-dev 
                   libpng-dev 
                   libjpeg-dev

                   # ubuntu 16.04 没有 libargon2-0 ,17.04 + 才有,php 7.2.0 新特性
                   # libargon2-0 

编译

$ ./configure --prefix=/usr/local/php 
    --with-config-file-path=/usr/local/php/etc 
    --with-config-file-scan-dir=/usr/local/php/etc/conf.d 
    --disable-cgi 
    --enable-fpm 
    --with-fpm-user=www-data 
    --with-fpm-group=www-data 
    --enable-ftp 
    --with-curl 
    --with-gd 
    --with-gettext 
    --with-iconv-dir 
    --with-kerberos 
    --with-libedit 
    --with-openssl 
    --with-pcre-regex 
    --with-pdo-mysql 
    --with-pdo-pgsql 
    --with-xsl 
    --with-zlib 
    --with-mhash 
    --with-png-dir=/usr/lib 
    --with-jpeg-dir=/usr/lib
    --with-freetype-dir=/usr/lib 
    --enable-mysqlnd 
    --enable-bcmath 
    --enable-libxml 
    --enable-inline-optimization 
    --enable-gd-native-ttf 
    --enable-gd-jis-conv 
    --enable-mbregex 
    --enable-mbstring 
    --enable-opcache 
    --enable-pcntl 
    --enable-shmop 
    --enable-soap 
    --enable-sockets 
    --enable-sysvsem 
    --enable-xml 
    --enable-zip 
    --enable-calendar 
    --enable-intl 
    --enable-exif

    # ubuntu 16.04 没有 libargon2-0 ,17.04 + 才有,php 7.2.0 新特性
    # --with-password-argon2 

若编译 Apache 模块请添加 --with-apxs2=/usr/local/apache2/bin/apxs 参数。

注意:Apache 模块方式运行 PHP 太古老,请采用新的 PHP-FPM 模式!https://www.khs1994.com/php/development/apache-php.html

错误排查

configure: error: no acceptable C compiler found

$ yum install -y gcc gcc-c++
$ sudo apt install gcc g++

configure: error: xml2-config not found. Please check your libxml2 installation.

$ yum install libxml2 libxml2-devel
$ sudo apt install libxml2-dev

configure: error: Cannot find OpenSSL…

$ yum install openssl openssl-devel
$ sudo apt install libssl-dev

configure: error: png.h not found.

$ yum install libcurl libcurl-devel
$ sudo apt install libcurl4-openssl-dev

configure: error: freetype-config not found.

$ yum install freetype freetype-devel libjpeg libjpeg-devel libpng libpng-devel
$ sudo apt install libfreetype6-dev

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

$ yum install libxslt libxslt-devel
$ sudo apt install libxslt1-dev

Debian9

具体查看参考链接2

checking for cURL in default path not found
configure: error: Please reinstall the libcurl distribution easy.h should be in /include/curl/
$ sudo apt install libcurl4-gnutls-dev

注意,目前安装该包不能解决问题!需要建立软链接!!

$ sudo ln -s /usr/include/x86_64-linux-gnu/curl /usr/local/include/

编译选项改为 --with-curl=/usr/local

安装

$ make -j "$(nproc)"

$ sudo make install

加入环境变量

编辑 ~/.bash_profile

export PATH=/usr/local/php/sbin:/usr/local/php/bin:$PATH

编辑 /etc/sudoers

注意:必须编辑此文件,否则 sudo 会找不到命令。

Defaults	secure_path="/usr/local/php/bin:/usr/local/php/sbin:..."

升级 pecl

$ sudo pecl update-channels

用户组

PHP-FPM 系统默认用户与用户组

Debian:www-data

RedHat: apache

复制配置文件

将源文件中的 php.ini-development 复制到安装目录中的 etc 子目录 ,并改名为 php.ini

在源文件夹中执行以下命令,复制 systemd 文件。

$ cp sapi/fpm/php-fpm.serviceq /etc/systemd/system/
$ systemctl daemon-reload

将安装目录中的 etc 子目录中的 php-fpm.conf.default 复制为 php-fpm.conf

将安装目录中的 etc/php-fpm.d 子目录中的 www.conf.default 复制为 www.conf

systemd

已经在上一步中配置,这里贴出文件内容,便于学习。

# It's not recommended to modify this file in-place, because it
# will be overwritten during upgrades.  If you want to customize,
# the best way is to use the "systemctl edit" command.

[Unit]
Description=The PHP FastCGI Process Manager
After=network.target

[Service]
Type=notify
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

启动

$ sudo php-fpm

# 或者使用 system

$ sudo systemctl daemon-reload

$ sudo systemctl start php-fpm

# 也可以设置开机自启动

$ sudo systemctl enable php-fpm

More Information