Nginx + fastCGI + php5-fpm (Ubuntu 10.04)

参考1: http://wiki.mediatemple.net/w/(ve):Install_PHP-FPM_on_Ubuntu_10.04
参考2: http://wiki.nginx.org/HttpCoreModule#try_files

年前的大事基本都办了, 我又有时间折腾了.  🙂

一直听说用 nginx + fastCGI + php5-fpm 方法跑 PHP 是相当快的. 于是我来试试. 自己的经济舱级别的 VPS 只支持 Ubuntu Server 10.04, php5-fpm 还不在软件源当中. 好在 PPA 源中已经有了.

前提: 如果还不能用 PPA, 先要:

$sudo apt-get install python-software-properties

然后添加 php5-fpm 的 PPA:

$sudo add-apt-repository ppa:brianmercer/php

假设只缺少 php5-fpm, 其它 nginx, php5, mysql 什么的都有了, 那就…

$sudo apt-get install php5-fpm

然后用缺省设置启动 php5-fpm:

$sudo update-rc.d enable php5-fpm
$sudo invoke-rc.d php5-fpm start

下一步是让 nginx 把 php 请求转发给 php5-fpm, 缺省监听端口是9000. 我的 nginx 配置样本(只是从 server 开始):

server{
        listen 80;
        server_name your.domain.name;
	client_max_body_size 2M;

	root   /path/to/your/site;
        expires 14d;
	index	index.html index.htm index.php

        gzip on;
        gzip_comp_level 3;
        gzip_types text/plain text/html text/css image/png application/json application/x-javascript text/xml application/xml+rss;
        gzip_vary on;
        gzip_buffers 16 8k;

        location / {
                try_files $uri $uri/ /index.php?q=$uri&$args;
        }

	location ~ \.php$ {
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_index index.php;
		fastcgi_param APPLICATION_ENV production;
		include /path/to/your/fcgi.conf;

		expires off;
	}
}

下面是 fcgi.conf 样本:

fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;

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 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/fastcgi_params, 貌似这个 fcgi.conf 只是增加了 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 一行. 不过我还是喜欢做个完整的 conf 再说 🙂

最后重启 nginx 服务试试吧, 是不是比以前快些呢? 我之前是用 nginx + apache2 + mod_php 的, 改善是明显的.

3 Replies to “Nginx + fastCGI + php5-fpm (Ubuntu 10.04)”

Leave a Reply

Your email address will not be published. Required fields are marked *