本文假定已通过《Centos7.4 安装Apache》,成功安装Apache。
PHP官方Apache2.x的安装说明:http://php.net/manual/zh/install.unix.apache2.php
提示:为了方便管理下载后的安装包,建议将需要编译的安装包统一放置在
/usr/src
目录下
cd /usr/src
下载软件包
wget php-7.2.15.tar.bz2 http://cn2.php.net/distributions/php-7.2.15.tar.bz2
也可以在电脑下载好,通过FTP传输到服务器目录
下载地址:http://www.php.net/downloads.php
php-7.2.15.tar.bz2 (sig) [14,708Kb] 07 Feb 2019
c93e7616946a463911818c7e9f9e21276c7793fb8c7cb15877188dd0546d0554
tar -xvf php-7.2.15.tar.bz2
cd php-7.2.15/
yum -y install wget vim pcre pcre-devel openssl openssl-devel httpd-devel libicu-devel gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel ncurses ncurses-devel curl curl-devel krb5-devel libidn libidn-devel openldap openldap-devel nss_ldap cmake boost-devel bison automake libevent libevent-devel gd gd-devel libtool* libxslt libxslt-devel readline readline-devel gmp gmp-devel libcurl libcurl-devel openjpeg-devel
在编译安装前,请先停止Apache服务。
service httpd stop
PHP编译参数注解:http://www.cnblogs.com/hubing/p/3735452.html
在这里可以用各种各样的参数来自定义 PHP,例如启动哪些扩展功能包的支持等。用./configure --help
命令可以列出当前可用的所有参数。
下面的命令会正确匹配 apxs 的路径。如果通过其他方式安装了 Apache,需要相应的调整 apxs 的路径。注意,在有些发行版本中,可能将 apxs 更名为 apxs2。
如果不确定apxs
的具体位置可执行以下命令进行查找:
find / -name 'apxs*'
输出:
/usr/bin/apxs
这个便是apxs文件所在路径
如果查找不到,可能是因为没安装httpd-devel
,运行命令:
yum install httpd-devel
安装完毕后,再次查找apxs
,这次就可以找到其所在位置了
检测配置无误后,运行以下命令:
./configure \
--prefix=/usr/local/php \
--with-apxs2=/usr/bin/apxs \
--with-config-file-path=/usr/local/php/etc \
--enable-mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--enable-pdo \
--with-iconv-dir \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-libzip \
--with-libxml-dir= \
--enable-xml \
--enable-session \
--disable-rpath \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--enable-mbregex \
--enable-mbstring \
--enable-intl \
--enable-pcntl \
--enable-ftp \
--with-gd \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext \
--disable-fileinfo \
--enable-opcache \
--enable-maintainer-zts \
--with-xsl \
--enable-tokenizer
make && make install
如果一切正常的话,会返回以下结果:
Build complete.
Don't forget to run 'make test'.
Installing PHP SAPI module: apache2handler
/usr/lib64/httpd/build/instdso.sh SH_LIBTOOL='/usr/lib64/apr-1/build/libtool' libphp7.la /usr/lib64/httpd/modules
/usr/lib64/apr-1/build/libtool --mode=install install libphp7.la /usr/lib64/httpd/modules/
libtool: install: install .libs/libphp7.so /usr/lib64/httpd/modules/libphp7.so
libtool: install: install .libs/libphp7.lai /usr/lib64/httpd/modules/libphp7.la
libtool: install: warning: remember to run `libtool --finish /usr/src/php-7.2.15/libs'
chmod 755 /usr/lib64/httpd/modules/libphp7.so
[activating module `php7' in /etc/httpd/conf/httpd.conf]
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20170718/
Installing PHP CLI binary: /usr/local/php/bin/
Installing PHP CLI man page: /usr/local/php/php/man/man1/
Installing phpdbg binary: /usr/local/php/bin/
Installing phpdbg man page: /usr/local/php/php/man/man1/
Installing PHP CGI binary: /usr/local/php/bin/
Installing PHP CGI man page: /usr/local/php/php/man/man1/
Installing build environment: /usr/local/php/lib/php/build/
Installing header files: /usr/local/php/include/php/
Installing helper programs: /usr/local/php/bin/
program: phpize
program: php-config
Installing man pages: /usr/local/php/php/man/man1/
page: phpize.1
page: php-config.1
Installing PEAR environment: /usr/local/php/lib/php/
[PEAR] Archive_Tar - installed: 1.4.4
[PEAR] Console_Getopt - installed: 1.4.1
[PEAR] Structures_Graph- installed: 1.1.1
[PEAR] XML_Util - installed: 1.4.3
[PEAR] PEAR - installed: 1.10.7
Wrote PEAR system config file at: /usr/local/php/etc/pear.conf
You may want to add: /usr/local/php/lib/php to your php.ini include_path
/usr/src/php-7.2.15/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin
ln -s -f phar.phar /usr/local/php/bin/phar
Installing PDO headers: /usr/local/php/include/php/ext/pdo/
在安装包目录里执行:
cp php.ini-production /usr/local/php/etc/php.ini
php.ini配置文件路径:/usr/local/php/etc/php.ini
php配置文件里session.save_path
参数也就是session文件存放目录默认是/temp
,这有可能会导致服务器上的其他用户有可能通过该目录的文件列表破解会话,建议将其改为php安装目录下。
找到以下内容并修改:
; where MODE is the octal representation of the mode. Note that this
; does not overwrite the process's umask.
; http://php.net/session.save-path
session.save_path = "/usr/local/php/tmp"
找到以下内容并修改:
; Decides whether PHP may expose the fact that it is installed on the server
; (e.g. by adding its signature to the Web server header). It is no security
; threat in any way, but it makes it possible to determine whether you use PHP
; on your server or not.
; http://php.net/expose-php
expose_php = Off
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = PRC
Apache2.*的配置文件httpd.conf默认位置:/etc/httpd/conf/httpd.conf
LoadModule 达式右边的路径必须指向系统中的 PHP 模块。以上的 make install 命令可能已经完成了这些,但务必要检查。
LoadModule php7_module /usr/lib64/httpd/modules/libphp7.so
在http.conf
文件尾部加入:
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>
添加index.php入口文件格式的支持
在httpd.conf文件里搜索 DirectoryIndex,在其参数后追加index.php:
<IfModule dir_module>
DirectoryIndex index.html index.php
</IfModule>
新建一个 php 环境变量文件:
touch /etc/profile.d/php.sh
打开php.sh
文件并写入:
vim /etc/profile.d/php.sh
PATH=$PATH:/usr/local/php/bin
export PATH
使配置文件生效:
source /etc/profile
然后可以运行:
php -v
输出:
PHP 7.2.15 (cli) (built: Mar 17 2019 14:44:52) ( ZTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
service httpd restart
要养成好习惯,每次编译完后都要把应用包解压出来的文件或目录进行删除。
rm -rf php-7.2.15
到这里,Centos7.4 编译安装PHP7.2就安装配置好了。