PHP運行神器 - 用HHVM減小你一半的服務器

image

Hiphop是Facebook開發一款PHP二進制化的一個工具,最開始是由php轉爲C++,可是後來發現編譯爲c++的話,許多的時間會花費在編譯代碼上面,調試不方便,對於代碼來講也不是即見即所得。php

因此hiphop經歷了這麼幾個階段:python

HPHPC=>HPHPI=>HHVMmysql

HPHPC是靜態編譯,也就是把php轉爲c++linux

HPHPI是一個過渡產品,相似php zend虛擬機,性能還不如zend虛擬機,可是能夠運行查看效果;ios

HHVM是在HPHPI基礎上,應用了JIT技術,性能已經接近了HPHPC,目前facebook打算慢慢拋棄HPHPC;nginx

性能上HPHPC和HHVM比zend虛擬機+加速器要節約cpu在50%在300%間(官方提供),,我實際應用中,通常節約CPU在100%-300%間左右,流量越大越明顯。c++

目前最新的官方2.1版本還存在着內存泄露的問題,對redis的支持只是提供了最基本的功能(咱們須要使用redis,因此對這一塊比較在乎,其它的擴展能夠去查看官方支持列表:https://github.com/facebook/h...git

源碼安裝過於麻煩,想嚐鮮的建議yum,官方的源碼安裝文檔:https://github.com/facebook/h...github

參照官方的安裝方式,用官方提供的yum源進行安裝,在博主的支持下修復了libevent的內存泄露問題golang

Step1:設置好yum源

rpm -ivh
wget -O /etc/yum.repos.d/hop5.repo
yum clean all

Step2:安裝好基礎環境包。。。比較多。。。

rpm -ivh ftp://rpmfind.net/linux/epel/beta/6/x86_64/libmcrypt-2.5.8-9.el6.x86_64.rpm
rpm -ivh ftp://rpmfind.net/linux/epel/beta/6/x86_64/libmcrypt-devel-2.5.8-9.el6.x86_64.rpm
yum install git svn cpp make autoconf automake libtool patch memcached gcc-c++ cmake wget boost-devel mysql-devel pcre-devel gd-devel libxml2-devel expat-devel libicu-devel bzip2-devel oniguruma-devel openldap-devel readline-devel libc-client-devel libcap-devel binutils-devel pam-devel elfutils-libelf-devel tbb-devel libdwarf-devel dbus-cxx-devel libunwind-devel php-mssql libtool

若是準備編譯安裝的話,裝如下的包就可以搭建好編譯環境,若是不像編譯安裝則略過

yum install -y boost.x86_64 
boost-chrono.x86_64 
boost-date-time.x86_64 
boost-devel.x86_64 
boost-filesystem.x86_64 
boost-graph.x86_64 
boost-graph-mpich2.x86_64 
boost-graph-openmpi.x86_64 
boost-iostreams.x86_64 
boost-jam.x86_64 
boost-locale.x86_64 
boost-math.x86_64 
boost-mpich2.x86_64 
boost-mpich2-devel.x86_64 
boost-mpich2-python.x86_64 
boost-openmpi.x86_64 
boost-openmpi-devel.x86_64 
boost-openmpi-python.x86_64 
boost-program-options.x86_64 
boost-python.x86_64 
boost-random.x86_64 
boost-regex.x86_64 
boost-serialization.x86_64 
boost-signals.x86_64 
boost-static.x86_64 
boost-system.x86_64 
boost-test.x86_64 
boost-thread.x86_64 
boost-timer.x86_64 
boost-wave.x86_64 
cpp.x86_64 
curl.x86_64 
gcc.x86_64 
gcc-c++.x86_64 
gcc-gfortran.x86_64 
gcc-objc.x86_64 
gcc-objc++.x86_64 
gcc-plugin-devel.x86_64 
glog.x86_64 
glog-devel.x86_64 
gmp.x86_64 
gmp-devel.x86_64 
gmp-static.x86_64 
golang.x86_64 
libcurl.x86_64 
libcurl-devel.x86_64 
libdwarf.x86_64 
libdwarf-devel.x86_64 
libdwarf-static.x86_64 
libdwarf-tools.x86_64 
libevent.x86_64 
libevent-devel.x86_64 
libgcc.x86_64 
libgfortran.x86_64 
libgfortran-static.x86_64 
libgomp.x86_64 
libmemcached.x86_64 
libmemcached-devel.x86_64 
libmpc.x86_64 
libmpc-devel.x86_64 
libmudflap.x86_64 
libmudflap-devel.x86_64 
libmudflap-static.x86_64 
libobjc.x86_64 
libquadmath.x86_64 
libquadmath-devel.x86_64 
libquadmath-static.x86_64 
libstdc++.x86_64 
libstdc++-devel.x86_64 
libstdc++-static.x86_64 
libtool.x86_64 
libtool-ltdl.x86_64 
libtool-ltdl-devel.x86_64 
lxc.x86_64 
lxc-devel.x86_64 
lxc-doc.x86_64 
lxc-libs.x86_64 
lxc-templates.x86_64 
mpfr.x86_64 
mpfr-devel.x86_64 
nginx.x86_64 
tbb.x86_64 
tbb-devel.x86_64 
tbb-doc.x86_64

若是這裏安裝 golang 包報錯能夠選擇不安裝這個包

若是不是編譯安裝,只要執行

yum install hiphop-php

Step3:解決HHVM對於libevent的內存泄露問題

git下libevent和hhvm的源碼包,之因此要hhvm的源碼包,是要在libevent源碼中打入hhvm提供的針對libevent的補丁,才能讓libevent支持hhvm

git clone git://github.com/libevent/libevent.git
git clone git://github.com/facebook/hiphop-php.git
cd libevent/
git checkout release-1.4.14b-stable

打補丁

cat ../hiphop-php/hphp/third_party/libevent-1.4.14.fb-changes.diff | patch -p1

打完補丁後改修一下libevent的http.c文件,解決內存泄露的關鍵大概在2319行這裏,在

if (req->evcon->state == EVCON_DISCONNECTED) {

下面,增長

req->userdone = 1;

Step4.編譯安裝libevent

./autogen.sh
./configure --prefix=/usr/ --libdir=/usr/lib64/ #注意路徑不要出錯,這裏是覆蓋了rpm安裝的libeventmake && make install

ok...安裝完畢

至此,運行php腳本直接執行hhvm script.php就好了

hhvm會把php先編譯成c++的代碼再去運行

image

相關文章
相關標籤/搜索