目前主流的php版本是5.6和7.1。和php 5相比,php 7对于性能的提升的很大的,对于自身的处理速度优化了很多,同时也改变了一些语法的使用。但由于很多软件都是基于php 5的,因此php 5和7的安装和配置都要熟练掌握。
安装PHP 5
1、下载安装包
1
2
3
4
5
6
7
8
9
10
11
|
[root@juispan src] # wget http://cn2.php.net/distributions/php-5.6.30.tar.gz
--2017-07-21 07:39:35-- http: //cn2 .php.net /distributions/php-5 .6.30. tar .gz
正在解析主机 cn2.php.net (cn2.php.net)... 220.181.136.41, 220.181.136.30, 220.181.136.55, ... 正在连接 cn2.php.net (cn2.php.net)|220.181.136.41|:80... 已连接。 已发出 HTTP 请求,正在等待回应... 200 OK 长度:19274631 (18M) [application /x-gzip ]
正在保存至: “php-5.6.30. tar .gz”
100%[==============================================>] 19,274,631 518KB /s 用时 34s
2017-07-21 07:40:10 (552 KB /s ) - 已保存 “php-5.6.30. tar .gz” [19274631 /19274631 ])
|
2、解压压缩包
1
|
[root@juispan src] # tar zxf php-5.6.30.tar.gz
|
3、配置php
1
2
|
[root@juispan src] # cd php-5.6.30
[root@juispan php-5.6.30] # ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2.4/bin/apxs --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif
|
在配置过程中,会遇到一个接一个的配置失败,这里需要耐心处理。
问题1:configure: error: xml2-config not found. Please check your libxml2 installation.
1
|
[root@juispan php-5.6.30] # yum install -y libxml2-devel
|
问题2:configure: error: Cannot find OpenSSL's <evp.h>
1
|
[root@juispan php-5.6.30] # yum install -y openssl-devel
|
问题3:configure: error: Please reinstall the BZip2 distribution
1
|
[root@juispan php-5.6.30] # yum install -y bzip2-devel
|
问题4:configure: error: jpeglib.h not found.
1
|
[root@juispan php-5.6.30] # yum install -y libjpeg-turbo-devel
|
问题5:configure: error: png.h not found.
1
|
[root@juispan php-5.6.30] # yum install -y libpng-devel
|
问题6:configure: error: freetype-config not found.
1
|
[root@juispan php-5.6.30] # yum install -y freetype-devel
|
问题7:configure: error: mcrypt.h not found. Please reinstall libmcrypt.
1
|
[root@juispan php-5.6.30] # yum install -y libmcrypt-devel
|
处理完以上问题后,重新配置出现以下文本:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
Generating files configure: creating . /config .status
creating main /internal_functions .c
creating main /internal_functions_cli .c
+--------------------------------------------------------------------+ | License: | | This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. | | If you do not agree with the terms of this license, you must abort |
| the installation process at this point. | +--------------------------------------------------------------------+ Thank you for using PHP.
config.status: creating php5.spec config.status: creating main /build-defs .h
config.status: creating scripts /phpize
config.status: creating scripts /man1/phpize .1
config.status: creating scripts /php-config
config.status: creating scripts /man1/php-config .1
config.status: creating sapi /cli/php .1
config.status: creating sapi /cgi/php-cgi .1
config.status: creating ext /phar/phar .1
config.status: creating ext /phar/phar .phar.1
config.status: creating main /php_config .h
config.status: executing default commands |
以上文本内容表示配置成功,如果不放心可以用“echo $?”确认下。
4、编译与安装
1
2
3
4
|
[root@juispan php-5.6.30] # make &&make install
[root@juispan php-5.6.30] # echo $?
0 [root@juispan php-5.6.30] # cp php.ini-production /usr/local/php/etc/php.ini
|
5、查看与验证
1
2
3
4
5
6
|
[root@juispan php-5.6.30] # du -sh /usr/local/apache2.4/modules/libphp5.so
37M /usr/local/apache2 .4 /modules/libphp5 .so
[root@juispan php-5.6.30] # cat /usr/local/apache2.4/conf/httpd.conf | grep -i php
LoadModule php5_module modules /libphp5 .so
[root@juispan php-5.6.30] # /usr/local/apache2.4/bin/httpd -M | tail -1
php5_module (shared)
|
前几章说过,php在LAMP架构里的作用只是Apache用于与mysql之间通讯的桥梁。因此,只要apache2.4的modules文件里有libphp5.so文件,且在配置文件里有相应的配置即可。即使删除php的安装目录也不会有太大的影响。
安装PHP 7
1、下载安装包
1
2
|
[root@juispan php-5.6.30] # cd /usr/local/src
[root@juispan src] # wget http://cn2.php.net/distributions/php-7.1.6.tar.bz2
|
2、解压压缩包
1
2
3
4
5
6
7
|
[root@juispan src] # tar jxf php-7.1.6.tar.bz2
tar (child): bzip2 :无法 exec : 没有那个文件或目录
tar (child): Error is not recoverable: exiting now
tar : Child returned status 2
tar : Error is not recoverable: exiting now
[root@juispan src] # yum install -y bzip2
[root@juispan src] # tar jxf php-7.1.6.tar.bz2
|
3、配置php
1
2
3
4
|
[root@juispan src] # cd php-7.1.6
[root@juispan php-7.1.6] # ./configure --prefix=/usr/local/php7 --with-apxs2=/usr/local/apache2.4/bin/apxs --with-config-file-path=/usr/local/php7/etc --with-pdo-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-libxml-dir --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-soap --enable-gd-native-ttf --enable-mbstring --enable-sockets --enable-exif
[root@juispan php-7.1.6] # echo $?
0 |
4、编译与安装
1
2
3
|
[root@juispan php-7.1.6] # make &&make install
[root@juispan php-7.1.6] # echo $?
0 |
5、查看与验证
1
2
3
4
5
6
7
8
9
|
[root@juispan php-7.1.6] # ls /usr/local/apache2.4/modules/libphp7.so
/usr/local/apache2 .4 /modules/libphp7 .so
[root@juispan php-7.1.6] # cp php.ini-production /usr/local/php7/etc/php.ini
[root@juispan php-7.1.6] # cat /usr/local/apache2.4/conf/httpd.conf | grep -i php
LoadModule php5_module modules /libphp5 .so
LoadModule php7_module modules /libphp7 .so
[root@juispan php-7.1.6] # /usr/local/apache2.4/bin/httpd -M | tail -2
php5_module (shared)
php7_module (shared)
|