本着环(没)保(钱)的原则,脱离DigitalOcean的苦海,从RAMNOD入手了最低端,便宜到一年只需要13.44刀(合人民币84块钱)的128M小内存VPS. 心想128M挂个博客外加偶尔翻翻墙,也够用了.

一、系统优化

目标:尽量减少内存占用

1. nginx php

ramnode初始的系统说实话,已经优化的不错了,干净的系统只占用了40M内存;只要把nginx、php-cgi、mysql拿apt-get安装好就可以了.

apt-get install nginx php5 php5-cgi spawn-fcgi mysql-server php5-mysql

安装好以后,修改nginx php-cgi配置,只保留1个进程,以减少内存的占用

2. nginx.conf

user www-data;
worker_processes 2;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
    use epoll;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 2;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/*;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

3. 使用spwan-fcgi启动php-cgi

/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 4 -f /usr/bin/php5-cgi -u www-data

4. 安装varnish减少(nginx php mysql)对内存的压力,针对wordpress对varnish进行配置;可参照(VPS折腾笔记6 – varnish)

backend cake {
    .host = "yjiang.tk";
    .port = "8000";
}
sub vcl_recv {
    if (
            req.http.host ~ "^yjiang.tk" 
            && 
            !(req.url ~ "shot.jpg$")
            && 
            !(req.url ~ "\/other\/*")
        ) {
        set req.backend = cake;
        if (!(req.url ~ "wp-(login|admin)")) {
            unset req.http.cookie;
        }
    }
}
sub vcl_deliver {   
    set resp.http.x-hits = obj.hits ; 
    if (obj.hits > 0) { 
        set resp.http.X-Cache = "hit cache"; 
    } 
    else { 
        set resp.http.X-Cache = "miss cache"; 
    } 
}
sub vcl_fetch {
    if (req.http.host ~ "^yjiang.tk") {
        set beresp.grace = 60m;
        if (!(req.url ~ "wp-(login|admin)")) {
            unset beresp.http.set-cookie;
        }
    }
}

整套环境搭完,在没有访问的情况下基本占用90M左右的内存,还是比较理想的.此时系统上算是完工了.(点此查看系统信息)

二、速度优化(由于VPS在地球的另一端,访问速度那自然不用说,肯定是比国内慢很多;所以另一件事就是尽量让访问速度更快.)

  1. 首先想到的就是尽量把静态资源使用国内免费的CDN,这样既能提高访问速度,又能减小服务器压力,一举两得. 使用七牛的wordpress插件把所有资源转移到国内qiniu.

  2. 因为使用了varnish来做前端缓存,不用再进行php解析+数据库,不但对大访问量的负载有帮助,速度上也能提高不少. (虽然每天也就几个IP)

  3. 卸载之前的disqus插件,改为多说,使VPS打开的速度进一步提升.

  4. 前端公共库及某些google字体服务选择使用某数字公司提供的CDN服务.

后记: 几个快速重启服务的脚本

php-cgi

#!/bin/bash
`pkill -9 php5-cgi`
/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 1 -f /usr/bin/php5-cgi -u www-data

varnish

#!/bin/bash
if [ -z $1 ]
then
    echo "defalut port 88 used"
    port=88
else
    port=$1
fi

pkill varnish

varnishd -f /etc/varnish/default.vcl \
         -s file,/tmp/varnish_cache.data,160M \
         -a 127.0.0.1:$port \
         -w 2

netstat -ntlp | grep varnish

备份数据库

#!/bin/sh
### crontab  ###
#1 4 * * 0 mysqldump -uroot bgjsy | gzip > /home/yjiang/Dropbox/db_backup/bgjsy/bgjsy-`date +\%Y-\%m-\%d`.sql.gz
################
dbfile=/root/db_backup/wordpress-`date +\%Y-\%m-\%d`.sql.gz
mysqldump --user='root' --password='' wordpress | gzip > $dbfile
scp $dbfile yjiang@42.96.185.104:~/Dropbox/db_backup/wordpress/