您的位置:首页 > 运维架构 > Nginx

Linux下安装php,并配置到nginx【最终版,完美解决问题】

2015-12-06 21:13 791 查看
1、 下载

        libxml2-2.6.32.tar.gz  http://download.csdn.net/detail/netlong339/1351852
        php-5.3.16.tar.gz      http://download.csdn.net/detail/aiyunbreak/5366061

2、建立目标文件夹

    mkdir /usr/local/php

    也就是说等下安装的php要安装到这个文件夹里面

3、解压:
将下载好的文件放在服务器上的某个位置:比如/root/xiebin
①先安装libxml,解压libxml2-2.6.32.tar.gz,
<span style="font-size:18px;">tar -zxvf libxml2-2.6.32.tar.gz</span>


cd /root/xiebin/libxml2-2.6.32
执行: ./configure
执行:make  
执行:make install

②在安装php,解压tar -zxvf php-5.3.16.tar.gz
cd /root/xiebin/php-5.3.16

执行:./configure --prefix=/usr/local/php --with-libxml-dir=/usr/local/libxml2 --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-libevent-dir=libevent

这里超级重要,php5.3以后支持php-fpm启动,如果这里没有配置好,就会导致后面无法启动php【apache作为服务器的话,无所谓,但我们现在是nginx】

./configure --prefix=/usr/local/php --with-libxml-dir=/usr/local/libxml2 --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-libevent-dir=libevent


执行:make  
执行:make install

4、配置文件
①ini文件:将安装文件中的php.ini-development拷贝到/usr/local/php/lib下

    cp /root/xiebin/php-5.3.16/php.ini-development  /usr/local/php/lib
将文件重命名成php.ini;我用的工具是WinScp,可以直接F2重命名,所以我一般先是拷贝,在F2命名
直接cp的时候被提示not find 这样的

②php-fpm文件:php自5.3以后将php-fpm集成过来了,这个很重要!!!下面需要启动php的时候用到。
将php-fpm.conf.default重命名成php-fpm.conf,即去掉.default

修改php-fpm中的配置:122行  将www换成root
                                           140行   user=root  
                                           group=root

5、启动php:实际上是启动php-fpm

   /usr/local/php/etc/php-fpm

这里你会遇到一个问题:please specify user and group other than root
                     FPM initialzation failed

   报错了,提示运行php-fpm运行用户和组只能选择其它的除了root以外.

   于是查看php-fpm文档,会发现其中一个选项:-R



   所以正确的启动应该是:/usr/local/php/sbin/php-fpm -R

/usr/local/php/sbin/php-fpm -R


我擦之前写错了:写成了,/usr/local/php/etc/php-fpm  [这是错误的写法]

   查看是否启动成功:

 

netstat -lnt | grep 9000

netstat -lnt | grep 9000


tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN

或者使用如下命令,查看是否9000端口被php-fpm占用:

netstat -tunpl | grep 9000

tcp        0      0 127.0.0.1:9000              0.0.0.0:*                   LISTEN      2124/php-fpm

 

5、nginx配置

  在nginx的配置文件中加一段

  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root           /www/web/;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
#fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
fastcgi_param  SCRIPT_FILENAME  /www/web/$fastcgi_script_name;
include        fastcgi_params;
}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
root           /www/web/;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
#fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
fastcgi_param  SCRIPT_FILENAME  /www/web/$fastcgi_script_name;
include        fastcgi_params;
}


ps:如何找到nginx的位置:#whereis nginx.conf
                        再送一个命令:找到文件which nginx  可执行文件名称 

   配置好了后,再次在家nginx,执行:/usr/sbin/nginx -s reload

/usr/sbin/nginx -s reload


   写个测试页面

   <?php
echo("this is my first php pagesss");

?>

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: