Fenying

Angus’ Home.


26 Dec 2016

第八章 编译安装 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 等源码

wget 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

tar -xzvf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7
./configure
make -j 4 && make install
/sbin/ldconfig
cd libltdl/
./configure --enable-ltdl-install
make -j 4 && make install
cd ../..
rm -Rf libmcrypt-2.5.7
ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.7 /usr/lib/libmcrypt.so.4.4.7
ln -s /usr/local/bin/libmcrypt_config /usr/bin/libmcrypt_config

0.4. 编译安装 mhash

tar -zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9
./configure
make -j 4 && make install
cd ..
rm -Rf mhash-0.9.9.9
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
/sbin/ldconfig

0.5. 编译安装 mcrypt

tar -xzvf mcrypt-2.6.7.tar.gz
cd mcrypt-2.6.7

export LD_LIBRARY_PATH=/usr/local/lib:/usr/local/lib64:$LD_LIBRARY_PATH
./configure
make -j 4 && make install

cd ..
rm -Rf mcrypt-2.6.7

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

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

0.6. 安装其他依赖

yum -y install \
  make \
  cmake \
  autoconf \
  automake \
  bison \
  flex \
  freetype \
  freetype-devel \
  fontconfig-devel \
  gettext-devel \
  libjpeg \
  libjpeg-devel \
  libpng \
  libpng-devel \
  libxml2 \
  libxml2-devel \
  libtool \
  libtool-ltdl \
  libtool-ltdl-devel \
  libtiff-devel \
  libXpm-devel \
  libicu \
  libicu-devel \
  libidn \
  libidn-devel \
  libxslt \
  libxslt-devel \
  bzip2 \
  bzip2-devel \
  ncurses \
  ncurses-devel \
  curl \
  curl-devel \
  e2fsprogs \
  e2fsprogs-devel \
  krb5 \
  krb5-devel \
  openldap \
  openldap-devel \
  nss_ldap \
  openldap-clients \
  openldap-servers \
  pam-devel \
  vim-enhanced \
  net-snmp-devel \
  libxml2 \
  libxml2-devel \
  libcurl-devel \
  libjpeg \
  libjpeg-devel \
  libpng \
  libpng-devel \
  libwebp \
  libwebp-devel \
  freetype \
  freetype-devel \
  gd \
  gd-devel

  zlib \
  zlib-devel \
  glibc \
  glibc-devel \
  glib2 \
  glib2-devel \
  openssl \
  openssl-devel \
  pcre \
  pcre-devel \

保存,执行

ldconfig

1. 编译 PHP


tar -zxvf php-7.1.0.tar.gz
cd php-7.1.0
./configure \
--prefix=$PHP_ROOT \
--with-config-file-path=$PHP_CONFIG \
--with-mcrypt \
--with-mhash \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-gd \
--with-iconv \
--with-zlib \
--with-xmlrpc \
--with-gettext \
--with-curl \
--with-jpeg-dir \
--with-freetype-dir \
--with-fpm-user=webhost \
--with-fpm-group=staff \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-bcmath \
--enable-fpm \
--enable-ftp \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-session \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-xml \
--enable-zip \
--without-gdbm \
--disable-fileinfo

make ZEND_EXTRA_LIBS='-liconv' -j 4 && make install

sudo -u admin mkdir $PHP_CONFIG/php-fpm.d/

cp $PHP_ROOT/etc/php-fpm.conf.default $PHP_CONFIG/php-fpm.conf

cp $PHP_ROOT/etc/php-fpm.d/www.conf.default $PHP_CONFIG/php-fpm.d/www.conf

cp php.ini-production $PHP_CONFIG/php.ini

cp php.ini-* $PHP_ROOT/etc/

cp -f sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

chmod 0700 /etc/init.d/php-fpm

2. 配置 PHP

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

pid = /www/pids/php-fpm.pid

# PHP-FPM 的错误日志
error_log = /www/logs/php-fpm.log

include=/www/config/php/php-fpm.d/*.conf

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

# listen 部分可以不修改,默认监听 tcp://127.0.0.1:9000
listen = /www/socks/php-fpm.sock
listen.owner = webhost
listen.group = staff
listen.mode = 0660

# 最大子进程数量控制
pm.max_children = 32
pm.min_spare_servers = 2
pm.max_spare_servers = 8
pm.max_requests = 1024

# FPM 状态查询页面地址,可以注释掉,从而禁止查看
pm.status_path = /php/fpm/status

# 访问日志,可以注释掉,则不开启
access.log = /www/logs/$pool.access.log
access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%"

# 慢速日志,即执行比较慢的请求的日志,通过 request_slowlog_timeout 配置“慢速”指多慢。
slowlog = /www/logs/$pool.slow.log
request_slowlog_timeout = 5s

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

# 开启短标记 <? ?>
short_open_tag = On

# POST 最大的数据长度,具体大小自行决定
post_max_size = 256M

# POST 上传文件的临时目录
upload_tmp_dir = /www/tmp/php/upload/

# POST 上传文件最大长度,具体大小自行决定
upload_max_filesize = 64M

# 设置错误日志
error_log = /www/logs/php_errors.log

# 显示错误提示
display_errors = On

# 默认的 MINE 类型
default_mimetype = "text/html"

# 设置默认时区
date.timezone = Asia/Shanghai

# 设置 Session 储存目录
session.save_path = "/www/tmp/php/sessions"

# Session 使用的 Cookies 名称
session.name = PHPSESSID

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

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

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

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

comments powered by Disqus