使用Monit监视VPS上的服务

How to use Monit to monitor VPS services.

早就从Tianyicui的twitter上了解了monit这个服务,但是一直没用上。直到最近自己VPS上的Mysql没有自动启动,才发现需要一个自动化的工具来监视VPS上重要进程(Nginx, PHP5-FPM, MySQL)的状态。
简单来讲,借鉴Monit的文档和 Graham King 的Blog,我的Monit配置如下。

zhanxw@zhanxw:~$ cat /etc/monit/conf.d/zhanxw.monit 
set daemon 120
set idfile /var/.monit.id
set alert me@zhanxw.com
set mailserver smtp.gmail.com port 587 username "XXXX@XXXX.com" password "XXXX" using tlsv1 with timeout 30 seconds # this basically allows you to use gmail's smtp server.  tlsv1 gets things going in SSL.
set logfile /var/log/monit.log

set httpd port 2812 
    use address zhanxw.com
    allow 141.211.10.47
    allow 205.185.127.61
    allow @zhanxw
    #allow admin:Monit

check system zhanxw.com
    if memory usage > 75% then alert

check process nginx with pidfile "/var/run/nginx.pid"
    start = "/etc/init.d/nginx start"
    stop = "/etc/init.d/nginx stop"
    if loadavg (1min) > 4 then alert
    if loadavg (5min) > 2 then alert
    if failed port 8080 and protocol http then restart
    if 2 restarts within 3 cycles then timeout
    alert me@zhanxw.com

check process php5-fpm pidfile "/var/run/php5-fpm.pid"
    start = "/usr/sbin/service php5-fpm start"
    stop = "/usr/sbin/service php5-fpm stop"
    if failed host 127.0.0.1 port 9000 then restart
    if 2 restarts within 3 cycles then timeout
    alert me@zhanxw.com
    
check process mysqld with pidfile "/var/lib/mysql/zhanxw.pid"
    start = "/usr/sbin/service mysql start"
    stop = "/usr/sbin/service mysql stop"
    if failed host 127.0.0.1 port 3306 then restart
    if 2 restarts within 3 cycles then timeout
    alert me@zhanxw.com

check process varnish with pidfile "/var/run/varnishd.pid"
    start = "/usr/sbin/service varnish start"
    stop = "/usr/sbin/service varnish stop"
    if failed port 80 and protocol http then restart
    if 2 restarts within 3 cycles then timeout
    alert me@zhanxw.com

这个配置的优势在于可以把服务进程的变化发到我的gmail信箱;可以用和SSH相同的密码登录Monit管理界面;可以自动重启关键进程(Nginx, PHP5-FPM,MySQL,Varnish)。我没有监控SSHD,因为这个进程不是zhanxw.com blog的关键部分,即不和WordPress服务相关。

本文没有提到安装Monit,实际上在ubuntu系统上可以用 sudo apt-get install monit 安装。

对于有图形化要求的系统监视,上面的Blog提到了另一个Open source soluiton: Munin。有兴趣的可以移步到那里学习。

小内存VPS的生存之道:Nginx + PHP FPM + Varnish

我用的是BuyVM.net一年$15的VPS,可想而知这个主机的配置是如何Economy: 128M 内存。原来使用的是Apache 1.3 prefork 和 Php_mod,系统稳定性非常好,然而性能可以说令人失望,打开一个简单的静态页面平均需要3秒,而使用wordpress及若干plugin后,差不多需要10秒以上才能访问页面,而且这是打开WP-Supercache后的性能。据我的观察,这是因为Apache进程会fork出很多子进程,这些进程吃掉了有限的VPS内存。2月的最后一天,我决定试一试传说中Nginx,看看它在小内存的VPS上表现是否优异。在8000端口打开Nginx后,使用http://whichloadsfaster.com/ab – Apache HTTP server benchmarking tool 比较Nginx和Apache的速度,毫无悬念的,Nginx要快,平均只用了1/4的时间。因此我下定决心,将整个VPS升级到Nginx+PHP FPM。具体的步骤如下:

1. 升级Ubuntu 10.04 LTS Lucid 到 10.10 Maverick

升级的目的是使用Ubuntu官方的PHP,因为只有10.10版的PHP5才包括了php5-fpm功能。

