怎样编译安装PHP5.2.17+PHP-fpm+Nginx+ZendOptimizer3.3.9

2021年3月28日 | 分类: 【技术】

【介绍】

某些程序需要安装php5.2.17和ZendOptimizer3.3.9。
php5.2.17 要求 Apache 2.2.32 。
HTTP/2 要求 Apache 2.4.29 。
因此转而寻求 nginx + php-fpm 模式。

参考:https://amon.org/php5217-apache

【依赖】

综合:

yum install yum-utils perl dos2unix apr apr-devel apr-util apr-util-devel curl curl-devel libcurl libcurl-devel wget gcc gcc-c++ flex git zip icu unzip libtiff-devel pam-devel libtool libtool-ltdl libtool-ltdl-devel bison bison-devel libxslt libxslt-devel ncurses ncurses-devel bzip2 bzip2-devel bzip2-libs libpsl libicu libjpeg libjpeg-devel unixODBC-devel net-snmp net-snmp-devel openldap-devel openldap-servers openldap-clients mhash-devel readline-devel libedit-devel sqlite-devel libffi libffi-devel guile guile-devel gmp gmp-devel gettext gettext-devel libidn libidn-devel unbound unbound-devel help2man texlive gtk-doc texinfo autogen autoconf mailcap git2cl gperf libev libev-devel jemalloc jemalloc-devel Cython setuptool trousers trousers-devel python python-devel patch

【安装 Nginx】

可以使用最新的 Nginx 版本。
参考:https://amon.org/nginx

【安装MySQL】

参考:https://amon.org/mysql51

【安装PHP 5.2.17】

安装系统环境需要的软件:

yum install gcc gcc-c++ autoconf libmcrypt libmcrypt-devel libmhash libmhash-devel libjpeg libjpeg-devel libpng libpng-devel gd gd-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-libs krb5-devel krb5-server libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers

编译安装 libiconv:

参考:https://amon.org/libiconv

编译安装 mcrypt:

参考:https://amon.org/mcrypt

编译安装 PHP-5.2.17:

下载源码:

cd /root && wget http://museum.php.net/php5/php-5.2.17.tar.gz && tar zxvf php-5.2.17.tar.gz

打补丁:

yum install patch
cd /root && wget http://php-fpm.org/downloads/php-5.2.17-fpm-0.5.14.diff.gz && gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1
cd /root && wget https://downloads.sourceforge.net/project/mapn/patch/php-5.2.17-max-input-vars.patch && cd php-5.2.17 && patch -p1 < ../php-5.2.17-max-input-vars.patch
sed -i "s/\!png_check_sig (sig, 8)/png_sig_cmp (sig, 0, 8)/" ext/gd/libgd/gd_png.c && ln -s /usr/lib64/libjpeg.so /usr/lib/libjpeg.so && ln -s /usr/lib64/libpng.so /usr/lib/libpng.so

关联库路径:

ln -s /usr/lib64/liblber* /usr/lib/
cp -frp /usr/lib64/libldap* /usr/lib/
cd /root && php-5.2.17 && curl -o php-5.2.17.patch https://mail.gnome.org/archives/xml/2012-August/txtbgxGXAvz4N.txt && patch -p0 -b < php-5.2.17.patch

开始编译PHP:

确认curl位置:

whereis curl

输出:

curl: /usr/bin/curl /usr/include/curl

开始编译:

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql/ --with-mysql-sock=/var/lib/mysql/mysql.sock  --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl=/usr/bin/curl --with-curlwrappers --enable-mbregex --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --enable-fpm --enable-fastcgi --enable-force-cgi-redirect 
make ZEND_EXTRA_LIBS='-liconv'
make install

注意:configure中加入了–enable-fpm –enable-fastcgi –enable-force-cgi-redirect ,去除了 –with-apxs2 。

修改php.ini文件

复制 php.ini :

cp php.ini-dist /usr/local/php/etc/php.ini

修改:

sed -i "s/output_buffering = Off/output_buffering = On/" /usr/local/php/etc/php.ini
sed -i "s/extension_dir =/; extension_dir =/" /usr/local/php/etc/php.ini
sed -i "s/; cgi.fix_pathinfo=0/cgi.fix_pathinfo=0/" /usr/local/php/etc/php.ini
sed -i 's%;open_basedir =%open_basedir ="/tmp/:/usr/local/apache2/htdocs/"%' /usr/local/php/etc/php.ini
sed -i "s/disable_functions =/disable_functions = popen,pentl_exec,passthru,exec,system,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,pfsockopen,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,show_source,escapeshellcmd,escapeshellarg,curl_exec,curl_multi_exec,parse_ini_file,assert/" /usr/local/php/etc/php.ini
sed -i "s/expose_php = On/expose_php = Off/" /usr/local/php/etc/php.ini
sed -i "s/display_errors = On/display_errors = Off/" /usr/local/php/etc/php.ini
sed -i "s/log_errors = Off/log_errors = On/" /usr/local/php/etc/php.ini

修改php-fpm.conf文件

php-fpm.conf 路径:/usr/local/php/etc/php-fpm.conf

启动 php-fpm :

