ScaleScaleScaleScale

Tips / Nginx


How to run WordPress with HHVM on Nginx

hhvm

Today we will cover the configuration of HHVM + Nginx to serve a WordPress blog. But first, let’s understand what is HHVM and how can it help to boost your WordPress performance.

What is HHVM?

HipHop for PHP (aka HHVM) is virtual machine that was designed originally by Facebook engineers to gain more performance over the traditional PHP platforms. It can execute apps written in Hack and PHP languages.

Is HHVM a new PHP?

If we look at the architecture of this promising technology, yes, it’s rather different than traditional PHP, because it uses a JIT (just-in-time) compilation approach to get better performance. But if you look at the outside, it can serve your PHP code same as any other PHP server, with almost 100% compatibility.

Some HHVM Features are:

  • Uses Hack Language
  • JIT Compilation
  • FastCGI support
  • Increasing PHP5 Parity
  • hphpd debugger
  • Compatible with 99% of 20 top PHP frameworks

So, now that you know what is HHVM, let’s start configuring  WordPress on Nginx with HHVM. I will assume you already have WordPress installed, and connecting with your MySQL/MariaDB database.

Installing & Configuring HHVM

Check out this quick guides to install HHVM on your box:

Configuring HHVM 

Now that you have HHVM installed, let’s configure it properly.

nano -w /etc/hhvm/config.hdf

Delete all the content if exist, and paste this code inside:

Log {
  Level = Warning
  AlwaysLogUnhandledExceptions = true
  RuntimeErrorReportingLevel = 8191
}
 
MySQL {
  TypedResults = false
}
 
Eval {
  JitASize = 67108864
  JitAStubsSize = 67108864
  JitGlobalDataSize = 22554432
}
nano -w /etc/hhvm/server.hdf 

Delete all the content if exist, and paste this code inside:

PidFile = /var/run/hhvm/pid

Server {
  Port = 80
  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.hdf"
StaticFile {
  FilesMatch {
    * {
      pattern = .*.(dll|exe)
      headers { 
        * = Content-Disposition: attachment 
      }
    }
  }
  Extensions : StaticMimeTypes
}

MySQL {
  TypedResults = false
}

Edit hhvm server.conf file:

nano -w /etc/hhvm/server.conf

Delete all the content if exist, and paste this code inside:

PidFile = /var/run/hhvm/pid
 
Server {
  IP = 127.0.0.1
  Port = 9000
}
 
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
  }
}
 
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
  }
}
 
MySQL {
  TypedResults = false
}
 
Eval {
        Jit = true
}
 
VirtualHost {
    * {
        Pattern = .*
        RewriteRules {
            * {
                 pattern = ^(.*)$
                 to = index.php$1
                 qsa = true
            }
        }
    }
}

Restart HHVM:

/etc/init.d/hhvm restart

Test if HHVM is running

netstat -plunt | grep hhvm

You should see hhvm is running at 9000 port.

[root@my.server.com:~]netstat -plunt | grep hhvm
tcp   0  0 :::9000     :::*     LISTEN   18751/hhvm  

 

Installing  & Configuring Nginx

Follow this posts to ensure Nginx is properly installed:

  • How to install Nginx on Ubuntu Linux
  • How to install Nginx on CentOS 6 / RHEL
  • How to install Nginx from source

Once you have Nginx installed, we will configure it to work with HHVM.

Configure nginx.conf file, in my case it’s located at /etc/nginx/nginx.conf

nano -w /etc/nginx/nginx.conf

Add this content inside and modify to match your needs:

user nginx;
worker_processes 8; # adjust according to the number of CPUs you have
error_log logs/error.log crit;
worker_rlimit_nofile 8192;

events {
worker_connections 800; # you might need to increase this setting for busy servers
use epoll; # Linux kernels 2.6.x change to epoll
}

http {
server_names_hash_max_size 2048;
server_names_hash_bucket_size 512;

server_tokens off;

include mime.types;
default_type application/octet-stream;

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 10;

# Gzip on
gzip on;
gzip_min_length 1100;
gzip_buffers 4 32k;
gzip_types text/plain application/x-javascript text/xml text/css;

ignore_invalid_headers on;
client_max_body_size 20m;
client_body_buffer_size 15m;
client_header_timeout 400;
client_body_timeout 400;
send_timeout 400;
connection_pool_size 256;
client_header_buffer_size 4k;
large_client_header_buffers 4 32k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;

# Cache most accessed static files
open_file_cache max=10000 inactive=10m;
open_file_cache_valid 2m;
open_file_cache_min_uses 1;
open_file_cache_errors on;

# Website Includes
include "/etc/nginx/conf.d/*.conf";
}

Now let’s create a virtual host file for your website:

nano -w /etc/nginx/conf.d/website.com.conf

Add this content inside and modify paths/names as you need:

### website.com

server {

access_log off;
log_not_found off;
error_log logs/error_log crit;

listen 80;
server_name website.com www.website.com;
location ~* .(gif|jpg|jpeg|png|ico|wmv|3gp|avi|mpg|mpeg|mp4|flv|mp3|mid|js|css|wml|swf)$ {
root /var/www/website.com;

expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}

location / {
root /var/www/website.com;
index index.php index.html index.htm;
# WordPress rewrite rules for permalinks
try_files $uri $uri/ /index.php?$args;
}

# php parsing using HHVM at port 9000
location ~ .php$ {
root /var/www/website.com;
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_buffer_size 128k;
fastcgi_buffers 256 4k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}

}

Replace  /var/www/website.com with the real path and website name.

Reload Nginx to apply changes

/etc/init.d/nginx reload

Test your website browsing http://www.yoursite.com

Popular search terms:

  • hhvm wordpress
  • nginx hhvm centos 7
profile

Esteban Borges

Linux Geek, Webperf Addict, Nginx Fan. CTO @Infranetworking

  • Alex

    In your nginxvista configuration you keep declaring the root inside each location block. Declaring it inside the server block is enough as it inherits down the locations

  • Brightcluster

    Wrong