HHVM+Nginx+MariaDB加速PHP网站

HHVM是Facebook开源的PHP、Hack执行引擎,因为Facebook大多数的网页都是用PHP写的(包括Mark Zuckerburg在宿舍写的那部分),而PHP官方的zend又是效率极低。HHVM的Benchmark显示其效率比现在广泛使用的PHP5.x高5~10倍。搭配mariadb和nginx这两个轻量级的服务器软件,PHP网页的执行效率可能是用LAMP(Linux Apache2 MySQL PHP)搭建的网页高2~3倍,具体参照本网站的加载速度。

以下是教程

默认安装了Ubuntu 14.04LTS或以上版本,如果没有的话不保证成功,自行替换命令或Docker部署ubuntu镜像

首先安装Nginx,

sudo apt-get update
sudo apt-get install nginx
service nginx restart
至此,访问你的网站,如etasry.ml,将会看到

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org

Please use the reportbug tool to report bugs in the nginx package with Debian. However, check existing bug reports before reporting a new bug.

Thank you for using nginx.

然后安装HipHop Virtual Machine

sudo apt-get install autoconf automake binutils-dev build-essential cmake g++ gawk git
  libboost-dev libboost-filesystem-dev libboost-program-options-dev libboost-regex-dev
  libboost-system-dev libboost-thread-dev libboost-context-dev libbz2-dev libc-client-dev libldap2-dev
  libc-client2007e-dev libcap-dev libcurl4-openssl-dev libdwarf-dev libelf-dev
  libexpat-dev libgd2-xpm-dev libgoogle-glog-dev libgoogle-perftools-dev libicu-dev
  libjemalloc-dev libmcrypt-dev libmemcached-dev libmysqlclient-dev libncurses-dev
  libonig-dev libpcre3-dev libreadline-dev libtbb-dev libtool libxml2-dev zlib1g-dev
  libevent-dev libmagickwand-dev libinotifytools0-dev libiconv-hook-dev libedit-dev
  libiberty-dev libxslt1-dev ocaml-native-compilers libsqlite3-dev libyaml-dev libgmp3-dev
  gperf libkrb5-dev libnotify-dev
安装完所有依赖项后,克隆源代码到本地
git clone git://github.com/facebook/hhvm.git --depth=1
cd hhvm
git submodule update --init --recursive
编译并安装,替换-J后面的数字为你的CPU数量
cmake -DMYSQL_UNIX_SOCK_ADDR=/var/run/mysqld/mysqld.sock .
make -j [number_of_processor_cores] # eg. make -j 4
sudo make install
二进制文件在/usr/local/bin/hhvm

随后执行/etc/hhvm/install-fastcgi.sh自动配置nginx,完成后你的nginx就会将PHP文件交给HHVM处理。

写个简单的脚本测试一下:

<?php
phpinfo();
?>
访问后如果你的HHVM执行这段代码的话应该返回HHVM Version 3.12.1

然而WordPress是不可以直接运行的,Nginx不负责解析.htaccess文件,所以Permalinks全部会挂,显示404或者403。

以下分享一个我的WordPress W3TotalCache的Nginx.conf,仅供语法参考

# BEGIN W3TC Page Cache cache
location ~ /wp-content/cache/page_enhanced.*html$ {
    add_header Vary "Accept-Encoding, Cookie";
}
location ~ /wp-content/cache/page_enhanced.*gzip$ {
    gzip off;
    types {}
    default_type text/html;
    add_header Vary "Accept-Encoding, Cookie";
    add_header Content-Encoding gzip;
}
# END W3TC Page Cache cache
# BEGIN W3TC Browser Cache
gzip on;
gzip_types text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
# END W3TC Browser Cache
# BEGIN W3TC Page Cache core
set $w3tc_rewrite 1;
if ($request_method = POST) {
    set $w3tc_rewrite 0;
}
if ($query_string != "") {
    set $w3tc_rewrite 0;
}
if ($http_cookie ~* "(comment_author|wp-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle)") {
    set $w3tc_rewrite 0;
}
if ($http_cookie ~* "(w3tc_preview)") {
    set $w3tc_rewrite _preview;
}
set $w3tc_enc "";
if ($http_accept_encoding ~ gzip) {
    set $w3tc_enc _gzip;
}
if (!-f "$document_root/wp-content/cache/page_enhanced/$http_host/$request_uri/_index$w3tc_rewrite.html$w3tc_enc") {
  set $w3tc_rewrite 0;
}
if ($w3tc_rewrite = 1) {
    rewrite .* "/wp-content/cache/page_enhanced/$http_host/$request_uri/_index$w3tc_rewrite.html$w3tc_enc" last;
}
# END W3TC Page Cache core

 

留言板