linux 安装php 然后跟nginx整合

php-fpm安装配置

nginx本身不能处理PHP,它只是个web服务器,当接收到请求后,如果是php请求,则发给php解释器处理,并把结果返回给客户端。

nginx一般是把请求发fastcgi管理进程处理,fascgi管理进程选择cgi子进程处理结果并返回被nginx

本文以php-fpm为例介绍如何使nginx支持PHP

一、编译安装php-fpm

什么是PHP-FPM

PHP-FPM是一个PHP FastCGI管理器,是只用于PHP的,可以在 http://php-fpm.org/download下载得到.

PHP-FPM其实是PHP源代码的一个补丁,旨在将FastCGI进程管理整合进PHP包中。必须将它patch到你的PHP源代码中,在编译安装PHP后才可以使用。

新版PHP已经集成php-fpm了,不再是第三方的包了,推荐使用。PHP-FPM提供了更好的PHP进程管理方式,可以有效控制内存和进程、可以平滑重载PHP配置,比spawn-fcgi具有更多优点,所以被PHP官方收录了。在./configure的时候带 –enable-fpm参数即可开启PHP-FPM,其它参数都是配置php的,具体选项含义可以查看这里

安装前准备

添加 epel 源

# rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

安装依赖

# yum install gcc bison bison-devel zlib-devel libmcrypt-devel mcrypt mhash-devel openssl-devel libxml2-devel libcurl-devel bzip2-devel readline-devel libedit-devel sqlite-devel
1 yum -y install gcc gcc-c++ glibc
yum -y install libmcrypt-devel mhash-devel libxslt-devel 
libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel 
zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel 
ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel 
krb5 krb5-devel libidn libidn-devel openssl openssl-devel
 
 cd /usr/local/src
 wget http://php.net/get/php-5.6.30.tar.gz/from/this/mirror
 tar -zvxf php-5.6.30.tar.gz
 cd php-5.6.30

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql --with-iconv-dir=/usr/ --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap=/usr --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --without-pear

make && make install
 

以上就完成了php-fpm的安装

编译的时候出现很多error,如下:
编译error:
1、configure: error: Cannot find OpenSSL's <evp.h>
办法:
yum  install  openssl.x86_64 openssl-devel.x86_64 -y
 
2、configure: error: Please reinstall the libcurl distribution - easy.h should be in <curl-dir>/include/curl/
办法:
yum install libcurl.x86_64 libcurl-devel.x86_64 -y
3、If configure fails try --with-vpx-dir=<DIR> configure: error: jpeglib.h not found.
办法:
yum install libjpeg.x86_64 libpng.x86_64 freetype.x86_64 libjpeg-devel.x86_64 libpng-devel.x86_64 freetype-devel.x86_64 -y
 
在yum这些包的时候,再次执行./configure的时候,再次出错。
然后,又yum一遍,提示出No package libjpeg-devel.x86_64,在yum库中没有找到这个包。
可以利用yum -y update命令更新yum库,更新完成以后,可以再次yum install,如果这一步成功,皆大欢喜。
如果依旧不可以,使用下一种方法,利用yum search 包名(例libjpeg-devel.x86_64可以写成libjpeg),可以搜出一些,可以找到相应其他的包
 
4、configure: error: Cannot find ldap.h
办法:
yum install php-ldap.x86_64 -y
yum install openldap-devel.x86_64 -y
 
5、configure: error: Don’t know how to define struct flock on this system, set –enable-opcache=no
解决方案:
vim /etc/ld.so.conf.d/local.conf  (local.conf可以随便起名称,以.conf为后缀就可)
添加
/usr/local/lib
:wq 保存
ldconfig -v  生效
 
6、error: Cannot find ldap libraries in /usr/lib 这个可能是由于libldap32和libldap64冲突:
解决方法:cp -frp /usr/lib64/libldap* /usr/lib/
在Linux下安装PHP过程中,编译时出现configure: error: libjpeg.(a|so) not found 错误的解决办法
 
检查之后发现已经安装了libjpeg

[root@localhost php-5.2.14]# yum list installed|grep libpng
libpng.x86_64           2:1.2.49-1.el6_2

或者:

[root@localhost php-5.2.14]# rpm -qa|grep libjpeg
libjpeg-turbo-1.2.1-3.el6_5.x86_64

安装libjpeg-devel

yum -y install libjpeg-devel

