服务不支持chkconfig

今天chkconfig –add nginx的时候,出现nginx 服务不支持chkconfig。
如果想让服务支持chkconfig,必须定义服务的启动级,启动优先级,关闭优先级,还有描述,如下

  1. # chkconfig: 345 85 15
  2. # description: Nginx is an HTTP server

345是启动级,85和15分别是启动优先级,关闭优先级,下面的就是描述了。

wordpress启用memcached

wordpress启用memcached需要环境符合两个条件,1,安装了php-memcached扩展,2,安装了memcached
1、启用方法很简单,如果你的memcached启用的是默认端口11211,则只需要下载object-cache.php文件到wp-content目录即可。
2、如果端口不是11211或者需要配置多台memcached,则需要修改wp-config.php文件,在文件中加入下面代码。

  1. global $memcached_servers;
  2. $memcached_servers = array(‘default’ => array(‘127.0.0.1:11211′,’192.168.1.101:11211’)); // 这里是你的 memcached 地址和端口
  3. global $blog_id;
  4. $blog_id = ‘centos_blog’; // 这里是用于区分服务器上有多个 wordpress 而避免使用同一内存缓存

Linux安装php扩展memcache

php扩展memcache的作用是为了支持memcached数据库缓存服务器,下面是安装方法。
1、下载并解压memcache文件

  1. wget -c http://pecl.php.net/get/memcache-3.0.6.tgz
  2. tar xzvf memcache-3.0.6.tgz
  3. cd memcache-3.0.6

2、执行phpize扩展安装程序,假设phpzie的路径为/usr/local/php/bin/phpize,具体的路径得根据自己的环境修改。

  1. /usr/local/php/bin/phpize

3、开始安装扩展memcache

  1. ./configure –enable-memcache –with-php-config=/usr/local/php/bin/php-config –with-zlib-dir
  2. make && make install

4、最后修改php.ini文件,在zend之前加入如下代码。

  1. [memcache]
  2. extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"
  3. extension=memcache.so

解决apache与vsftpd写入权限共存问题

我们有时候会遇到apache有写入权限时,vsftpd又不能上传,反过来,vsftpd能上传,但apache又没有写入权限了。我们现在来看看如何解决这个问题。解决的思路是,把ftp用户添加到apache用户组,并设置用户组有写入权限。
假设现在我们建有ftp用户为:centos,httpd进程以apache用户运行。
1、添加centos到用户组apache

  1. usermod -a -G apache centos

2、设置网站根目录/var/www的所有组为apache

  1. chown -R :apache /var/www

3、设置网站根目录的权限为用户组有读写权限

  1. chmod -R g+rw /var/www

最后还需要确认vsftpd的配置文件中的local_umask=002,这是使新上传的文件的用户组有写入权限。

wordpress评论与回复慢的原因及解决方法

博客回复读者的评论发现都超过30秒,一直懒得理。今天连续回复几条评论,发现实在无法忍受了,就开始着手解决这一问题。
1、是不是评论的程序代码有问题?可以排除,因为一回复读者评论,后台马上显示回复内容,只是浏览器一直显示正在提交评论…
2、开始查看vps的进程,发现有一个以用户apache运行的sendmail进程,这个应该可以确认是sendmail的问题。
3、查看sendmail日志。日志如下:
My unqualified host name (centos) unknown; sleeping for retry
unable to qualify my own domain name (centos) — using short name
大概的意思是:centos不是完整的主机名,无法使用,等待重试。
原因:因为centos不是完整的主机名,所以sendmail无法解析出IP,sendmail就一直尝试解析,导致http连接无法结束,一直到sendmail进程超时。
解决方法:
1、设置完整的主机名(重启后会丢失):

  1. hostname devops.webres.wang

2、永久设置主机名
修改/etc/sysconfig/network文件,把hostname centos改为hostname devops.webres.wang

varnish前端缓存测试

