Friday, December 24, 2010

HipHop for PHP をインストールして apache で proxy してみた


依存関係が相当面倒なので、rpm が用意されている CentOS 5 にインストールすることにした。
CentoOS 5系であれば、以下のようなページも用意されている。
https://github.com/facebook/hiphop-php/wiki/Installing-or-Building-HipHop-PHP-via-RPM-on-CentOS-5
それでも相当いろんなパッケージを入れないといけない。

説明のとおり、yum の設定用パッケージなどを以下のとおり実行
[sikaku@localhost hiphop_rpm]$ sudo rpm -ivh http://epel.osuosl.org/5/x86_64/epel-release-5-4.noarch.rpm
[sikaku@localhost hiphop_rpm]$ sudo rpm -ivh http://dl.iuscommunity.org/pub/ius/stable/Redhat/5/x86_64/ius-release-1.0-6.ius.el5.noarch.rpm
[sikaku@localhost hiphop_rpm]$ sudo rpm -ivh http://pkg.tag1consulting.com/hphp/x86_64/hphp-release-1.0-2.el5.noarch.rpm
[sikaku@localhost hiphop_rpm]$ sudo rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/x86_64/epel-release-5-3.noarch.rpm
[sikaku@localhost hiphop_rpm]$ sudo rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-5.rpm
[sikaku@localhost hiphop_rpm]$ sudo rpm -Uvh http://centos.alt.ru/repository/centos/5/x86_64/centalt-release-5-3.noarch.rpm
[sikaku@localhost hiphop_rpm]$ sudo rpm -Uvh http://dag.wieers.com/rpm/packages/rpmforge-release/rpmforge-release-0.3.6-1.el5.rf.x86_64.rpm
[sikaku@localhost hiphop_rpm]$ sudo yum update
以下のとおり、大量に yum でパッケージを入れる。 何度もエラーと戦い続けて、以下が必要なことがわかった。
[sikaku@localhost hiphop_rpm]$ sudo yum install libidn-devel openssl-devel cvs cvsps gcc gcc-c++ libmcrypt zlib-devel automake libmcrypt mhash libtidy mysql
[sikaku@localhost hiphop_rpm]$ sudo yum install cmake mysql-devel pcre-devel gd-devel expat-devel libmcrypt-devel libcap-devel binutils-devel flex tbb-devel
以下に用意されているパッケージを全部ダウンロードする。
http://sourceforge.net/projects/hphp/files/CentOS%205%2064bit/
このダウンロードは、Irvine などでリンクインポートして一括ダウンロードすると良い。
mysqlのライブラリが古いと、エラーが出るので以下だけ、直接 mysql からダウンロードしてくる。
[sikaku@localhost hiphop_rpm]$ wget "http://dev.mysql.com/get/Downloads/MySQL-5.1/MySQL-shared-community-5.1.54-1.rhel5.x86_64.rpm/from/http://ftp.jaist.ac.jp/pub/mysql/"
で、以下のとおりインストール。一部ダウンロードしたファイルで、依存関係が問題で入らないのがあったので、除外している。
[sikaku@localhost hiphop_rpm]$ sudo rpm -Uvh hiphop-php-1.0-2.x86_64.rpm  boost-1.37.0-1.x86_64.rpm libicu-4.2.1-6.x86_64.rpm oniguruma-5.9.2-1.x86_64.rpm php52-5.2.12-1.ius.x86_64.rpm php52-cli-5.2.12-1.ius.x86_64.rpm php52-common-5.2.12-1.ius.x86_64.rpm tbb-2.2-1.20090809.x86_64.rpm php52-mysql-5.2.12-1.ius.x86_64.rpm MySQL-shared-community-5.1.54-1.rhel5.x86_64.rpm php52-pdo-5.2.12-1.ius.x86_64.rpm libxml2-*.rpm boost-devel-1.37.0-1.x86_64.rpm libevent-*.rpm php52-gd-5.2.12-1.ius.x86_64.rpm curl-devel-7.20.0-1hiphop.x86_64.rpm curl-7.20.0-1hiphop.x86_64.rpm icu-4.2.1-6.x86_64.rpm libicu-devel-4.2.1-6.x86_64.rpm oniguruma-devel-5.9.2-1.x86_64.rpm flex-2.5.35-7.x86_64.rpm
実際に実行してみた。vm環境で 256M しか割り当ててなかったからか、スワップしまくって7分もかかった。
[sikaku@localhost dev]$ cat test.php
<?php
 echo "hello world!";
