php-fpm

php-fpm 专门为高负载网站设计,与 nginx 搭配,效果最佳。php-fpm 需要对 php 源码打补丁重编译,整个过程比较复杂且慢。个人比较喜欢使用现成的包方式安装,通过 centos.alt.ru 这个源,可以得到相当新的 php-fpm 包。 当然,这个源还有许多其它包。

1. 添加 rusia-repo 源,将以下内容保存到 /etc/yum.repos.d/rusia-repo.repo :

1
2
3
4
5
6
[rusia-repo]
name=CentOS-$releasever  rusia packages for $basearch
baseurl=http://centos.alt.ru/pub/repository/centos/5/i386/
enabled=1
gpgcheck=0
protect=1

然后开始安装 php-fpm 和必要的 php extension

1
yum install php-mysql php-mcrypt php-mbstring php-gd php-fpm

添加 php-fpm 自启动

1
chkconfig --level 345 php-fpm on

nginx

1. nginx 源码不大,编译安装简单,推荐直接从官网下载源码自行编译安装:

1
2
3
4
wget http://nginx.org/download/nginx-0.7.67.tar.gz
tar zxvf nginx-0.7.67.tar.gzcd nginx-0.7.67./configure
make
make install

2. 制作 nginx 启动脚本 vim /etc/init.d/nginx

 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
# v.0.0.1
# create by jackbillow at 2007.10.15
# nginx - This shell script takes care of start ing and stopping nginx.
#
# chkconfig: - 60 50
# description: nginx [engine x] is light http web/proxy server
# that answers incoming ftp service requests.
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
nginx_path="/usr/local/nginx"
nginx_pid="/usr/local/nginx/logs/nginx.pid"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginx_path/sbin/nginx ] || exit 0
RETVAL=0
prog="nginx"
start() {
# Start daemons.
if [ -e $nginx_pid -a ! -z $nginx_pid ];then
echo "nginx already running...."
exit 1
fi

if [ -e /usr/local/nginx/conf/nginx.conf ];then
 echo -n $"Starting $prog: "
 $nginx_path/sbin/nginx -c /usr/local/nginx/conf/nginx.conf &
 RETVAL=$?
[ $RETVAL -eq 0 ] && {
touch /var/lock/subsys/$prog
success $"$prog"
}
echo
else
RETVAL=1
fi
return $RETVAL
}
# Stop daemons.
stop() {
 echo -n $"Stopping $prog: "
 killproc -d 10 $nigx_path/sbin/nginx
 RETVAL=$?
 echo
 [ $RETVAL = 0 ] && rm -f $nginx_pid /var/lock/subsys/$prog
}
# See how we were called.
case "$1" in
start)
 start
 ;;
stop)
 stop
 ;;
reload)
 kill -HUP `cat $nginx_pid`
 ;;
reconfigure)
 stop
 start
 ;;
status)
 status $prog
 RETVAL=$?
 ;;
*)
 echo $"Usage: $0 {start|stop|reconfigure|reload|status}"
 exit 1
esac
exit $RETVAL

保存退出,改模式,加启动:

chmod +x nginx

chkconfig –level 2345 nginx on

3. 配置 nginx 虚拟主机:

每个虚拟主机都要配置 php 的话,比较麻烦,可以把这些共通的设置单独保存到一个文件,再在 server 节 include 一下。

virtual_host_share.conf :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
index  index.html index.htm index.php;
location ~* ^.+.(jpg|jpeg|png|ico|gif|css|js)$ {
 access_log   off;
 expires      30d;
}
location ~ .php$ {
 fastcgi_pass   127.0.0.1:9000;
 fastcgi_index  index.php;
 fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
 fastcgi_connect_timeout 60;
 fastcgi_send_timeout 180;
 fastcgi_read_timeout 180;
 fastcgi_buffer_size 128k;
 fastcgi_buffers 4 256k;
 fastcgi_busy_buffers_size 256k;
 fastcgi_temp_file_write_size 256k;
 fastcgi_intercept_errors on;
 include        fastcgi_params;
}

配置虚拟主机例子:

1
2
3
4
5
6
7
8
9
server {
  listen: 80;
  server_name example.com www.example.com;
  root /path/to/your/website/public;
  #access_log /path/to/your/website/log/access.log;
  #error_log /path/to your/website/log/error.log;

  include /path/to/virtual_host_share.conf;
}

好了,启动 nginx 和 php-fpm 测试一下吧。

1
2
/etc/init.d/nginx start
/etc/init.d/php-fpm start