当前位置:首页 > linux教程 > 列表

lnmp环境配置之编译源码安装PHP的方法

发布:smiling 来源: PHP粉丝网  添加日期:2015-04-22 23:45:55 浏览: 评论:0 

PHP是lnmp环境中必不可少的一个关键的东西了,我们不管是nginx,apache要执行php都需要安装php了,下面小编为各位介绍编译源码安装PHP的方法,希望文章可以帮助到各位.

我们使用vagrant建立虚拟环境,这里使用"chef/centos-6.5"这个box,这个box是一个比较纯净的CentOS-6.5系统,关于Vagrant如何使用,请参考Vagrant快速入门.

  1. $ vagrant init chef/centos-6.5 
  2. $ vagrant up 

执行上述命令之后,就已经建立了一个centos-6.5的虚拟机并且启动了,这时我们使用命令ssh连接到虚拟机.

$ vagrant ssh

提示符变成了[vagrant@localhost ~]$,说明成功连接到了虚拟机,接下来,我们就可以开始PHP开发环境的安装配置了.

如果不使用vagrant,可以自己安装一个CentOS系统或者是虚拟机,以下步骤与vagrant没有直接关系.

编译源码安装PHP

首先,下载PHP安装文件,我们使用源码编译安装 PHP 5.4.35,到PHP官网下载PHP安装文件.

  1. $ wget http://jp1.php.net/distributions/php-5.4.35.tar.gz 
  2. $ tar -zxvf php-5.4.35.tar.gz 
  3. $ cd php-5.4.35 

接下来对PHP源码进行编译安装,进入到源码目录之后,执行下列命令安装.

注意:如果需要mysql的话,最好是在变异的时候就提供参数并且指定为使用mysqlnd库,否则单独编译 扩展的形式安装只能使用MySQL Client Library.

  1. $ ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --enable-ftp --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-gettext --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd 

执行上述命令之后,提示如下错误:

configure: error: no acceptable C compiler found in $PATH

这是因为没有安装gcc编译器,我们需要先安装gcc.

$ sudo yum install gcc

安装之后,重新编译,这次出现了新的错误:

configure: error: xml2-config not found. Please check your libxml2 installation.

提示找不到libxml2,没问题,安装一下就行了.

$ sudo yum install libxml2-devel

继续重新编译,编译安装的过程就是不断解决问题的过程,每次遇到问题,我们去解决问题,没有什么是能难道我们的.

configure: error: Cannot find OpenSSL's <evp.h>

因为我们启用了--with-openssl,因此,我们需要安装openssl-devel.

$ sudo yum install openssl-devel

再次编译,提示:

  1. configure: error: Please reinstall the libcurl distribution - 
  2.     easy.h should be in <curl-dir>/include/curl/ 

错误已经说明了,安装一下libcurl.

$ sudo yum install libcurl-devel

继续编译,我们还会遇到如下错误:

configure: error:jpeglib.h not found.

因为我们的编译参数中提供了对GD库的支持,因此需要安装以下几个库.

  1. $ sudo yum install libjpeg libjpeg-devel 
  2. $ sudo yum install libpng libpng-devel 
  3. $ sudo yum install freetype freetype-devel 

安装了这么多lib,总该成功了吧,再次编译,悲剧的是,又报错了:

configure: error: mcrypt.h not found. Please reinstall libmcrypt.

我们还需要安装libmcrypt,这个lib在yum中是没有的,因此需要下载下来,手动编译.

  1. $ wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz 
  2. $ tar -zxvf libmcrypt-2.5.7.tar.gz 
  3. $ cd libmcrypt-2.5.7 
  4. $ ./configure 
  5. $ make 
  6. $ sudo make install 

好了,我们再编译一次,这次一定要成功了,再不成功就不玩了,幸运的是,这次configure成功,一鼓作气,编译安装:

  1. $ make 
  2. $ sudo make install 

一切都顺利的话,我们已经成功编译并且安装了PHP,安装目录在/usr/local/php,最后,我们需要提供php的配置文件php.ini.

  1. $ sudo cp php.ini-development  /usr/local/php/etc/php.ini 
  2. $ sudo mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf 

PHP的安装目录由configure的--prefix=目录参数指定,另外,这里我们搭建的是用于开发的环境,如果需要作为生产环境,则需要注意一些安全性问题,同时,建议不要拷贝php.ini-development文件了,而是拷贝php.ini-production文件.

查看一下PHP的版本:

  1. $ /usr/local/php/bin/php --version 
  2. PHP 5.4.35 (cli) (built: Nov 25 2014 08:23:11) 
  3. Copyright (c) 1997-2014 The PHP Group 
  4. Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies 

为了操作方便,可以将php的bin目录添加到环境变量,编辑~/.bash_profile,在export PATH上面添加下面一行内容:

