Ubuntu下安装Nginx、Php-fpm、Php

来源:互联网 发布:广通网络 编辑:程序博客网 时间:2024/05/16 15:22

安装Nginx
sudo apt-get install nginx

安装php及php常用插件
sudo apt-get install php5-cgi php-apc php5-curl php5-gd php5-mysql php5-mcrypt php5-memcache php5-memcached

安装Php-fpm
sudo apt-get install php5-fpm

替换/etc/nginx/fastcgi_params

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

/etc/nginx/sites-enabled/default(具体可以看blog中的nginx多域名配置)
站点中增加php支持
        location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param APPLICATION_ENV production;
                include fastcgi_params;
        }

范例:
server {
        listen   80;
        server_name caige.com;
        root   /home/caige/www;
        index  index.php index.html index.htm;
        access_log  /var/log/nginx/caige.access.log;

           location / {

                index  index.html index.htm index.php;

        }

        location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param APPLICATION_ENV production;
                include fastcgi_params;
        }
}


重启php-fpm、nginx
sudo /etc/init.d/php5-fpm restart
sudo /etc/init.d/nginx restart

或者
service php5-fpm restart
service nginx restart






手动安装php版本(未成功。。。)

tar zxvf php-5.3.8.tar.gz
./configure  --prefix=/usr/local/php --with-mysql=mysqlnd --enable-fastcgi --enable-debug
#ubuntu安装Note that the MySQL client library is not bundled anymore!
#有时会出现上面的错误,无法继续make,原因是未找到mysql库,--with-mysql=mysqlnd路径不知道的话就写mysqlnd
#./configure --prefix=/usr/local/php --enable-fpm  --with-mcrypt=/usr/local/libmcrypt --with-zlib --enable-mbstring --with-openssl --with-mysql --with-mysqli --with-mysql-sock --with-gd --with-jpeg-dir=/usr/lib --enable-gd-native-ttf  --enable-pdo --with-pdo-mysql --with-gettext --with-curl --with-pdo-mysql --enable-sockets --enable-bcmath --enable-xml --with-bz2 --enable-zip --enable-freetype
#
make
sudo make install







安装php5-fpm
复制代码 代码如下:
sudo  apt-get  update
sudo  apt-get install  php5-fpm

其它必要的软件安装接
复制代码 代码如下:
sudo   apt-get   install   nginx

配置php-fpm
php-fpm的解析器是C/S结构,它的配置文件位于:
(1)/etc/php5/fpm/php-fpm.conf
(2)/etc/php5/fpm/pool.d/
一般没什么严格的配置的要求,或者说这块我还没有具体的研究每个配置参数的意义
我采用了tcp模式与fastcgi进程进行连接,因此我修改了tcp监听的地址和端口,修改了一下监视目录的名称,这里不做具体详细解释了,大家可以参考官方文档根据自己的需求进行配置
listen = 127.0.0.1:9000
;listen = /var/run/php-fpm/php-fpm.sock
可以运行
;listen = 127.0.0.1:9000
listen = /var/run/php-fpm/php-fpm.sock
运行不了.....
重启php5-fpm

配置nginx
前言
nginx本身并不会对php语言进行解析,这个区别于apache(apache有在带的mod_php模块进行php解析).nginx是通过fastcgi将客户端的php请求交给后台的php5-fpm进程管理器,php5-fpm具有解析php的功能

注意:
include /etc/nginx/fastcgi_params中一个参数设置需要修改,修改如下:
复制代码 代码如下:

fastcgi_param   SCRIPT_NAME             $document_root$fastcgi_script_name;

因为脚本的名称不加上$document_root,php5-fpm是无法找到需要执行的php脚本的绝对路径的
重启nginx
复制代码 代码如下:

sudo  /etc/init.d/nginx  restart

测试fastcgi_finish_request()函数
复制代码 代码如下:

<?php
echo "OK";
fastcgi_finish_request(); /* 响应完成, 关闭连接 */
sleep(5);
file_put_contents("/tmp/fastcgi.log", "hello",FILE_APPEND);
sleep(5);
file_put_contents("/tmp/fastcgi.log", "world",FILE_APPEND);
?>

说明:
用最大的白话说,fastcgi_finish_request()可以提前关闭和客户端的连接,把需要返回的数据返回给客户端,但是函数之后的分支业务逻辑还是继续在后台运行!

0 0