第八章 编译安装 PHP

该文章根据 CC-BY-4.0 协议发表,转载请遵循该协议。
本文地址:https://fenying.net/book/minicentos-handbook/08.compile-php/

本章编译安装 PHP 7.1.0 。

0. 准备安装包和依赖包

0.0. 依赖列表

名称 版本 官网
libiconv 1.1.4 http://www.gnu.org/software/libiconv/
libmcrypt 2.5.7 http://mcrypt.hellug.gr/lib/
mcrypt 2.6.8 http://mcrypt.sourceforge.net/
mhash 0.9.9.9 http://mhash.sourceforge.net/

0.1. 下载 PHP 等源码

1wget http://hk1.php.net/get/php-7.1.0.tar.gz/from/this/mirror -O php-7.1.0.tar.gz

0.2. 编译安装 libiconv

在编译安装 Git 时已经安装,

0.3. 编译安装 libmcrypt

 1tar -xzvf libmcrypt-2.5.7.tar.gz
 2cd libmcrypt-2.5.7
 3./configure
 4make -j 4 && make install
 5/sbin/ldconfig
 6cd libltdl/
 7./configure --enable-ltdl-install
 8make -j 4 && make install
 9cd ../..
10rm -Rf libmcrypt-2.5.7
11ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
12ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
13ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
14ln -s /usr/local/lib/libmcrypt.so.4.4.7 /usr/lib/libmcrypt.so.4.4.7
15ln -s /usr/local/bin/libmcrypt_config /usr/bin/libmcrypt_config

0.4. 编译安装 mhash

 1tar -zxvf mhash-0.9.9.9.tar.gz
 2cd mhash-0.9.9.9
 3./configure
 4make -j 4 && make install
 5cd ..
 6rm -Rf mhash-0.9.9.9
 7ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
 8ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
 9ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
10ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
11/sbin/ldconfig

0.5. 编译安装 mcrypt

1tar -xzvf mcrypt-2.6.7.tar.gz
2cd mcrypt-2.6.7
3
4export LD_LIBRARY_PATH=/usr/local/lib:/usr/local/lib64:$LD_LIBRARY_PATH
5./configure
6make -j 4 && make install
7
8cd ..
9rm -Rf mcrypt-2.6.7

如果在 ./configure 这一步报错,则执行

1./configure --with-libmcrypt-prefix=/usr/local

0.6. 安装其他依赖

 1yum -y install \
 2  make \
 3  cmake \
 4  autoconf \
 5  automake \
 6  bison \
 7  flex \
 8  freetype \
 9  freetype-devel \
10  fontconfig-devel \
11  gettext-devel \
12  libjpeg \
13  libjpeg-devel \
14  libpng \
15  libpng-devel \
16  libxml2 \
17  libxml2-devel \
18  libtool \
19  libtool-ltdl \
20  libtool-ltdl-devel \
21  libtiff-devel \
22  libXpm-devel \
23  libicu \
24  libicu-devel \
25  libidn \
26  libidn-devel \
27  libxslt \
28  libxslt-devel \
29  bzip2 \
30  bzip2-devel \
31  ncurses \
32  ncurses-devel \
33  curl \
34  curl-devel \
35  e2fsprogs \
36  e2fsprogs-devel \
37  krb5 \
38  krb5-devel \
39  openldap \
40  openldap-devel \
41  nss_ldap \
42  openldap-clients \
43  openldap-servers \
44  pam-devel \
45  vim-enhanced \
46  net-snmp-devel \
47  libxml2 \
48  libxml2-devel \
49  libcurl-devel \
50  libjpeg \
51  libjpeg-devel \
52  libpng \
53  libpng-devel \
54  libwebp \
55  libwebp-devel \
56  freetype \
57  freetype-devel \
58  gd \
59  gd-devel
 1
 2  zlib \
 3  zlib-devel \
 4  glibc \
 5  glibc-devel \
 6  glib2 \
 7  glib2-devel \
 8  openssl \
 9  openssl-devel \
10  pcre \
11  pcre-devel \

保存,执行

1ldconfig

1. 编译 PHP

 1
 2tar -zxvf php-7.1.0.tar.gz
 3cd php-7.1.0
 4./configure \
 5--prefix=$PHP_ROOT \
 6--with-config-file-path=$PHP_CONFIG \
 7--with-mcrypt \
 8--with-mhash \
 9--with-mysqli=mysqlnd \
