1. 程式人生 > 其它 >PHP7 編譯安裝

PHP7 編譯安裝

本文介紹了手動編譯安裝 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