一,准备工作
实验平台为CentOS6.6,先下载所需的安装包,我使用的是php-5.4.26.tar.gz,下载地址 http://mirrors.sohu.com/php/
编译安装的目录:/usr/local/
二,编译及配置php-5.4.26
1,编译安装php-5.4.26
<code> [root@localhost ~]# tar xf php-5.4.26.tar.gz [root@localhost ~]# cd php-5.4.26/ [root@localhost php-5.4.26]# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib=/usr/local/zlib/ --with-libxml-dir=/usr/local/libxml2/ --enable-xml --enable-sockets --with-mcrypt=/usr/local/libmcrypt/ --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-fpm 选项解释: --prefix=/usr/local/php 指定 php 安装目录 --with-apxs2=/usr/local/apache/bin/apxs 整合 apache,apxs功能是使用mod_so中的LoadModule指令,加载指定模块到 apache,要求 apache 要打开SO模块 --with-config-file-path=/usr/local/php/etc 指定php.ini位置 --with-MySQL=/usr/local/mysql mysql安装目录,对mysql的支持 --with-mysqli=/usr/local/mysql/bin/mysql_config mysqli扩展技术不仅可以调用MySQL的存储过程、处理MySQL事务,而且还可以使访问数据库工作变得更加稳定。 --enable-safe-mode 打开安全模式 --enable-ftp 打开ftp的支持 --enable-zip 打开对zip的支持 --with-bz2 打开对bz2文件的支持 --with-jpeg-dir 打开对jpeg图片的支持 --with-png-dir 打开对png图片的支持 --with-freetype-dir 打开对freetype字体库的支持 --without-iconv 关闭iconv函数,种字符集间的转换 --with-libXML-dir 打开libxml2库的支持 --with-XMLrpc 打开xml-rpc的c语言 --with-zlib-dir 打开zlib库的支持 --with-gd 打开gd库的支持 --enable-gd-native-ttf 支持TrueType字符串函数库 --with-curl 打开curl浏览工具的支持 --with-curlwrappers 运用curl工具打开url流 --with-ttf 打开freetype1.*的支持,可以不加了 --with-xsl 打开XSLT 文件支持,扩展了libXML2库 ,需要libxslt软件 --with-gettext 打开gnu 的gettext 支持,编码库用到 --with-pear 打开pear命令的支持,PHP扩展用的 --enable-calendar 打开日历扩展功能 --enable-mbstring 多字节,字符串的支持 --enable-bcmath 打开图片大小调整,用到zabbix监控的时候用到了这个模块 --enable-sockets 打开 sockets 支持 --enable-exif 图片的元数据支持 --enable-magic-quotes 魔术引用的支持 --disable-rpath 关闭额外的运行库文件 --disable-debug 关闭调试模式 --with-mime-magic=/usr/share/file/magic.mime 魔术头文件位置 CGI方式安装才用的参数 --enable-fpm 打上PHP-fpm 补丁后才有这个参数,CGI方式安装的启动程序 --enable-fastCGI 支持fastcgi方式启动PHP --enable-force-CGI-redirect 重定向方式启动PHP --with-ncurses 支持ncurses 屏幕绘制以及基于文本终端的图形互动功能的动态库 --enable-pcntl freeTDS需要用到的,可能是链接mssql 才用到 mhash和mcrypt算法的扩展 --with-mcrypt 算法 --with-mhash 算法 </code>
出现错误:
<code> configure: error: xml2-config not found. Please check your libxml2 installation. </code>
解决方法:
去官网 http://xmlsoft.org/sources/下载源码。我用的版本是libxml2-2.9.0.tar.gz, 执行命令:
<code> [root@localhost ~]# tar -zxvf libxml2-2.9.0.tar.gz [root@localhost ~]# cd libxml2-2.9.0/ [root@localhost ~]# ./configure --prefix=/usr/local/libxml2/ [root@localhost ~]# make && make install </code>
重新编译,编译时修改选项为–with-libxml-dir=/usr/local/libxml2,
<code> [root@localhost php-5.4.26]# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib=/usr/local/zlib/ --with-libxml-dir=/usr/local/libxml2/ --enable-xml --enable-sockets --with-mcrypt=/usr/local/libmcrypt/ --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-fpm </code>
出现错误:
<code> configure: error: Cannot find libz </code>
解决方法:
去网址 http://www.zlib.net/下载zlib源码,我下的是zlib-1.2.8.tar.gz,执行命令:
<code> [root@localhost ~]# tar -zxvf zlib-1.2.8.tar.gz [root@localhost ~]# cd zlib-1.2.8/ [root@localhost ~]# ./configure --prefix=/usr/local/zlib [root@localhost ~]# mak && make install </code>
重新编译,编译时修改选项为–with-zlib-dir=/usr/local/zlib
<code> [root@localhost php-5.4.26]# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib=/usr/local/zlib/ --with-libxml-dir=/usr/local/libxml2/ --enable-xml --enable-sockets --with-mcrypt=/usr/local/libmcrypt/ --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-fpm </code>
出现错误:
<code> configure: error: Please reinstall the BZip2 distribution </code>
解决方法:
去网站 http://www.bzip.org/downloads.html下载源码,我用的是bzip2-1.0.6.tar.gz ,执行命令:
<code> [root@localhost ~]# tar -zxvf bzip2-1.0.6.tar.gz [root@localhost ~]# cd bzip2-1.0.6/ [root@localhost ~]# make && make install ---->没有configure文件,此处不用执行./configure , </code>
重新编译,不用修改选项:
<code> [root@localhost php-5.4.26]# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib=/usr/local/zlib/ --with-libxml-dir=/usr/local/libxml2/ --enable-xml --enable-sockets --with-mcrypt=/usr/local/libmcrypt/ --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-fpm </code>
出现错误:
<code> configure: error: mcrypt.h not found. Please reinstall libmcrypt. </code>
解决方法:
去网站 http://mcrypt.hellug.gr/lib/index.html的下载源码,我下的是libmcrypt-2.5.8.tar.bz2 ;执行命令:
<code> [root@localhost ~]# tar -zxvf libmcrypt-2.5.8.tar.bz2 [root@localhost ~]# cd libmcrypt-2.5.8/ [root@localhost ~]# ./configure prefix=/usr/local/libmcrypt/ [root@localhost ~]# make && make install </code>
重新编译,修改参数为–with-mcrypt=/usr/local/libmcrypt
<code> [root@localhost php-5.4.26]# ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib=/usr/local/zlib/ --with-libxml-dir=/usr/local/libxml2/ --enable-xml --enable-sockets --with-mcrypt=/usr/local/libmcrypt/ --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-fpm 。。。。。。。。 。。。。。。。。 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. </code>
这次编译就不会再出问题了,出现以上界面就是成功了。然后执行:
<code> [root@localhost php-5.4.26]# make && make install </code>
为php提供配置文件,php源码解压后的目录里有两个文件php.ini-development 和php.ini-production ,一个开发环境下使用的,一个为生产环境下使用的,我们需要的是php.ini-production:
<code> [root@localhost php-5.4.26]# cp php.ini-production /etc/php.ini </code>
2,配置php-fpm
为php-fpm提供Sysv init脚本,并将其添加至服务列表:
<code> [root@localhost php-5.4.26]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm [root@localhost php-5.4.26]# chmod +x /etc/init.d/php-fpm [root@localhost php-5.4.26]# chkconfig --add php-fpm [root@localhost php-5.4.26]# chkconfig php-fpm on </code>
为php-fpm提供配置文件:
<code> [root@localhost php-5.4.26]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf </code>
编辑php-fpm的配置文件,配置fpm的相关选项为你所需要的值,并启用pid文件:
<code> [root@localhost php-5.4.26]# vim /usr/local/php/etc/php-fpm.conf 25 pid = run/php-fpm.pid #定义pid文件,在/usr/local/php/var/run/,启用即可,不需修改,要修改必须和/etc/rc.d/init.d/php-fpm中的定义一块修改。 225 pm.max_children = 100 #最多有多少个子进程 230 pm.start_servers = 10 #刚开始启动多少个空闲进程 235 pm.min_spare_servers = 5 240 pm.max_spare_servers = 20 </code>
接下来就可以启动php-fpm了:
<code> [root@localhost conf]# service php-fpm start Starting php-fpm done </code>
其监听在127.0.0.1:9000,会启动n个空闲进程
<code> [root@localhost feiyu.com]# netstat -tlnup |grep php-fpm tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 16325/php-fpm </code>
使用如下命令来验正(如果此命令输出有中几个php-fpm进程就说明启动成功了):
<code> [root@localhost feiyu.com]# ps aux |grep php-fpm |wc -l 12 </code>
3、 编辑apache配置文件httpd.conf,使apache支持php
<code> [root@localhost ~]# vim /etc/httpd/httpd.conf #添加如下二行 381 AddType application/x-httpd-php .php #支持.php结尾的文件 382 AddType application/x-httpd-php-source .phps #支持源码文件 </code>
定位至DirectoryIndex index.html ,修改为:
<code> 251 DirectoryIndex index.php index.html </code>
未配置此项则会显示源码,如下图:
而后重新启动httpd,或让其重新载入配置文件即可测试php是否已经可以正常使用。
四、安装xcache,为php加速,(注:2.0以后的版本对于php5.0以后的支持)
1、安装,本次使用的是xcache-3.2.0.tar.gz
<code> [root@localhost ~]# tar xf xcache-3.2.0.tar.gz [root@localhost ~]# cd xcache-3.2.0 [root@localhost xcache-3.2.0]# /usr/local/php/bin/phpize #php有很多扩展功能,用phpize给PHP动态添加扩展 Configuring for: PHP Api Version: 20100412 Zend Module Api No: 20100525 Zend Extension Api No: 220100525 如果出现下面的情况,此时需要安装m4和autoconf这两个包! [root@localhost xcache-3.2.0]# /usr/local/php/bin/phpize Configuring for: PHP Api Version: 20100412 Zend Module Api No: 20100525 Zend Extension Api No: 220100525 Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable. Then, rerun this script. [root@localhost xcache-3.2.0]# yum install autoconf m4 -y [root@localhost xcache-3.2.0]# /usr/local/php/bin/phpize #重新执行此步骤 [root@localhost xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config [root@localhost xcache-3.2.0]# make [root@www xcache-3.2.0]# make install 。。。。。 Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20100525/ ------>最后一行会出现 </code>
2、编辑php.ini,整合php和xcache:使php支持xcache的功能
首先将xcache提供的样例配置导入php.ini
<code> [root@localhost xcache-3.2.0] # mkdir /etc/php.d [root@localhost xcache-3.2.0] # cp xcache.ini /etc/php.d </code>
说明:xcache.ini文件在xcache的源码目录中。
接下来编辑/etc/php.d/xcache.ini,找到zend_extension开头的行,修改为如下行:
<code> extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so ----->复制make install 后出现的代码 </code>
注意:如果php.ini文件中有多条extension指令行,要确保此新增的行排在第一位。
然后重启php-fpm生效
(注:xcache-3.2.0已将zend_extension改为extension,也不用编辑此行了,它能自动找到并生效)。
五,测试
1,首先测试php是否成功连接到数据库
首先启动数据库,设置数据库密码为123:
<code> [root@localhost html]# service mysqld start [root@localhost html]# mysqladmin -uroot password '123' [root@localhost html]# cd /var/www/html/ </code>
[root@localhost html]# vim index.php
<?php
$link = mysql_connect(“192.168.0.147″,”root”,”123456″);
if(!$link)
echo “failed”;
else
echo “success”;
?>
刷新浏览器,出现success则成功:
2,再测试php是否成功启用
<code> [root@localhost html]# vim index.php </code>
<?php
phpinfo();
?>
然后打开网页,输入本机IP,出现以下画面则成功。
3,测试xcache
重新启动php-fpm,
<code> [root@localhost ~]# service php-fpm restart Gracefully shutting down php-fpm . done Starting php-fpm done </code>
刷新网页找xcache模块,
<code> [root@localhost ~]# netstat -tlnup Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1848/sshd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1723/cupsd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2062/master tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 16897/php-fpm tcp 0 0 :::22 :::* LISTEN 1848/sshd tcp 0 0 ::1:631 :::* LISTEN 1723/cupsd tcp 0 0 ::1:25 :::* LISTEN 2062/master tcp 0 0 :::3306 :::* LISTEN 24509/mysqld tcp 0 0 :::80 :::* LISTEN 16368/httpd udp 0 0 0.0.0.0:68 0.0.0.0:* 24809/dhclient udp 0 0 0.0.0.0:631 0.0.0.0:* 1723/cupsd </code>
到此,LAMP全部编译完成,当然还没有编译Linux,后续会继续更新。
在测试php中会可能出现以下问题:
<code> [root@sta wordpress]# tail /var/log/nginx/error.log 2015/09/07 15:54:31 [error] 2155#0: *1194 FastCGI sent in stderr: "Access to the script '/usr/html/wordpress/wp-admin/js/language-chooser.min.js' has been denied (see security.limit_extensions)" while reading response header from upstream, client: 222.24.51.74, server: www.xiyousta.com, request: "GET /wordpress/wp-admin/js/language-chooser.min.js?ver=4.3 HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "222.24.51.147", referrer: "http://222.24.51.147/wordpress/wp-admin/install.php" </code>
经过查找资料发现 从5.3.9开始,php官方加入了一个配置”security.limit_extensions”,默认状态下只允许执行扩展名为”.php”的文件,造成了其他类型的文件不支持的问题。
所以更改策略如下:
修改/usr/local/php/etc/php-fpm.conf,找到security.limit_extensions把他修改为:
<code> security.limit_extensions=.php .html .js .css .jpg .jpeg .gif .png .htm#(常用的文件扩展名) </code>
然后问题就解决啦!
附:开启php的gd库
一、下载软件
gd-2.0.35.tar.gz http://www.boutell.com/gd/
jpegsrc.v6b.tar.gz http://www.ijg.org/
libpng-1.2.18.tar.gz http://sourceforge.net/projects/libpng/
zlib-1.2.3.tar.gz http://sourceforge.net/projects/zlib/
freetype-2.4.8.tar.gz http://sourceforge.net/projects/freetype/
php-5.3.5.tar.gz http://www.php.net
二、安装步骤:
先安装zlib,freetype,libpng,jpeg,再装GD,再装PHP,安装gd之前可能会需要gettext-devel库的支持。
三、安装开始
1、安装libpng
[root@localhost opt]# tar -zxvf libpng-1.2.18.tar.gz
[root@localhost opt]# cd libpng-1.2.18
[root@localhost libpng-1.2.18]# cp scripts/makefile.linux makefile
[root@localhost libpng-1.2.18]# make
[root@localhost libpng-1.2.18]# make install
注意,这里的makefile不是用./configure生成,而是直接从scripts/里拷一个
2、安装zlib
[root@localhost opt]# tar -zxvf zlib-1.2.3.tar.gz
[root@localhost opt]# cd zlib-1.2.3
[root@localhost zlib-1.2.3]# ./configure –prefix=/usr/local/zlib –shared
[root@localhost zlib-1.2.3]# make
[root@localhost zlib-1.2.3]# make install
[root@localhost zlib-1.2.3]# cp /usr/local/zlib-1.2.3/zlib.h /usr/include/zlib.h
3、安装freetype
[root@localhost opt]# tar -zxvf freetype-2.4.8.tar.gz
[root@localhost opt]# cd freetype-2.4.8
[root@localhost freetype-2.4.8]# ./configure –prefix=/usr/local/freetype
[root@localhost freetype-2.4.8]# make
[root@localhost freetype-2.4.8]# make install
4、安装jpeg支持
[root@localhost opt]# tar -zxvf jpegsrc.v6b.tar.gz
[root@localhost opt]# cd jpeg-6b/
[root@localhost jpeg-6b]# ./configure –prefix=/usr/local/jpeg6 –enable-shared
[root@localhost jpeg-6b]# cp /usr/bin/libtool ./
[root@localhost jpeg-6b]# mkdir /usr/local/man/man1
[root@localhost jpeg-6b]# make
[root@localhost jpeg-6b]# mkdir /usr/local/jpeg6
[root@localhost jpeg-6b]# mkdir -p /usr/local/jpeg6/include
[root@localhost jpeg-6b]# mkdir -p /usr/local/jpeg6/lib
[root@localhost jpeg-6b]# mkdir -p /usr/local/jpeg6/bin
[root@localhost jpeg-6b]# mkdir -p /usr/local/jpeg6/man/man1
[root@localhost jpeg-6b]# make install-lib
[root@localhost jpeg-6b]# make install
注意:这里configure一定要带–enable-shared参数,不然,不会生成共享库
这个jpeg软件一般默认安装,不指定路径的话,应该很顺利的;但是如果指定安装目录后,就需要我们手动创建很多目录。
上边安装的都是gd库的支持性的插件,下边开始安装gd库。
5、安装gd库
[root@localhost opt]# tar -zxvf gd-2.0.35.tar.gz
[root@localhost opt]# cd gd-2.0.35
[root@localhost gd-2.0.35]# ./configure –prefix=/usr/local/gd –with-zlib=/usr/local/zlib/ –with-png –with-freetype=/usr/local/freetype/ –with-
jpeg=/usr/local/jpeg6/
//这里需要指定安装的gd库需要的各个插件的安装目录,若安装时没有指定目录,就使用–with参数。
安装GD可能出现的错误:
当make 时出现
configure.ac:64: error: possibly undefined macro: AM_ICONV
下载gettext-devel库然后安装上就行了:
[root@localhost gd-2.0.35]# yum install gettext-devel -y
[root@localhost gd-2.0.35]# ./configure –prefix=/usr/local/gd –with-zlib=/usr/local/zlib/ –with-png –with-freetype=/usr/local/freetype/ –with-jpeg=/usr/local/jpeg6/
[root@localhost gd-2.0.35]# make
[root@localhost gd-2.0.35]# make install
这样gd库就安装完毕了,
6、安装PHP5
[root@localhost opt]# tar -zxvf php-5.3.5.tar.gz
[root@localhost opt]# cd php-5.3.5
[root@localhost php-5.3.5]# ./configure –prefix=/usr/local/php –with-apxs2=/usr/local/apache2/bin/apxs –with-png-dir –with-zlib-dir=/usr/local/zlib/ –with-freetype-dir=/usr/local/freetype/ –with-jpeg-dir=/usr/local/jpeg6/ –with-gd=/usr/local/gd/
[root@localhost php-5.3.5]# make
[root@localhost php-5.3.5]# make install
这php+gd就安装完了。
或者只编译安装gd库:
cd php-5.3.6
cd ext
cd gd
然后再这个目录执行命令 /usr/local/php/bin/phpize。什么时候需要用到 phpize 呢?当我们需要再加些模块,又不想重新编译php,这些我们就可以用phpize了。我的PHP安装在 /usr/local/php/ 这个目录里,可以根据个人情况修改。
/usr/local/php/bin/phpize
现在可以进行单独编译安装了:
view sourceprint?
./configure –with-php-configure=/usr/local/php/bin/php-config –with-jpeg-dir=/usr/local/jpeg –with-png-dir=/usr/local/libpng –with-freetype-dir=/usr/local/freetype
或者
./configure –with-php-config=/usr/local/php/bin/php-config –with-jpeg-dir=/usr/local/jpeg –with-png-dir=/usr/local/libpng –with-freetype-dir=/usr/local/freetype
make && make install
接着修改php.ini文件 在 ;extension=php_zip.dll 下面添加一行 extension=gd.so
重启 Nginx 之后再看看是否安装成功了。再执行 /usr/local/php/bin/php -m 查看gd模块是否加载成功。
service nginx restart
/usr/local/php/bin/php -m
或者使用方法二:
cd /usr/local/src/php-5.2.6/ext/curl/
/usr/local/php-fcgi/bin/phpize
./configure –with-php-config=/usr/local/php/bin/php-config
make && make install
然后将生成的文件路径复制到/etc/php.d/gd.ini中:
extension=/usr/local/php-5.6/lib/php/extensions/no-debug-non-zts-20131226/gd.so