10--with-pdo-mysql=mysqlnd \
11--with-gd \
12--with-iconv \
13--with-zlib \
14--with-xmlrpc \
15--with-gettext \
16--with-curl \
17--with-jpeg-dir \
18--with-freetype-dir \
19--with-fpm-user=webhost \
20--with-fpm-group=staff \
21--disable-debug \
22--disable-rpath \
23--enable-shared \
24--enable-bcmath \
25--enable-fpm \
26--enable-ftp \
27--enable-gd-native-ttf \
28--enable-gd-jis-conv \
29--enable-inline-optimization \
30--enable-mbregex \
31--enable-mbstring \
32--enable-opcache \
33--enable-pcntl \
34--enable-session \
35--enable-shmop \
36--enable-soap \
37--enable-sockets \
38--enable-sysvsem \
39--enable-xml \
40--enable-zip \
41--without-gdbm \
42--disable-fileinfo
43
44make ZEND_EXTRA_LIBS='-liconv' -j 4 && make install
45
46sudo -u admin mkdir $PHP_CONFIG/php-fpm.d/
47
48cp $PHP_ROOT/etc/php-fpm.conf.default $PHP_CONFIG/php-fpm.conf
49
50cp $PHP_ROOT/etc/php-fpm.d/www.conf.default $PHP_CONFIG/php-fpm.d/www.conf
51
52cp php.ini-production $PHP_CONFIG/php.ini
53
54cp php.ini-* $PHP_ROOT/etc/
55
56cp -f sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
57
58chmod 0700 /etc/init.d/php-fpm

2. 配置 PHP

编辑 /www/config/php/php-fpm.conf,修改如下内容:

1pid = /www/pids/php-fpm.pid
2
3# PHP-FPM 的错误日志
4error_log = /www/logs/php-fpm.log
5
6include=/www/config/php/php-fpm.d/*.conf

保存,接着修改 /www/config/php/php-fpm.d/www.conf,修改如下内容:

 1# listen 部分可以不修改,默认监听 tcp://127.0.0.1:9000
 2listen = /www/socks/php-fpm.sock
 3listen.owner = webhost
 4listen.group = staff
 5listen.mode = 0660
 6
 7# 最大子进程数量控制
 8pm.max_children = 32
 9pm.min_spare_servers = 2
10pm.max_spare_servers = 8
11pm.max_requests = 1024
12
13# FPM 状态查询页面地址,可以注释掉,从而禁止查看
14pm.status_path = /php/fpm/status
15
16# 访问日志,可以注释掉,则不开启
17access.log = /www/logs/$pool.access.log
18access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%"
19
20# 慢速日志,即执行比较慢的请求的日志,通过 request_slowlog_timeout 配置“慢速”指多慢。
21slowlog = /www/logs/$pool.slow.log
22request_slowlog_timeout = 5s

保存,修改 /www/config/php/php.ini 文件,修改如下部分:

 1# 开启短标记 <? ?>
 2short_open_tag = On
 3
 4# POST 最大的数据长度,具体大小自行决定
 5post_max_size = 256M
 6
 7# POST 上传文件的临时目录
 8upload_tmp_dir = /www/tmp/php/upload/
 9
10# POST 上传文件最大长度,具体大小自行决定
11upload_max_filesize = 64M
12
13# 设置错误日志
14error_log = /www/logs/php_errors.log
15
16# 显示错误提示
17display_errors = On
18
19# 默认的 MINE 类型
20default_mimetype = "text/html"
21
22# 设置默认时区
23date.timezone = Asia/Shanghai
24
25# 设置 Session 储存目录
26session.save_path = "/www/tmp/php/sessions"
27
28# Session 使用的 Cookies 名称
29session.name = PHPSESSID

保存,修改文件 /etc/init.d/php-fpm,修改如下内容

1php_fpm_CONF=/www/config/php/php-fpm.conf
2php_fpm_PID=/www/pids/php-fpm.pid

保存。现在可以通过下面的命令启动和关闭 php-fpm 了。

1service php-fpm start
2service php-fpm stop
comments powered by Disqus