基本信息
安装环境
CentOS:CentOS Linux release 7.6.1810 (Core)
Linux:Linux version 3.10.0-1062.el7.x86_64
GCC:gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
软件版本
jpegsrc.v9c.tar.gz
libpng-1.6.34.tar.gz
php-7.2.8.tar.gz
redis-4.1.0.tgz
libmemcached-1.0.18.tar.gz
memcached-3.0.4.tgz
mongodb-1.5.2.tgz
验证时间
2020/05/25
准备工作
安装Apache
参考《Apache安装笔记》
安装编译环境
如果已经安装了 Apache 环境,则此步可跳过
本地化
如果是新环境,我们需要设置时区以保证时间显示正确
timedatectl set-timezone Asia/Shanghai
安装wget
如果环境里没有wget,通过yum安装一下
yum -y install wget
安装gcc
如果环境里没有编译工具,通过yum安装一下
yum -y install gcc gcc-c++ make
安装依赖包
安装 zlib、libxml、curl、freetype
yum -y install zlib-devel libxml* curl-devel freetype-devel
建立环境根目录
mkdir -p /tongfu.net/env/
建立安装包目录并进入
mkdir /packages cd /packages
安装PHP 7
准备
jpegsrc.v9c.tar.gz
libpng-1.6.34.tar.gz
php-7.2.8.tar.gz
下载安装包
wget http://www.ijg.org/files/jpegsrc.v9c.tar.gz wget http://jaist.dl.sourceforge.net/project/libpng/libpng16/1.6.34/libpng-1.6.34.tar.gz wget https://www.php.net/distributions/php-7.2.8.tar.gz
安装jpegsrc
tar xzvf jpegsrc.v9c.tar.gz cd jpeg-9c ./configure --prefix=/tongfu.net/env/jpeg-9c make && make install cd ..
安装libpng
tar xzvf libpng-1.6.34.tar.gz cd libpng-1.6.34 ./configure --prefix=/tongfu.net/env/libpng-1.6.34 make && make install cd ..
安装php
tar -xzvf php-7.2.8.tar.gz cd php-7.2.8 ./configure --prefix=/tongfu.net/env/php-7.2.8 \ --with-apxs2=/tongfu.net/env/httpd-2.4.38/bin/apxs \ --enable-mbstring \ --enable-exif \ --enable-soap \ --with-curl \ --with-gd \ --with-jpeg-dir=/tongfu.net/env/jpeg-9c \ --with-png-dir=/tongfu.net/env/libpng-1.6.34 \ --with-freetype-dir \ --enable-fpm \ --enable-mysqlnd \ --with-pdo-mysql \ --with-config-file-path=/tongfu.net/env/php-7.2.8/etc \ --with-mysqli make && make install cp php.ini-* /tongfu.net/env/php-7.2.8/etc/ cp php.ini-development /tongfu.net/env/php-7.2.8/etc/php.ini mkdir /tongfu.net/env/php-7.2.8/logs/ cd ..
初始化
添加默认索引文件
搜索 DirectoryIndex,在最后添加 index.php
添加php文件映射
搜索 AddType application/x-gzip .gz .tgz,在后面添加
[root@tongfunet]# vi /tongfu.net/env/httpd-2.4.38/conf/httpd.conf DirectoryIndex index.html index.htm index.php AddType application/x-httpd-php .php
授权php错误日志目录
我们需要根据 Apache 的配置文件里的 User 和 Group 对 php 的错误日志目录进行授权,否则 error_log 就会不生效
默认的 Apache 的 User 和 Group 都是 daemon,可自行更改
chown daemon.daemon /tongfu.net/env/php-7.2.8/logs/
重启Apache
systemctl restart httpd
测试
按下面的操作执行后,应该会得到 phpinfo 打印的信息
[root@tongfunet]# cat > /tongfu.net/env/httpd-2.4.38/htdocs/test.php <<EOF <?php phpinfo(); EOF [root@tongfunet]# curl -s "http://localhost:8080/test.php" | grep "PHP API" <tr><td class="e">PHP API </td><td class="v">20170718 </td></tr>
如果没有正确返回信息的话,可以使用先反复 stop 直到提示“not running”后再 start 的方式重启Apache
安装redis扩展
安装redis扩展
wget http://pecl.php.net/get/redis-4.1.0.tgz tar -xzvf redis-4.1.0.tgz cd redis-4.1.0 /tongfu.net/env/php-7.2.8/bin/phpize ./configure --with-php-config=/tongfu.net/env/php-7.2.8/bin/php-config make && make install cd ..
配置redis扩展
找到 extension 位置,添加redis
[root@tongfunet]# vi /tongfu.net/env/php-7.2.8/etc/php.ini extension=redis
重启Apache
systemctl restart httpd
测试
[root@tongfunet]# curl -s "http://localhost:8080/test.php" | grep redis <h2><a name="module_redis">redis</a></h2> <tr><td class="e">Registered save handlers </td><td class="v">files user redis rediscluster </td></tr> This program is free software; you can redistribute it and/or modify it under the terms of the PHP License as published by the PHP Group and included in the distribution in the file: LICENSE
安装libmemcached库
安装libmemcached库
wget https://launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz tar -xzvf libmemcached-1.0.18.tar.gz cd libmemcached-1.0.18 ./configure --prefix=/tongfu.net/env/libmemcached-1.0.18 \ --with-memcached=/tongfu.net/env/memcached-1.5.10/ make && make install cd ..
安装memcached扩展
安装memcached扩展
wget http://pecl.php.net/get/memcached-3.0.4.tgz tar -xzvf memcached-3.0.4.tgz cd memcached-3.0.4 /tongfu.net/env/php-7.2.8/bin/phpize ./configure --with-php-config=/tongfu.net/env/php-7.2.8/bin/php-config \ --with-libmemcached-dir=/tongfu.net/env/libmemcached-1.0.18/ \ --disable-memcached-sasl make && make install cd ..
配置mecached扩展
找到 extension 位置,添加memcached
[root@tongfunet]# vi /tongfu.net/env/php-7.2.8/etc/php.ini extension=memcached
重启Apache
systemctl restart httpd
测试
[root@tongfunet]# curl -s "http://localhost:8080/test.php" | grep memcached <h2><a name="module_memcached">memcached</a></h2> <tr class="h"><th>memcached support</th><th>enabled</th></tr> <tr><td class="e">libmemcached version </td><td class="v">1.0.18 </td></tr> <tr><td class="e">memcached.compression_factor</td><td class="v">1.3</td><td class="v">1.3</td></tr> <tr><td class="e">memcached.compression_threshold</td><td class="v">2000</td><td class="v">2000</td></tr> <tr><td class="e">memcached.compression_type</td><td class="v">fastlz</td><td class="v">fastlz</td></tr> <tr><td class="e">memcached.default_binary_protocol</td><td class="v">0</td><td class="v">0</td></tr> <tr><td class="e">memcached.default_connect_timeout</td><td class="v">0</td><td class="v">0</td></tr> <tr><td class="e">memcached.default_consistent_hash</td><td class="v">0</td><td class="v">0</td></tr> <tr><td class="e">memcached.serializer</td><td class="v">php</td><td class="v">php</td></tr> <tr><td class="e">memcached.sess_binary_protocol</td><td class="v">1</td><td class="v">1</td></tr> <tr><td class="e">memcached.sess_connect_timeout</td><td class="v">0</td><td class="v">0</td></tr> <tr><td class="e">memcached.sess_consistent_hash</td><td class="v">1</td><td class="v">1</td></tr> <tr><td class="e">memcached.sess_lock_expire</td><td class="v">0</td><td class="v">0</td></tr> <tr><td class="e">memcached.sess_lock_max_wait</td><td class="v">not set</td><td class="v">not set</td></tr> <tr><td class="e">memcached.sess_lock_retries</td><td class="v">5</td><td class="v">5</td></tr> <tr><td class="e">memcached.sess_lock_wait</td><td class="v">not set</td><td class="v">not set</td></tr> <tr><td class="e">memcached.sess_lock_wait_max</td><td class="v">2000</td><td class="v">2000</td></tr> <tr><td class="e">memcached.sess_lock_wait_min</td><td class="v">1000</td><td class="v">1000</td></tr> <tr><td class="e">memcached.sess_locking</td><td class="v">1</td><td class="v">1</td></tr> <tr><td class="e">memcached.sess_number_of_replicas</td><td class="v">0</td><td class="v">0</td></tr> <tr><td class="e">memcached.sess_persistent</td><td class="v">0</td><td class="v">0</td></tr> <tr><td class="e">memcached.sess_prefix</td><td class="v">memc.sess.</td><td class="v">memc.sess.</td></tr> <tr><td class="e">memcached.sess_randomize_replica_read</td><td class="v">0</td><td class="v">0</td></tr> <tr><td class="e">memcached.sess_remove_failed_servers</td><td class="v">0</td><td class="v">0</td></tr> <tr><td class="e">memcached.sess_sasl_password</td><td class="v"><i>no value</i></td><td class="v"><i>no value</i></td></tr> <tr><td class="e">memcached.sess_sasl_username</td><td class="v"><i>no value</i></td><td class="v"><i>no value</i></td></tr> <tr><td class="e">memcached.sess_server_failure_limit</td><td class="v">0</td><td class="v">0</td></tr> <tr><td class="e">memcached.store_retry_count</td><td class="v">2</td><td class="v">2</td></tr> <tr><td class="e">Registered save handlers </td><td class="v">files user redis rediscluster memcached </td></tr>
安装imap扩展
安装imap扩展
yum -y install libc-client ln -s /usr/lib64/libc-client.so /usr/lib/libc-client.so wget http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/l/libc-client-2007f-16.el7.x86_64.rpm wget http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/u/uw-imap-2007f-16.el7.x86_64.rpm wget http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/u/uw-imap-devel-2007f-16.el7.x86_64.rpm rpm -ivh libc-client-2007f-16.el7.x86_64.rpm rpm -ivh uw-imap-2007f-16.el7.x86_64.rpm rpm -ivh uw-imap-devel-2007f-16.el7.x86_64.rpm cd php-7.2.8/ext/imap /tongfu.net/env/php-7.2.8/bin/phpize ./configure --with-imap \ --with-imap-ssl \ --with-kerberos \ --with-php-config=/tongfu.net/env/php-7.2.8/bin/php-config make && make install cd ../../../
配置imap扩展
找到 extension 位置,添加imap
[root@tongfunet]# vi /tongfu.net/env/php-7.2.8/etc/php.ini extension=imap
重启Apache
systemctl restart httpd
测试
[root@tongfunet]# curl -s "http://localhost:8080/test.php" | grep imap <tr><td class="e">Protocols </td><td class="v">dict, file, ftp, ftps, gopher, http, https, imap, imaps, ldap, ldaps, pop3, pop3s, rtsp, scp, sftp, smtp, smtps, telnet, tftp </td></tr> <h2><a name="module_imap">imap</a></h2>
安装zlib扩展
安装zlib扩展
cd php-7.2.8/ext/zlib/ mv config0.m4 config.m4 /tongfu.net/env/php-7.2.8/bin/phpize ./configure --with-php-config=/tongfu.net/env/php-7.2.8/bin/php-config make && make install cd ../../../
配置zlib扩展
找到 extension 位置,添加zlib
[root@tongfunet]# vi /tongfu.net/env/php-7.2.8/etc/php.ini extension=zlib
重启Apache
systemctl restart httpd
测试
[root@tongfunet]# curl -s "http://localhost:8080/test.php" | grep zlib <tr><td class="e">Registered PHP Streams</td><td class="v">php, file, glob, data, http, ftp, compress.zlib, phar</td></tr> <tr><td class="e">Registered Stream Filters</td><td class="v">convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk, zlib.*</td></tr> <h2><a name="module_zlib">zlib</a></h2> <tr><td class="e">Stream Wrapper </td><td class="v">compress.zlib:// </td></tr> <tr><td class="e">Stream Filter </td><td class="v">zlib.inflate, zlib.deflate </td></tr> <tr><td class="e">zlib.output_compression</td><td class="v">Off</td><td class="v">Off</td></tr> <tr><td class="e">zlib.output_compression_level</td><td class="v">-1</td><td class="v">-1</td></tr> <tr><td class="e">zlib.output_handler</td><td class="v"><i>no value</i></td><td class="v"><i>no value</i></td></tr>
安装MongoDB扩展
安装MongoDB扩展
wget http://pecl.php.net/get/mongodb-1.5.2.tgz tar -xzvf mongodb-1.5.2.tgz cd mongodb-1.5.2 /tongfu.net/env/php-7.2.8/bin/phpize ./configure --with-php-config=/tongfu.net/env/php-7.2.8/bin/php-config make && make install cd ../
配置MongoDB扩展
找到 extension 位置,添加zlib
[root@tongfunet]# vi /tongfu.net/env/php-7.2.8/etc/php.ini extension=mongodb
重启Apache
systemctl restart httpd
测试
[root@tongfunet]# curl -s "http://localhost:8080/test.php" | grep mongodb <h2><a name="module_mongodb">mongodb</a></h2> <tr><td class="e">mongodb.debug</td><td class="v"><i>no value</i></td><td class="v"><i>no value</i></td></tr>
配置PHP
基本配置
关闭错误提示,把 On 改为 Off
[root@tongfunet]# vi /tongfu.net/env/php-7.2.8/etc/php.ini display_errors = Off
更改默认错误报警级别,把 E_ALL 改为 E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
[root@tongfunet]# vi /tongfu.net/env/php-7.2.8/etc/php.ini error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
设置错误报警输出路径,建议放到独立文件内
[root@tongfunet]# mkdir /tongfu.net/env/php-7.2.8/logs [root@tongfunet]# chmod 0777 /tongfu.net/env/php-7.2.8/logs [root@tongfunet]# vi /tongfu.net/env/php-7.2.8/etc/php.ini error_log = /tongfu.net/env/php-7.2.8/logs/php_errors.log
设置时区
[root@tongfunet]# vi /tongfu.net/env/php-7.2.8/etc/php.ini date.timezone = Asia/Shanghai
设置主要扩展模块
[root@tongfunet]# vi /tongfu.net/env/php-7.2.8/etc/php.ini extension=redis extension=memcached extension=imap extension=zlib extension=mongodb
重启Apache
systemctl restart httpd
测试
[root@tongfunet]# cat > /tongfu.net/env/httpd-2.4.38/htdocs/test.php <<EOF <?php echo "Now is ". date("Y-m-d H:i:s"). "\n";bad_fun_call(); EOF
要点1,时间要正确
[root@tongfunet]# curl 'http://localhost:8080/test.php' Now is 2019-02-28 20:37:40
要点2,日志里要有错误记录
[root@tongfunet]# cat /tongfu.net/env/php-7.2.8/logs/php_errors.log [28-Feb-2019 20:39:13 Asia/Shanghai] PHP Fatal error: Uncaught Error: Call to undefined function bad_fun_call() in /tongfu.net/env/httpd-2.4.38/htdocs/test.php:1 Stack trace: tfart_0 {main} thrown in /tongfu.net/env/httpd-2.4.38/htdocs/test.php on line 1