从WordPress 升级失败中恢复

今天在Wordpress里升级suffusion, 但因为VPS内存不够,升级没成功。重启VPS之后,发现如下提示“Briefly unavailable for scheduled maintenance. Check back in a minute.”,处理方法是: 删除blog/.maintenance

但诡异的问题是发现整个blog仍然无法访问,页面完全是空的(blank page,网页的source code什么也没有)。一开始认为是升级Wordpress造成了文件系统损坏,我用关键字Wordress, upgrade, suffusion, fail, blank page等等搜索了很多页面浪费了1-2个小时后,突然发现不是和升级Wordpress 过程有关,也不和wp-config.php文件的末尾有没有空行有关,而是MySQL进程没有启动!!

这里写此文来纪念失去的2个小时吧。

PS:
在这浪费的时间里发现Wordpress系统设计的很精巧:theme的安装文件和配置文件是分离开的,也就是说改变wp-content/themes/suffusion/并不影响以前的字体、颜色、版式设置……

How to set up BuyVM with LAMP, WordPress and VPN

在2011年1月份的最后一天,我非常幸运的发现BuyVM.net的每年15美元的VPS计划居然还没卖光。按耐不住的我立刻掏钱。之后就有了本文。我将分三部分介绍如何安装和调试(Optimize) LAMP, 安装及迁移WordPress 和设置VPN.

1. LAMP的安装和优化

如果像我一样用BuyVM.net 每年$15美元的计划,你可以选择Ubuntu 10.10 LAMP 系统,这样你自己就拥有了LAMP整套系统,但是这样的系统不能适应Wordpress程序——内存经常不够用。因此我们必须想办法减少Apache2和MySQL的内存消耗。

对于Apache,通过检查aptitude程序可以发现,Ubuntu安装的是Apache-prefork-mpm版本(版本号中的prefork表示的是和Apache 1.3类似的架构)。这个架构下比较关键的参数是最少和最多的apache进程的个数。如果不限制,则会很容易出现8-10个apache进程,有的进程占内存30-40M,很快你的VPS就会反应变慢,甚至crash。参考apache – How to reduce memory usage on a Unix webserver – Server Fault之后,在/etc/apache2.conf里面,我们需要这样的设定:

StartServers          1
MinSpareServers       1
MaxSpareServers       5
ServerLimit          16
MaxClients           16
MaxRequestsPerChild   0
ListenBacklog        100

为了减少MySQL的内存占用,我们需要改动/etc/my.cnf,有文章(Google “MySQL reduce memory”)建议不直接使用MySQL提供的为small memory使用的配置文件(例如:考虑到Wordpress经常会同时使用多达10个表进行查询),因此给出关键部分([mysqld]部分)的我的参考配置如下:

[mysqld]
user            = mysql
port            = 3306
socket          = /var/run/mysqld/mysqld.sock
skip-locking
key_buffer_size = 1M
max_allowed_packet = 1M
table_open_cache = 10
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 64K
skip-innodb
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# if all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (using the "enable-named-pipe" option) will render mysqld useless!
#
#skip-networking
server-id       = 1
# Uncomment the following if you want to log updates
#log-bin=mysql-bin
# binary logging format - mixed recommended
#binlog_format=mixed
# Uncomment the following if you are using InnoDB tables
#innodb_data_home_dir = /var/lib/mysql/
#innodb_data_file_path = ibdata1:10M:autoextend
#innodb_log_group_home_dir = /var/lib/mysql/
# You can set .._buffer_pool_size up to 50 - 80 %
# of RAM but beware of setting memory usage too high
#innodb_buffer_pool_size = 16M
#innodb_additional_mem_pool_size = 2M
# Set .._log_file_size to 25 % of buffer pool size
#innodb_log_file_size = 5M
#innodb_log_buffer_size = 8M
#innodb_flush_log_at_trx_commit = 1
#innodb_lock_wait_timeout = 50

2. WordPress的安装

