Today I am going to share an interesting article with you all. Although we all know, Facebook, the most used social network with over 1.3 billion active users worldwide, as of June 2014, is built on PHP. PHP is the most common scripting language for server side web development. So the problem in scripting language in general is their performance. They are easy to learn, easy to use, feature rich, but lack in performance. Below is the set of different languages for it’s performance against the CPU timing.
Therefore, this performance issue creates lots of implications for facebook. Bad performance can limit a lot of features for users, and requires lots of resources. So that raises a question for facebook. How they can make PHP faster and improve its performance. This is why Facebook started Facebook Hiphop compiler project to save resources on their servers.
HipHop is a source code transformer for PHP script code. It programmatically convert your PHP source code into optimized C++ ,and then it uses g++ to compile it. It executes the source code in such a manner that it will remain in a semantically equivalent manner. It sacrifices the not often used features ( such as eval() ) of PHP in exchange of getting the improved performance. It includes a code transformer, which is a re-implementation of PHP’s runtime system, and a rewrite of many common PHP Extensions to take full advantage of these performance optimizations.
Its about 6 times faster than standard PHP code implementation.
What is HHVM?
HipHop helped facebook to increase the performance of their code. Their developers were also forced to develop a totally separate HipHop interpreter (HPHPi) that requires a lot of effort and resources to maintain. So, last year, they decided to create a small team to demonstrate the dynamic translation of PHP code into static native machine code. The result was HipHop Virtual Machine HHVM which is based upon HipHop language runtime.
Now, the world famous Facebook uses HHVM for the every day software development. HHVM, the open source virtual software is constructed to configure the Hack and PHP programs. The JIT (Just-in-time) approach used in HHVM improves the performance quality and increases the flexibility in development.
Installing HipHop Virtual Machine (HHVM)
Installing HHVM is quite easy and will not take more than a few minutes. Mentioned below are the commands which you can run from the command line to run HHVM smoothly
wget -O – http://dl.hhvm.com/conf/hhvm.gpg.key | sudo apt-key add –
echo deb http://dl.hhvm.com/ubuntu trusty main | sudo tee /etc/apt/sources.list.d/hhvm.list
sudo apt-get update
sudo apt-get install hhvm
To check that installation of HHVM, type
hhvm –help
You can get the prebuilt installation packages for HHVM for different OS from below link :
https://github.com/facebook/hhvm/wiki/Prebuilt%20Packages%20for%20HHVM
Let us now test a PHP file from the command line using HHVM
Type the following command on the command line:
root@aman:~$ touch start_with_hhvm.php
This will create a file named start_with_hhvm.php in the current location. Open this file using nano or vim, and enter the below content into it.
<?php
echo “Here is the first test script using HHVMn”;
Once this file has been created, HHVM can be used for execution using the following command:
root@aman:~$ hhvm start_with_hhvm.php
This will print the below output :
Here is the first test script using HHVM
Let us test a PHP file using the HHVM Server (accessible from the browser)
Place the same file anywhere in the web server(Apache or Nginx). Suppose, it is placed in /var/www folder. First start the hhvm server from the command line with following command:
root@aman:~$ hhvm -m server
This command will start the server (on port 80). Now you can test the file by typing the below mentioned url in the browser :
http://localhost/start_with_hhvm.php
This will showcase this output in browser :
Here is the first test script using HHVM
Porting Your existing PHP Application to HHVM
If you want to start serving your existing PHP application using HHVM, instead of Apache, the Apache service has to be stopped. You can do this by using this command:
root@aman:~$ service apache2 stop
The above mentioned command will stop Apache ( free up port 80 which is commonly used for Apache). Now, HHVM can use this port. Further, we need to start the HHVM server in the root directory of your existing PHP application. The most common location of this root directory on Ubuntu is /var/www/ ,and in latest ubuntu it is /var/www/html/.
Change the location to this directory using this command:
root@aman:~$ cd /var/www
When you are in this directory, you just need to start the HHVM server by using the following:
root@aman:~$ hhvm -m server
This command will start the HHVM server, and this will begin serving your PHP application from the current directory.
Important Note
HHVM is currently corporated with lots of commonly used PHP extensions. If an application uses a PHP extension that hasn’t been incorporated yet, choosing HHVM will break your application. The complete list of PHP extensions which are successfully ported over to HHVM can be found from below link
https://github.com/facebook/hhvm/tree/master/hphp/runtime/ext/
Conclusion
If you have done everything correct, you are ready to start the HHVM and serve your PHP based websites. HHVM can help you serve a lot more visitors to your website with much lower hardware requirements.