PHP官网:http://www.php.net/
前面讲到,PHP是作为Apache的一个模块存在的,而PHP的当前主流版本为5.6/7.1,所以我们两个都去安装。
安装PHP5
下载并解压php5源码包:
# cd /usr/local/src
# wget http://cn2.php.net/distributions/php-5.6.36.tar.bz2
# tar jxvf php-5.6.36.tar.bz2
然后配置编译各种参数(进入php5目录):
# cd php-5.6.36
# ./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
关于PHP的编译参数比较多,编译参数可以指定我们需要的功能模块,跟前面的httpd类似。上面的这些参数算是常用的,如果没有特殊要求,直接使用这些参数即可。
另外,apxs是httpd的一个工具,因为有它才会自动把PHP模块安装到httpd的modules目录下,也就是说PHP将会以一个模块的形式和httpd结合在一起工作。
在上一步编译PHP的过程中,我遇到了这些错误:
configure: error: xml2-config not found. Please check your libxml2 installation.
安装所需的包然后继续编译:
# yum install -y libxml2-devel
# ./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
configure: error: Cannot find OpenSSL’s <evp.h>
安装所需的包然后继续编译:
# yum install -y openssl openssl-devel
# ./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
configure: error: Please reinstall the BZip2 distribution
安装所需的包然后继续编译:
# yum install -y bzip2 bzip2-devel
# ./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
configure: error: jpeglib.h not found.
终于不再提示错误,有这样的信息:
# ./configure --prefix=/usr/local/php --with-apxs2=/usr/local/apache2.4/bin/apxs --with-config-file-path=/usr/loc
+--------------------------------------------------------------------+
| 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
make && make install
然后把配置文件放到/usr/local/php/etc/目录下改名为php.ini
cp php.ini-production /usr/local/php/etc/php.ini
# /usr/local/php/bin/php -i
phpinfo()
PHP Version => 5.6.36
安装PHP7
下载并解压php7源码包:
# cd /usr/local/src/
# wget http://cn2.php.net/distributions/php-7.1.6.tar.bz2
# tar jxvf php-7.1.6.tar.bz2
然后配置编译各种参数(进入php7目录):
# cd 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-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
和PHP5有两处不同:./configure --prefix=/usr/local/php7 --with-config-file-path=/usr/local/php7/etc。
+--------------------------------------------------------------------+
| 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 php7.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: main/php_config.h is unchanged
config.status: executing default commands
configure: WARNING: unrecognized options: --with-mysql
make && make install
然后把配置文件放到/usr/local/php/etc/目录下改名为php.ini
cp php.ini-production /usr/local/php7/etc/php.ini
安装完成以后,我们可以查看:
# ls /usr/local/apache2.4/modules/libphp*
/usr/local/apache2.4/modules/libphp5.so /usr/local/apache2.4/modules/libphp7.so
# ls /usr/local/apache2.4/modules/libphp7.so
/usr/local/apache2.4/modules/libphp7.so
# du -sh !$
du -sh /usr/local/apache2.4/modules/libphp7.so
37M /usr/local/apache2.4/modules/libphp7.so
可以看到,已经加载PHP7模块。
# /usr/local/apache2.4/bin/apachectl -M
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)
Apache和PHP结合
httpd的主配置文件是 /usr/local/apache2.4/conf/httpd.conf,对于这个文件,我们要做4处修改:
# vim /usr/local/apache2.4/conf/httpd.conf #做下面修改
ServerName www.example.com:80
<Directory />
AllowOverride none
Require all granted #目的是允许所有请求,如果不做修改,则访问时候会显示403错误
</Directory>
AddType application/x-gzip .gz .tgz
AddType application/x-httpd-php .php #注意php和.php之间有空格
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
可以测试一下配置语法是否正确:
# /usr/local/apache2.4/bin/apachectl -t
然后重新加载配置文件:
# /usr/local/apache2.4/bin/apachectl graceful
我们还需要测试是否正确解析PHP:
# vim /usr/local/apache2.4/htdocs/1.php
<?php
echo "php解析正常";
?>
# curl localhost/1.php
php解析正常[root@localhost php-5.6.36]#