可以下载Wordpress的最新版,然后用他提供的Famous 5 minutes 安装完毕。我遇到的问题是如何把旧系统(http://zhanxw.dyndns.info/blog)迁移到这个新地址(http://zhanxw.com/blog),那么官方(http://codex.wordpress.org/Moving_WordPress) 提供了英文文档应对这种情况。粗看起来比较复杂,但原理上相当自然:

(1) 备份旧系统的blog文件夹和数据库;

(2) 拷贝这两样并安装到新域名下;

(3) 在新域名下激活系统(就是访问一下,结果登录的时候被转回旧系统);

(4) 到旧系统中在Setting里把主机(domain)改成新的域名;

(5) 把旧系统的blog文件夹和数据库再次拷贝到新的域名下;

(6) 在新的域名下登录,这回就应该没问题了!

3. VPN的安装

主要参考两个文章:

(1).Linode VPS PPTP VPN 安装配置教程 – VPS侦探

(2).Ubuntu 上安装 pptp » jKey.lu

其中按照1的步骤可以进行到iptables命令前,这时候看文章2的iptables命令即可。(注意默认系统不提供iptables,需手动安装)。注意,如果不完成文章2提到的步骤,在Windows 7里面你仍然可以连接到VPN,但无法访问任何网页。

4. 使用BuyVM的其他经验

Web方式的管理界面可以通过http://manage.buyvm.net 来进行。登录之后可以进行开关机,重启,查看CPU、内存、带宽使用情况,还可以通过一个Java Application以Console方式登录到VPS。

为了推广站点,我们可以使用Google Analytics去了解访客的来源,也可以到Goolge Site Admin网页提交自己的站点。

另外,对于我使用的Wordpress,推荐使用的Plugin包括:

(1)SI Captcha : 在访客输入comment时提示CAPTHA

(2)Akismet :防止无聊的Spammer

(3)NextGEN Gallery:提供一个展示自己图片的方式

(4)Shareaholic:在每一个Post下面增加一行,方便访客将内容转发到delicious,twitter……

(5)MathJAX:为Wordpress提供LaTeX语法支持,方便今后输入和显示数学公式

(6)Limit Login Attempt:当用户错误输入密码超过一定次数时,拒绝该用户的‘恶意’猜测。

Trackback and Pingback

讲解trackback/pingback的文章:

WordPress Trackback Tutorial

讲解了如何测试对方的wordpress是否支持trackback/pingback,并且检查自己的post是否ping 了对方的post。
这是很实用的技巧。

EDIT:
刚刚发现随便乱用trackback是不礼貌的行为。所以下面的测试现在已经不起作用了。

这个用来测试matrix67 blog的trackback功能。

Trackback: http://www.matrix67.com/blog/archives/2660/trackback

用wordpress的话来说,trackback, pingback都是一个相同的目的,但后者更安全,可靠。

他们的目的(在我这个文章的例子里)是,我可以在我的blog发表comment,同时我的comment会在matrix67的blog里出现。

既然如此,那么来看看需要多久我的comment会在这个网页出现 :)

http://www.matrix67.com/blog/archives/2660

Uninitialized variable makes a mysterious bug

Reason:

I used strtod() function in C, forgot to set errno to 0, then after calling strtod(), the value of errno is unpredictable.

When found the bug:

I tried to use Ptyhon ctypes module. In script mode, python pyCtypes.py always crash but in command line mode, the code becomes all right. It’s mysterious running Python in different ways turns out to give different results.

First, I thought it is clear that Python has some bug, otherwise the same Python code should give the same result.

Then I realized my Python code use a DLL routine using C language, and I recalled in the man page of “strtod”, it says we need to initialize errno value to zero every time before calling.

Example code:
Line 1 should be added to ensure correctness.

        errno = 0;
        vec->value[vec->len++] = strtod(temp, NULL);
        //vector_print(vec);
        if (errno != 0) {
            perror("strtod");
            fprintf(stderr, "%s\n", temp);
            exit(EXIT_FAILURE);
        }

Note:

WordPress supports syntax highlight.

I am using SyntaxHighlighter Evolved http://wordpress.org/extend/plugins/syntaxhighlighter/.

Official documentation mentions another way: http://en.support.wordpress.com/code/posting-source-code/.