具体方法(翻译自http://www.howtoforge.com/how-to-upgrade-ubuntu-10.04-lucid-lynx-to-10.10-maverick-meerkat-desktop-and-server-p2):

aptitude install update-manager-core

改变 /etc/update-manager/release-upgrades 中Prompt=normal

do-release-upgrade

我在升级中反复遇到关于procps的这个提示:start: Unknow Jobs: procps

解决方法就是建立一个/etc/init/procps.conf,然后继续apt-get upgrade就行。

原理是service 这个命令会调用/etc/init.d下面的脚本,而procps的脚本会用start命令,start procps命令会调用initctl start procps, 这个过程中需要/etc/init/procps.conf来设定procps相关的参数。在ubuntu升级的时候,这个配置文件缺失造成了上述问题。


2. 安装Nginx

先卸载Apache, 用apt-get install nginx就行,之后参考:

http://www.howtoforge.com/installing-nginx-with-php-5.3-and-php-fpm-on-ubuntu-lucid-lynx-10.04-without-compiling-anything

我这里也列出了自己的nginx.conf文件内容,注意我使用了rewrite功能。这一功能在Apache里是用mod_rewrite支持,在.htaccess里指定的。我们写在nginx.conf文件里也不复杂。另外,末尾的backend语句似乎是必须的,没有它PHP似乎就无法工作。

My /etc/nginx/nginx.conf

user www-data;
worker_processes  1;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

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

http {
 include       /etc/nginx/mime.types;

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

 sendfile        on;
 #tcp_nopush     on;

 #keepalive_timeout  0;
 keepalive_timeout  65;
 tcp_nodelay        on;

 gzip  on;
 gzip_disable "MSIE [1-6]\.(?!.*SV1)";

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

My /etc/nginx/sites-enabled/default

 server {
 listen   8080;
 server_name  zhanxw.com;
 access_log  /var/log/nginx/localhost.access.log;

 root    /var/www;
## Default location
 location / {
 root   /var/www;
 index  index.php index.html;
 }

location /blog/ {
 index index.php;
 if (-e $request_filename) {
 break;
 }
 rewrite ^/blog/(.+)$ /blog/index.php?q=$1 last;
 }

## Images and static content is treated different
 location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
 access_log        off;
 expires           30d;
 root /var/www;
 }

## Parse all .php file in the /var/www directory
 location ~ .php$ {
 fastcgi_split_path_info ^(.+\.php)(.*)$;
 fastcgi_pass   backend;
 fastcgi_index  index.php;
 fastcgi_param  SCRIPT_FILENAME  /var/www$fastcgi_script_name;
 include fastcgi_params;
 fastcgi_param  QUERY_STRING     $query_string;
 fastcgi_param  REQUEST_METHOD   $request_method;
 fastcgi_param  CONTENT_TYPE     $content_type;
 fastcgi_param  CONTENT_LENGTH   $content_length;
 fastcgi_intercept_errors        on;
## Images and static content is treated different
 location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
 access_log        off;
 expires           30d;
 root /var/www;
 }

## Parse all .php file in the /var/www directory
 location ~ .php$ {
 fastcgi_split_path_info ^(.+\.php)(.*)$;
 fastcgi_pass   backend;
 fastcgi_index  index.php;
 fastcgi_param  SCRIPT_FILENAME  /var/www$fastcgi_script_name;
 include fastcgi_params;
 fastcgi_param  QUERY_STRING     $query_string;
 fastcgi_param  REQUEST_METHOD   $request_method;
 fastcgi_param  CONTENT_TYPE     $content_type;
 fastcgi_param  CONTENT_LENGTH   $content_length;
 fastcgi_intercept_errors        on;
 fastcgi_ignore_client_abort     off;
 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;
 }

## Disable viewing .htaccess & .htpassword
 location ~ /\.ht {
 deny  all;
 }
}
upstream backend {
 server 127.0.0.1:9000;
}

3. 安装PHP FPM

使用apt-get install php5-fpm php-apc php5-cgi php5-cli php5-mysql php5-common php-pear php5-curl php5-suhosin php5-gd php5-imagick imagemagick php5-mhash php5-mcrypt即可。

注意安装后可以用service php5-fpm start来检查是否有缺失的php5模块。例如如果见到:

* Starting PHP5 FPM… PHP Warning: PHP Startup: Unable to load dynamic library ‘/usr/lib/php5/20090626+lfs/mcrypt.so’ – /usr/lib/php5/20090626+lfs/mcrypt.so: cannot open shared object file: No such file or directory in Unknown on line 0

Feb 27 15:40:28.053288 [WARNING] [pool www] pm.start_servers is not set. It’s been set to 10.

这说明应该安装php5-mcrypt。后面的WARNING可以忽略。

另外,小内存VPS上使用PHP-FPM可以控制其进程个数。在我的VPS上,单个PHP-FPM可以使用多达100M的内存,同时我的访问量很少,因此我设置PHP-FPM使用static方式维持2个进程。从实践来看系统可以保持合理的响应时间,同时内存不会被用光。

我的PHP-FPM 的配置文件/etc/php5/fpm/pool.d/www.conf  列在下面:

 ...
; 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 = static

; 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 = 2

...

4.安装eAccelerator

主要参考:http://developer.mindtouch.com/en/kb/Improve_PHP_performance_with_eAccelerator_on_Ubuntu_8.04_(Debian)

基本上下载,编译,安装。之后在php.ini中加入下面几行即可(注意.so文件的路径):

 ; eAccelerator configuration
; Note that eAccelerator may also be installed as a PHP extension or as a zend_extension
; If you are using a thread safe build of PHP you must use
; zend_extension_ts instead of zend_extension
;extension                       = "/usr/lib/php5/20060613+lfs/eaccelerator.so"
zend_extension                  = "/usr/lib/php5/20090626+lfs/eaccelerator.so"
eaccelerator.shm_size           = "16"
eaccelerator.cache_dir          = "/var/cache/eaccelerator"
eaccelerator.enable             = "1"
eaccelerator.optimizer          = "1"
eaccelerator.check_mtime        = "1"
eaccelerator.debug              = "0"
eaccelerator.filter             = ""
eaccelerator.shm_max            = "0"
eaccelerator.shm_ttl            = "0"
eaccelerator.shm_prune_period   = "0"
eaccelerator.shm_only           = "0"
eaccelerator.compress           = "1"
eaccelerator.compress_level     = "9"
eaccelerator.allowed_admin_path = "/var/www/eaccelerator"

安装完之后注意检查<?php phpinfo(); ?>页面的输出,保证eAccelerator开启。

5. 启用Varnish

偶然间听说Varnish可以做代理,提供系统响应速度。以我的经验来看,对于静态页面,通过Varnish获取页面和直接使用nginx差别不大(动态页面未测试)。但用Varnish在80端口监听,听起来可以把真正的服务器挡在Varnish之后,似乎可以增强安全性吧。我在VPS上安装Varnish,应注意Varnish本意的版本变化较快,在配置时应注意路径的变化。需要配置两个文件,第一个是

/etc/default/varnish 应改成START=yes,否则会出现这个错误Not starting HTTP accelerator varnishd http://twitter.com/#!/grosser/status/5249558112108544);另一个是/etc/varnish/default.vcl,我参考了http://wowubuntu.com/varnish.html 以及http://blog.mudy.info/2009/04/my-varnish-vcl-for-wordpress/,这里列出我的配置文件/etc/varnish/default.vcl:

 backend default {
.host = "localhost";
.port = "8080";
}
acl purge {
"localhost";
}
sub vcl_recv {
if (req.request == "PURGE") {
if (!client.ip ~ purge) {
error 405 "Not allowed.";
}
return(lookup);
}
if (req.url ~ "^/$") {
unset req.http.cookie;
}
}
sub vcl_hit {
if (req.request == "PURGE") {
set obj.ttl = 0s;
error 200 "Purged.";
}
}
sub vcl_miss {
if (req.request == "PURGE") {
error 404 "Not in cache.";
}
if (!(req.url ~ "wp-(login|admin)")) {
unset req.http.cookie;
}
if (req.url ~ "^/[^?]+.(jpeg|jpg|png|gif|ico|js|css|txt|gz|zip|lzma|bz2|tgz|tbz|html|htm)(\?.|)$") {
unset req.http.cookie;
set req.url = regsub(req.url, "\?.$", "");
}
if (req.url ~ "^/$") {
unset req.http.cookie;
}
}
sub vcl_fetch {
if (req.url ~ "^/$") {
unset beresp.http.set-cookie;
}
if (!(req.url ~ "wp-(login|admin)")) {
unset beresp.http.set-cookie;
}
}

插曲1:重置MySQL密码

好就不用MySQL的root密码,重置密码(适用于MySQL 5.1)可以参考:

Generic method http://dev.mysql.com/doc/refman/5.1/en/resetting-permissions.html

只需要3步:以--skip-grant-tables参数启动Mysql-server;启动mysql;执行

UPDATE mysql.user SET Password=PASSWORD('MyNewPass') WHERE User='root';
FLUSH PRIVILEGES;

插曲2:使Firefox 支持Java Applets

在设置PHP-FPM 参数不当时,有可能整个VPS的内存全部被吃光,这时没法用SSH登录,很多命令(sudo、ls、top)都无法执行,这时候可以用BuyVM.net 提供的 manage.buyvm.net 页面,以Java Applets方式以root身份登录到VPS。默认情况下,Ubuntu的Firefox不支持Java Applets,解决方法就是安装:icedtea6-plugin .


另附:Wordpress中插入代码的方法:

注意这里使用了中文的全角括号,以免Wordpress当成真正的代码。

【sourcecode language=”css”】
your code here
【/sourcecode】

具体的语言可以有:

  • actionscript3
  • bash
  • coldfusion
  • cpp
  • csharp
  • css
  • delphi
  • erlang
  • fsharp
  • diff
  • groovy
  • javascript
  • java
  • javafx
  • matlab (keywords only)
  • objc
  • perl
  • php
  • text
  • powershell
  • python
  • r
  • ruby
  • scala
  • sql
  • vb
  • xml

http://en.support.wordpress.com/code/posting-source-code/

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