网站首页文章详情

PHP运行神器 - 用HHVM减少你一半的服务器

发布时间:2015-01-28 11:43编辑:胜男

Hiphop是Facebook开发一款PHP二进制化的一个工具,最开始是由php转为C++,但是后来发现编译为c++的话,许多的时间会花费在编译代码上面,调试不方便,对于代码来说也不是即见即所得。

所以hiphop经历了这么几个阶段:

HPHPC=>HPHPI=>HHVM

HPHPC是静态编译,也就是把php转为c++

HPHPI是一个过渡产品,类似php zend虚拟机,性能还不如zend虚拟机,但是可以运行查看效果;

HHVM是在HPHPI基础上,应用了JIT技术,性能已经接近了HPHPC,目前facebook打算慢慢抛弃HPHPC;

性能上HPHPC和HHVM比zend虚拟机+加速器要节约cpu在50%在300%间(官方提供),,我实际应用中,一般节约CPU在100%-300%间左右,流量越大越明显。



在这里我把最近对这东西的一些简单的研究分享一下,对这玩意儿有兴趣的童鞋可以加群讨论:

HHVM学习研究 群号:271240899


目前最新的官方2.1版本还存在着内存泄露的问题,对redis的支持只是提供了最基本的功能(我们需要使用redis,所以对这一块比较在意,其它的扩展可以去查看官方支持列表:https://github.com/facebook/hiphop-php/tree/master/hphp/runtime/ext)


源码安装过于麻烦,想尝鲜的建议yum,官方的源码安装文档:https://github.com/facebook/hiphop-php/wiki/Building-and-installing-HHVM-on-CentOS-6.3


参照官方的安装方式,用官方提供的yum源进行安装,在博主的支持下修复了libevent的内存泄露问题



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安装的libevent

make && make install
ok...安装完毕



至此,

运行php脚本直接执行hhvm script.php就行了

hhvm会把php先编译成c++的代码再去运行


本人转自:http://www.68idc.cn/help/jiabenmake/qita/2013090451955.html