介绍
介绍
php7.4.6是一个划时代的产品,它让php对于强类型编程的支持度提升到了前所未有的的高度,不仅仅支持函数参数强类型,还支持函数返回值强类型,甚至对象属性也都支持强类型了
在这样的环境下编写php是非常舒服的,一写复杂的功能也可以通过php来实现了,可靠性比着前面的版本要强太多了
基本信息
安装环境
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)
软件版本
php-7.4.6.tar.gz
redis-4.1.0.tgz
libmemcached-1.0.18.tar.gz
memcached-3.0.4.tgz
mongodb-1.5.2.tgz
验证时间
2020/09/13
准备工作
安装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、sqlite
yum -y install zlib-devel libxml* curl-devel libjpeg-devel freetype-devel sqlite-devel
安装 oniguruma,这个因为是国外的服务器所以下载比较慢
wget http://mirror.centos.org/centos-7/7.9.2009/cloud/x86_64/openstack-queens/Packages/o/oniguruma-6.7.0-1.el7.x86_64.rpm wget http://mirror.centos.org/centos-7/7.9.2009/cloud/x86_64/openstack-queens/Packages/o/oniguruma-devel-6.7.0-1.el7.x86_64.rpm rpm -ivh oniguruma-6.7.0-1.el7.x86_64.rpm rpm -ivh oniguruma-devel-6.7.0-1.el7.x86_64.rpm
建立环境根目录
建立软件安装根目录
mkdir -p /tongfu.net/env/
建立安装包目录并进入
建立软件安装包保存目录
mkdir /packages cd /packages
安装PHP 7
准备
php-7.4.6.tar.gz
下载安装包
下载php-7.4.6安装包
wget https://www.php.net/distributions/php-7.4.6.tar.gz
百度网盘资源
php-7.4.6.tar.gz
链接: https://pan.baidu.com/s/1jXFk92F5W7vgkAkrl8ViLA 提取码: ii3t
redis-4.1.0.tgz
链接: https://pan.baidu.com/s/1TM5YXhYXX7bQspmjsj5UdA 提取码: q2ek
libmemcached-1.0.18.tar.gz
链接: https://pan.baidu.com/s/1Zp8ap0ImCO91zNpP-VZ6uw 提取码: un3a
libc-client-2007f-16.el7.x86_64.rpm
链接: https://pan.baidu.com/s/1LzolVh8OTArVyEM3-M3IuQ 提取码: 6j4s
uw-imap-2007f-16.el7.x86_64.rpm
链接: https://pan.baidu.com/s/1Bi90W20OX6Wjv5H5WJ7gRA 提取码: w3xr
uw-imap-devel-2007f-16.el7.x86_64
链接: https://pan.baidu.com/s/1dFoNoQSXFI1ykkKq-X9dng 提取码: gdf8
mongodb-1.7.4.tgz
链接: https://pan.baidu.com/s/1gkaZfXhQWOKKCasOui6_YA 提取码: m5wf
安装php
tar -xzvf php-7.4.6.tar.gz cd php-7.4.6 ./configure --prefix=/tongfu.net/env/php-7.4.6 \ --with-apxs2=/tongfu.net/env/httpd-2.4.46/bin/apxs \ --enable-mbstring \ --enable-exif \ --enable-soap \ --with-jpeg \ --with-curl \ --enable-gd \ --with-freetype \ --enable-fpm \ --enable-mysqlnd \ --with-pdo-mysql \ --with-config-file-path=/tongfu.net/env/php-7.4.6/etc \ --with-mysqli make && make install libtool --finish /packages/php-7.4.6/libs cp php.ini-* /tongfu.net/env/php-7.4.6/etc/ cp php.ini-development /tongfu.net/env/php-7.4.6/etc/php.ini mkdir /tongfu.net/env/php-7.4.6/logs/ cd ../
测试
命令行测试
按下面的操作执行后,会得到 phpinfo 打印的信息
[root@tfdev]# /tongfu.net/env/php-7.4.6/bin/php -r "phpinfo();" | grep "PHP API" PHP API => 20190902
配置Apache
添加默认索引文件
搜索 DirectoryIndex,在最后添加 index.php
[root@tfdev]# vi /tongfu.net/env/httpd-2.4.46/conf/httpd.conf DirectoryIndex index.html index.htm index.php
添加php文件映射
搜索 AddType application/x-gzip .gz .tgz,在后面添加
[root@tfdev]# vi /tongfu.net/env/httpd-2.4.46/conf/httpd.conf 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.4.6/logs/
重启Apache
重启后apache就支持运行php程序了
systemctl restart httpd
安装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.4.6/bin/phpize ./configure --with-php-config=/tongfu.net/env/php-7.4.6/bin/php-config make && make install cd ../
配置redis扩展
找到 extension 位置,添加redis
[root@tfdev]# vi /tongfu.net/env/php-7.4.6/etc/php.ini extension=redis
重启Apache
systemctl restart httpd
测试
[root@tfdev]# /tongfu.net/env/php-7.4.6/bin/php -r "phpinfo();" | grep redis redis Registered save handlers => files user redis rediscluster memcached This program is free software; you can redistribute it and/or modify
安装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.1.5.tgz tar -xzvf memcached-3.1.5.tgz cd memcached-3.1.5 /tongfu.net/env/php-7.4.6/bin/phpize ./configure --with-php-config=/tongfu.net/env/php-7.4.6/bin/php-config \ --with-libmemcached-dir=/tongfu.net/env/libmemcached-1.0.18/ \ --disable-memcached-sasl make && make install cd ../
配置mecached扩展
找到 extension 位置,添加memcached
[root@tfdev]# vi /tongfu.net/env/php-7.4.6/etc/php.ini extension=memcached
重启Apache
systemctl restart httpd
测试
[root@tfdev]# /tongfu.net/env/php-7.4.6/bin/php -r "phpinfo();" | grep memcached memcached memcached support => enabled libmemcached version => 1.0.18 memcached.compression_factor => 1.3 => 1.3 memcached.compression_threshold => 2000 => 2000 memcached.compression_type => fastlz => fastlz memcached.default_binary_protocol => Off => Off memcached.default_connect_timeout => 0 => 0 memcached.default_consistent_hash => Off => Off memcached.serializer => php => php memcached.sess_binary_protocol => On => On memcached.sess_connect_timeout => 0 => 0 memcached.sess_consistent_hash => On => On memcached.sess_consistent_hash_type => ketama => ketama memcached.sess_lock_expire => 0 => 0 memcached.sess_lock_max_wait => not set => not set memcached.sess_lock_retries => 5 => 5 memcached.sess_lock_wait => not set => not set memcached.sess_lock_wait_max => 150 => 150 memcached.sess_lock_wait_min => 150 => 150 memcached.sess_locking => On => On memcached.sess_number_of_replicas => 0 => 0 memcached.sess_persistent => Off => Off memcached.sess_prefix => memc.sess.key. => memc.sess.key. memcached.sess_randomize_replica_read => Off => Off memcached.sess_remove_failed_servers => Off => Off memcached.sess_sasl_password => no value => no value memcached.sess_sasl_username => no value => no value memcached.sess_server_failure_limit => 0 => 0 memcached.store_retry_count => 2 => 2 Registered save handlers => files user redis rediscluster memcached
安装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.4.6/ext/imap /tongfu.net/env/php-7.4.6/bin/phpize ./configure --with-imap \ --with-imap-ssl \ --with-kerberos \ --with-php-config=/tongfu.net/env/php-7.4.6/bin/php-config make && make install cd ../../../
配置imap扩展
找到 extension 位置,添加imap
[root@tfdev]# vi /tongfu.net/env/php-7.4.6/etc/php.ini extension=imap
重启Apache
systemctl restart httpd
测试
[root@tfdev]# /tongfu.net/env/php-7.4.6/bin/php -r "phpinfo();" | grep imap Protocols => dict, file, ftp, ftps, gopher, http, https, imap, imaps, ldap, ldaps, pop3, pop3s, rtsp, scp, sftp, smtp, smtps, telnet, tftp imap imap.enable_insecure_rsh => Off => Off
安装zlib扩展
安装zlib扩展
cd php-7.4.6/ext/zlib/ mv config0.m4 config.m4 /tongfu.net/env/php-7.4.6/bin/phpize ./configure --with-php-config=/tongfu.net/env/php-7.4.6/bin/php-config make && make install cd ../../../
配置zlib扩展
找到 extension 位置,添加zlib
[root@tfdev]# vi /tongfu.net/env/php-7.4.6/etc/php.ini extension=zlib
重启Apache
systemctl restart httpd
测试
[root@tfdev]# /tongfu.net/env/php-7.4.6/bin/php -r "phpinfo();" | grep zlib Registered PHP Streams => php, file, glob, data, http, ftp, compress.zlib, phar Registered Stream Filters => convert.iconv.*, string.rot13, string.toupper, string.tolower, string.strip_tags, convert.*, consumed, dechunk, zlib.* libmongoc compression zlib => enabled zlib Stream Wrapper => compress.zlib:// Stream Filter => zlib.inflate, zlib.deflate zlib.output_compression => Off => Off zlib.output_compression_level => -1 => -1 zlib.output_handler => no value => no value
安装MongoDB扩展
安装MongoDB扩展
wget http://pecl.php.net/get/mongodb-1.7.4.tgz tar -xzvf mongodb-1.7.4.tgz cd mongodb-1.7.4 /tongfu.net/env/php-7.4.6/bin/phpize ./configure --with-php-config=/tongfu.net/env/php-7.4.6/bin/php-config make && make install cd ../
配置MongoDB扩展
找到 extension 位置,添加zlib
[root@tfdev]# vi /tongfu.net/env/php-7.4.6/etc/php.ini extension=mongodb
重启Apache
systemctl restart httpd
测试
[root@tfdev]# /tongfu.net/env/php-7.4.6/bin/php -r "phpinfo();" | grep mongodb mongodb mongodb.debug => no value => no value
配置PHP
基本配置
关闭错误提示,把 On 改为 Off
[root@tfdev]# vi /tongfu.net/env/php-7.4.6/etc/php.ini display_errors = Off
更改默认错误报警级别,把 E_ALL 改为 E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
[root@tfdev]# vi /tongfu.net/env/php-7.4.6/etc/php.ini error_reporting = E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
设置错误报警输出路径,建议放到独立文件内
[root@tfdev]# vi /tongfu.net/env/php-7.4.6/etc/php.ini error_log = /tongfu.net/env/php-7.4.6/logs/php_errors.log
设置时区
[root@tfdev]# vi /tongfu.net/env/php-7.4.6/etc/php.ini date.timezone = Asia/Shanghai
设置主要扩展模块
[root@tfdev]# vi /tongfu.net/env/php-7.4.6/etc/php.ini extension=redis extension=memcached extension=imap extension=zlib extension=mongodb
重启Apache
systemctl restart httpd
测试PHP
命令行测试
通过php程序执行php代码
[root@tfdev]# /tongfu.net/env/php-7.4.6/bin/php -r "echo \"Now is \". date(\"Y-m-d H:i:s\"). \"\n\";bad_fun_call();"
要点1,时间要正确
Now is 2020-05-28 11:42:47
要点2,日志里要有错误记录
[root@tfdev]# cat /tongfu.net/env/php-7.4.6/logs/php_errors.log [28-May-2020 11:44:38 Asia/Shanghai] PHP Fatal error: Uncaught Error: Call to undefined function bad_fun_call() in Command line code:1 Stack trace: tfart_0 {main} thrown in Command line code on line 1
浏览器测试
在apache的htdocs目录下建立index.php
[root@tfdev]# echo "<?php phpinfo();" > /tongfu.net/env/httpd-2.4.46/htdocs/index.php
打开浏览器,访问网页 http://192.168.1.168:8080/index.php,可以看到php的服务器信息