需求
fedora26下使用yum安装好apache2.4 和 php7.0后,需要添加apache2.4对php5.6的支持.
方法
介绍方法前,需要理解一个文件,libphp5.0.so,当php作为apache的一个模块运行,随apache一起启动时,需要在编译php时生成此文件,并在apache配置文件中修改配置项,加载这个文件.
因此如果想添加多版本支持,只需通过编译生成不同版本的libphp动态库文件,然后在apache配置文件中加载不同版本的库文件即可.
生成库文件
可以通过下载php5.6源码进行编译安装,在编译时添加选项--with-apxs2=用来生成libphp5.0.so, 此选项后应该填写本机apxs命令的完整路径(路径可通过which apxs查看),如果提示"no apxs in",可通过yum安装httpd-devel得到.
[root@MiWiFi-R3D-srv php56]# yum install httpd-devel
[root@MiWiFi-R3D-srv php56]# which apxs
/usr/bin/apxs
获取到apxs路径后,在解压后的源码文件夹中进行编译,安装即可
[root@MiWiFi-R3D-srv php56]#./configure --prefix=/usr/local/ --with-apxs2=/usr/bin/apxs
[root@MiWiFi-R3D-srv php56]#make
[root@MiWiFi-R3D-srv php56]#make install
安装成功后,查看apahce模块文件,如果有文件libphp5.so,即表示成功生成.
[root@MiWiFi-R3D-srv modules]# pwd
/etc/httpd/modules
[root@MiWiFi-R3D-srv modules]# ll -th
总用量 42M
-rwxr-xr-x 1 root root 31M 4月 26 11:35 libphp5.so
-rwxr-xr-x 1 root root 4.3M 2月 28 20:13 libphp7.so
-rwxr-xr-x 1 root root 4.5M 2月 28 20:13 libphp7-zts.so
-rwxr-xr-x 1 root root 250K 11月 7 16:30 mod_http2.so
....
....
查看apache配置文件,确认libphp5.so文件已被apahce加载,默认情况下,编译php成功后会自动添加,如果没有,可以手动添加:
LoadModule php5_module /usr/lib64/httpd/modules/libphp5.so
[root@MiWiFi-R3D-srv www]# vim /etc/httpd/conf/httpd.conf
.............
..............
LoadModule php5_module /usr/lib64/httpd/modules/libphp5.so
Include conf.modules.d/*.con
..............
..............
配置当前版本
编辑apache中对应php模块的配置文件,将原来的
[root@MiWiFi-R3D-srv www]# vim /etc/httpd/conf.d/php.conf
#.....省略大概39行,无需改动......
#行号39
#
# Cause the PHP interpreter to handle files with a .php extension.
#
SetHandler application/x-httpd-php
#
# Uncomment the following lines to allow PHP to pretty-print .phps
# files as PHP source code:
#
#
# SetHandler application/x-httpd-php-source
#
#
# Apache specific PHP configuration options
# those can be override in each configured vhost
#
php_value session.save_handler "files"
php_value session.save_path "/var/lib/php/session"
php_value soap.wsdl_cache_dir "/var/lib/php/wsdlcache"
#php_value opcache.file_cache "/var/lib/php/opcache"
#结束
验证
重启apache服务,使用phpinfo查看php版本信息;
版本切换
如果想换回php7版本,注释掉"LoadModule php5_module /usr/lib64/httpd/modules/libphp5.so",并修改
[root@MiWiFi-R3D-srv www]# vim /etc/httpd/conf/httpd.conf
.............
..............
#LoadModule php5_module /usr/lib64/httpd/modules/libphp5.so
Include conf.modules.d/*.con
..............
..............
[root@MiWiFi-R3D-srv www]# vim /etc/httpd/conf.d/php.conf
#
# Cause the PHP interpreter to handle files with a .php extension.
#
SetHandler application/x-httpd-php
#
# Uncomment the following lines to allow PHP to pretty-print .phps
# files as PHP source code:
#
#
# SetHandler application/x-httpd-php-source
#
#
# Apache specific PHP configuration options
# those can be override in each configured vhost
#
php_value session.save_handler "files"
php_value session.save_path "/var/lib/php/session"
php_value soap.wsdl_cache_dir "/var/lib/php/wsdlcache"
#php_value opcache.file_cache "/var/lib/php/opcache"