Personal tools
You are here: Home Zope&Plone Tips Squid導入でスピードアップ
Document Actions

Squid導入でスピードアップ

by maru last modified 2007-11-22 18:00

鯖のマシンスペック的にPloneを動かすにはかなりカツカツ状態。なのでProxy Cacheを挟み込んで少しでもアクセススピードアップを図ります。

【環境】

  • Python-2.4.4 (Linux)
  • PIL-1.1.6
  • Zope-2.9.7-final (Linux)
  • Plone-2.5.3-final
  • squid-2.6.STABLE16 (←New!)

いくら何でもレスポンス遅杉

 本サイトのコンテンツの増加に比例して肥大化するZODB。マメにcronでpackしていればある程度速度は回復するようですが、それでもかなり遅い。CPUパワー云々もありますが、メモリ不足によるswapの増大が最大の原因かと思われ。

 鯖運用にあまり関係のないXとかそぎ落とすなどチューニングを施せば回復の余地はあると思うんですけど・・・他の用途でも使ってますから、ちょっとそれは㍉。

 と言うわけで、Plone側にテコ入れをしてスピードアップを図ろうと思った次第。そのテコ入れというのは 題名通り、『Squid』 っつーproxy cacheの導入。正直言うと半信半疑なんですけどねwww

 今回の作業は 『CacheFuを使ってスピードUP』 をお手本に進めて行きます。本鯖では既に Apache を Plone/Zope のフロントエンドとして動かしてますので、お手本通りに Apache→Squid→Plone/Zopeにしてやります。

Squidインストール

 うちの Linux は Gentoo ではない(RH8.0・・・)のでtar玉からインストールです。現在の最新は squid-2.6.STABLE16 のようですな。

[hoge@ns1 hoge]$ tar zxvf squid-2.6.STABLE16.tar.gz
[hoge@ns1 hoge]$ cd squid-2.6.STABLE16
[hoge@ns1 squid-2.6.STABLE16]$ ./configure --datarootdir=/usr/local/share/squid --mandir=/usr/local/man --localstatedir=/var/squid --sysconfdir=/etc/squid --enable-default-err-language=Japanese
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for gawk... gawk
(略)
[hoge@ns1 squid-2.6.STABLE16]$ make
Making all in lib
(略)
[hoge@ns1 squid-2.6.STABLE16]$ su
[root@ns1 squid-2.6.STABLE16]# make install
Making install in lib
(略)
[root@ns1 squid-2.6.STABLE16]#

 これでインストール完了。デフォルトだと /usr/local/squid に log とか*.conf とか cacheファイル とかが全部入ってしまうのでこんな感じに path を指定しました。んで、squid を専用垢で動かすのでそれなりの group と user を追加しておきます。

[root@ns1 squid-2.6.STABLE16]# groupadd -g *** squid
[root@ns1 squid-2.6.STABLE16]# useradd squid -u *** -g squid -s /sbin/nologin

CacheFuのおまけconfig

 $[ZOPEINSTANCE]/Products/CacheFu/squid/squid.cfg を以下のように書き直す。

[host]
name: www.maruz.net

[python]
# python binary
binary: /opt/python2.4.4/bin/python

[squid]
# squid version
version: 2.6
# squid binary file
binary: /usr/local/squid/sbin/squid
# effective user for the squid daemon
# proxy on freebsd ?
user: squid
# directory for squid configuration files
config_dir: /etc/squid
# directory for squid log files
log_dir: /var/squid/logs
# directory for the squid cache files
cache_dir: /var/squid/cache
# maximum size of disk cache in MB (should be at most 85% of available disk space)
cache_size_mb: 1500 (←でふぉだと1GBですがちょっと増やした)
# squid serves the site directly (True) or squid runs behind apache (False)
direct: False
# squid address when squid runs behind apache
address: 127.0.0.1
# squid port when squid runs behind apache
port: 3128
# email address displayed on squid error pages
admin_email: hostmaster@maruz.net

[supported-protocols]
# protocols and ports supported by the site
http: 80

[accelerated-hosts]
# urls to be accelerated with corresponding zope urls, ports, and directories
www.maruz.net: 127.0.0.1:8090/plone

 んで、スクリプト実行。

