CentOS6.2 试用PHP HHVM

时间:2021-08-26 10:30:26

关于HHVM的介绍

http://en.wikipedia.org/wiki/HipHop_Virtual_Machine

http://www.hhvm.com/

https://github.com/facebook/hhvm

在CentOS6.2上安装

目前,对ubuntu、Debian等Linux支持到位(https://github.com/facebook/hhvm/wiki/Prebuilt%20Packages%20for%20HHVM)。

You can install a prebuilt package or compile from source.

事实上,rpm包安装就已经很麻烦了,源码编译更是伤不起(可以参考:http://www.xuebuyuan.com/642409.html),所以还是以yum来配置较好。

1.添加合适的源

  cd /etc/yum.repos.d/
wget http://www.hop5.in/yum/el6/hop5.repo
yum makecache

注意,不要和你当前的repo源冲突,尤其是一些共有的包,所以适时只选其一。

2.预先安装部分依赖

先不用急着安装hhvm,你会发现一堆依赖。所以我们需要yum安装或rpm手动安装如下的包。

升级gcc到4.6

yum install gcc.x86_64 —setopt=protected_multilib=false

安装一些依赖

yum -y 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

一些版本的包,centos对应源中是没有的,特别是ImageMagick相关的东东。

所以单独还安装了以下包,可能会有一些冲突提示,我们就直接rpm -ivh --replacefiles *.rpm了

fftw-3.2.1-3.1.el6.x86_64.rpm
libjpeg-turbo-1.2.1-3.el6_5.x86_64.rpm
libmcrypt-2.5.8-9.el6.x86_64.rpm
zlib-1.2.3-29.el6.x86_64.rpm
libjpeg-6b-38.x86_64.rpm
liblcms2-2.4-1.el6.x86_64.rpm
xz-5.2.1

至此,可以执行终极Boss命令了:

yum install hhvm

结果如下:

CentOS6.2 试用PHP HHVM

看看相关命令行:

CentOS6.2 试用PHP HHVM

2. Run HHVM

写一个php脚本,我们将在多种模式下运行

<?php
echo "hello world\n";
phpinfo();

2.1 命令行run下

CentOS6.2 试用PHP HHVM

2.2 Server模式

hhvm安装默认的配置在/etc/hhvm/下,默认是server.hdf,还有php.ini。

这里我们下修改下server.hdf,如下:

PidFile = /var/run/hhvm/pid

Server {
Port = 9090
SourceRoot = /data/lebyzhao/test/
DefaultDocument = test.php
} Log {
Level = Warning
AlwaysLogUnhandledExceptions = true
RuntimeErrorReportingLevel = 8191
UseLogFile = true
UseSyslog = false
File = /data/log/hhvm/error.log
Access {
* {
File = /data/log/hhvm/access.log
Format = %h %l %u % t \"%r\" %>s %b
}
}
} Repo {
Central {
Path = /var/log/hhvm/.hhvm.hhbc
}
} # 以下内容省略...

执行/etc/init.d/hhvm start,

注意:这是一个wrapper脚本,实际是执行了:hhvm --mode server --user root --config /etc/hhvm/server.hdf ,hdf里面的配置是可以在命令行直接写的:hhvm --mode server -vServer.Type=fastcgi -vServer.Port=9090

CentOS6.2 试用PHP HHVM

OK,这便是运行了一个支持HTTP的PHP Server。你当前可以用Nginx代理或Curl来访问。

这里我们修改现有的nginx配置,让其转发到hhvm server上即可

        location ~ .*\.php$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://localhost:9090;
}

从Nginx上测试一把:

CentOS6.2 试用PHP HHVM

2.3 FastCGI模式

我们可以用如下命令来启动:

hhvm --mode server -vServer.Type=fastcgi -vServer.Port=
## 后台运行
hhvm --mode daemon -vServer.Type=fastcgi -vServer.Port=

此时,是用了Fastcgi协议的(HTTP不能在请求到了,比如可能提示:FastCGI protocol: received an invalid record),

相应修改Nginx对应的FastCGI配置:

       location ~ .*\.php$ {
fastcgi_pass 127.0.0.1:9090;
include fastcgi.conf;
}

自配置fastcgi.conf如下:

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param HTTPS $https if_not_empty; fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name; # PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;

重新加载Nginx配置后,再次请求Nginx,curl看到的结果与HHVM在server模式下,Nginx代理的结果完全一致。

参考: http://hhvm.com/blog/1817/fastercgi-with-hhvm

---------

http://www.tuicool.com/articles/uaqYFr