linux 学习笔记-039-安装 php5,安装 php7

发布于 2018-02-28  469 次阅读


安装 PHP5

PHP 官网:http://php.net/

当前主流版本为 5.6/7.1(目前很大一部分网站还在用 php5 版本)

在 LAMP 架构中,php 作为 apache 的模块存在,不需要另外启动

编译安装:

[root@am-01:~#] cd /usr/local/src/

[root@am-01:/usr/local/src#] wget http://cn2.php.net/distributions/php-5.6.30.tar.gz

--2018-02-28 22:35:01--  http://cn2.php.net/distributions/php-5.6.30.tar.gz

正在解析主机 cn2.php.net (cn2.php.net)... 123.125.23.169, 123.125.23.172, 123.125.23.168, ...

正在连接 cn2.php.net (cn2.php.net)|123.125.23.169|:80... 已连接。

已发出 HTTP 请求,正在等待回应... 200 OK

长度:19274631 (18M) [application/x-gzip]

正在保存至: “php-5.6.30.tar.gz”



100%[=========================================================>] 19,274,631   413KB/s 用时 37s    



2018-02-28 22:35:43 (507 KB/s) - 已保存 “php-5.6.30.tar.gz” [19274631/19274631])

[root@am-01:/usr/local/src#] tar xvf php-5.6.30.tar.gz


#下载 php5 的包,解压
[root@am-01:/usr/local/src#] ./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


#--prefix 指定安装目录,--with-apxs2 为 apache 的一个工具,能自动的把模块添加到 httpd 中,--with-config-file-path 指定 php 配置文件的路径,配置文件的名称为 php.ini,--with-mysql、--with-pdo-mysql、--with-mysqli 指定 mysql 的路径,这里有三个,分别代表了 mysql 的三个不同的库,是为了让 php 能支持 mysql,其他的是指定一些 php 所需要的模块
configure: error: Cannot find OpenSSL's <evp.h>


#提示这个错误,按关键字查询,安装 openssl-devel 包
[root@am-01:/usr/local/src/php-5.6.30#] yum list | grep openssl

openssl.x86_64                           1:1.0.1e-42.el7               @anaconda

openssl-libs.x86_64                      1:1.0.1e-42.el7               @anaconda

apr-util-openssl.x86_64                  1.5.2-6.el7                   base

globus-gsi-openssl-error.x86_64          3.8-1.el7                     epel

globus-gsi-openssl-error-devel.x86_64    3.8-1.el7                     epel

globus-gsi-openssl-error-doc.noarch      3.8-1.el7                     epel

globus-openssl-module.x86_64             4.8-1.el7                     epel

globus-openssl-module-devel.x86_64       4.8-1.el7                     epel

globus-openssl-module-doc.noarch         4.8-1.el7                     epel

mingw32-openssl.noarch                   1.0.2h-1.el7                  epel

mingw32-openssl-static.noarch            1.0.2h-1.el7                  epel

mingw64-openssl.noarch                   1.0.2h-1.el7                  epel

mingw64-openssl-static.noarch            1.0.2h-1.el7                  epel

openssl.x86_64                           1:1.0.2k-8.el7                base

openssl-devel.i686                       1:1.0.2k-8.el7                base

openssl-devel.x86_64                     1:1.0.2k-8.el7                base

openssl-libs.i686                        1:1.0.2k-8.el7                base

openssl-libs.x86_64                      1:1.0.2k-8.el7                base

openssl-perl.x86_64                      1:1.0.2k-8.el7                base

openssl-static.i686                      1:1.0.2k-8.el7                base

openssl-static.x86_64                    1:1.0.2k-8.el7                base

openssl098e.i686                         0.9.8e-29.el7.centos.3        base

openssl098e.x86_64                       0.9.8e-29.el7.centos.3        base

rubygem-openssl_cms_2_0_0.x86_64         0.0.2-1.20140212git7fea071.el7

rubygem-openssl_cms_2_0_0-doc.noarch     0.0.2-1.20140212git7fea071.el7

xmlsec1-openssl.i686                     1.2.20-7.el7_4                updates

xmlsec1-openssl.x86_64                   1.2.20-7.el7_4                updates

xmlsec1-openssl-devel.i686               1.2.20-7.el7_4                updates

xmlsec1-openssl-devel.x86_64             1.2.20-7.el7_4                updates

[root@am-01:/usr/local/src/php-5.6.30#] yum -y install openssl-devel.x86_64

已加载插件:fastestmirror

base                                                                        | 3.6 kB  00:00:00

epel/x86_64/metalink                                                        | 5.0 kB  00:00:00

epel                                                                        | 4.7 kB  00:00:00

extras                                                                      | 3.4 kB  00:00:00

………………省略部分输出信息………………

作为依赖被升级:

dracut-config-rescue.x86_64 0:033-502.el7_4.1       dracut-network.x86_64 0:033-502.el7_4.1

e2fsprogs.x86_64 0:1.42.9-10.el7                    e2fsprogs-libs.x86_64 0:1.42.9-10.el7

glib2.x86_64 0:2.50.3-3.el7                         kmod.x86_64 0:20-15.el7_4.7

krb5-libs.x86_64 0:1.15.1-8.el7                     libcom_err.x86_64 0:1.42.9-10.el7

libgudev1.x86_64 0:219-42.el7_4.7                   libselinux.x86_64 0:2.5-11.el7

libselinux-python.x86_64 0:2.5-11.el7               libselinux-utils.x86_64 0:2.5-11.el7

libsepol.x86_64 0:2.5-6.el7                         libss.x86_64 0:1.42.9-10.el7

openssl.x86_64 1:1.0.2k-8.el7                       openssl-libs.x86_64 1:1.0.2k-8.el7

systemd-libs.x86_64 0:219-42.el7_4.7                systemd-sysv.x86_64 0:219-42.el7_4.7



完毕!


#安装完 openssl-devel 后继续编译安装 php5
configure: error: Please reinstall the BZip2 distribution


#这次提示这个,安装 bzip2-devel 包
[root@am-01:/usr/local/src/php-5.6.30#] yum list | grep bzip

bzip2.x86_64                             1.0.6-13.el7                  @base

bzip2-libs.x86_64                        1.0.6-13.el7                  @base

bzip2-devel.i686                         1.0.6-13.el7                  base

bzip2-devel.x86_64                       1.0.6-13.el7                  base

bzip2-libs.i686                          1.0.6-13.el7                  base

lbzip2.x86_64                            2.5-1.el7                     epel

lbzip2-utils.x86_64                      1.0-2.el7                     epel

libzip.i686                              0.10.1-8.el7                  base

libzip.x86_64                            0.10.1-8.el7                  base

libzip-devel.i686                        0.10.1-8.el7                  base

libzip-devel.x86_64                      0.10.1-8.el7                  base

mingw32-bzip2.noarch                     1.0.6-3.el7                   epel

mingw32-bzip2-static.noarch              1.0.6-3.el7                   epel

mingw64-bzip2.noarch                     1.0.6-3.el7                   epel

mingw64-bzip2-static.noarch              1.0.6-3.el7                   epel

pbzip2.x86_64                            1.1.12-1.el7                  epel

[root@am-01:/usr/local/src/php-5.6.30#] yum -y install bzip2-devel.x86_64

已加载插件:fastestmirror

Loading mirror speeds from cached hostfile

* epel: mirror01.idc.hinet.net

正在解决依赖关系

--> 正在检查事务

---> 软件包 bzip2-devel.x86_64.0.1.0.6-13.el7 将被 安装

--> 解决依赖关系完成

………………省略部分输出信息………………

已安装:

bzip2-devel.x86_64 0:1.0.6-13.el7



完毕!


#安装完 bzip2-devel 后继续编译安装 php5
configure: error: jpeglib.h not found.


#这次提示这个错误,安装 libjpeg-turbo-devel 包
[root@am-01:/usr/local/src/php-5.6.30#] yum list | grep libjpeg

libjpeg-turbo.i686                       1.2.90-5.el7                  base

libjpeg-turbo.x86_64                     1.2.90-5.el7                  base

libjpeg-turbo-devel.i686                 1.2.90-5.el7                  base

libjpeg-turbo-devel.x86_64               1.2.90-5.el7                  base

libjpeg-turbo-static.i686                1.2.90-5.el7                  base

libjpeg-turbo-static.x86_64              1.2.90-5.el7                  base

libjpeg-turbo-utils.x86_64               1.2.90-5.el7                  base

mingw32-libjpeg-turbo.noarch             1.3.1-4.el7                   epel

mingw32-libjpeg-turbo-static.noarch      1.3.1-4.el7                   epel

mingw64-libjpeg-turbo.noarch             1.3.1-4.el7                   epel

mingw64-libjpeg-turbo-static.noarch      1.3.1-4.el7                   epel

[root@am-01:/usr/local/src/php-5.6.30#] yum -y install libjpeg-turbo-devel.x86_64

已加载插件:fastestmirror

Loading mirror speeds from cached hostfile

* epel: mirror01.idc.hinet.net

………………省略部分输出信息………………

作为依赖被安装:

libjpeg-turbo.x86_64 0:1.2.90-5.el7



完毕!


#安装完 libjpeg-turbo-devel 后继续编译安装 php5
configure: error: png.h not found.


#这次提示这个错误,安装 libpng-devel 包
[root@am-01:/usr/local/src/php-5.6.30#] yum list | grep png

libpng.i686                              2:1.5.13-7.el7_2              base

libpng.x86_64                            2:1.5.13-7.el7_2              base

libpng-devel.i686                        2:1.5.13-7.el7_2              base

libpng-devel.x86_64                      2:1.5.13-7.el7_2              base

libpng-static.i686                       2:1.5.13-7.el7_2              base

libpng-static.x86_64                     2:1.5.13-7.el7_2              base

libpng12.i686                            1.2.50-10.el7                 base

libpng12.x86_64                          1.2.50-10.el7                 base

libpng12-devel.i686                      1.2.50-10.el7                 base

libpng12-devel.x86_64                    1.2.50-10.el7                 base

mingw32-libpng.noarch                    1.6.21-1.el7                  epel

mingw32-libpng-static.noarch             1.6.21-1.el7                  epel

mingw64-libpng.noarch                    1.6.21-1.el7                  epel

mingw64-libpng-static.noarch             1.6.21-1.el7                  epel

optipng.x86_64                           0.7.4-4.el7                   base

pngcrush.x86_64                          1.7.59-4.el7                  base

pnglite.x86_64                           0.1.17-1.el7.8                epel

pnglite-devel.x86_64                     0.1.17-1.el7.8                epel

pngnq.x86_64                             1.1-9.el7                     base

pngquant.x86_64                          2.7.2-1.el7                   epel

python-pypng.noarch                      0.0.16-1.el7                  epel

texlive-dvipng.noarch                    2:svn26689.1.14-38.el7        base

texlive-dvipng-bin.x86_64                2:svn26509.0-38.20130427_r30134.el7

texlive-dvipng-doc.noarch                2:svn26689.1.14-38.el7        base

[root@am-01:/usr/local/src/php-5.6.30#] yum -y install libpng-devel.x86_64

已加载插件:fastestmirror

Loading mirror speeds from cached hostfile

* epel: mirror01.idc.hinet.net

正在解决依赖关系

………………省略部分输出信息………………

已安装:

libpng-devel.x86_64 2:1.5.13-7.el7_2



作为依赖被安装:

libpng.x86_64 2:1.5.13-7.el7_2



完毕!


#安装完 libpng-devel 后继续编译安装 php5
configure: error: freetype-config not found.


#这次提示这个错误,安装 freetype-devel 包
[root@am-01:/usr/local/src/php-5.6.30#] yum list | grep freetype

freetype.x86_64                          2.4.11-9.el7                  @anaconda

freetype.i686                            2.4.11-15.el7                 base

freetype.x86_64                          2.4.11-15.el7                 base

freetype-demos.x86_64                    2.4.11-15.el7                 base

freetype-devel.i686                      2.4.11-15.el7                 base

freetype-devel.x86_64                    2.4.11-15.el7                 base

mingw32-freetype.noarch                  2.5.4-1.el7                   epel

mingw32-freetype-static.noarch           2.5.4-1.el7                   epel

mingw64-freetype.noarch                  2.5.4-1.el7                   epel

mingw64-freetype-static.noarch           2.5.4-1.el7                   epel

python-freetype.noarch                   0.4.2-2.el7                   epel

[root@am-01:/usr/local/src/php-5.6.30#] yum -y install freetype-devel.x86_64

已加载插件:fastestmirror

Loading mirror speeds from cached hostfile

* epel: mirror01.idc.hinet.net

正在解决依赖关系

………………省略部分输出信息………………

已安装:

freetype-devel.x86_64 0:2.4.11-15.el7



作为依赖被升级:

freetype.x86_64 0:2.4.11-15.el7



完毕!


#安装完 freetype-devel 后继续编译安装 php5
[root@am-01:/usr/local/src#] ./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

………………省略部分输出信息………………

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

[root@am-01:/usr/local/src/php-5.6.30#] echo $?

0


#这一次./configure 成功了,没有错误提示
[root@am-01:/usr/local/src/php-5.6.30#] make

[root@am-01:/usr/local/src/php-5.6.30#] make install

[root@am-01:/usr/local/src/php-5.6.30#] echo $?

0


#编译安装 php5,测试是正常安装的
[root@am-01:/usr/local/src/php-5.6.30#] cp php.ini-production /usr/local/php/etc/php.ini


#把 php5 的配置文件拷贝到编译时所指定的目录中,其中 php.ini-production 用在线上、生产环境, php.ini-development 用在测试、开发环境
[root@am-01:/usr/local/src/php-5.6.30#] ls /usr/local/php/

bin  etc  include  lib  php


#核心的二进制文件在 bin 目录下
[root@am-01:/usr/local/src/php-5.6.30#] ls /usr/local/apache2.4/modules/libphp5.so

/usr/local/apache2.4/modules/libphp5.so

[root@am-01:/usr/local/src/php-5.6.30#] ls -l /usr/local/apache2.4/modules/libphp5.so

-rwxr-xr-x 1 root root 37752952 2 月  28 23:45 /usr/local/apache2.4/modules/libphp5.so


#这个文件为 httpd 中跟 php5 有关的扩展模块
[root@am-01:/usr/local/src/php-5.6.30#] /usr/local/php/bin/php -m

[PHP Modules]

bz2

Core

ctype

date

dom

ereg

exif

fileinfo

filter

gd

hash

iconv

json

libxml

mbstring

mcrypt

mysql

mysqli

openssl

pcre

PDO

pdo_mysql

pdo_sqlite

Phar

posix

Reflection

session

SimpleXML

soap

sockets

SPL

sqlite3

standard

tokenizer

xml

xmlreader

xmlwriter

zlib



[Zend Modules]


#查看 php 所加载的模块
[root@am-01:/usr/local/src/php-5.6.30#] /usr/local/apache2.4/bin/apachectl -M

AH00557: httpd: apr_sockaddr_info_get() failed for am-01

AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message

Loaded Modules:

core_module (static)

so_module (static)

http_module (static)

mpm_event_module (static)

authn_file_module (shared)

authn_core_module (shared)

authz_host_module (shared)

authz_groupfile_module (shared)

authz_user_module (shared)

authz_core_module (shared)

access_compat_module (shared)

auth_basic_module (shared)

reqtimeout_module (shared)

filter_module (shared)

mime_module (shared)

log_config_module (shared)

env_module (shared)

headers_module (shared)

setenvif_module (shared)

version_module (shared)

unixd_module (shared)

status_module (shared)

autoindex_module (shared)

dir_module (shared)

alias_module (shared)

php5_module (shared)


#这里可以看到,php 作为 httpd 的一个扩展模块加载了
[root@am-01:/usr/local/src/php-5.6.30#] vim /usr/local/apache2.4/conf/httpd.conf

#LoadModule dav_module modules/mod_dav.so

LoadModule status_module modules/mod_status.so

LoadModule autoindex_module modules/mod_autoindex.so

#LoadModule info_module modules/mod_info.so

#LoadModule cgid_module modules/mod_cgid.so

#LoadModule dav_fs_module modules/mod_dav_fs.so

#LoadModule vhost_alias_module modules/mod_vhost_alias.so

#LoadModule negotiation_module modules/mod_negotiation.so

LoadModule dir_module modules/mod_dir.so

#LoadModule actions_module modules/mod_actions.so

#LoadModule speling_module modules/mod_speling.so

#LoadModule userdir_module modules/mod_userdir.so

LoadModule alias_module modules/mod_alias.so

#LoadModule rewrite_module modules/mod_rewrite.so

LoadModule php5_module        modules/libphp5.so


#这个文件为 apache 的配置文件,可以看到自动添加了 php5 相关的模块信息,其中带“#”号的语句是指还没启用的模块
[root@am-01:/usr/local/src/php-5.6.30#] /usr/local/php/bin/php -i | less


#可以查看 php 的相关信息,包括了编译参数,配置文件路径

安装 php7

编译安装:

[root@am-01:/usr/local/src/php-5.6.30#] cd /usr/local/src/

[root@am-01:/usr/local/src#] wget http://cn2.php.net/distributions/php-7.1.6.tar.bz2

[root@am-01:/usr/local/src#] tar xvf php-7.1.6.tar.bz2

[root@am-01:/usr/local/src#] cd php-7.1.6/


#下载 php7 的源码包并解压出来
[root@am-01:/usr/local/src/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@am-01:/usr/local/src/php-7.1.6#] make

[root@am-01:/usr/local/src/php-7.1.6#] make install


#编译安装 php7,注意安装目录--prefix 和配置文件所在路径--with-config-file-path 要跟 php5 有所区别,php7 编译安装没有--with-mysql

#php5 和 php7 可以同时存在一台机器,但 apache 调用的时候需要做好指定
[root@am-01:/usr/local/src/php-7.1.6#] cp php.ini-production  /usr/local/php7/etc/php.ini


#把 php7 的配置文件拷贝到编译时所指定的目录中,其中 php.ini-production 用在线上、生产环境, php.ini-development 用在测试、开发环境
[root@am-01:/usr/local/src/php-7.1.6#] ls -l /usr/local/apache2.4/modules/libphp7.so

-rwxr-xr-x 1 root root 38369243 3 月   1 00:31 /usr/local/apache2.4/modules/libphp7.so


#这个文件为 httpd 中跟 php7 有关的扩展模块
[root@am-01:/usr/local/src/php-7.1.6#] /usr/local/php7/bin/php -m

[PHP Modules]

bz2

Core

ctype

date

dom

exif

fileinfo

filter

gd

hash

iconv

json

libxml

mbstring

mcrypt

mysqli

openssl

pcre

PDO

pdo_mysql

pdo_sqlite

Phar

posix

Reflection

session

SimpleXML

soap

sockets

SPL

sqlite3

standard

tokenizer

xml

xmlreader

xmlwriter

zlib



[Zend Modules]


#查看 php7 加载的模块,跟 php5 基本一致
[root@am-01:/usr/local/src/php-7.1.6#] /usr/local/apache2.4/bin/apachectl -M

AH00557: httpd: apr_sockaddr_info_get() failed for am-01

AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1. Set the 'ServerName' directive globally to suppress this message

Loaded Modules:

core_module (static)

so_module (static)

http_module (static)

mpm_event_module (static)

authn_file_module (shared)

authn_core_module (shared)

authz_host_module (shared)

authz_groupfile_module (shared)

authz_user_module (shared)

authz_core_module (shared)

access_compat_module (shared)

auth_basic_module (shared)

reqtimeout_module (shared)

filter_module (shared)

mime_module (shared)

log_config_module (shared)

env_module (shared)

headers_module (shared)

setenvif_module (shared)

version_module (shared)

unixd_module (shared)

status_module (shared)

autoindex_module (shared)

dir_module (shared)

alias_module (shared)

php5_module (shared)

php7_module (shared)


#可以见到 php7 已被加载
[root@am-01:/usr/local/src/php-7.1.6#] vim /usr/local/apache2.4/conf/httpd.conf

#LoadModule dav_module modules/mod_dav.so

LoadModule status_module modules/mod_status.so

LoadModule autoindex_module modules/mod_autoindex.so

#LoadModule info_module modules/mod_info.so

#LoadModule cgid_module modules/mod_cgid.so

#LoadModule dav_fs_module modules/mod_dav_fs.so

#LoadModule vhost_alias_module modules/mod_vhost_alias.so

#LoadModule negotiation_module modules/mod_negotiation.so

LoadModule dir_module modules/mod_dir.so

#LoadModule actions_module modules/mod_actions.so

#LoadModule speling_module modules/mod_speling.so

#LoadModule userdir_module modules/mod_userdir.so

LoadModule alias_module modules/mod_alias.so

#LoadModule rewrite_module modules/mod_rewrite.so

LoadModule php5_module        modules/libphp5.so

LoadModule php7_module        modules/libphp7.so


#可以看到 php7 的扩展模块已经被启用,如果需要用 php7,就把 php5 那一行注释掉

扩展

php 中 mysql,mysqli,mysqlnd,pdo 到底是什么:

http://blog.csdn.net/u013785951/article/details/60876816

查看编译参数:

http://ask.apelearn.com/question/1295