技术知识库

帮助中心 >  产品文档 >  云服务器 >  Ubuntu 16.04下编译安装php7.2

安装编译支持扩展

sudo apt-get install gcc autoconf curl libxml2 libxml2-dev libssl-dev bzip2 libbz2-dev libjpeg-dev  libpng12-dev libfreetype6-dev libgmp-dev libmcrypt-dev libreadline6-dev libsnmp-dev libxslt1-dev libcurl4-openssl-dev

这是必须的,因为编译前的配置需要它们的支持。


新建php用户组和php用户

sudo groupadd -r php

sudo useradd -r -g php -s /bin/false -d /usr/local/php7 -M php


下载并解压PHP源码

选择一个合适的节点源码文件下载,并进行解压操作


wget -O php-7.2.0.tar.gz https://www.landui.com/php/php-src/archive/php-7.2.0.tar.gz

tar -zxvf php-7.2.0.tar.gz

cd php-src-php-7.2.0

配置并编译&安装PHP

根据自己的实际需求进行配置


./configure \

--prefix=/usr/local/php7 \

--exec-prefix=/usr/local/php7 \

--bindir=/usr/local/php7/bin \

--sbindir=/usr/local/php7/sbin \

--includedir=/usr/local/php7/include \

--libdir=/usr/local/php7/lib/php \

--mandir=/usr/local/php7/php/man \

--with-config-file-path=/usr/local/php7/etc \

--with-mysql-sock=/var/run/mysql/mysql.sock \

--with-mhash \

--with-openssl \

--with-mysqli=shared,mysqlnd \

--with-pdo-mysql=shared,mysqlnd \

--with-pgsql=/usr/local/pgsql \

--with-pdo-pgsql=/usr/local/pgsql \

--with-gd \

--with-iconv \

--with-zlib \

--enable-zip \

--enable-inline-optimization \

--disable-debug \

--disable-rpath \

--enable-shared \

--enable-xml \

--enable-bcmath \

--enable-shmop \

--enable-sysvsem \

--enable-mbregex \

--enable-mbstring \

--enable-ftp \

--enable-pcntl \

--enable-sockets \

--with-xmlrpc \

--enable-soap \

--without-pear \

--with-gettext \

--enable-session \

--with-curl \

--with-jpeg-dir \

--with-freetype-dir \

--enable-opcache \

--enable-fpm \

--with-fpm-user=php \

--with-fpm-group=php \

--without-gdbm \

--disable-fileinfo

输出以下信息时就表示配置完成了!


Generating files

configure: creating ./config.status

creating main/internal_functions.c

creating main/internal_functions_cli.c

+--------------------------------------------------------------------+

| License:                                                           |

| This software is subject to the PHP License, available in this     |

| distribution in the file LICENSE.  By continuing this installation |

| process, you are bound by the terms of this license agreement.     |

| If you do not agree with the terms of this license, you must abort |

| the installation process at this point.                            |

+--------------------------------------------------------------------+


Thank you for using PHP.


config.status: creating php7.spec

config.status: creating main/build-defs.h

config.status: creating scripts/phpize

config.status: creating scripts/man1/phpize.1

config.status: creating scripts/php-config

config.status: creating scripts/man1/php-config.1

config.status: creating sapi/cli/php.1

config.status: creating sapi/fpm/php-fpm.conf

config.status: creating sapi/fpm/www.landui.com

config.status: creating sapi/fpm/init.d.php-fpm

config.status: creating sapi/fpm/php-fpm.service

config.status: creating sapi/fpm/php-fpm.8

config.status: creating sapi/fpm/status.html

config.status: creating sapi/cgi/php-cgi.1

config.status: creating ext/phar/phar.1

config.status: creating ext/phar/phar.phar.1

config.status: creating main/php_config.h

config.status: executing default commands

编译(结束后会提示运行测试)、测试(过程很漫长但是很有必要的)、安装


make

make test

make install

安装完成后应该是有类似以下的输出信息:


Installing shared extensions:     /usr/local/php7/lib/php/extensions/no-debug-non-zts-20170718/

Installing PHP CLI binary:        /usr/local/php7/bin/

Installing PHP CLI man page:      /usr/local/php7/php/man/man1/

Installing PHP FPM binary:        /usr/local/php7/sbin/

Installing PHP FPM defconfig:     /usr/local/php7/etc/

Installing PHP FPM man page:      /usr/local/php7/php/man/man8/

Installing PHP FPM status page:   /usr/local/php7/php/php/fpm/

Installing phpdbg binary:         /usr/local/php7/bin/

Installing phpdbg man page:       /usr/local/php7/php/man/man1/

Installing PHP CGI binary:        /usr/local/php7/bin/

Installing PHP CGI man page:      /usr/local/php7/php/man/man1/

Installing build environment:     /usr/local/php7/lib/php/build/

Installing header files:          /usr/local/php7/include/php/

Installing helper programs:       /usr/local/php7/bin/

  program: phpize

  program: php-config

