从零搭建LNMP环境(一) - 编译源码安装PHP

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

$ vagrant init chef/centos-6.5
$ vagrant up

执行上述命令以后,就已经创建了一个centos-6.5的虚拟机而且启动了,这时咱们使用命令ssh链接到虚拟机。html

$ vagrant ssh

提示符变成了[vagrant@localhost ~]$ ,说明成功链接到了虚拟机。接下来,咱们就能够开始PHP开发环境的安装配置了。mysql

若是不使用vagrant,能够本身安装一个CentOS系统或者是虚拟机,如下步骤与vagrant没有直接关系。git

####编译源码安装PHPgithub

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

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

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

注意,若是须要mysql的话,最好是在变异的时候就提供参数而且指定为使用mysqlnd库,不然单独编译 扩展的形式安装只能使用MySQL Client Library。安全

$ ./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

执行上述命令以后,提示以下错误:bash

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

这是由于没有安装gcc编译器,咱们须要先安装gcc。app

$ 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

再次编译,提示

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

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

$ sudo yum install libcurl-devel

继续编译,咱们还会遇到以下错误

configure: error: jpeglib.h not found.

由于咱们的编译参数中提供了对GD库的支持,所以须要安装如下几个库。

$ sudo yum install libjpeg libjpeg-devel
$ sudo yum install libpng libpng-devel
$ sudo yum install freetype freetype-devel

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

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

咱们还须要安装libmcrypt,这个lib在yum中是没有的,所以须要下载下来,手动编译。

$ wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar.gz
$ tar -zxvf libmcrypt-2.5.7.tar.gz
$ cd libmcrypt-2.5.7
$ ./configure
$ make
$ sudo make install

好了,咱们再编译一次,此次必定要成功了,再不成功就不玩了。。。幸运的是,此次configure成功, 一气呵成,编译安装:

$ make
$ sudo make install

一切都顺利的话,咱们已经成功编译而且安装了PHP,安装目录在/usr/local/php

最后,咱们须要提供php的配置文件php.ini

$ sudo cp php.ini-development  /usr/local/php/etc/php.ini
$ 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的版本:

$ /usr/local/php/bin/php --version
PHP 5.4.35 (cli) (built: Nov 25 2014 08:23:11)
Copyright (c) 1997-2014 The PHP Group
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使用的是哪一个配置文件?

$ strace -e open php 2>&1 |grep php.ini
open("/usr/local/php/bin/php.ini", O_RDONLY) = -1 ENOENT (No such file or directory)
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库,最后在文档中看到下面这段内容:

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扩展

$ wget https://github.com/eaccelerator/eaccelerator/archive/master.zip -O eaccelerator.zip
$ sudo yum install unzip
$ unzip eaccelerator.zip
$ cd eaccelerator-master/
$ phpize
$ ./configure --enable-shared
$ make
$ sudo make install

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

zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/eaccelerator.so"
eaccelerator.shm_size="16"
eaccelerator.cache_dir="/tmp/eaccelerator"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_ttl="0"
eaccelerator.shm_prune_period="0"
eaccelerator.shm_only="0"

执行php -v能够看到

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

#####安装Xdebug扩展

$ wget http://github.com/xdebug/xdebug/archive/master.zip -O xdebug.zip
$ unzip xdebug.zip
$ cd xdebug-master
$ /usr/local/php/bin/phpize
$ ./configure --enable-xdebug
$ make
$ sudo make install

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

zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/xdebug.so"
xdebug.remote_enable=1
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_connect_back=1
;xdebug.remote_autostart=1
相关文章
相关标签/搜索