Ubuntu 18.04下编译安装php7.4
in 技巧 with 13 comments

Ubuntu 18.04下编译安装php7.4

in 技巧 with 13 comments

文章是记录自己安装的一个过程,其中的配置完全是个人的需求。

你可能对如下文章也感兴趣~
[Ubuntu 16.04下编译安装Nginx]
[Ubuntu 16.04下编译安装PostgreSQL 10.3]

php

前期准备

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 libonig-dev libzip-dev

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

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

下载并解压PHP源码

截至到目前,php的最新稳定版本为php-7.2.0,那么就用它了。

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


wget http://am1.php.net/distributions/php-7.2.0.tar.xz
xz -d php-7.2.0.tar.xz
tar -xvf php-7.2.0.tar

wget -O php-7.2.0.tar.gz https://github.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-iconv \
--with-zlib \
--with-zip \
--enable-gd \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-xml \
--enable-bcmath \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-soap \
--enable-exif \
--without-pear \
--with-gettext \
--enable-session \
--with-curl \
--with-jpeg \
--with-freetype \
--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.conf
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 命令:

cp sapi/fpm/init.d.php-fpm /usr/local/bin/php-fpm
chmod +x /usr/local/bin/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
cat <<EOF >/usr/local/php7/etc/php-fpm.d/www.conf
; 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配置文件的 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

好了!终于填坑完成了!

--EOF--

Responses
  1. kk

    好的 谢谢您

    Reply
  2. kk

    请问,php.ini不需要配置吗??我这几天连续出错,不知道为何,
    1》php7怎么配置连接mysql或mariadb.
    2》要么就出现打开网站首页 ,就把首页下载下来了。
    3》要么安装了typecho,除了首页 其他全是404.
    快崩溃了 ,也没个完整的教程!!

    Reply
    1. Kay
      @kk

      你说的三个问题跟php.ini文件都没有关系

      Reply
      1. kk
        @Kay

        博主这个用的什么博客程序啊

        Reply
        1. Kay
          @kk

          用了typecho

          Reply
          1. kk
            @Kay

            是不是我用了mariadb10.4的原因哦 nginx版本1.16.1 都是自己编译的 问题一大堆 现在安装了ubuntu官方源里面的版本 typeccho除了首页其他全是404

            Reply
            1. Kay
              @kk

              跟数据库关系不大,检查nginx配置

              Reply
          2. kk
            @Kay

            现在又把所有的步骤从头到尾按您的重新做了一次 ,打开首页还是下载 无语了

            Reply
            1. Kay
              @kk

              那跟这篇文章应该关系也不大吧,建议检查nginx的配置 index 以及对php支持要配置的 本站还有一些关于nginx配置的文 你可以看看

              Reply
  3. 大佬大佬,我都用宝塔的

    Reply
    1. Kay
      @小霖

      我之前也是偷懒 apt-get install φ( ̄∇ ̄o)

      Reply
  4. 我用5.6,安心。
    好多程序还没跟上速度,不支持7.1

    Reply
    1. Kay
      @Mr.He

      又不是生产环境,个人博客基本上没有啥不支持的扩展哈

      Reply