Elton's Blog

Tag: wordpress

Gentoo下安装Nginx+php

by on 七.19, 2009, under Linux, PHP

使用nginx(engin x)和spawn-fcgi来共同支持php

安装nginx

1
emerge -av nginx

安装spawn-fcgi

1
emerge -av spawn-fcgi

启动spawn-fcgi

1
spawn-fcgi -a 127.0.0.1 -p 9000 -f /usr/bin/php-cgi -C 10

a 表示绑定的ip地址
p 表示端口号
f 表示fcgi的应用程序,在这里是制定php的cgi版本的程序
C 表示spawn的child的个数

执行netstat检查spwan-fcgi是否正常启动,可以看到9000端口是否已经开始监听

1
netstat -tnpl

配置nginx

编辑nginx.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
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
user nginx nginx;
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
 
error_log /var/log/nginx/error_log info;
 
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
 
events {
        worker_connections  8192;
        use epoll;
}
 
http {
        include         /etc/nginx/mime.types;
        default_type    application/octet-stream;
 
        log_format main
                '$remote_addr - $remote_user [$time_local] '
                '"$request" $status $bytes_sent '
                '"$http_referer" "$http_user_agent" '
                '"$gzip_ratio"';
 
        client_header_timeout   10m;
        client_body_timeout     10m;
        client_max_body_size 50m; #最大允许上传50M的附件
        send_timeout            10m;
 
        connection_pool_size            512;
        client_header_buffer_size       8k;
        large_client_header_buffers     4 4k;
        request_pool_size               1024k;
 
        gzip on;
        gzip_min_length 1k;
        gzip_buffers    4 8k;
        gzip_http_version  1.1;
        gzip_types   text/plain application/x-javascript text/css application/xml;
 
        output_buffers  1 1024k;
        postpone_output 1460;
 
        sendfile        on;
        tcp_nopush      on;
        tcp_nodelay     on;
 
        keepalive_timeout       75 20;
        ignore_invalid_headers  on;
        index index.html;
 
        include /data/www/vhosts/*.conf;

其中,worker_processes表示worker进程的个数,一般跟CPU个数相同
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000
表示每个CPU处理一个worker

虚拟主机配置

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
server {
        index           index.htm index.html index.php
        server_name     blog.domain.com;
        access_log      /var/log/nginx/domain.access_log main;
        error_log       /var/log/nginx/domain.error_log info;
        root            /data/www/domain/htdocs/blog;
 
        location / {
                if (-f $request_filename/index.html){
                        rewrite (.*) $1/index.html break;
                }
 
                if (-f $request_filename/index.php){
                        rewrite (.*) $1/index.php;
                }
 
                if (!-f $request_filename){
                        rewrite (.*) /index.php;
                }
        }
 
        location ~ .*.php$ {
                include /etc/nginx/fastcgi_params;
                fastcgi_index index.php;
                fastcgi_pass  127.0.0.1:9000;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;        }
 
         location ~ .(htm|html|gif|jpg|jpeg|png|bmp|ico|css|js)$ {
                expires 30d;
        }
}

上面的配置以wordpress的静态url为例
其中,location ~ .*.php$ 部分是配置php的fcgi
expires,是设置静态资源的缓存时间
rewrite部分是设置wordpress静态url时候需要用到的rewrite

Nginx日常维护

Nginx 支持下表中的信号:
信号名 作用描述
TERM, INT 快速关闭程序,中止当前正在处理的请求
QUIT 处理完当前请求后,关闭程序
HUP 重新加载配置,并开启新的工作进程,关闭就的进程,此操作不会中断请求
USR1 重新打开日志文件,用于切换日志,例如每天生成一个新的日志文件
USR2 平滑升级可执行程序
WINCH 从容关闭工作进程

如要重新加载配置文件就使用如下命令

1
#kill -HUP <pid>

pid是nginx的进称号,通过netstat -tnpl可以查到

Nginx 监控

通过在配置文件中加入:

1
2
3
4
location ~ ^/status/ {
	    stub_status on; #Nginx 状态监控配置
	    access_log off;
	 }

就可以使用http://yourdomain.com/stauts监控nginx的状态。
可以看到类似这样的信息

1
2
3
4
Active connections: 70
server accepts handled requests
 14553819 14553819 19239266
Reading: 0 Writing: 3 Waiting: 67
  • active connections – 当前 Nginx 正处理的活动连接数.
  • server accepts handled requests — 总共处理了 14553819 个连接 , 成功创建 14553819 次握手 ( 证明中间没有失败的 ), 总共处理了 19239266 个请求 ( 平均每次握手处理了 1.3 个数据请求 )
  • reading — nginx 读取到客户端的 Header 信息数
  • writing — nginx 返回给客户端的 Header 信息数。
  • waiting — 开启 keep-alive 的情况下,这个值等于 active – (reading + writing),意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接。

参考:
http://www.ibm.com/developerworks/cn/web/wa-lo-nginx/index.html
http://wiki.nginx.org/NginxChs
http://blog.chinaunix.net/u/12909/showart_1831422.html

Leave a Comment :, , , more...

在wordpress上配置memcached

by on 七.13, 2009, under Linux

Memcached 是一种高性能的分布式内存对象缓存系统。在动态应用,Memcached 既能提高访问的速度,同时还减低了数据库的负载。

如果想在wordpress上使用memcache,需要按照如下步骤来做(以gentoo为例)
1. 安装memcached

1
emerge -av net-misc/memcached

2. 安装php的memcached扩展
因为dev-php5/pecl-memcache被gentoo mask了,所以需要先修改/etc/portage/package.keywords,添加:

1
emerge -av dev-php5/pecl-memcache

3. 下载wordpress的memcached插件
http://dev.wp-plugins.org/browser/memcached/trunk
可以通过 SVN 工具去获取,SVN 地址为:
http://svn.wp-plugins.org/memcached/trunk/
4. 启动memcached

1
memcached -d -m 512 -u root -l 127.0.0.1 -p 11211 -c 1024 -P /tmp/memcached.pid

-d选项是启动一个守护进程,
-m是分配给Memcache使用的内存数量,单位是MB,我这里是512MB,
-u是运行Memcache的用户,我这里是root,
-l是监听的服务器IP地址,如果有多个地址的话,我这里指定了服务器的IP地址127.0.0.1,
-p是设置Memcache监听的端口,我这里设置了11211,最好是1024以上的端口,
-c选项是最大运行的并发连接数,默认是1024,
-P是设置保存Memcache的pid文件,我这里是保存在 /tmp/memcached.pid

如果要结束Memcache进程,执行:

1
# kill `cat /tmp/memcached.pid`

如果想查看memcache的状态,可以使用如下命令

1
2
3
4
5
6
memcached-tool 10.0.0.5:11211 display    # shows slabs
memcached-tool 10.0.0.5:11211            # same.  (default is display)
memcached-tool 10.0.0.5:11211 stats      # shows general stats
memcached-tool 10.0.0.5:11211 dump       # dumps keys and values
memcached-tool 10.0.0.5:11211 move 7 9   # takes 1MB slab from class #7
                                                                             #  to class #9.

5. 上传object-cache.php到wp-content目录后,就可以完成设置了。 看看是不是你的wordpress快了很多

7 Comments :, , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit my friends!

A few highly recommended friends...

Archives

All entries, chronologically...