本文是基于Centos7.0搭建的, 安装php, 有两种方式安装,一种是通过源码编译安装,一种是通过yum安装,yum安装很简单, 直接输入yum -y install php即可,默认安装的是php5.4版本的, 这里要安装php5.6, 所以选择源码安装的方式。
安装php之前 ,需要先安装以下依赖:
yum install openssl openssl-devel libxml2-devel libxml2 bzip2 bzip2-devel curl-devel php-mcrypt libmcrypt
libmcrypt-devel readline-devel
其中由于默认的yum源没有 libmcrypt-devel这个包,只能借助epel的yum源,所以先安装epel,再安装libmcrypt:
yum install -y epel-release
yum install -y libmcrypt-devel
如果找不到epel的话,就得更换源或者直接下载libmcrypt源码包编译, 这个很简单,不展示了。
下载php5.6源码:
wget http://mirrors.sohu.com/php/php-5.6.31.tar.gz
解压源码包:
tar xzvf php-5.6.31.tar.gz
cd php-5.6.31
配置编译选项:
./configure
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-opcache --enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-mysql=mysqlnd \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-gettext \
--enable-mbstring \
--with-iconv \
--with-mcrypt \
--with-mhash \
--with-openssl \
--enable-bcmath \
--enable-soap \
--with-libxml-dir \
--enable-pcntl \
--enable-shmop \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-sockets \
--with-curl \
--with-zlib \
--enable-zip \
--with-bz2 \
--with-readline \
--with-apxs2=/usr/bin/apxs
其中,最后一项--with-apxs2=/usr/bin/apxs中apxs是在安装了apache(httpd)之后才存在。
在configure时添加--with-apxs2=/usr/bin/apxs的原因是为了生成libphp5.so给到apache(httpd)使用, 否则apache打开php文件时会出现下载界面,而不做解析,这个在配置apache支持php module的时候会提到, 这里不加此选项也可以,新版本的php都支持动态添加模块, 也就是说, 后面如果需要此模块,再单独configure即可。
configure成功后, 会出现以下内容:
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/fpm/php-fpm.conf
config.status: creating sapi/fpm/init.d.php-fpm
config.status: creating sapi/fpm/php-fpm.service
config.status: creating sapi/fpm/php-fpm.8
config.status: creating sapi/fpm/status.html
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, 经过一段时间的等待, 会看到以下内容:
Build complete.
Don't forget to run 'make test'.
虽然它提示要执行make test, 不过我本人测试过, 会出现以下类似问题:
FAILED TEST SUMMARY
---------------------------------------------------------------------
Bug #52202 (CURLOPT_PRIVATE gets clobbered) [ext/curl/tests/bug52202.phpt]
Bug #64267 (CURLOPT_INFILE doesn't allow reset) [ext/curl/tests/bug64267.phpt]
Bug #71523 (Copied handle with new option CURLOPT_HTTPHEADER crashes while curl_multi_exec) [ext/curl/tests/bug71523.phpt]
Multicast support: IPv6 receive options [ext/sockets/tests/mcast_ipv6_recv.phpt]
sem_acquire with nowait [ext/sysvsem/tests/nowait.phpt]
FPM: Startup and connect [sapi/fpm/tests/002.phpt]
FPM: Test IPv6 support [sapi/fpm/tests/003.phpt]
FPM: Test IPv4/IPv6 support [sapi/fpm/tests/004.phpt]
FPM: Test IPv4 allowed clients [sapi/fpm/tests/005.phpt]
FPM: Test IPv6 allowed clients (bug #68428) [sapi/fpm/tests/006.phpt]
FPM: Test IPv6 all addresses and access_log (bug #68421) [sapi/fpm/tests/007.phpt]
FPM: Test multi pool (dynamic + ondemand + static) (bug #68423) [sapi/fpm/tests/008.phpt]
FPM: Test Unix Domain Socket [sapi/fpm/tests/009.phpt]
FPM: Test IPv4 all addresses (bug #68420) [sapi/fpm/tests/011.phpt]
FPM: Test reload configuration (bug #68442) [sapi/fpm/tests/012.phpt]
FPM: Test for log_level in fpm_unix_init_main #68381 [sapi/fpm/tests/013.phpt]
FPM: Test for pm.start_servers default calculation message being a notice and not a warning #68458 [sapi/fpm/tests/014.phpt]
FPM: Test splited configuration and load order #68391 [sapi/fpm/tests/016.phpt]
FPM: Test fastcgi_finish_request function [sapi/fpm/tests/017.phpt]
FPM: Test global prefix [sapi/fpm/tests/019.phpt]
FPM: Test pool prefix [sapi/fpm/tests/020.phpt]
FPM: HTTP_PROXY - CVE-2016-5385 [sapi/fpm/tests/022-cve-2016-5385.phpt]
目前还没解决, 如果有已经解决的高手, 帮忙告知下, 谢谢!
此问题对使用php影响不大,所以暂时忽略以上问题。
接着继续执行:
make install
成功的话可以看到以下结果:
Installing PHP SAPI module: apache2handler
/usr/lib64/httpd/build/instdso.sh SH_LIBTOOL='/usr/lib64/apr-1/build/libtool' libphp5.la /usr/lib64/httpd/modules
/usr/lib64/apr-1/build/libtool --mode=install install libphp5.la /usr/lib64/httpd/modules/
libtool: install: install .libs/libphp5.so /usr/lib64/httpd/modules/libphp5.so
libtool: install: install .libs/libphp5.lai /usr/lib64/httpd/modules/libphp5.la
libtool: install: warning: remember to run `libtool --finish /home/hwlxhy/software/php-5.6.36/libs'
chmod 755 /usr/lib64/httpd/modules/libphp5.so
[activating module `php5' in /etc/httpd/conf/httpd.conf]
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/
Installing PHP CLI binary: /usr/local/php/bin/
Installing PHP CLI man page: /usr/local/php/php/man/man1/
Installing PHP FPM binary: /usr/local/php/sbin/
Installing PHP FPM config: /usr/local/php/etc/
Installing PHP FPM man page: /usr/local/php/php/man/man8/
Installing PHP FPM status page: /usr/local/php/php/php/fpm/
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 - already installed: 1.4.3
[PEAR] Console_Getopt - already installed: 1.4.1
[PEAR] Structures_Graph- already installed: 1.1.1
[PEAR] XML_Util - already installed: 1.4.2
[PEAR] PEAR - already installed: 1.10.5
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
/home/hwlxhy/software/php-5.6.36/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/
添加环境变量:
vi /etc/profile
在末尾加入:
PATH=$PATH:/usr/local/php/bin
export PATH
使改动立即生效:
source /etc/profile
然后php -v, 可以看到:
PHP 5.6.36 (cli) (built: Jun 29 2018 01:00:26)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
进入php安装包路径, 拷贝配置文件:
cd php-5.6.31
cp php.ini-production /usr/local/php/etc/php.ini
或者
cp php.ini-development /usr/local/php/etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp . /sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod a+x /etc/init.d/php-fpm
此时手动启动php-fpm, 发现报错:
/usr/local/php/sbin/php-fpm
报错:
ERROR: [pool www] cannot get uid for user '@php_fpm_user@'
ERROR: FPM initialization failed
解决办法:
vi /usr/local/php/etc/php-fpm.conf
把 user 和 group 修改为当前用户和用户组即可。
接着将php-fpm服务添加到chkconfig列表:
chkconfig --add php-fpm
设置开机自启动:
chkconfig php-fpm on
以后重启和停止php的方式为:
service php-fpm start
service php-fpm stop
service php-fpm restart
service php-fpm reload
查看是否启动成功: