Monday, 26 October 2015

Install HHVM in Ubuntu Linux Server for high performance PHP applications and high performance wordpress websites

HHVM (HipHop Virtual Machine) is an open-source virtual machine designed for executing programs written in Hack and PHP developed at Facebook. HHVM uses a just-in-time (JIT) compilation approach to achieve superior performance while maintaining the development flexibility that PHP provides.

HHVM runs much of the world’s existing PHP. Developers and hosts are adopting HHVM. It can run the top 20 Github PHP frameworks out of the box. The HHVM team, along with many wonderful community members, has made it a stated, high priority goal to run all existing PHP code existing out in the wild.


Why you should use HHVM instead of PHP?

Install HHVM in Ubuntu Server 14.04 LTS

1. Add the Ubuntu 14.04 LTS HHVM repository to the server

wget -O - http://dl.hhvm.com/conf/hhvm.gpg.key | apt-key add -
echo deb http://dl.hhvm.com/ubuntu trusty main | tee /etc/apt/sources.list.d/hhvm.list

apt-get update

apt-get install hhvm

2. Confirm that hhvm is installed by running below command :

hhvm --help

3. Install apache2

apt-get install apache2

4. Add HHVM to  apache

/usr/share/hhvm/install_fastcgi.sh 

/etc/init.d/apache2 restart

5. Configure the server to use HHVM instead of PHP for all php commands

update-rc.d hhvm defaults

/usr/bin/update-alternatives  --install /usr/bin/php php /usr/bin/hhvm 60

6. Configure Apache to use HHVM

cat /etc/apache2/sites-available

<VirtualHost *:80>
ServerAdmin [email protected]
        DocumentRoot /var/www/html
       <Directory />
                Options FollowSymLinks
                AllowOverride All
        </Directory>
        <Directory /var/www/html>
                DirectoryIndex index.php index.html
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>
#ProxyPass / fcgi://127.0.0.1:9000/path/to/your/www/root/goes/here/
# Or if you used a unix socket
# ProxyPass / unix://var/run/hhvm/sock|fcgi://127.0.0.1:9000/path/to/your/www/root/goes/here/
#ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/path/to/your/www/root/goes/here/$1 

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

7. Configure HHVM

cat /etc/hhvm/server.ini

; php options

pid = /var/run/hhvm/pid

; hhvm specific 

hhvm.server.port = 9000
hhvm.server.type = fastcgi
hhvm.server.default_document = index.php
hhvm.log.use_log_file = true
hhvm.log.file = /var/log/hhvm/error.log
hhvm.repo.central.path = /var/run/hhvm/hhvm.hhbc

8. Configure HHVM as an alternative to  PHP

Edit /etc/hhvm/php.ini 

; php options
session.save_handler = files
session.save_path = /var/lib/hhvm/sessions
session.gc_maxlifetime = 1440

; hhvm specific 
hhvm.log.level = Warning
hhvm.log.always_log_unhandled_exceptions = true
hhvm.log.runtime_error_reporting_level = 8191
hhvm.mysql.typed_results = false

9. Configure HHVM as a service in Ubuntu

cat /etc/init.d/hhvm

CONFIG_F
ILE="/etc/hhvm/server.ini"
SYSTEM_CONFIG_FILE="/etc/hhvm/php.ini"
RUN_AS_USER="www-data"
RUN_AS_GROUP="www-data"
ADDITIONAL_ARGS=""


Note: Only important configurations are given, actual file has more contents

10. Configure HHVM with Fast CGI

cat /etc/apache2/mods-available/hhvm_proxy_fcgi.conf 

ProxyPassMatch ^/(.+\.(hh|php)(/.*)?)$ fcgi://127.0.0.1:9000/var/www/html/$1

Note: Change the document root accordingly to serve the site

11. Install MySQL server

sudo apt-get install mysql-server php5-mysql


CREATE USER [email protected] IDENTIFIED BY 'my_db_pass';

GRANT ALL PRIVILEGES ON my_db.* TO [email protected];

FLUSH PRIVILEGES;

12. Download and install wordpress

Now you can download and install wordpress and follow the basic wordpress installation steps. It should just work fine. Instead of using PHP, it uses HHVM if you have followed the above configurations correctly. You don't have to do any thing special here to make wordpress use HHVM.

References:
http://blakepetersen.io/how-to-set-up-and-configure-hhvm-on-ubuntu-14-04/
https://richardtape.com/2015/03/07/from-php-to-hhvm/

Extras:

Below given are the useful HHVM commands:

update-alternatives: using /usr/bin/hhvm to provide /usr/bin/php (php) in auto mode
********************************************************************
* HHVM is installed.
*
* Running PHP web scripts with HHVM is done by having your
* webserver talk to HHVM over FastCGI. Install nginx or Apache,
* and then:
* $ sudo /usr/share/hhvm/install_fastcgi.sh
* $ sudo /etc/init.d/hhvm restart
* (if using nginx)  $ sudo /etc/init.d/nginx restart
* (if using apache) $ sudo /etc/init.d/apache restart
*
* Detailed FastCGI directions are online at:
* https://github.com/facebook/hhvm/wiki/FastCGI
*
* If you're using HHVM to run web scripts, you probably want it
* to start at boot:
* $ sudo update-rc.d hhvm defaults
*
* Running command-line scripts with HHVM requires no special setup:
* $ hhvm whatever.php
*
* You can use HHVM for /usr/bin/php even if you have php-cli
* installed:
* $ sudo /usr/bin/update-alternatives \
*    --install /usr/bin/php php /usr/bin/hhvm 60




No comments:

Post a Comment