今天给lamp一键安装包增加了一个扩展-varnish,主要作为wordpress博客的缓存。下面我们来用ab工具做一个简单的并发为100,连接数为1000的压力测试,一个是没有启用varnish缓存的,一个是varnish前面缓存的。
服务器参数:系统centos 5 32位,单核,Intel(R) Xeon(R) CPU L5630 @ 2.13GHz,1G内存
没有经过varnish缓存的测试结果:
负载40.44
Concurrency Level: 100
Time taken for tests: 305.766 seconds
Complete requests: 1000
Failed requests: 1
(Connect: 0, Receive: 0, Length: 1, Exceptions: 0)
Write errors: 0
Non-2xx responses: 1
Total transferred: 19746015 bytes
HTML transferred: 19521638 bytes
Requests per second: 3.27 [#/sec] (mean)
Time per request: 30576.620 [ms] (mean)
Time per request: 305.766 [ms] (mean, across all concurrent requests)
Transfer rate: 63.07 [Kbytes/sec] received
开启了varnish前端缓存的
负载0.05

Concurrency Level: 100
Time taken for tests: 12.720 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 20086736 bytes
HTML transferred: 19821261 bytes
Requests per second: 78.62 [#/sec] (mean)
Time per request: 1271.991 [ms] (mean)
Time per request: 12.720 [ms] (mean, across all concurrent requests)
Transfer rate: 1542.15 [Kbytes/sec] received

这两组数据不用我说,你也应该明白了。

Linux安装配置varnish web加速器

Varnish是一款高性能的开源HTTP加速器,它可以来做纯粹的代理服务器,负载均衡,但varnish最主要的功能是缓存加速,也是它最出色的地方。下面介绍如何安装和使用。

  1. wget -c http://repo.varnish-cache.org/source/varnish-3.0.1.tar.gz
  2. tar xzvf varnish-3.0.1.tar.gz
  3. cd varnish-3.0.1
  4. ./configure –prefix=/usr/local/varnish
  5. make
  6. make install
  7. groupadd varnish
  8. useradd -d /var/lib/varnish -g varnish -s /sbin/nologin varnish
  9. ln -s /usr/local/varnish/sbin/varnishd /usr/sbin/varnishd

启动varnish:

  1. varnishd -f /usr/local/varnish/etc/varnish/default.vcl -s malloc,1G -g varnish -u varnish -T 127.0.0.1:2000

关闭varnish:

  1. pkill varnish

启动参数介绍:
-f /usr/local/etc/varnish/default.vcl
这个 –f 选项指定varnishd使用哪个配置文件。
-s malloc,1G
这个 –s 选项用来确定varnish使用的存储类型和存储容量,我使用的是malloc类型(malloc是一个C函数,用于分配内存空间), 1G 定义多少内存被malloced,1G = 1gigabyte。
-T 127.0.0.1:2000
Varnish有一个基于文本的管理接口,启动它的话可以在不停止varnish的情况下来管理varnish。您可以指定管理软件监听哪个接口。当然您不能让全世界的人都能访问您的varnish管理接口,因为他们可以很轻松的通过访问varnish管理接口来获得您的root访问权限。我推荐只让它监听本机端口。如果您的系统里有您不完全信任的用户,您可以通过防火墙规则来限制他访问varnish的管理端口。
-a 0.0.0.0:8080
这一句的意思是制定varnish监听所有IP发给8080端口的http请求,如果在生产环境下,您应该让varnish监听80,这也是默认的。
vcl配置文件的介绍请执行如何命令查看:
man /usr/local/varnish/share/man/man7/vcl.7
也可以查看在线文档:https://www.varnish-cache.org/docs/3.0/

wordpress varnish vcl配置文件

如需使用varnish作wordpress缓存,已经开发有一键包http://devops.webres.wang/lampv/
使用如下配置文件,建议安装wordpress插件Varnish HTTP Purge来自动清除缓存,此配置文件实现的功能如下:
1、varnish作为前端,使用80端口
2、允许127.0.0.1和devops.webres.wang清除缓存
3、在缓存之前,删除常见静态文件的cookie。
4、http.x-forwarded-for获取真实IP。
5、不缓存wordpress后台页面,不缓存已登录的用户和保留评论者cookie。
6、后端服务器状态检查,如发生故障,继续以旧缓存内容服务。
下面是配置文件内容:

  1. backend wp1
  2. {
  3. .host = “127.0.0.1”;
  4. .port = “8000”;
  5. .probe = {
  6.                 .url = “/”;
  7.                 .timeout = 1s;
  8.                 .interval = 60s;
  9.                 .window = 1;
  10.                 .threshold = 1;
  11.         }
  12. }
  13. acl purge {
  14. “127.0.0.1”;
  15. “devops.webres.wang”;
  16. }
  17. sub vcl_recv {
  18.           if (req.request == “PURGE”) {
  19.                 if (!client.ip ~ purge) {
  20.                         error 405 “Not allowed.”;
  21.                 }
  22.                 return (lookup);
  23.         }
  24. }
  25. # Do the PURGE thing
  26. sub vcl_hit {
  27.         if (req.request == “PURGE”) {
  28.                 purge;
  29.                 error 200 “Purged.”;
  30.         }
  31. }
  32.  
  33. sub vcl_miss {
  34.         if (req.request == “PURGE”) {
  35.                 purge;
  36.                 error 200 “Purged.”;
  37.         }
  38. }
  39. sub vcl_recv {
  40.     ## always cache these images & static assets
  41.     if (req.request == “GET” && req.url ~ “.(css|js|gif|jpg|jpeg|bmp|png|ico|img|tga|wmf|html|htm)$”) {
  42.       remove req.http.cookie;
  43.       return(lookup);
  44.     }
  45.     if (req.request == “GET” && req.url ~ “(xmlrpc.php|wlmanifest.xml)”) {
  46.       remove req.http.cookie;
  47.       return(lookup);
  48.     }
  49. ## get real ip address
  50. if (req.http.x-forwarded-for) {
  51.  set req.http.X-Forwarded-For =
  52.  req.http.X-Forwarded-For + “, “+ client.ip;
  53.  } else {
  54.  set req.http.X-Forwarded-For = client.ip;
  55.  }
  56.     ##never cache POST requests
  57.     if (req.request == “POST”)
  58.     {
  59.       set req.backend = wp1;
  60.       return(pass);
  61.     }
  62.  
  63.     ### do not cache these files:
  64.     ##never cache the admin pages, or the server-status page
  65.     if (req.request == “GET” && (req.url ~ “(wp-admin|wp-login|server-status)”))
  66.     {
  67.       return(pipe);
  68.     }
  69.  
  70.     #DO cache this ajax request
  71.     if(req.http.X-Requested-With == “XMLHttpRequest” && req.url ~ “recent_reviews”)
  72.     {
  73.       return (lookup);
  74.     }
  75.  
  76.     #dont cache ajax requests
  77.     if(req.http.X-Requested-With == “XMLHttpRequest” || req.url ~ “nocache” || req.url ~ “(control.php|wp-comments-post.php|wp-login.php|bb-login.php|bb-reset-password.php|register.php)”)
  78.     {
  79.         return (pass);
  80.     }
  81.  
  82.     if (req.http.Cookie && req.http.Cookie ~ “wordpress_”) {
  83.         set req.http.Cookie = regsuball(req.http.Cookie, “wordpress_test_cookie=”, “; wpjunk=”);
  84.     }
  85.     ### don’t cache authenticated sessions
  86.     if (req.http.Cookie && req.http.Cookie ~ “(wordpress_|PHPSESSID|comment_author_)”) {
  87.         return(pass);
  88.     }
  89.  
  90.     ### parse accept encoding rulesets to make it look nice
  91.     if (req.http.Accept-Encoding) {
  92.         if (req.http.Accept-Encoding ~ “gzip”) {
  93.         set req.http.Accept-Encoding = “gzip”;
  94.         } elsif (req.http.Accept-Encoding ~ “deflate”) {
  95.         set req.http.Accept-Encoding = “deflate”;
  96.         } else {
  97.         # unkown algorithm
  98.         remove req.http.Accept-Encoding;
  99.         }
  100.     }
  101. if (req.backend.healthy) {
  102.                 set req.grace = 120s; /* Only enable if you don’t mind slightly stale content */
  103.         } else {
  104.                 set req.grace = 24h;
  105.         }
  106.  
  107.     return(lookup);
  108. }
  109. sub vcl_fetch {
  110. set beresp.grace = 24h; /* Keep at longest used in vcl_recv */
  111. set beresp.ttl = 1h;
  112. }