PHP7 编译安装

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

GitHub:https://github.com/khs1994-php/builder

修订记录

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

  • 有些编译报错,你确实是正确的安装了依赖包,可以通过建立软链接解决

  • 本文随着 PHP 版本升级而更新,一些增加、修改已经在注释中列出,注意查看。

用户与用户组

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

  • Debian:www-data

  • RedHat: apache

安装依赖包

RedHat

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$ 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
$ sudo apt install autoconf \
dpkg-dev \
file \
ca-certificates \
curl \
xz-utils \
libc6-dev \
make \
pkg-config \
re2c \
gcc g++ \
libedit-dev \
libsodium-dev \
zlib1g-dev \
libxml2-dev \
libssl-dev \
libsqlite3-dev \
libfreetype6-dev \
libxslt1-dev \
libcurl4-gnutls-dev \
libpq-dev \
libmemcached-dev \
libpng-dev \
libjpeg-dev \
libzip-dev

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

编译

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
$ ./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-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 \
--with-sodium \
--with-libzip

#
# --enable-gd-native-ttf 7.2.0 remove
#
# --with-libzip 7.2.0 add
# 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

1
2
3
$ yum install -y gcc gcc-c++

$ sudo apt install gcc g++

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

1
2
3
$ yum install libxml2 libxml2-devel

$ sudo apt install libxml2-dev

configure: error: Cannot find OpenSSL…

1
2
3
$ yum install openssl openssl-devel

$ sudo apt install libssl-dev

configure: error: png.h not found.

1
2
3
$ yum install libcurl libcurl-devel

$ sudo apt install libcurl4-openssl-dev

configure: error: freetype-config not found.

1
2
3
$ 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

1
2
3
$ yum install libxslt libxslt-devel

$ sudo apt install libxslt1-dev

Debian 9

具体查看参考链接2

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

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

安装

1
2
3
$ make -j "$(nproc)"

$ sudo make install

加入环境变量

编辑 ~/.bash_profile

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

编辑 /etc/sudoers

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

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

升级 pecl

1
$ sudo pecl update-channels

复制配置文件

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

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

1
2
3
$ 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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 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

启动

1
2
3
4
5
6
7
8
9
10
11
$ sudo php-fpm

# 或者使用 system

$ sudo systemctl daemon-reload

$ sudo systemctl start php-fpm

# 也可以设置开机自启动

$ sudo systemctl enable php-fpm

More Information

0%