编译安装LAMP之php(fpm模块)

一,准备工作
实验平台为CentOS6.6,先下载所需的安装包,我使用的是php-5.4.26.tar.gz,下载地址 http://mirrors.sohu.com/php/

编译安装的目录:/usr/local/

二,编译及配置php-5.4.26

1,编译安装php-5.4.26

  1. [root@localhost ~]# tar xf php-5.4.26.tar.gz
  2. [root@localhost ~]# cd php-5.4.26/
  3. [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
  4. 选项解释:
  5. --prefix=/usr/local/php
  6. 指定 php 安装目录
  7. --with-apxs2=/usr/local/apache/bin/apxs
  8. 整合 apacheapxs功能是使用mod_so中的LoadModule指令,加载指定模块到 apache,要求 apache 要打开SO模块
  9. --with-config-file-path=/usr/local/php/etc
  10. 指定php.ini位置
  11. --with-MySQL=/usr/local/mysql
  12. mysql安装目录,对mysql的支持
  13. --with-mysqli=/usr/local/mysql/bin/mysql_config
  14. mysqli扩展技术不仅可以调用MySQL的存储过程、处理MySQL事务,而且还可以使访问数据库工作变得更加稳定。
  15. --enable-safe-mode 打开安全模式
  16. --enable-ftp 打开ftp的支持
  17. --enable-zip 打开对zip的支持
  18. --with-bz2 打开对bz2文件的支持
  19. --with-jpeg-dir 打开对jpeg图片的支持
  20. --with-png-dir 打开对png图片的支持
  21. --with-freetype-dir 打开对freetype字体库的支持
  22. --without-iconv 关闭iconv函数,种字符集间的转换
  23. --with-libXML-dir 打开libxml2库的支持
  24. --with-XMLrpc 打开xml-rpcc语言
  25. --with-zlib-dir 打开zlib库的支持
  26. --with-gd 打开gd库的支持
  27. --enable-gd-native-ttf 支持TrueType字符串函数库
  28. --with-curl 打开curl浏览工具的支持
  29. --with-curlwrappers 运用curl工具打开url
  30. --with-ttf 打开freetype1.*的支持,可以不加了
  31. --with-xsl 打开XSLT 文件支持,扩展了libXML2 ,需要libxslt软件
  32. --with-gettext 打开gnu gettext 支持,编码库用到
  33. --with-pear 打开pear命令的支持,PHP扩展用的
  34. --enable-calendar 打开日历扩展功能
  35. --enable-mbstring 多字节,字符串的支持
  36. --enable-bcmath 打开图片大小调整,用到zabbix监控的时候用到了这个模块
  37. --enable-sockets 打开 sockets 支持
  38. --enable-exif 图片的元数据支持
  39. --enable-magic-quotes 魔术引用的支持
  40. --disable-rpath 关闭额外的运行库文件
  41. --disable-debug 关闭调试模式
  42. --with-mime-magic=/usr/share/file/magic.mime 魔术头文件位置
  43. CGI方式安装才用的参数
  44. --enable-fpm
  45. 打上PHP-fpm 补丁后才有这个参数,CGI方式安装的启动程序
  46. --enable-fastCGI
  47. 支持fastcgi方式启动PHP
  48. --enable-force-CGI-redirect
  49. 重定向方式启动PHP
  50. --with-ncurses
  51. 支持ncurses 屏幕绘制以及基于文本终端的图形互动功能的动态库
  52. --enable-pcntl freeTDS需要用到的,可能是链接mssql 才用到
  53. mhashmcrypt算法的扩展
  54. --with-mcrypt 算法
  55. --with-mhash 算法

出现错误:

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

解决方法:

去官网 http://xmlsoft.org/sources/下载源码。我用的版本是libxml2-2.9.0.tar.gz, 执行命令:

  1. [root@localhost ~]# tar -zxvf libxml2-2.9.0.tar.gz
  2. [root@localhost ~]# cd libxml2-2.9.0/
  3. [root@localhost ~]# ./configure --prefix=/usr/local/libxml2/
  4. [root@localhost ~]# make && make install

重新编译,编译时修改选项为–with-libxml-dir=/usr/local/libxml2,

  1. [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

出现错误:

  1. configure: error: Cannot find libz

解决方法:

去网址 http://www.zlib.net/下载zlib源码,我下的是zlib-1.2.8.tar.gz,执行命令:

  1. [root@localhost ~]# tar -zxvf zlib-1.2.8.tar.gz
  2. [root@localhost ~]# cd zlib-1.2.8/
  3. [root@localhost ~]# ./configure --prefix=/usr/local/zlib
  4. [root@localhost ~]# mak && make install

重新编译,编译时修改选项为–with-zlib-dir=/usr/local/zlib

  1. [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

出现错误:

  1. configure: error: Please reinstall the BZip2 distribution

解决方法:

去网站 http://www.bzip.org/downloads.html下载源码,我用的是bzip2-1.0.6.tar.gz ,执行命令:

  1. [root@localhost ~]# tar -zxvf bzip2-1.0.6.tar.gz
  2. [root@localhost ~]# cd bzip2-1.0.6/
  3. [root@localhost ~]# make && make install ---->没有configure文件,此处不用执行./configure ,

重新编译,不用修改选项:

  1. [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

出现错误:

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

解决方法:

去网站 http://mcrypt.hellug.gr/lib/index.html的下载源码,我下的是libmcrypt-2.5.8.tar.bz2 ;执行命令:

  1. [root@localhost ~]# tar -zxvf libmcrypt-2.5.8.tar.bz2
  2. [root@localhost ~]# cd libmcrypt-2.5.8/
  3. [root@localhost ~]# ./configure prefix=/usr/local/libmcrypt/
  4. [root@localhost ~]# make && make install

重新编译,修改参数为–with-mcrypt=/usr/local/libmcrypt

  1. [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
  2. 。。。。。。。。
  3. 。。。。。。。。
  4. Generating files
  5. configure: creating ./config.status
  6. creating main/internal_functions.c
  7. creating main/internal_functions_cli.c
  8. +--------------------------------------------------------------------+
  9. | License: |
  10. | This software is subject to the PHP License, available in this |
  11. | distribution in the file LICENSE. By continuing this installation |
  12. | process, you are bound by the terms of this license agreement. |
  13. | If you do not agree with the terms of this license, you must abort |
  14. | the installation process at this point. |
  15. +--------------------------------------------------------------------+
  16. Thank you for using PHP.

这次编译就不会再出问题了,出现以上界面就是成功了。然后执行:

  1. [root@localhost php-5.4.26]# make && make install

为php提供配置文件,php源码解压后的目录里有两个文件php.ini-development 和php.ini-production ,一个开发环境下使用的,一个为生产环境下使用的,我们需要的是php.ini-production:

  1. [root@localhost php-5.4.26]# cp php.ini-production /etc/php.ini

2,配置php-fpm

为php-fpm提供Sysv init脚本,并将其添加至服务列表:

  1. [root@localhost php-5.4.26]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
  2. [root@localhost php-5.4.26]# chmod +x /etc/init.d/php-fpm
  3. [root@localhost php-5.4.26]# chkconfig --add php-fpm
  4. [root@localhost php-5.4.26]# chkconfig php-fpm on

为php-fpm提供配置文件:

  1. [root@localhost php-5.4.26]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

编辑php-fpm的配置文件,配置fpm的相关选项为你所需要的值,并启用pid文件:

  1. [root@localhost php-5.4.26]# vim /usr/local/php/etc/php-fpm.conf
  2. 25 pid = run/php-fpm.pid #定义pid文件,在/usr/local/php/var/run/,启用即可,不需修改,要修改必须和/etc/rc.d/init.d/php-fpm中的定义一块修改。
  3. 225 pm.max_children = 100 #最多有多少个子进程
  4. 230 pm.start_servers = 10 #刚开始启动多少个空闲进程
  5. 235 pm.min_spare_servers = 5
  6. 240 pm.max_spare_servers = 20

接下来就可以启动php-fpm了:

  1. [root@localhost conf]# service php-fpm start
  2. Starting php-fpm done

其监听在127.0.0.1:9000,会启动n个空闲进程

  1. [root@localhost feiyu.com]# netstat -tlnup |grep php-fpm
  2. tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 16325/php-fpm

使用如下命令来验正(如果此命令输出有中几个php-fpm进程就说明启动成功了):

  1. [root@localhost feiyu.com]# ps aux |grep php-fpm |wc -l
  2. 12

3、 编辑apache配置文件httpd.conf,使apache支持php

  1. [root@localhost ~]# vim /etc/httpd/httpd.conf #添加如下二行
  2. 381 AddType application/x-httpd-php .php #支持.php结尾的文件
  3. 382 AddType application/x-httpd-php-source .phps #支持源码文件

定位至DirectoryIndex index.html ,修改为:

  1. 251 DirectoryIndex index.php index.html

未配置此项则会显示源码,如下图:

QQ图片20150520202233

而后重新启动httpd,或让其重新载入配置文件即可测试php是否已经可以正常使用。

四、安装xcache,为php加速,(注:2.0以后的版本对于php5.0以后的支持)

1、安装,本次使用的是xcache-3.2.0.tar.gz

  1. [root@localhost ~]# tar xf xcache-3.2.0.tar.gz
  2. [root@localhost ~]# cd xcache-3.2.0
  3. [root@localhost xcache-3.2.0]# /usr/local/php/bin/phpize #php有很多扩展功能,用phpize给PHP动态添加扩展
  4. Configuring for:
  5. PHP Api Version: 20100412
  6. Zend Module Api No: 20100525
  7. Zend Extension Api No: 220100525
  8. 如果出现下面的情况,此时需要安装m4autoconf这两个包!
  9. [root@localhost xcache-3.2.0]# /usr/local/php/bin/phpize
  10. Configuring for:
  11. PHP Api Version: 20100412
  12. Zend Module Api No: 20100525
  13. Zend Extension Api No: 220100525
  14. Cannot find autoconf. Please check your autoconf installation and the
  15. $PHP_AUTOCONF environment variable. Then, rerun this script.
  16. [root@localhost xcache-3.2.0]# yum install autoconf m4 -y
  17. [root@localhost xcache-3.2.0]# /usr/local/php/bin/phpize #重新执行此步骤
  18. [root@localhost xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
  19. [root@localhost xcache-3.2.0]# make
  20. [root@www xcache-3.2.0]# make install
  21. 。。。。。
  22. Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20100525/ ------>最后一行会出现

2、编辑php.ini,整合php和xcache:使php支持xcache的功能

首先将xcache提供的样例配置导入php.ini

  1. [root@localhost xcache-3.2.0] # mkdir /etc/php.d
  2. [root@localhost xcache-3.2.0] # cp xcache.ini /etc/php.d

说明:xcache.ini文件在xcache的源码目录中。

接下来编辑/etc/php.d/xcache.ini,找到zend_extension开头的行,修改为如下行:

  1. extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache.so ----->复制make install 后出现的代码

注意:如果php.ini文件中有多条extension指令行,要确保此新增的行排在第一位。

然后重启php-fpm生效

(注:xcache-3.2.0已将zend_extension改为extension,也不用编辑此行了,它能自动找到并生效)。

五,测试

1,首先测试php是否成功连接到数据库

首先启动数据库,设置数据库密码为123:

  1. [root@localhost html]# service mysqld start
  2. [root@localhost html]# mysqladmin -uroot password '123'
  3. [root@localhost html]# cd /var/www/html/

[root@localhost html]# vim index.php

<?php

$link = mysql_connect(“192.168.0.147″,”root”,”123456″);

if(!$link)

echo “success”;

else

echo “failer”;

?>

刷新浏览器,出现success则成功:

QQ图片20150520214547

2,再测试php是否成功启用

  1. [root@localhost html]# vim index.php

<?php

phpinfo();

?>

然后打开网页,输入本机IP,出现以下画面则成功。

QQ图片20150520210254

3,测试xcache

重新启动php-fpm,

  1. [root@localhost ~]# service php-fpm restart
  2. Gracefully shutting down php-fpm . done
  3. Starting php-fpm done

刷新网页找xcache模块,

QQ图片20150520210222

  1. [root@localhost ~]# netstat -tlnup
  2. Active Internet connections (only servers)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  4. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1848/sshd
  5. tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1723/cupsd
  6. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2062/master
  7. tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 16897/php-fpm
  8. tcp 0 0 :::22 :::* LISTEN 1848/sshd
  9. tcp 0 0 ::1:631 :::* LISTEN 1723/cupsd
  10. tcp 0 0 ::1:25 :::* LISTEN 2062/master
  11. tcp 0 0 :::3306 :::* LISTEN 24509/mysqld
  12. tcp 0 0 :::80 :::* LISTEN 16368/httpd
  13. udp 0 0 0.0.0.0:68 0.0.0.0:* 24809/dhclient
  14. udp 0 0 0.0.0.0:631 0.0.0.0:* 1723/cupsd

到此,LAMP全部编译完成,当然还没有编译Linux,后续会继续更新。

在测试php中会可能出现以下问题:

  1. [root@sta wordpress]# tail /var/log/nginx/error.log
  2. 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"

经过查找资料发现 从5.3.9开始,php官方加入了一个配置”security.limit_extensions”,默认状态下只允许执行扩展名为”.php”的文件,造成了其他类型的文件不支持的问题。

所以更改策略如下:

修改/usr/local/php/etc/php-fpm.conf,找到security.limit_extensions把他修改为:

  1. security.limit_extensions=.php .html .js .css .jpg .jpeg .gif .png .htm#(常用的文件扩展名)

然后问题就解决啦!

原文地址:https://www.cnblogs.com/mengdeep/p/4850377.html