您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

centos6.5使用hhvm为wordpress加速

2024/4/26 13:51:58发布3次查看
近期由于appfog的免费套餐不让用了,将自己的blog站点从国外迁移到阿里云上。阿里云的配置为1核cpu 1g memory ,最初使用的是nginx + php-fpm的组合 。在使用了更种cache和cdn手段优化后,打开伪静态页面时速度还可以。不过在打开page页面时出奇的慢。使用we
近期由于appfog的免费套餐不让用了,将自己的blog站点从国外迁移到阿里云上。阿里云的配置为1核cpu  1g memory ,最初使用的是nginx + php-fpm的组合 。在使用了更种cache和cdn手段优化后,打开伪静态页面时速度还可以。不过在打开page页面时出奇的慢。使用web在线测试工具后排除了网络自身的问题(当前办公环境的网络挺烂,上个外网都要各种代理)后,登陆云主机查看进程信息和资源信息后,发现在打开page页时,php-fpm进程cpu使用率一直维持在99% ~ 100% 。
在尝试使用xcache 、apache + mod_php 等各种优化组合后问题依旧不见好转。万般无奈之下决定尝试使用facebook 开发的神器 ———— hhvm 。
一、hhvm简介
hhvm(hiphop virtual machine)会将php代码转换成高级别的字节码(通常称为中间语言)。然后在运行时通过即时(jit)编译器将这些字节码转换为x64的机器码。在这些方面,hhvm十分类似与c#的clr和java的jvm。 
2008年facebook就开始使用hiphop(现在成为hphp),这是一种php执行引擎;最初是为了将fackbook的大量php代码转成c++,以提高性能和节约资源。最初的版本成为hphpc,是一个php到c++的编译器。
2010年初,facebook将hiphop平台开源,在后续的发展中,hiphop开发出更高的版本,也就是hhvm 。hhvm加强了hphpc的健壮性,同时还修复了许多重要错误。
二、hhvm的安装
这里以centos6.x为例,增加epel 及gleez 源,并安装hhvm:
#增加epel源,并安装相关依赖包rpm -uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpmyum install libmcrypt-devel glog-devel jemalloc-devel tbb-devel libdwarf-devel mysql-devel\libxml2-devel libicu-devel pcre-devel gd-devel boost-devel sqlite-devel pam-devel \bzip2-devel oniguruma-devel openldap-devel readline-devel libc-client-devel libcap-devel \libevent-devel libcurl-devel libmemcached-devel#增加gleez源,并安装hhvmrpm -uvh http://yum.gleez.com/6/x86_64/gleez-repo-6-0.el6.noarch.rpmyum -y install hhvm
除了gleez源,也可以使用hop5源:
wget http://www.hop5.in/yum/el6/hop5.repoyum clean allyum install hhvm
安装完成后,可以通过以下命令查看进行确认:
[root@91it ~]# hhvm -awelcome to hiphop debugger!type help or ? for a complete list of commands.note: no server specified, debugging local scripts only.if you want to connect to a server, launch with -h or use: [m]achine [c]onnect hphpd>
注:ubuntu、mint、debian等其他平台的安全这里就不再介绍,安装时具体可以参看hhvm 在 github上的wiki 介绍 。
三、hhvm 的运行模式及配置文件
hhvm有三种运行模式:web服务器模式、fastcgi 模式、socket模式
1、web服务器模式
类似于python、ruby的web框架运行模式,hhvm可以不依赖其他web服务器,直接以web模式运行。其可以通过以下命令直接运行:
/usr/bin/hhvm --mode server --user www -vserver.port=80
也可以通过读取配置文件进行运行:
#cat /etc/hhvm.hdfserver { port = 80 sourceroot = /var/www/ #指定web站点的根目录}eval { jit = true}log { level = error uselogfile = true file = /var/log/hhvm/error.log access { * { file = /var/log/hhvm/access.log format = %h %l %u %t \%r\ %>s %b } }}#指定虚拟主机virtualhost { * { prefix = blog.361way.com #这里是server节点 sourceroot根目录下的子目录名称,本例中表示虚拟主机位于/var/www/blog pathtranslation = blog checkexistencebeforerewrite = true #虚拟主机域名 servername = www.361way.com pattern = .* rewriterules { dirindex { pattern = ^/(.*)/$ to = $1/index.php qsa = true } } }}#指定静态文件及规则staticfile { filesmatch { * { pattern = .*\.(dll|exe) headers { * = content-disposition: attachment } } } extensions { css = text/css gif = image/gif html = text/html jpe = image/jpeg jpeg = image/jpeg jpg = image/jpeg png = image/png tif = image/tiff tiff = image/tiff txt = text/plain }}
可以通过下面的命令通过读取配置文件直接运行:
/usr/bin/hhvm --mode daemon --user www-data --config /etc/hhvm.hdf
2、fastcgi模式
hhvm也可以像php-fpm 一样,通过监听某个端口,前端通过apache、nginx进行反向代理进行访问,如监听在9000端口,就可以通过以下命令运行:
/usr/bin/hhvm --mode daemon --user www -vserver.type=fastcgi -vserver.port=9000
以前端为nginx为例,nginx的反向代理配置部分如下 ( 可以看出和使用php-fpm模式没什么区别 ):
location ~ .*\.(hh|php|php5)?$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param script_filename $document_root$fastcgi_script_name; include fastcgi_params; }
同样也可以通过读取配置文件,配置更多的参数,hhvm配置文件如下:
# cat /etc/hhvm/server.hdfpidfile = /var/run/hhvm/pidserver { port = 9000 type=fastcgi # file_socket = /var/run/hhvm/hhvm.sock # sourceroot = /var/www/ defaultdocument = index.php}log { level = warning alwayslogunhandledexceptions = true runtimeerrorreportinglevel = 8191 uselogfile = true usesyslog = false file = /var/log/hhvm/error.log access { * { file = /var/log/hhvm/access.log format = %h %l %u % t \%r\ %>s %b } }}repo { central { path = /var/log/hhvm/.hhvm.hhbc }}#include /usr/share/hhvm/hdf/static.mime-types.hdfstaticfile { filesmatch { * { pattern = .*\.(dll|exe) headers { * = content-disposition: attachment } } } extensions : staticmimetypes}mysql { typedresults = false}
通过以下命令读取配置文件运行:
/usr/bin/hhvm --config /etc/hhvm/server.hdf --user www --mode daemon
3、socket模式
该模式大部分配置文件同fastcgi 模式下的参数一致,这点和php-fpm 运行在端口模式与socket模式一样。运行socket模式下需要开启下面配置中的参数:
# file_socket = /var/run/hhvm/hhvm.sock
同样,也可以直接在命令行中增加vserver.file_socket参数进行运行。
四、hhvm为什么这么快?
相对于原生的php,网上很多人评测hhvm是原生的5-6倍的有之,10-20倍的亦有之。且不管网上的这些评测结果,单说我在阿里云上运行的效果。页面打开的速度显然较之前快了不止一倍,而且较之前使用php-fpm的方式运行,稳定性也有所增强。根据我当前的配置hhvm进程占用内存在20%左右(200m左右),当有页面进行解析时,也基本维持在这个范围,这点很类似java的jvm模式 ,预先将内存点用。
通过查询发现,hhvm使用了以下技术:
字节码:是一种人类无法阅读的代码,专门用来给编译器高效执行的。当hhvm首次加载项目时,它会将所有的php代码转换成字节码;字节码的生成是与平台无关的。 机器码:是一系列供cpu执行的指令。用过汇编的都应该清楚机器码,估计没人喜欢用汇编编程。通过编译器就可以把汇编转成机器码,然后供cpu处理。 jit(即时)编译器:即时编译是种软件优化技术,指在运行时才会去编译字节码。字节码会存放在内存中,然后jit编译器会根据需要加载并编译所涉的字节码。 通过以上技术,hiphop和hhvm获得了性能的提升。
原文地址:centos6.5使用hhvm为wordpress加速, 感谢原作者分享。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录