Ubuntu编译安装最新版PHP7.4

PHP7.4 正式版已于11月28日发布,先来体验一下~

了解PHP7.4的新特性请点击:php7.4新特性

安装环境

操作系统:Ubuntu 18.04
PHP版本: PHP 7.4
Web服务器:Nginx 1.17.4
数据库:Mariadb 10.0.38(MySQL的一个分支,与MySQL兼容)

安装之前

按照惯例更新软件列表:

1
sudo apt-get update

安装PHP7.4

  1. 下载PHP 7.4的源代码到Ubuntu上:

    1
    `wget https://www.php.net/distributions/php-7.4.0.tar.gz`

    如果下载速度太慢,可以使用PHP 7.4国内高速下载地址:

    1
    wget https://gz-1257226027.cos.ap-guangzhou.myqcloud.com/php-7.4.0.tar.gz
  2. 解压,进入php7.4源代码目录:

    1
    2
    tar zxvf php-7.4.0.tar.gz
    cd php-7.4.0/
  3. 先安装PHP需要的依赖

    1
    2
    3
    4
    sudo apt-get install libzip-dev bison autoconf build-essential pkg-config git-core\
    libltdl-dev libbz2-dev libxml2-dev libxslt1-dev libssl-dev libicu-dev libpspell-dev\
    libenchant-dev libmcrypt-dev libpng-dev libjpeg8-dev libfreetype6-dev libmysqlclient-dev\
    libreadline-dev libcurl4-openssl-dev librecode-dev libsqlite3-dev libonig-dev
  4. 配置PHP安装参数

    1
    ./configure --prefix=/usr/local/php7 --with-config-file-scan-dir=/usr/local/php7/etc/php.d --with-config-file-path=/usr/local/php7/etc --enable-mbstring --enable-zip --enable-bcmath --enable-pcntl --enable-ftp --enable-xml --enable-shmop --enable-soap --enable-intl --with-openssl --enable-exif --enable-calendar --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-opcache --enable-fpm --enable-session --enable-sockets --enable-mbregex --enable-wddx --with-curl --with-iconv --with-gd --with-jpeg-dir=/usr --with-png-dir=/usr --with-zlib-dir=/usr --with-freetype-dir=/usr --enable-gd-jis-conv --with-openssl --with-pdo-mysql=mysqlnd --with-gettext=/usr --with-zlib=/usr --with-bz2=/usr --with-recode=/usr --with-xmlrpc --with-mysqli=mysqlnd
  5. 执行编译安装

    1
    sudo make install

    如果编译过程中提示内存不足:

    virtual memory exhausted: Cannot allocate memory
    Makefile:921: recipe for target ‘ext/fileinfo/libmagic/apprentice.lo’ failed
    make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1

    请先给服务器添加虚拟内存,参考: 为Linux添加虚拟内存Swap

  6. 复制配置文件

    1
    2
    3
    sudo cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
    sudo cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
    sudo cp php.ini-production /usr/local/php7/etc/php.ini
  7. 添加环境变量,编辑 /etc/profile,文件末尾添加一行:

    1
    PATH=/usr/local/php7/bin:/usr/local/php7/sbin:$PATH
  8. 使环境变量生效:

    1
    source /etc/profile
  9. 将php添加到sudo环境变量,编辑 /etc/sudoers文件中的 Defaults secure_path,后面加上PHP路径:

    1
    /usr/local/php7/bin:/usr/local/php7/sbin:
  10. 启动php-fpm

    1
    sudo php-fpm

    如果提示找不到用户组nobody的gid:

    [15-Nov-2019 15:53:49] ERROR: [pool www] cannot get gid for group ‘nobody’
    [15-Nov-2019 15:53:49] ERROR: FPM initialization failed

    就要先创建nobody用户组:

    1
    sudo groupadd nobody

    再重新执行sudo php-fpm即可启动。

  11. 使用php -v命令查看php版本:

    1
    2
    3
    4
    # php -v
    PHP 7.4.0 (cli) (built: Nov 28 2019 13:38:00) ( NTS )
    Copyright (c) The PHP Group
    Zend Engine v3.4.0, Copyright (c) Zend Technologies