[root@localhost php-5.2.14]# yum list installed|grep libjpeg
libjpeg-turbo.x86_64    1.2.1-3.el6_5   @base                                   
libjpeg-turbo-devel.x86_64
[root@localhost php-5.2.14]# rpm -ql libjpeg-turbo-devel.x86_64
/usr/include/jconfig.h
/usr/include/jerror.h
/usr/include/jmorecfg.h
/usr/include/jpeglib.h
/usr/lib64/libjpeg.so
/usr/share/doc/libjpeg-turbo-devel-1.2.1
/usr/share/doc/libjpeg-turbo-devel-1.2.1/coderules.txt
/usr/share/doc/libjpeg-turbo-devel-1.2.1/example.c
/usr/share/doc/libjpeg-turbo-devel-1.2.1/jconfig.txt
/usr/share/doc/libjpeg-turbo-devel-1.2.1/libjpeg.txt
/usr/share/doc/libjpeg-turbo-devel-1.2.1/structure.txt

关于安装php时 --with-mysql命令参数问题

如果是rpm安装mysql则直接写成--with-mysql

如果是编译安装mysql则写成--with-mysql=mysql安装路劲

以下的错误是:

/usr/bin/ld: ext/ldap/.libs/ldap.o: undefined reference to symbol 'ber_scanf'

/usr/bin/ld: note: 'ber_scanf' is defined in DSO /lib64/liblber-2.4.so.2 so try adding it to the linker command line 
/lib64/liblber-2.4.so.2: could not read symbols: Invalid operation 
collect2: ld returned 1 exit status 

./configure后

编辑MakeFile

找到 开头是 'EXTRA_LIBS' 这一行 在结尾加上 '-llber' 然后执行 make && make install

完成后的目录

Generating phar.php
Generating phar.phar
PEAR package PHP_Archive not installed: generated phar will require PHP's phar extension be enabled.
directorytreeiterator.inc
pharcommand.inc
directorygraphiterator.inc
invertedregexiterator.inc
clicommand.inc
phar.inc

Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/
Installing PHP CLI binary:        /usr/local/php/bin/
Installing PHP CLI man page:      /usr/local/php/php/man/man1/
Installing PHP FPM binary:        /usr/local/php/sbin/
Installing PHP FPM config:        /usr/local/php/etc/
Installing PHP FPM man page:      /usr/local/php/php/man/man8/
Installing PHP FPM status page:   /usr/local/php/php/php/fpm/
Installing PHP CGI binary:        /usr/local/php/bin/
Installing PHP CGI man page:      /usr/local/php/php/man/man1/
Installing build environment:     /usr/local/php/lib/php/build/
Installing header files:           /usr/local/php/include/php/
Installing helper programs:       /usr/local/php/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php/php/man/man1/
  page: phpize.1
  page: php-config.1
/usr/local/src/php-5.6.30/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin
ln -s -f phar.phar /usr/local/php/bin/phar
Installing PDO headers:           /usr/local/php/include/php/ext/pdo/

下面是对php-fpm运行用户进行设置

1、为php提供配置文件

1 cp php.ini-production /usr/local/php/etc/php.ini

2、为php-fpm提供配置文件

1 cd /usr/local/php
2 cp etc/php-fpm.conf.default etc/php-fpm.conf
3 vim etc/php-fpm.conf

修改
user = www
group = www

如果www用户不存在,那么先添加www用户

1 groupadd www
2 useradd -g www www
修改
pm.max_children = 150
pm.start_servers = 8
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pid = /usr/local/php/var/run/php-fpm.pid 

3、启动php-fpm

执行

1 /usr/local/php/sbin/php-fpm
使用如下命令来验正(如果此命令输出有中几个php-fpm进程就说明启动成功了):
1 ps aux | grep php-fpm

3、nginx和php-fpm整合

编辑/usr/local/nginx/nginx.conf

1 vim /usr/local/nginx/nginx.conf

修改如下:

红框中“#”号去掉

红框中“#”号去掉

添加红框中内容

改成如下

重新载入nginx的配置文件:

1 /usr/loca/nginx/sbin/nginx -s reload

4、测试php文件

 在/usr/local/nginx/html下创建index.php文件,输入如下内容

1 <?php
2     phpinfo();
3 ?>

5、浏览器访问

访问http://你的服务器ip/index.php,皆可以见到php信息了。

原文地址:https://www.cnblogs.com/songanwei/p/9369692.html