CentOS + Nginx + PHP-FPM,FastCGI 配置CodeIgniter

nginx官方现在已经针对centos提供了repository,所以现在可以直接通过yum来安装啦,很方便。

nginx官方安装教程:http://nginx.org/en/download.html

第一步,先导入nginx最新稳定版的官方repository

1

rpm-ivh http://nginx.org/packages/rhel/5/noarch/RPMS/nginx-release-rhel-5-0.el5.ngx.noarch.rpm

安装nginx

1

yum install nginx

第二步,安装php-fpm和一些依赖的PHP扩展

1

yum install php-fpm php-cli php-pdo php-mysql php-mcrypt php-mbstring php-gd php-tidy php-xml php-xmlrpc php-pear php-pecl-memcache

什么是FastCGI?

FastCGI是语言无关的、可伸缩架构的CGI开放扩展,其主要行为是将CGI解释器保存在内存中并因此获得较高的性能。众所周知,CGI解释器的反复加载是CGI性能低下的主要原因,如果CGI解释器保存在内存中并接受FastCGI进程管理器调度,则可以提供更好的性能、伸缩性、Fail-Over特性等。

FastCGI工作原理

FastCGI进程管理器自身初始化,启动多个CGI解释器进程(多个PHP-CGI进程)并等待来自WebServer的连接。php-fpm作为进程管理器启动多个php-cgi

进程。启动php-cgi FastCGI进程时,可以配置以TCP和UNIX套接字两种方式启动

然后设置一下开机自启

1

2

3

chkconfig--level345mysqld on

chkconfig--level345php-fpm on

chkconfig--level345nginx on

第三步,修改nginx配置文件

首先,fastcgi_params 要加入这一行

vim /etc/nginx/fastcgi_params

1

fastcgi_param SCRIPT_FILENAME$document_root$fastcgi_script_name;

然后修改nginx配置文件的server部分

vim /etc/nginx/conf.d/default.conf

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

server{

listen80;

server_name localhost;

index index.htmlindex.htmindex.php;

root/home/xxx;#网站的根目录

location/aaa/{#网站相对于根目录的地址

if($request_filename!~*/(index\.php|images|1-s|sitemap\.xml|robots\.txt|image|css|js|lib|resource|uploads/thumb)){#这里是针对CodeIgniter目的是隐藏index.php

rewrite^/(.*)$/aaa/index.php?$1last;

}

}

location/bbb/{#配置另外一个相对于根目录的测试网站

if($request_filename!~*/(index\.php|images|1-s|sitemap\.xml|robots\.txt|image|css|js|lib|resource|uploads/thumb)){

rewrite^/(.*)$/bbb/index.php?$1last;

}

}

location~\.php${

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

include fastcgi_params;

}

}

P.S CodeIgniter config.php文件中的$config['uri_protocol']需要设置成AUTO

最后启动nginx和php-fpm就行啦

1

2

service nginx restart

/etc/init.d/php-fpm start

但这只是最基本的nginx php-fpm和CodeIgniter的配置,针对服务器的优化策略还是要自己制定。

引用:http://www.linux-centos.com/2011/12/29/centos-nginx-php-fpm-%E9%85%8D%E7%BD%AEcodeigniter/