?>
[sikaku@localhost dev]$ time hphp test.php
hello world!
real    7m8.916s
user    1m2.269s
sys     0m33.538s
[sikaku@localhost dev]$
ポート8999 で起動してみる。
[sikaku@localhost dev]$ mkdir bin
[sikaku@localhost dev]$ time hphp test.php --keep-tempdir=1 -o bin
hello world!
real    9m0.035s
user    1m5.599s
sys     0m34.827s
[sikaku@localhost dev]$ bin/program -m server -p 8999
Could not mlockall
loading static content...
loading static content took 0'00" (0 ms) wall time
page server started
admin server started
all servers started
以下のようにアクセスできる。
[sikaku@localhost ~]$ curl "http://localhost:8999/test.php"
hello world!
続いて、apache の設定を以下のように追加する。mod_rewrite か、mod_proxy で proxy 設定を書く。
[sikaku@localhost dev]$ cat /etc/httpd/conf.d/custom.conf
#RewriteEngine on
#RewriteRule /(.*) http://localhost:8999/$1 [P,L]
ProxyPassMatch ^/(.*)$ http://localhost:8999/$1
[sikaku@localhost dev]$ sudo /etc/init.d/httpd restart
80 ポートからアクセスできた。
[sikaku@localhost ~]$ curl "http://localhost/test.php"
hello world!

Sunday, December 5, 2010

libev を使った web server Lighttz を make してみた

まずは、libev のインストール

[192.168.162.128] cvs -z3 -d :pserver:anonymous@cvs.schmorp.de/schmorpforge co libev
[192.168.162.128] cd libev
[192.168.162.128] autoheader
[192.168.162.128] aclocal
[192.168.162.128] autoconf
[192.168.162.128] touch ltmain.sh
[192.168.162.128] automake -a
[192.168.162.128] ./configure --prefix=$HOME/local
[192.168.162.128] rm libtool
[192.168.162.128] ln -s /usr/bin/libtool libtool
[192.168.162.128] make
[192.168.162.128] make install
libtool がうまく動かなかったので置き換え lighttz の方では、以下のように Makefile を書き換えて gmake
[192.168.162.128] cat Makefile
lighttz: lighttz.o
        gcc -o lighttz ./lighttz.o -L$$HOME/local/lib -lev -u is_default_loop

lighttz.o: lighttz.c
        gcc -O3 -Wall -c -fmessage-length=0 -MMD -MP -MF"lighttz.d" -MT"lighttz.d" -I../libev -o $@ ./lighttz.c

clean:
        rm -f lighttz.o lighttz

start:
        LD_LIBRARY_PATH=/home/sikaku/local/lib/ ./lighttz &
起動してみる。
[192.168.162.128] LD_LIBRARY_PATH=/home/sikaku/local/lib/ ./lighttz &

[192.168.162.128] curl http://localhost:8002/
curl: (18) transfer closed with 1 bytes remaining to read
Hello World

Hello World

[192.168.162.128]
どうやら、1byte 書き出しが少ないようだ。
以下のように修正するとうまくいく
Index: lighttz.c
===================================================================
--- lighttz.c   (revision 103)
+++ lighttz.c   (working copy)
@@ -76,7 +76,7 @@
        char superjared[]="HTTP/1.1 200 OK\r\nContent-Length: 336\r\nConnection: close\r\nContent-Type: text/html\r\nDate: Sat, 26 Apr 2008 01:13:35 GMT\r\nServer: lighttz/0.1\r\n\r\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\" version=\"-//W3C//DTD XHTML 1.1//EN\" xml:lang=\"en\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"/><title>Hello World</title></head><body><p>Hello World</p></body></html>";

        if (revents & EV_WRITE){
-               write(cli->fd,superjared,strlen(superjared));
+               write(cli->fd,superjared,strlen(superjared)+1);
                ev_io_stop(EV_A_ w);
        }
        close(cli->fd);