聚會時間公告: 因應COSCUP 2011, Kalug 8月份休會一次

九月 20, 2011

小惡魔AppleBOY
AppleBOY
is about »

tag cloud

» 安裝 XCache 加速 PHP 執行速度

php-logo

前言

最近 FreeBSD 系統常常會當機,加上 Varnish cache + APC 一直給我出包,所以這次就換了一套加速 PHP 執行的套件 XCache,這是一套由華人所開發的加速器,可以參考篇台灣PHP聯盟論壇所發表 PHP 加速器 – xcache,裡面可以看到這篇裡面就有中文的對話,非常好玩,這次也順便把 PHP 升級到 5.3.8 版本,還有 Varnish 一次升級到 3.0.1 版本。

各家 PHP 加速器

大家都知道目前網路上知名的 PHP 加速器 apc, eaccelerator, phpa, truck-mmcache,這幾套網路上資料很多,大家都可以試著玩看看,尤其是前面兩套 APC 及 eaccelerator,phpa 目前已經不再維護了,truck-mmcache 版本好像也沒啥在更新,距離上次更新是 2009-07-17,Xcache 作者研究 truck-mmcache 跟 APC 已經很長的時間,他發現 APC 的程式碼比起 truck-mmcache 還要簡單更容易瞭解,所以大家也可以研究 APC 相關程式碼。

Xcache 的由來

大家一定很想知道作者為什麼想要開發一套新的 PHP 加速器 Xcache 呢?早期作者還沒開啟此專案以前,都是在使用 APC 跟 eaccelerator,如果碰到任何 bug,作者都會第一時間告訴官方,作者也跟 eaccelerator 其中一位開發者 zoeloelip 有互動,zoeloelip 是第一位看到 XCache 程式碼,也因為他看過了 Xcache,所以他參考 Xcache 作者 Disassembler idea 來改寫 ea_dasm.c 大部分程式碼。這部份作者也是猜測的,最後作者有提到他為什麼寫了這專案,其實很簡單。

  • 作者有太多 idea 無法實做在 ea/apc
  • ea/apc 架構過於龐大,所以無法實際用在裡面
  • 作者用來練功

最後作者也認同 ea/apc 是不錯的加速器,大家可以實際用看看,感受一下加速過後的 PHP。也可以參考 FeatureList 看看 Xcache 有哪些功能。

安裝 Xcache

這邊環境是 FreeBSD 7.1 Release ports 安裝方式:

$ cd /usr/ports/www/xcache/; make install clean

安裝完成之後要進行設定,設定檔放在 /usr/local/share/examples/xcache/,裡面也包含了 admin 管理介面。

$ cp /usr/local/share/examples/xcache/xcache.ini /usr/local/etc/php/

接著附上我機器的設定檔

[xcache-common]
extension = xcache.so

[xcache.admin]
xcache.admin.enable_auth = On
xcache.admin.user = "admin"
xcache.admin.pass = "xxxxxxx"

[xcache]
xcache.shm_scheme =        "mmap"
xcache.size  =               16M
xcache.count =                 1
xcache.slots =                8K
xcache.ttl   =                 0
xcache.gc_interval =           0
xcache.var_size  =            2M
xcache.var_count =             1
xcache.var_slots =            8K
xcache.var_ttl   =             0
xcache.var_maxttl   =          0
xcache.var_gc_interval =     300
xcache.test =                Off
xcache.readonly_protection = On
xcache.mmap_path =    "/tmp/xcache"
xcache.coredump_directory =   ""
xcache.cacher =               On
xcache.stat   =               On
xcache.optimizer =           Off

[xcache.coverager]
xcache.coverager =          Off
xcache.coveragedump_directory = ""

這邊有要注意的地方就是在 Unix 系統設定 xcache.mmap_path 的時候,這是檔案而並非是目錄,這邊設定好之後,請先建立 /tmp/xcache 檔案

$ touch /tmp/cache && chmod 777 /tmp/cache

安裝完成之後請重新啟動 apache 或 lighttpd:

$ /usr/local/etc/rc.d/apache22 restart
$ /usr/local/etc/rc.d/lighttpd restart

最後直接可以看到 phpinfo 的相關訊息: 補個圖
phpinfo
或者是打看看 php -v 也可以看到訊息:

PHP 5.3.8 (cli) (built: Sep 20 2011 14:37:25)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies
    with XCache v1.3.2, Copyright (c) 2005-2011, by mOo

安裝 Xcache 管理介面

設定管理者帳號密碼 (/usr/local/etc/php/xcache.ini 裡面) 請注意

xcache.admin.enable_auth = On
xcache.admin.user = "admin"
xcache.admin.pass = "xxxxxxxxxxxx"

密碼該如何產生呢?可以透過 php 指令去產生您要的 md5 密碼

php -r "echo md5('admin');"

或者是透過網頁

<?php
echo md5("password");
?>

最後將 admin 目錄放到您網站根目錄底下即可

$ cp -r /usr/local/share/examples/xcache/admin /usr/local/www/apache22/data/xcache

網址列打入 http://xxxxx/xcache 並且輸入上面所設定好的帳號密碼即可
XCache 1.3.2 管理頁面

參考網站:
http://xcache.lighttpd.net/wiki/InstallAdministration

Related View

五月 28, 2009

小惡魔AppleBOY
AppleBOY
is about »

tag cloud

» [網站] 好站連結 (二)


PHP

MySQL

WWW

jQuery

html

FreeBSD

javascript

MySQL

Linux

Related View

五月 6, 2009

小惡魔AppleBOY
AppleBOY
is about »

