Centos7上编译安装PHP7
PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言,主要适用于Web开发领域。在linux系统中通常可以使用yum方式安装PHP,但会存在版本老旧固定的问题,我们可以使用编译安装的方式安装最新版的PHP。
PHP通常包括两种安装模式,一种是作为httpd服务模块运行的模式,另一种时成为独立运行并独立监听端口的程序。本文将介绍第二种方式,也即PHP-FPM模式。
1 获取资源包
PHP程序包可以从官网下载最新版,连接为http://www.php.net/downloads.php
,将下载的压缩包导入到linux系统,并将其解压。
[root@Centos7 R2 ~]#ls /app/packages/php-7.1.11.tar.xz
/app/packages/php-7.1.11.tar.xz
[root@Centos7 R2 ~]#cd /app/source/
[root@Centos7 R2 source]#ls
httpd-2.4.28
[root@Centos7 R2 source]#tar -xf /app/packages/php-7.1.11.tar.xz
[root@Centos7 R2 source]#ls
httpd-2.4.28 php-7.1.11
2 准备编译环境
编译安装需要必要的编译组件,以及支持PHP相关功能的开发包,我们在此使用Yum方式一并安装。要注意,由于部分包需要使用EPEL源,因此在安装之前,我们需要配置好Yum源。
[root@Centos7 R2 source]#yum -y groupinstall "development tools"
[root@Centos7 R2 source]#yum -y install bzip2-devel freetype-devel \
gmp-devel libcurl-devel libjpeg-devel libmcrypt-devel libpng-devel \
libxml2-devel libxslt-devel mysql-devel openssl-devel readline-devel
3 编译安装PHP
由于采用FPM模式安装PHP,我们需要在运行configure脚本时,需要使用--enable-fpm
指定该模式,下面是我的安装过程以及配置。
生成编译配置文件:
[root@Centos7 R2 source]#cd php-7.1.11/
[root@Centos7 R2 php-7.1.11]#./configure \
> --prefix=/usr/local/src/php --with-config-file-path=/etc \
> --with-config-file-scan-dir=/etc/php.d --enable-fpm \
> --enable-inline-optimization --disable-debug \
> --disable-rpath --enable-shared --enable-soap \
> --with-libxml-dir --with-xmlrpc --with-openssl \
> --with-mcrypt --with-mhash --with-pcre-regex \
> --with-sqlite3 --with-zlib --enable-bcmath \
> --with-iconv --with-bz2 --enable-calendar \
> --with-curl --with-cdb --enable-dom --enable-exif \
> --enable-fileinfo --enable-filter --with-pcre-dir \
> --enable-ftp --with-gd --with-openssl-dir --with-jpeg-dir \
> --with-png-dir --with-zlib-dir --with-freetype-dir \
> --enable-gd-native-ttf --enable-gd-jis-conv --with-gettext \
> --with-gmp --with-mhash --enable-json --enable-mbstring \
> --enable-mbregex --enable-mbregex-backtrack --with-libmbfl \
> --with-onig --enable-pdo --with-mysqli=mysqlnd \
> --with-pdo-mysql=mysqlnd --with-zlib-dir --with-pdo-sqlite \
> --with-readline --enable-session --enable-shmop \
> --enable-simplexml --enable-sockets --enable-sysvmsg \
> --enable-sysvsem --enable-sysvshm --enable-wddx \
> --with-libxml-dir --with-xsl --enable-zip \
> --enable-mysqlnd-compression-support --with-pear --enable-opcache
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for a sed that does not truncate output... /usr/bin/sed
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
checking for cc... cc
…… ……
编译及安装:
[root@Centos7 R2 php-7.1.11]#make -j 4 && make install
等待编译安装结束
4 准备配置文件及启动脚本文件
编译安装完成后,在启用PHP、服务前,我们还需要准备相关的配置文件及启动脚本,这些文件已经包含在源码解压后的资源中,我们只需要将相关文件拷贝到指定目录即可。
[root@Centos7 R2 php-7.1.11]#cp php.ini-production /etc/php.ini
[root@Centos7 R2 php-7.1.11]#ll /etc/php.ini
-rw-r--r-- 1 root root 71096 Nov 5 21:27 /etc/php.ini
[root@Centos7 R2 php-7.1.11]#cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm #从解压文件拷贝服务脚本文件到/etc目录下
[root@Centos7 R2 php-7.1.11]#chmod +x /etc/init.d/php-fpm #给启动脚本执行权限
[root@Centos7 R2 php-7.1.11]#chkconfig --add php-fpm #设置服务开机启动
[root@Centos7 R2 php-7.1.11]#chkconfig --list php-fpm
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
php-fpm 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@Centos7 R2 php-7.1.11]#cd /usr/local/src/php/etc/
[root@Centos7 R2 etc]#cp php-fpm.conf.default php-fpm.conf
[root@Centos7 R2 etc]#cp php-fpm.d/www.conf.default php-fpm.d/www.conf
[root@Centos7 R2 etc]#ls *
pear.conf php-fpm.conf php-fpm.conf.default
php-fpm.d:
www.conf www.conf.default
5 添加Httpd对PHP的支持
在PHP编译安装完成后,我们还需要针对httpd的配置文件做相应设定以支持PHP程序及相关文件:
[root@Centos7 R2 etc]#cat >> /usr/local/src/httpd/conf/httpd.conf <<eof
> AddType application/x-httpd-php .php
> AddType application/x-httpd-php-source .phps
> ProxyRequests Off
> ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/$1
> eof
[root@Centos7 R2 etc]#tail -n 4 /usr/local/src/httpd/conf/httpd.conf
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
ProxyRequests Off
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/var/www/html/$1
[root@Centos7 R2 etc]#sed -i 's/DirectoryIndex/DirectoryIndex index.php/' /usr/local/src/httpd/conf/httpd.conf
[root@Centos7 R2 etc]#cat /usr/local/src/httpd/conf/httpd.conf | grep "DirectoryIndex"
# DirectoryIndex index.php: sets the file that Apache will serve if a directory
DirectoryIndex index.php index.html
[root@Centos7 R2 etc]#sed -i 's/#LoadModule proxy_fcgi/LoadModule proxy_fcgi/' /usr/local/src/httpd/conf/httpd.conf
[root@Centos7 R2 etc]#sed -i 's/#LoadModule proxy_module/LoadModule proxy_module/' /usr/local/src/httpd/conf/httpd.conf
[root@Centos7 R2 etc]#cat /usr/local/src/httpd/conf/httpd.conf |grep "proxy_fcgi"
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
[root@Centos7 R2 etc]#cat /usr/local/src/httpd/conf/httpd.conf |grep "proxy_module"
LoadModule proxy_module modules/mod_proxy.so
6 启动服务并测试
在测试之前,我们可以编写个PHP页面,然后通过浏览器访问是否可以得到预期的界面:
[root@Centos7 R2 etc]#cat > /var/www/html/index.php << eof
> <?php
> echo phpinfo();
> ?>
> eof
[root@Centos7 R2 etc]#cat /var/www/html/index.php
<?php
echo phpinfo();
?>
测试文件准备好了,我们现在重启httpd服务,启动php-fpm服务,同时检查httpd连接PHP的两个重要模块是否加载:
[root@Centos7 R2 etc]#systemctl start php-fpm
[root@Centos7 R2 etc]#systemctl status php-fpm
● php-fpm.service - LSB: starts php-fpm
Loaded: loaded (/etc/rc.d/init.d/php-fpm; bad; vendor preset: disabled)
Active: active (running) since Sun 2017-11-05 22:12:17 CST; 13min ago
Docs: man:systemd-sysv-generator(8)
Process: 764 ExecStart=/etc/rc.d/init.d/php-fpm start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/php-fpm.service
├─927 php-fpm: master process (/usr/local/src/php/etc/php-fpm.conf)
├─956 php-fpm: pool www
└─957 php-fpm: pool www
Nov 05 22:12:16 Centos7 systemd[1]: Starting LSB: starts php-fpm...
Nov 05 22:12:17 Centos7 php-fpm[764]: Starting php-fpm done
Nov 05 22:12:17 Centos7 systemd[1]: Started LSB: starts php-fpm.
[root@Centos7 R2 etc]#apachectl -k restart
[root@Centos7 R2 etc]#httpd -M |grep proxy
proxy_module (shared)
proxy_fcgi_module (shared)
[root@Centos7 R2 etc]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 10 172.18.42.52:53 *:*
LISTEN 0 10 192.168.25.52:53 *:*
LISTEN 0 10 127.0.0.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 127.0.0.1:953 *:*
LISTEN 0 128 127.0.0.1:9000 *:*
LISTEN 0 80 :::3306 :::*
LISTEN 0 128 :::80 :::*
LISTEN 0 10 ::1:53 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 128 ::1:953 :::*
服务正常,模块加载正常,80端口和9000端口监听正常,我们通过浏览器输入ip访问,可以达到预期的关于PHP安装信息的页面。我们也可以通过elinks -dump
命令得到访问页面返回的结果:
[root@Centos7 R2 etc]#elinks -dump http://192.168.25.52/ | head -n 7
[1]PHP logo
PHP Version 7.1.11
System Linux Centos7 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22
16:42:41 UTC 2016 x86_64
Build Date Nov 5 2017 21:17:32
至此,PHP已独立运行模式编译安装完成。