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 的, 改善是明显的.

笔记: Nginx的转发

就像我启用了新的.info域名这样, 我会希望依旧在用旧的.cn域名访问的朋友在访问旧域名时浏览器会跳转到新域名. 另外如果你注册了很多域名指向同一个网站, 并且希望跳转到主域名, 那么Nginx的配置可以这样写:

server {
listen 80;
server_name old-domain1 old-domain2 old-domain3;
rewrite ^(.*)$ http://new-domain permanent;
}
UPDATE1:
操作符~表示大小写匹配, 而~*表示忽略大小写. !表示取反. 例如 ~ abc 匹配 abc, 而~* abc 匹配Abc ABC等.
括号里面的是参数, 例如
rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;
🙂

给Centos安装Nginx

10172022

Centos的缺省软件源里竟然没有Nginx,真让我感到意外了。还好,参考一下,办法还是现成的。

Red Hat Enterprise Linux / CentOS Linux Enable EPEL (Extra Packages for Enterprise Linux) Repository

还是做个笔记吧:

# rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-release-5-3.noarch.rpm

之后就可以yum install nginx啦。 🙂

UPDATE. 现在已经是release-5-4了, 不过不是什么问题吧.

UPDATE2. 查看Centos版本:

# cat /etc/*release*

查看Centos是32bit还是64bit版本:

# uname -a
… 2.6.18-028stab070.14 #1 SMP Thu Nov 18 16:04:02 MSK 2010 i686 i686 i386 GNU/Linux << 32bit

UPDATE3. 将CentOS标配PHP5.1.x升级到5.2.x, 因为Joomla1.6什么的会用到. http://wiki.centos.org/HowTos/PHP_5.1_To_5.2

 

#/etc/yum.repos.d/CentOS-Testing.repo
# CentOS-Testing:
# !!!! CAUTION !!!!
# This repository is a proving grounds for packages on their way to CentOSPlus and CentOS Extras.
# They may or may not replace core CentOS packages, and are not guaranteed to function properly.
# These packages build and install, but are waiting for feedback from testers as to
# functionality and stability. Packages in this repository will come and go during the
# development period, so it should not be left enabled or used on production systems without due
# consideration.
[c5-testing]
name=CentOS-5 Testing
baseurl=http://dev.centos.org/centos/$releasever/testing/$basearch/
enabled=1
gpgcheck=1
gpgkey=http://dev.centos.org/centos/RPM-GPG-KEY-CentOS-testing
includepkgs=php*

然后yum update就可以了. 😀

用Nginx给Joomla!提速

Joomla!是个不错的基于PHP的CMS,在LAMP环境下安装运行都非常方便,不过性能并非最优。一个提速的方法是用Nginx服务静态内容。

Nginx的配置片段:

upstream apache1{
server 127.0.0.1:8001;
}

server{
listen 80;
server_name mysite.com.cn www.mysite.com.cn;

location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js)$ {
root   /home/raymond/public_html/www.mysite.com.cn;
expires 14d;

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

location / {
proxy_pass    http://apache1;
proxy_set_header   Host             $http_host;
proxy_set_header   X-Real-IP        $remote_addr;
proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

}

}

apache2的配置片段

<VirtualHost 127.0.0.1:8001>
ServerName www.mysite.com.cn
DocumentRoot /home/raymond/public_html/www.mysite.com.cn
</VirtualHost>

记得把apache2主配置文件中的Listen 80 改为Listen 8001。如果有多个虚拟主机,还要加上Listen 8002……