如何源码编译安装PHP7
PHP7于2015年12月3日发布正式版。有的发行版可以用第三方的PHP包安装,但大多数发行版现在都没有打包。如果你现在就想感受一下PHP7的性能,那么请看下面的步骤用源码编译安装PHP7。这篇文章也适用于其他PHP版本的编译安装,只要修改一下命令中的PHP版本号。
安装GCC编译器以及其他依赖
CentOS7
sudo yum group install "Development Tools"
Debian/Ubuntu
sudo apt-get install build-essential autoconf automake libtool bison re2c
删除PHP5
CentOS
sudo yum remove php php-common php-cli php-fpm php-mysql php-pdo
Debian/Ubuntu
sudo apt-get purge php5*
下载PHP7源码
你可以用两种方式下载PHP7源码,一是用Git克隆PHP7,二是从PHP官网下载页面下载。我推荐使用Git clone.
git clone http://git.php.net/repository/php-src.git cd php-src git checkout PHP-7.0
进行编译安装
./buildconf //用来生成configure脚本
./configure \ --prefix=/usr \ --with-config-file-path=/etc \ --enable-mbstring \ --enable-zip \ --enable-bcmath \ --enable-pcntl \ --enable-ftp \ --enable-exif \ --enable-calendar \ --enable-sysvmsg \ --enable-sysvsem \ --enable-sysvshm \ --enable-wddx \ --with-curl \ --with-mcrypt \ --with-iconv \ --with-gmp \ --with-pspell \ --with-gd \ --with-jpeg-dir=/usr \ --with-png-dir=/usr \ --with-zlib-dir=/usr \ --with-xpm-dir=/usr \ --with-freetype-dir=/usr \ --with-t1lib=/usr \ --enable-gd-native-ttf \ --enable-gd-jis-conv \ --with-openssl \ --with-pdo-mysql=/usr \ --with-gettext=/usr \ --with-zlib=/usr \ --with-bz2=/usr \ --with-recode=/usr \ --with-mysqli=/usr/bin/mysql_config
在configure的时候会出现很多常见的错误,所以呢,我先告诉你解决的办法。安装下面的dev或devel包就行了。如果你想查看每个错误对应的解决办法,那么请看文章的结尾。
CentOS安装devel包
sudo yum install libxml2-devel openssl-devel bzip2-devel curl-devel libjpeg-devel libpng-devel libXpm-devel freetype-devel gmp-devel libmcrypt-devel mysql-devel aspell-devel recode-devel
Debian/Ubuntu安装dev包
sudo apt-get install libxml2-dev libssl-dev libbz2-dev libjpeg-dev libpng-dev libxpm-dev libfreetype6-dev libgmp-dev libgmp3-dev libmcrypt-dev libmysqlclient15-dev libpspell-dev librecode-dev
再用make编译安装
make -jN
make test
sudo make install
-jN代表N个job。用CPU的数量替换N。如果你的CPU是4核的,那么make -j4。如果在执行第一个make命令时出现编译错误,那么可以执行make clean命令,再用make重新编译。
configure时的常见错误
xml2-config not found
如果configure时出现 error: xml2-config not found. 错误,那么要行安装libxml2-dev
Debian/Ubuntu
sudo apt-get install libxml2-dev
CentOS
sudo yum install libxml2-devel
Cannot find OpenSSL’s <evp.h>
如果出现 error: Cannot find OpenSSL’s <evp.h> 错误,那么执行下面的命令
Debian/Ubuntu
sudo apt-get install libssl-dev
CentOS
sudo yum install openssl-devel
Please reinstall the BZip2 distribution
CentOS:
sudo yum install bzip2-devel
Debian/Ubuntu
sudo apt-get install libbz2-dev
Please reinstall the libcurl distribution
CnetOS
sudo yum install curl-devel
jpeglib.h not found
CentOS
sudo yum install libjpeg-devel
Debian/Ubuntu
sudo apt-get install libjpeg-dev
png.h not found
CentOS
sudo yum install libpng-devel
Debian/Ubuntu
sudo apt-get install libpng-dev
xpm.h not found
CentOS
sudo yum install libXpm-devel
Debian/Ubuntu
sudo apt-get install libxpm-dev
freetype-config not found
CentOS
sudo yum install freetype-devel
Debian/Ubuntu
sudo apt-get install libfreetype6-dev
Unable to locate gmp.h
CentOS
sudo yum install gmp-devel
Debian/Ubuntu
sudo apt-get install libgmp-dev libgmp3-dev ln -s /usr/include/x86_64-linux-gnu/gmp.h /usr/include/gmp.h
mcrypt.h not found. Please reinstall libmcrypt.
CentOS
sudo yum install libmcrypt-devel
Debian/Ubuntu
sudo apt-get install libmcrypt-dev
Please reinstall the mysql distribution
CentOS
sudo yum install mysql-devel
Debian/Ubuntu
sudo apt-get install libmysqlclient15-dev
Cannot find pspell
CentOS
sudo yum install aspell-devel
Debian/Ubuntu
sudo apt-get install libpspell-dev
Can not find recode.h anywhere under /usr /usr/local /usr /opt.
CentOS
sudo yum install recode-devel -y
Debian/Ubuntu
sudo apt-get install librecode-dev