Installing man pages:             /usr/local/php7/php/man/man1/

  page: phpize.1

  page: php-config.1

/home/ubuntu/php-7.2.0/build/shtool install -c ext/phar/phar.phar /usr/local/php7/bin

ln -s -f phar.phar /usr/local/php7/bin/phar

Installing PDO headers:           /usr/local/php7/include/php/ext/pdo/

排错

由于环境变量问题,配置过程会出现如下报错信息


configure: error: Cannot find OpenSSL's libraries

openssl version 可以看到 OpenSSL 1.0.2g 1 Mar 2016

解决方法,找到libssl.so软链接到/usr/lib下:


sudo find / -name libssl.so

sudo ln -s /usr/lib/x86_64-linux-gnu/libssl.so /usr/lib

当然我的排错过程远不止这么简单,如果你按照我的第一个步骤做了,那么仅仅只要做这个操作。


配置PHP

配置环境变量

配置PHP_HOME


vim /etc/profile

在最底下一行的上面添加如下内容:


#set for PHP

export PHP_HOME=/usr/local/php7

export PATH=$PHP_HOME/bin:$PATH

保存并退出,编译/etc/profile 使配置生效


source /etc/profile

配置 PHP-FPM

增加 php-fpm 命令:


cp sapi/fpm/init.d.php-fpm /usr/local/bin/php-fpm

chmod +x /usr/local/bin/php-fpm

初始化 PHP 和 PHP-FPM 的配置

cp php.ini-production /usr/local/php7/etc/php.ini

cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf

当然了,为了更精简以及后续的修改方便php-fpm.conf也可以直接使用下面的方式配置


cat <<EOF >/usr/local/php7/etc/php-fpm.conf

;;;;;;;;;;;;;;;;;;

; Global Options ;

;;;;;;;;;;;;;;;;;;

[global]

; Pid file

; Note: the default prefix is /usr/local/php7/var

pid = run/php-fpm.pid

; Error log file

; Note: the default prefix is /usr/local/php7/var

error_log = log/php-fpm.log


;;;;;;;;;;;;;;;;;;;;

; Pool Definitions ;

;;;;;;;;;;;;;;;;;;;;

include=/usr/local/php7/etc/php-fpm.d/*.conf


EOF

新建 php-fpm 的 pool 进程池配置

cat <<EOF >/usr/local/php7/etc/php-fpm.d/www.landui.com

; Start a new pool named 'www'

[www]

listen = /usr/local/php7/var/run/$pool-php-fpm.sock

listen.backlog = -1

listen.allowed_clients = 127.0.0.1

listen.owner = www-data

listen.group = www-data

listen.mode = 0660

user = www-data

group = www-data

pm = dynamic

pm.max_children = 10

pm.start_servers = 2

pm.min_spare_servers = 1

pm.max_spare_servers = 6

pm.status_path = /status

ping.path = /ping

ping.response = pong

request_terminate_timeout = 100

request_slowlog_timeout = 10s

slowlog = /usr/local/php7/var/log/$pool.log.slow

EOF

让NGINX用PHP7.2.0来解析PHP

找到Nginx配置文件的 fastcgi_pass 

将 unix:/run/php/php7.0-fpm.sock; 改为 php-fpm进程池的监听地址unix:/usr/local/php7/var/run/www-php-fpm.sock;


以下是一个例子:


站点配置基本例子


server {

    listen 80 default_server;

    listen [::]:80 default_server;



    root /srv/www/html;


    # Add index.php to the list if you are using PHP

    index index.php index.html index.htm index.nginx-debian.html;


    server_name _;


    location / {

        # First attempt to serve request as file, then

        # as directory, then fall back to displaying a 404.

        try_files $uri $uri/ =404;

    }


    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

    #

    location ~ \.php$ {

        include /usr/local/nginx/conf/fastcgi_params;

    #

    #    # With php7.0-cgi alone:

    #    fastcgi_pass 127.0.0.1:9000;

    #    # With php7.0-fpm:

        fastcgi_pass unix:/usr/local/php7/var/run/wwww-php-fpm.sock;

        fastcgi_param  SCRIPT_FILENAME  /srv/www/html$fastcgi_script_name;


    }


    # deny access to .htaccess files, if Apache's document root

    # concurs with nginx's one

    #

    #location ~ /\.ht {

    #    deny all;

    #}

}

设置开机自动启动 PHP-FPM

cp sapi/fpm/php-fpm.service   /etc/systemd/system/

chmod +x /etc/systemd/system/php-fpm.service


保存以后,设置开机自启以及开启服务:

systemctl enable php-fpm.service

systemctl start php-fpm.service


提交成功!非常感谢您的反馈,我们会继续努力做到更好!

这条文档是否有帮助解决问题?

非常抱歉未能帮助到您。为了给您提供更好的服务,我们很需要您进一步的反馈信息:

在文档使用中是否遇到以下问题: