小内存vps上wordpress优化

wordpress网站优化

由于图便宜,买了一个就128M内存的vps。所以非常关注性能的问题。

性能主要是2点,php和mysql的部分。

mysql的部分网上很多都有所有说到了,不启动innodb,限制binlog cache, 限制sort buffer等等。具体可以参见如下

[mysql]
no-auto-rehash

[mysqld]
user = mysql
port = 3306
open_files_limit = 600
back_log = 20
max_connections = 100
max_connect_errors = 200
table_cache = 60
external-locking = FALSE
max_allowed_packet = 16M
sort_buffer_size = 128K
join_buffer_size = 128K
thread_cache_size = 10
thread_concurrency = 8
query_cache_size = 0M
query_cache_limit = 2M
query_cache_min_res_unit = 2k
default_table_type = MyISAM
thread_stack = 192K
transaction_isolation = READ-UNCOMMITTED
tmp_table_size = 512K
max_heap_table_size = 32M
long_query_time = 1
log_long_format
server-id = 1
binlog_cache_size = 2M
max_binlog_cache_size = 4M
max_binlog_size = 512M
expire_logs_days = 7
key_buffer_size = 4M
read_buffer_size = 1M
read_rnd_buffer_size = 2M
bulk_insert_buffer_size = 2M
myisam_sort_buffer_size = 4M
myisam_max_sort_file_size = 10G
myisam_max_extra_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover
skip-innodb

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

php的部分。
使用fastcgi, 前端使用nginx来进行fastcgi代理。 fastcgi使用的php-fpm。

使用php-fpm要特别注意如下设置:

; Choose how the process manager will control the number of child processes.
; Possible Values:
; static – a fixed number (pm.max_children) of child processes;
; dynamic – the number of child processes are set dynamically based on the
; following directives:
; pm.max_children – the maximum number of children that can
; be alive at the same time.
; pm.start_servers – the number of children created on startup.
; pm.min_spare_servers – the minimum number of children in ‘idle’
; state (waiting to process). If the number
; of ‘idle’ processes is less than this
; number then some children will be created.
; pm.max_spare_servers – the maximum number of children in ‘idle’
; state (waiting to process). If the number
; of ‘idle’ processes is greater than this
; number then some children will be killed.
; Note: This value is mandatory.
pm = dynamic

; The number of child processes to be created when pm is set to ‘static’ and the
; maximum number of child processes to be created when pm is set to ‘dynamic’.
; This value sets the limit on the number of simultaneous requests that will be
; served. Equivalent to the ApacheMaxClients directive with mpm_prefork.
; Equivalent to the PHP_FCGI_CHILDREN environment variable in the original PHP
; CGI.
; Note: Used when pm is set to either ‘static’ or ‘dynamic’
; Note: This value is mandatory.
pm.max_children = 50

; The number of child processes created on startup.
; Note: Used only when pm is set to ‘dynamic’
; Default Value: min_spare_servers + (max_spare_servers – min_spare_servers) / 2
pm.start_servers = 2

; The desired minimum number of idle server processes.
; Note: Used only when pm is set to ‘dynamic’
; Note: Mandatory when pm is set to ‘dynamic’
pm.min_spare_servers = 1

; The desired maximum number of idle server processes.
; Note: Used only when pm is set to ‘dynamic’
; Note: Mandatory when pm is set to ‘dynamic’
pm.max_spare_servers = 2

不然我的小内存vps很快就会挂的。

listen的部分使用sock的方式,毕竟都是本机,而且sock的速度要比tcp要来的快的多。

listen = /var/run/phpfpm.sock

以上就是php的部分。

下面是nginx的部分

开启fastcgi cache

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 32k;
fastcgi_buffers 4 32k;
fastcgi_busy_buffers_size 64k;
fastcgi_temp_file_write_size 64k;
fastcgi_cache_path /opt/server/nginx/fastcgi_cache_dir levels=1:2 keys_zone=cache_fastcgi:128m inactive=1d max_size=1g;

fastcgi_cache cache_fastcgi;
fastcgi_cache_valid 200 302 301 1h;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_cache_key $request_method://$host$request_uri;

静态文件过期时间

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
}

gzip压缩
由于这个vps不光内存少,1个月的带宽也少,那能减少传输就减少一点。

gzip on;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_types text/plain text/xml text/css application/x-javascript;

wordpress本身的优化

使用wp-super-cache插件,这个插件可以把动态的php转换成静态的html代码,这样减少了很多fastcgi的压力,其实这个是最根本的解决方法。而且这些html又被缓存到了nginx中,这样访问的速度就会非常非常的快了。

###########################################

Best regards
Timo Seven
Linux System Admin & MySQL DBA

预感wordpress.com将火

今日最大的消息是微软将live space迁移至wordpress.com
消息来自http://www.cnbeta.com/articles/122987.htm
就是不知道迁移之后的域名怎么变化?

wordpress.com可是都要注册的,难道微软同意共享用户信息?

本周一,微软已经与WordPress母公司Automattic达成了协议。从今天开始,Windows Live的博主们可以通过迁移工具,将自己的博客转到WordPress。整个迁移工作将在未来几个月完成。最后迁移期限是 2011年3月。

也许对于我们来说不是个好消息,到时候GFW封锁WORDPRESS.COM会更加勤快的。

###########################################

Best regards
Timo Seven
blog: http://www.timoseven.com
twitter: http://twitter.com/zauc
Linux System Admin & MySQL DBA

如何不用翻墙访问wordpress.com

gfw

很奇怪,我们可以访问www.wordpress.com的网站,但是一旦建立了博客,用xxx.wordpress.com想访问自己的blog却是找不到服务器了。

其实我们ping下www.wordpress.com和xxx.wordpress.com就能发现不同的结果.

C:Documents and SettingsAdministrator>ping www.wordpress.com
Pinging wordpress.com [76.74.254.126] with 32 bytes of data:
Reply from 76.74.254.126: bytes=32 time=242ms TTL=49
Reply from 76.74.254.126: bytes=32 time=238ms TTL=49
Reply from 76.74.254.126: bytes=32 time=238ms TTL=49
Reply from 76.74.254.126: bytes=32 time=241ms TTL=49

而我们ping xxx.wordpress.com的结果却是

C:Documents and SettingsAdministrator>ping zauc.wordpress.com
Pinging lb.wordpress.com [72.233.2.58] with 32 bytes of data:
Request timed out.
Request timed out.
Request timed out.
Request timed out.

所以我们可以很容易发现自己单独域名的博客会全部会转到lb.wordpress.com这个域名下。但是这个IP明显是被GFW给屏蔽掉了。但是看这个域名就知道这个只是前端负载均衡设备的IP。但是其实一般真实设备的IP一般都是在这个真实IP的周围。

C:Documents and SettingsAdministrator>ping 72.233.2.59
Pinging 72.233.2.59 with 32 bytes of data:
Request timed out.
Ping statistics for 72.233.2.59:
Packets: Sent = 1, Received = 0, Lost = 1 (100% loss),
Control-C
C:Documents and SettingsAdministrator>ping 72.233.2.57
Pinging 72.233.2.57 with 32 bytes of data:
Reply from 72.233.2.57: bytes=32 time=226ms TTL=43
Reply from 72.233.2.57: bytes=32 time=226ms TTL=43
Reply from 72.233.2.57: bytes=32 time=227ms TTL=43
Reply from 72.233.2.57: bytes=32 time=227ms TTL=43
Ping statistics for 72.233.2.57:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 226ms, Maximum = 227ms, Average = 226ms

找到可以PING通的IP了,windows系统指需要用记事本修改以下文件,指定下Hosts就可以了。

C:\WINDOWS\system32drivers\etc\hosts
72.233.2.57 zauc.wordpress.com

那以后不需要使用代理服务器来访问自己的BLOG了。