PATH=$PATH:/usr/local/php/bin

然后执行如下命令:

$ source ~/.bash_profile

这样,我们就可以直接使用命令,而不需要添加目录了.

小技巧:如何查看PHP使用的是哪个配置文件?

  1. $ strace -e open php 2>&1 |grep php.ini 
  2. open("/usr/local/php/bin/php.ini", O_RDONLY) = -1 ENOENT (No such file or directory) 
  3. open("/usr/local/php/etc/php.ini", O_RDONLY) = 3 

如果没有安装strace命令,使用yum install strace 安装即可.

安装扩展

安装完成基本的PHP了,接下来我们需要安装一些符合业务需要的扩展,安装yaf开发框架扩展,执行以下命令,使用pecl进行安装:

$ sudo /usr/local/php/bin/pecl install yaf

不出意外的话,上述命令足以完成yaf的安装,接下来,需要在php.ini文件中启用yaf扩展,编辑/usr/local/php/etc/php.ini,加入以下内容.

extension=yaf.so

安装mysql和mysqli扩展

安装mysql相关扩展,推荐使用mysqlnd库,但是找了半天,实在是没有找到好的办法单独编译mysql扩展使用 mysqlnd库,最后在文档中看到下面这段内容:

  1. The MySQL database extensions must be configured to use the MySQL Client Library. In order to use the MySQL Native Driver, PHP needs to be built specifying that the MySQL database extensions are compiled with MySQL Native Driver support. This is done through configuration options prior to building the PHP source code. 

这里说的是如果安装mysql扩展的话,只能使用MySQL Client Library,百度/谷歌有好多安装教程,如果希望使用mysqlnd库的话,只能在编译PHP的时候指定,因此,好像是只能重新编译PHP了,如果你有好的办法,可以交流交流.

安装eAccelerator扩展

  1. $ wget https://github.com/eaccelerator/eaccelerator/archive/master.zip -O eaccelerator.zip 
  2. $ sudo yum install unzip 
  3. $ unzip eaccelerator.zip 
  4. $ cd eaccelerator-master/ 
  5. $ phpize 
  6. $ ./configure --enable-shared 
  7. $ make 
  8. $ sudo make install 

在php.ini中增加eAccelerator的配置信息:

  1. zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/eaccelerator.so" 
  2. eaccelerator.shm_size="16" 
  3. eaccelerator.cache_dir="/tmp/eaccelerator" 
  4. eaccelerator.enable="1" 
  5. eaccelerator.optimizer="1" 
  6. eaccelerator.check_mtime="1" 
  7. eaccelerator.debug="0" 
  8. eaccelerator.filter="" 
  9. eaccelerator.shm_ttl="0" 
  10. eaccelerator.shm_prune_period="0" 
  11. eaccelerator.shm_only="0" 

执行php -v可以看到:

  1. $ php -v 
  2. PHP 5.4.35 (cli) (built: Nov 25 2014 10:40:18) 
  3. Copyright (c) 1997-2014 The PHP Group 
  4. Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies 
  5.     with eAccelerator v1.0-dev, Copyright (c) 2004-2012 eAccelerator, by eAccelerator 

安装Xdebug扩展:

  1. $ wget http://github.com/xdebug/xdebug/archive/master.zip -O xdebug.zip 
  2. $ unzip xdebug.zip 
  3. $ cd xdebug-master 
  4. $ /usr/local/php/bin/phpize 
  5. $ ./configure --enable-xdebug 
  6. $ make 
  7. $ sudo make install 

接下来配置php.ini,加入该扩展:

  1. zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so" 
  2. xdebug.remote_enable=1 
  3. xdebug.remote_host=localhost 
  4. xdebug.remote_port=9000 
  5. xdebug.remote_connect_back=1 
  6. ;xdebug.remote_autostart=1 

安装OpCache扩展:因为eAccelerator已经没人维护好长时间了,所以,可以考虑使用OpCache.

  1. $ wget http://pecl.php.net/get/zendopcache-7.0.3.tgz 
  2. $ tar -zxvf zendopcache-7.0.3.tgz 
  3. $ cd zendopcache-7.0.3 
  4. $ phpize 
  5. $ make 
  6. $ sudo make install 

接下来需要配置php.ini,启用该扩展.

注意:如果与XDebug一起使用的话,需要确保OpCache在Xdebug之前加载.

  1. zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/opcache.so" 
  2. opcache.memory_consumption=128 
  3. opcache.interned_strings_buffer=8 
  4. opcache.max_accelerated_files=4000 
  5. opcache.revalidate_freq=60 
  6. opcache.fast_shutdown=1 
  7. opcache.enable_cli=1

Tags: lnmp环境配置 安装PHP

分享到: