---
title: "linux安装php"
author: "student_lp"
date: "2014-07-20T14:51:57Z"
tags: []
categories: ["互联网"]
slug: "linux-php"
draft: false
lastmod: "2014-07-20T14:54:04Z"
id: 2094173
url: "https://student-lp.iteye.com/blog/2094173"
---

**一、安装GD，这是php对图片库的支持库。**

①安装freetype（这是一个字体引擎）。

下载：[http://download.chinaunix.net/download/0004000/3295.shtml](http://download.chinaunix.net/download/0004000/3295.shtml)

解压：tar -zxvf freetype-version.tar.gz.

进入：cd freetype-version.

预编译：./configure --prefix=/usr/local/freetype --enable-shared --enable-static；

编译安装：make && make install。

②安装libpng（这些库被其他程式用于读写png文件）。

下载：[http://download.chinaunix.net/download/0006000/5976.shtml](http://download.chinaunix.net/download/0006000/5976.shtml)

 解压：tar -zxvf libpng-version.tar.gz.

进入：cd libpng-version.

预编译：./configure --prefix=/usr/local/libpng --enable-shared --enable-static；

编译安装：make && make install。

③安装jpegsrc(这些库被其他程式用于读写jpg或者jpeg文件)。

下载：[http://download.chinaunix.net/download/0006000/5095.shtml](http://download.chinaunix.net/download/0006000/5095.shtml)

 解压：tar -zxvf jpegsrc.v8d.tar.gz.

进入：cd jpeg-8d.

预编译：./configure --prefix=/usr/local/jpeg --enable-shared --enable-static；

编译安装：make && make install。

④安装gd库（支持 PNG/JPEG/GIF以及其他格式的图像，GD一般被用来创建图表、图形、缩略图以及其他图像相关的处理操作）。

但是安装gd库，需要gettext和gettext-devel包。如果在有网络的情况下，可以运行一下命令：yum -y install gettext gettext-devel；安装这些支持库。如果没有网络，则挂载centos的DVD安装盘，然后打开光盘，在里面有centos文件夹，然后运行：ls |grep gettext；查找出gettext和gettext-devel等rpm包。然后安装：rpm -ivh gettext-version.i386.rpm;rpm -ivh gettext-devel-version.i386.rpm；安装完成。

    安装完成上面的支持包后，开始下载安装gd库。

    下载地址：[http://download.chinaunix.net/download/0004000/3479.shtml](http://download.chinaunix.net/download/0004000/3479.shtml)

    解压：tar -jxvz gd-2.0.35.tar.bz2

    进入：cd gd-2.0.35

    预编译：./configure --prefix=/usr/local/gd --with-png=/usr/local/libpng --with-jpeg=/usr/local/jpeg/ --with-freetype=/usr/local/freetype

    编译安装：make && make install

**二、安装php需要的其他工具包**

①安装php扩展cURL(curl是一款著名的字符界面下的下载工具，支持HTTP、HTTPS、FTP、FTPS、DICT、TELNET、LDAP、FILE,和GOPHER。此外还具有cookies支持、断点续传、FTP上传、密码支持、SSL支持和代理支持等特性。curl同时还提供了一套libcurl的库，开发者可以基于这个库开发其他下载工具。在php中这个扩展库还是非常棒，是fsockopen等等相关的有效的替代品。)

    下载地址：[http://download.chinaunix.net/download/0002000/1943.shtml](http://download.chinaunix.net/download/0002000/1943.shtml)

    解压：tar -jxvf curl-version.tar.bz2

    进入：cd curl-version

    预编译：./configure --prefix=/usr/local/curl

    编译安装：make && make install

②安装libxml库（libxml2软件包提供允许用户操控XML文件的函数库,包含有读、修改和写XML和HTML文件支持）

    下载地址：[http://download.chinaunix.net/download/0007000/6095.shtml](http://download.chinaunix.net/download/0007000/6095.shtml)

     解压：tar -zxvf libxml2-version.tar.gz

     进入：cd libxml2-version

     预编译：./configure --prefix=/usr/local/libxml2

     编译安装：make && make install

③安装libiconv库（主要是解决多语言编码处理转换等应用问题）

    下载地址：[http://download.chinaunix.net/download/0006000/5879.shtml](http://download.chinaunix.net/download/0006000/5879.shtml)

    解压：tar -zxvf libiconv-version.tar.gz

    进入：cd libiconv-version

    预编译：./configure --prefix=/usr/local/libiconv

    编译安装：make && make install

④安装libmcrypt库（libmcrypt是加密算法扩展库。支持DES, 3DES, RIJNDAEL, Twofish, IDEA, GOST, CAST-256, ARCFOUR, SERPENT, SAFER+等算法）

    下载：[http://download.chinaunix.net/download/0006000/5917.shtml](http://download.chinaunix.net/download/0006000/5917.shtml)

    解压：tar -zxvf libmcrypt-version.tar.gz

    进入：cd libmcrypt-version

    预编译：./configure --prefix=/usr/local/libmcrypt

    编译安装：make && make install

⑤安装mhash库（）

    解压：tar -zxvf mhash-0.9.9.9.tar.gz

    进入：cd mhash-version

    预编译：./configure --prefix=/usr/local/mhash

    编译安装： make && make install

⑥安装mcrypt库。在这里，我们需要先安装libmcrypt和mhash。这是因为安装mcrypty需要这两个库的支持。

    解压：tar -zxvf mcrypt-2.6.8.tar.gz

    进入：cd mcrypt-version

    设置mcrypt安装过程中对libmcrypt和mhash的依赖：

        export LD\_LIBRARY\_PATH=/usr/local/libmcrypt/lib/:/usr/local/mhash/lib:$LD\_LIBRARY\_PATH  
        export LDFLAGS="-L/usr/local/mhash/lib/ -I/usr/local/mhash/lib"  
        export CFLAGS="-I/usr/local/mhash/include/"

    预编译：./configure --prefix=/usr/local/mcrypt --with-libmcrypt-prefix=/usr/local/libmcrypt

    编译安装：make && make install。

**三、安装php**

①php安装

    下载php-5.3.\*.tar.gz版本的php。在php-5.3.\*以后的版本中，已经自带了php-fpm管理了。不需要在给php安装php-fpm的补丁了。

    解压：tar -zxvf php-5.3.\*.tar.gz；

    预编译：./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql/ --with-jpeg-dir=/usr/local/jpeg/  --with-freetype-dir=/usr/local/freetype/ --enable-gd-native-ttf --with-png-dir=/usr/local/libpng --enable-soap --enable-sockets --with-curl=/usr/local/curl/ --with-libxml-dir=/usr/local/libxml2/ --with-zlib=/usr/local/zlib/ --enable-mbstring --enable-zip --enable-fpm --disable-debug --with-iconv-dir=/usr/local/libiconv/ --with-gd=/usr/local/gd/ --with-mcrypt=/usr/local/libmcrypt/ --with-openssl=/usr/local/openssl/ --with-mhash=/usr/local/mhash/

    编译安装：make && make install。

②安装完成后设置

由于php5.3内置了fastcgi的支持，即php-fpm。所以配置一下就可以应用php-fpm工具管理fastcgi了。

    生成配置文件：cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf（即将php安装到的目录中的etc下的php-fpm.conf.default复制一份并重命名为php-fpm.conf）

    打开配置文件中的下面几行注释（php-fpm.conf）:

       pid = run/php-fpm.pid   
       pm.start\_servers = 20   
       pm.min\_spare\_servers = 5   
       pm.max\_spare\_servers = 35

    完成上面操作后，就可以启动php-fpm了：/usr/local/php/sbin/php-fpm start（stop|restart）；

**四、设置php-fpm服务每次开机启动项**

  cp  $PHP\_SOURCE\_DIR/sapi/fpm/init.d.php-fpm  /etc/init.d/php-fpm ($PHP\_SOURCE\_DIR--php的安装目录)  
  chmod 700 /etc/init.d/php-fpm   
  chkconfig --add php-fpm   
  chkconfig --level 36 php-fpm on   
  服务方式启动php-fpm   
  service php-fpm restart

**五、安装php的扩展工具**

①安装ZendGuardLoader(从php5.2以后zend optimizer就改名为ZendGuardLoader)

    下载ZendGuardLoader：[http://www.zend.com/products/guard/downloads](http://www.zend.com/products/guard/downloads)

    解压：tar -zxvf ZendGuardLoader-php-5.3-linux-glibc23-i386.tar.gz;

    创建文件夹：mkdir -p /usr/local/Zend/lib；

    复制ZendGuardLoader.so：cp ZendGuardLoader.so /usr/local/Zend/lib/

    在php.ini中添加ZendGuardLoader的扩展信息：

        \[Zend Guard]  
        zend\_extension=/usr/local/zend/ZendGuardLoader.so  
        zend\_loader.enable=1  
        zend\_loader.disable\_licensing=0  
        zend\_loader.obfuscation\_level\_support=3  
        zend\_loader.license\_path=

    重启php-fpm：service php-fpm restart;

注意：在重启php-fpm时有可能遇到以下问题： cannot restore segment prot after reloc: Permission denied。

     遇到以上问题后，不要着急，这是SELinux限制导致的。我们可以先看看当前SELinux的限制状态：getenforce

     如果是强制状态，在这个时候，最简单的方式是关闭强制状态：setenforce 0；有可能带来安装问题。

     但这是就可以正常加载ZendGuardLoader了。成功后，查看phpinfo()。有如下信息

![](http://dl2.iteye.com/upload/attachment/0099/3587/6a569b7b-1936-3116-b011-1da4d944bdec.jpg)  
②php加速器eaccelerator：

    解压：tar jxvf eaccelerator-0.9.6.1.tar.bz2  
    进入：cd eaccelerator-0.9.6.1  
    安装php外挂模块：/usr/local/php/bin/phpize  
    预编译：./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config  
    编译安装：make && make install  
    配置eAccelerator加速PHP：mkdir -p /usr/local/php/eaccelerator\_cache  
编辑php.ini文件：  
\[eaccelerator]  
zend\_extension="/usr/local/php52/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"  
eaccelerator.shm\_size="64"  
eaccelerator.cache\_dir="/usr/local/php52/eaccelerator\_cache"  
eaccelerator.enable="1"  
eaccelerator.optimizer="1"  
eaccelerator.check\_mtime="1"  
eaccelerator.debug="0"  
eaccelerator.filter=""  
eaccelerator.shm\_max="0"  
eaccelerator.shm\_ttl="3600"  
eaccelerator.shm\_prune\_period="3600"  
eaccelerator.shm\_only="0"  
eaccelerator.compress="1"  
eaccelerator.compress\_level="9"  
保存然后重启php-fpm：service php-fpm restart。  
**注意：**1、编译gd-2.0.35.tar.gz时报错：

gcc -DHAVE\_CONFIG\_H -I. -I. -I. -I/usr/local/freetype/include/freetype2 -I/usr/local/freetype/include -I/usr/local/freetype/include -I/usr/local/jpeg/include -g -O2 -MT gd\_png.lo -MD -MP -MF .deps/gd\_png.Tpo -c gd\_png.c -fPIC -DPIC -o .libs/gd\_png.o  
gd\_png.c:16:53: error: png.h: No such file or directory  
gd\_png.c:47: error: expected specifier-qualifier-list before 'jmp\_buf'  
gd\_png.c:54: error: expected ')' before 'png\_ptr'  
gd\_png.c:82: error: expected ')' before 'png\_ptr'  
gd\_png.c:92: error: expected ')' before 'png\_ptr'

解决办法：#vi gd\_png.c

将：（#include "png.h" /* includes zlib.h and setjmp.h \*/）

替换成：（#include "/usr/local/libpng/include/png.h" /* includes zlib.h and setjmp.h \*/）  
然后再make就可以了