# Usage: /usr/local/php/sbin/php-fpm {start|stop|quit|restart|reload|logrotate}
/usr/local/php/sbin/php-fpm start

php-fpm占用的是9000端口,查看下进程:

ps -ef|grep php-fpm

输出:

root     20414     1  0 17:54 ?        00:00:03 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/php/etc/php-fpm.conf
nginx    20415 20414  0 17:54 ?        00:00:00 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/php/etc/php-fpm.conf
nginx    20416 20414  0 17:54 ?        00:00:00 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/php/etc/php-fpm.conf
nginx    20417 20414  0 17:54 ?        00:00:00 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/php/etc/php-fpm.conf
nginx    20418 20414  0 17:54 ?        00:00:00 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/php/etc/php-fpm.conf
nginx    20419 20414  0 17:54 ?        00:00:00 /usr/local/php/bin/php-cgi --fpm --fpm-config /usr/local/php/etc/php-fpm.conf
root     25826 25551  0 21:52 pts/0    00:00:00 grep --color=auto php-fpm

查看PHP版本:

/usr/local/php/bin/php -v

结果如下:

PHP 5.2.17p1 (cli) (built: Feb 22 2017 04:00:57)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2010 Zend Technologies
    with Zend Optimizer v3.3.9, Copyright (c) 1998-2009, by Zend Technologies

【安装:PHP5扩展模块】

编译安装memcache:

官网:http://www.php.net/memcache
下载:https://pecl.php.net/package/memcache

wget https://pecl.php.net/get/memcache-2.2.6.tgz && tar zxvf memcache-2.2.6.tgz && cd memcache-2.2.6
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install

安装ZendOptimizer 3.3.9:

解压ZendOptimizer 3.3.9,并复制ZendOptimizer.so到/usr/local/Zend/lib/

wget http://downloads.zend.com/optimizer/3.3.9/ZendOptimizer-3.3.9-linux-glibc23-x86_64.tar.gz && tar -zxvf ZendOptimizer-3.3.9-linux-glibc23-x86_64.tar.gz && cd ZendOptimizer-3.3.9-linux-glibc23-x86_64 && cd data/5_2_x_comp && mkdir -p /usr/local/Zend/lib && cp ZendOptimizer.so /usr/local/Zend/lib/

注:/usr/local/Zend/lib/这个目录可以自定义。

在/usr/local/php/etc/php.ini文件尾部添加

[Zend]
zend_optimizer.optimization_level=1023
zend_optimizer.encoder_loader=1
zend_extension=/usr/local/Zend/lib/ZendOptimizer.so

【排错】

报错:libphp5.so: undefined symbol: _fpm_event_base_free

参考:http://blog.sina.com.cn/s/blog_3eba8f1c0100mfac.html

注意:这里 configure 时不要设置 –with-apxs2 ,它是告诉 PHP 以编译成模块方式让 Apache 来支持。就意味着,编译 PHP 以 PHP-FPM 的方式来支持FastCGI的话,基本上就不能和Apache一起使用了.

参考:https://www.php.cn/php-weizijiaocheng-328370.html

报错:/usr/local/php/bin/php-cgi: No such file or directory

当启动 php-fpm 时出现此错误。

参考:https://zhidao.baidu.com/question/1690162090083811348.html

如果 php5217 + nginx, 需要用到 /usr/local/php/bin/php-cgi ,注意在编译 php 时去掉 ./configure –with-apxs2 这个选项,重新编译即会在 bin 目录中生成 php-cgi。

报错:Starting php_fpm Mar 27 17:14:50.019698 [ERROR] fpm_unix_conf_wp(), line 124: please specify user and group other than root, pool ‘default’

当启动 php-fpm 时出现此错误。

参考:https://yq.aliyun.com/articles/524099

修改 php-fpm.conf (/usr/local/php/etc/php-fpm.conf)

			Unix user of processes
		<!--	<value name="user">nobody</value>				-->
				<value name="user">nginx</value>

			Unix group of processes
		<!--	<value name="group">nobody</value>		-->
				<value name="group">nginx</value>

报错:PHP Warning: Unknown: open_basedir restriction in effect. File(…) is not within the allowed path(s): (/tmp/:/…/) in Unknown on line 0″

参考:https://muoutiaowu.blog.csdn.net/article/details/51909766

根目录 /usr/local/nginx/html 没有包含在php配置的 open_basedir 里。

编辑 php.ini:

open_basedir ="/tmp/:/usr/local/nginx/html"

报错:缺少 mysqli 扩展,请检查 PHP 配置。

使用低版本PHP,phpMyAdmin 报错。那么补充编译 mysqli 扩展。

参考:https://amon.org/php-mysqli

报错:phpMyAdmin 高级功能尚未完全设置,部分功能未激活。请点击这里查看原因。

快速设置高级功能:

通过 examples/create_tables.sql 创建必需的数据表。 文档
创建一个用户并授予其访问上一步操作中创建的数据表的权限。 文档
在配置文件 (config.inc.php) 中启用高级功能,参见 config.sample.inc.php 中的范例。 文档
请重新登录 phpMyAdmin 以加载新配置并使其生效。