[root@ns1 squid]# export PYTHONPATH=$PYTHONPATH:/usr/local/zope/Zope-2.9.7/lib/python
[root@ns1 squid]# /opt/python2.4.4/bin/python makeconfig
Configuration file [squid.cfg]:
Template directory [templates]:
Output directory [output]:
Generating files for squid behind apache
Generating $[ZOPEINSTANCE]/Products/CacheFu/squid/output/deploy
Generating $[ZOPEINSTANCE]/Products/CacheFu/squid/output/httpd.conf
Generating $[ZOPEINSTANCE]/Products/CacheFu/squid/output/iRedirector.py
Generating $[ZOPEINSTANCE]/Products/CacheFu/squid/output/purge_squid
Generating $[ZOPEINSTANCE]/Products/CacheFu/squid/output/squid.conf
Generating $[ZOPEINSTANCE]/CacheFu/squid/output/squidAcl.py
Generating $[ZOPEINSTANCE]/Products/CacheFu/squid/output/squidRewriteRules.py
Generating $[ZOPEINSTANCE]/Products/CacheFu/squid/output/timing.py
[root@ns1 squid]#
[root@ns1 squid]# cd output/
[root@ns1 output]# mkdir /var/squid/cache
[root@ns1 output]# ./deploy
Copying config / helper files to /etc/squid
Giving squid ownership of its config and helper files
Giving squid read and execute access to its config / helper files
Giving squid write access to /var/squid/logs and /var/squid/cache
Remember to initialize the squid cache (squid -z)
Add the items in httpd.conf to the virtual host blocks of your apache config file
[root@ns1 output]#

 生成されたhttpd.conf ファイルより、本鯖apache の conf ファイルに記述を移植。既にVirtualHostとか使ってapacheからploneにすっ飛ばしてあるのでその部分を書き換えって感じですな。

   (略)

<VirtualHost *:80>
ServerName www.maruz.net
RewriteEngine On
# rewrite URI (without top slash) to Plone port(8090)
# RewriteRule ^/(.*) http://localhost:8090/VirtualHostBase/http/www.maruz.net:80/plone/VirtualHostRoot/$1 [L,P] ※これが昔のやつ
RewriteRule ^/(.*)/$ http://127.0.0.1:3128/http/www.maruz.net/80/$1 [L,P] -
# Pass all other urls straight through |- これが今回移植した行
RewriteRule ^/(.*)$ http://127.0.0.1:3128/http/www.maruz.net/80/$1 [L,P] -
</VirtualHost>

   (略)

Squidのcache初期化と自動起動設定

↑で『squid -z しやがれ!』と言われたのでそのように。これでcacheファイル群が生成、初期化されると。
[root@ns1 output]# /usr/local/squid/sbin/squid -z
2007/11/15 23:33:52| Creating Swap Directories
[root@ns1 output]#

 boot 時に Squid を勝手に起動させる為、↓の内容のファイルを作成して、chmodで実行権付与とaddconfigを行う。

◆/etc/rc.d/init.d/squid

#!/bin/sh
#
# This starts and stops squid proxy cache
#
# chkconfig: 345 90 42
# description: squid is Proxy server.

. /etc/init.d/functions

# Source networking is up.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

# Check that squid.conf exists.
[ -f /etc/squid/squid.conf ] || exit 0 # See how we were called.

SQUID=/usr/local/squid/sbin/squid
LOGFILE=/var/squid/squid.out
RETVAL=0

case "$1" in
start)
echo -n $"Starting Squid services: "
echo "Startup: `date`" >> $LOGFILE
$SQUID -sY >> $LOGFILE 2>&1 && success || failure
RETVAL=$?
echo
;;
stop)
echo -n $"Shutting down Squid services: "
echo "Shutdown: `date`" >> $LOGFILE
$SQUID -k kill >> $LOGFILE 2>&1 && success || failure
RETVAL=$?
echo
;;
restart)
$0 stop
$0 start
;;
*)
echo "Usage: squid {start|stop|restart}"
exit 1
esac

exit $RETVAL

 これは 『Squidの設定 【Squid Web プロキシ & キャッシュ】』 にあったものと/usr/local/squid/bin/RunCache 内のスクリプトをベースに作成。RunCache 経由で立ち上げるよりシンプルに。

ついでにメモしとくと、本来なら stop の中で squid -k shutdown を使ったほうがいいんじゃないかと思うのですが、shutdown で restart(stop → start)を行うとshutdownの処理が未了のままstart を走らせてしまうので kill にしちゃいました。

Plone上での設定

 これであとはPlone上の「サイト設定→プロダクトを追加・削除」で、CMFSquidTool 1.3.1 と CacheSetup 1.0.2 をインストールして、CacheFuの設定はお手本通りにしました。(←なにがなんだかさっぱりだったのでw)

 これで一連の設定は完了。あとは、squid 起動、apache、Zope(zopeは一応) のリスタートをして・・・さて、どのぐらい速くなるのでしょうかね(ワクテカ

参考させていただいたサイト