tag cloud

» [網站] 好站連結(一)


FreeBSD

Linux

MySQL

Python

CSS

Apache

PHP

jQuery

javascript

WWW

html

Related View

七月 14, 2008
» [tips] rewrite debian/ubuntu 's lighttpd conf script from perl to python

Today I want to port lighttpd on another platform which basically a debian sarge system but without perl and dpkg package system on it. Since it's a debian based platform so I start from porting debian's binary lighttpd package, however I've found there're some perl script lays in /usr/share/lighttpd which are used when lighttpd startup.

While I can easily dump the result of perl script into a textfile,
and then startup my lighttpd correctly, I thought "maybe port it to python is not a bad idea." (since my target platform has python!), so here is the effort:
create-mime.assign.py

#!/usr/bin/python
#
# This script directly translate from debian's lighttpd perl script:
# create-mime.assign.pl
#
# Author: timchen119.at.nospam.gmail.com
# License: Public Domain
#
import sys

try:
f = open("/etc/mime.types",'r')
extensions = {}
print "mimetype.assign = ("
for line in f:
line = line.strip()
if line.startswith('#'): continue
if line != "":
splitlist = line.split()
if len(splitlist) < 2: continue
mime = splitlist[0]
for ext in splitlist[1:]:
if ext in extensions.keys(): continue
extensions[ext] = 1
print '".%s" => "%s",' % (ext,mime)
f.close()
print ")"
except Exception,e:
print e
sys.exit(1)


include-conf-enabled.py
#!/usr/bin/python
#
# This script directly translate from debian's lighttpd perl script:
# include-conf-enabled.pl
#
# Author: timchen119.at.nospam.gmail.com
# License: Public Domain
#

import os,glob

confdir = "/etc/lighttpd/"
enabled = "conf-enabled/*.conf"

os.chdir(confdir)

for file in sorted(glob.glob(enabled)):
print 'include "%s"' % file

use-ipv6.py
#!/usr/bin/python
#
# This script directly translate from ubuntu's lighttpd perl script:
# use-ipv6.pl
#
# Author: timchen119.at.nospam.gmail.com
# License: Public Domain
#

import socket

##this sometimes not accurate. (like in vserver mode)
#if socket.has_ipv6:
#

try:
if socket.socket(socket.AF_INET6,socket.SOCK_STREAM,0):
print 'server.use-ipv6 = "enable"'
except:
pass

All of these files can be found in http://kalug.linux.org.tw/~tim/lighttpd-debian-python-script/
Well something quite interesting happened when I port the debian's create-mime.assign.pl into python, It's that my python script's final result is not equivalent to perl one and has more mime types than its :
--- perlmime.txt    2008-07-14 15:29:23.000000000 +0800
+++ pymime.txt 2008-07-14 15:29:33.000000000 +0800
@@ -114,6 +114,11 @@
".dvi" => "application/x-dvi",
".rhtml" => "application/x-httpd-eruby",
".flac" => "application/x-flac",
+".pfa" => "application/x-font",
+".pfb" => "application/x-font",
+".gsf" => "application/x-font",
+".pcf" => "application/x-font",
+".pcf.Z" => "application/x-font",
".mm" => "application/x-freemind",
".gnumeric" => "application/x-gnumeric",
".sgf" => "application/x-go-sgf",
@@ -193,6 +198,11 @@
".pk" => "application/x-tex-pk",
".texinfo" => "application/x-texinfo",
".texi" => "application/x-texinfo",
+".~" => "application/x-trash",
+".%" => "application/x-trash",
+".bak" => "application/x-trash",
+".old" => "application/x-trash",
+".sik" => "application/x-trash",
".t" => "application/x-troff",
".tr" => "application/x-troff",
".roff" => "application/x-troff",
@@ -282,6 +292,7 @@
".tgf" => "chemical/x-mdl-tgf",
".mcif" => "chemical/x-mmcif",
".mol2" => "chemical/x-mol2",
+".b" => "chemical/x-molconn-Z",
".gpt" => "chemical/x-mopac-graph",
".mop" => "chemical/x-mopac-input",
".mopcrt" => "chemical/x-mopac-input",

So I start to dig why this happened, and I've found a strange perl regex filter all these mimetypes out, I believe it's a minor bug in original perl program. (or it does implicitly doing something meaningful? well I can't figure it out.)
--- create-mime.assign.pl    2008-07-14 15:35:58.000000000 +0800
+++ create-mime.assign.pl.new 2008-07-14 15:36:07.000000000 +0800
@@ -7,7 +7,7 @@
chomp;
s/\#.*//;
next if /^\w*$/;
- if(/^([a-z0-9\/+-.]+)\s+((?:[a-z0-9.+-]+[ ]?)+)$/) {
+ if(/^([A-Za-z0-9\/+-.~%]+)\s+((?:[A-Za-z0-9.+-~%]+[ ]?)+)$/) {
foreach(split / /, $2) {
# mime.types can have same extension for different
# mime types

replace this line and this will produce same results as mine.

usage:
just copy these py scripts to /usr/share/lighttpd
and change these lines if you're using debian based system
#### external configuration files
## mimetype mapping
#include_shell "/usr/share/lighttpd/create-mime.assign.pl"
include_shell "/usr/share/lighttpd/create-mime.assign.py"

## load enabled configuration files,
## read /etc/lighttpd/conf-available/README first
#include_shell "/usr/share/lighttpd/include-conf-enabled.pl"
include_shell "/usr/share/lighttpd/include-conf-enabled.py"

support:

biggo.com.tw

biggo.sg

A Django site.