Setting up Nginx, HHVM, and Percona for Laravel

hhvmHHVM is an open-source virtual machine designed for executing programs written in Hack and PHP. HHVM uses a just-in-time (JIT) compilation approach to achieve superior performance while maintaining the development flexibility that PHP provides. This tutorial guides how to set up Nginx, HHVM, and Percona for Laravel Projects on Ubuntu.

First, we need to update system and install essentials (and remove apache2 if necessary)

[bash]apt-get update
apt-get install -y unzip vim git-core curl wget build-essential python-software-properties
apt-get remove apache2*[/bash]

Nginx

[bash]sudo add-apt-repository -y ppa:nginx/stable
sudo apt-get update
sudo apt-get install -y nginx
[/bash]

HHVM and FastCGI

First, we will add repo and install HHVM

[bash]wget -O – http://dl.hhvm.com/conf/hhvm.gpg.key | sudo apt-key add –
echo deb http://dl.hhvm.com/ubuntu saucy main | sudo tee /etc/apt/sources.list.d/hhvm.list
sudo apt-get update
sudo apt-get install hhvm
[/bash]

After installing, we will see the following screen:

[bash]********************************************************************
* HHVM is installed. Here are some more things you might want to do:
*
* Configure your webserver to use HHVM:
* $ sudo /usr/share/hhvm/install_fastcgi.sh
* $ sudo /etc/init.d/nginx restart
* $ sudo /etc/init.d/apache restart
* $ sudo /etc/init.d/hhvm restart
*
* Run command line scripts with HHVM:
* $ hhvm whatever.php
*
* 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
********************************************************************[/bash]

Next, we need to configure HHVM run under fast-cgi

[bash]sudo /usr/share/hhvm/install_fastcgi.sh # This restarts Nginx for us
sudo update-rc.d hhvm defaults # Set this to start on system bootup
sudo service hhvm restart # Restart the service now
[/bash]

After installing in a system with NginX, HHVM will configure Nginx for us automatically. We can view the content of the new file /etc/nginx/hhvm.conf and will see that it is similar to what PHP-FPM does. This file is also included inside the default nginx server at /etc/nginx/sites-available/default.

Next, we need to create a kind of “alias” so that when php is called, it calls hhvm. Since we likely have items (composer, phpunit) which assume “php” (technically php-cli) is available via the command line, we need a way to run HHVM instead of PHP. Note that the second to last line of our above HHVM install output says as much. We can run the one-liner displayed to use HHVM when PHP is called:

[bash]sudo /usr/bin/update-alternatives –install /usr/bin/php php /usr/bin/hhvm 60[/bash]

So from now on, we can use php as normal:

[bash]
[email protected]:~# php -v
HipHop VM 3.0.1 (rel)
Compiler: tags/HHVM-3.0.1-0-g97c0ac06000e060376fdac4a7970e954e77900d6
Repo schema: a1146d49c5ba0d6db903beb3a4ed8a3766fef182[/bash]

We can try by creating an index.php file and see if it can work well with NginX:

[bash]
[email protected]:~# echo "<?php phpinfo();" | sudo tee /usr/share/nginx/html/index.php
<?php phpinfo();
[email protected]:~# curl localhost/index.php
HipHop
[/bash]

Percona Server

Adding Repo:

[bash]apt-key adv –keyserver keys.gnupg.net –recv-keys 1C4CBDCDCD2EFD2A
echo "deb http://repo.percona.com/apt `lsb_release -cs` main" >> /etc/apt/sources.list.d/percona.list
echo "deb-src http://repo.percona.com/apt `lsb_release -cs` main" >> /etc/apt/sources.list.d/percona.list
apt-get update[/bash]

Then, we will install percona server:

[bash]sudo apt-get install percona-server-server-5.6 percona-server-client-5.6[/bash]

Configuring for Laravel Projects

As Laravel is tested as 100% compatible with HHVM (source), we can confident to use HHVM in production for Laravel projects.

First, we need to create a NginX host for the website:

[bash]vim /etc/nginx/sites-available/mywebsite[/bash]

with the following content:

[bash]server {
listen 80 default_server;

root /home/MYWEBSITE/public;
index index.html index.htm index.php;

server_name MYWEBSITE.com www.MYWEBSITE.com;

access_log /var/log/nginx/MYWEBSITE.laravel-access.log;
error_log /var/log/nginx/MYWEBSITE.laravel-error.log error;

charset utf-8;

location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}

location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; }

error_page 404 /index.php;

include hhvm.conf; # The HHVM Magic Here

# Deny .htaccess file access
location ~ /\.ht {
deny all;
}
}
[/bash]

Then, we will disable the default and enable the new laravel website:

[bash]sudo rm /etc/nginx/sites-enabled/default # Remove the sym-linked default config
sudo ln -s /etc/nginx/sites-available/mywebsite /etc/nginx/sites-enabled/mywebsite # Create a sym-link to the laravel config
sudo service nginx reload[/bash]

Install Composer

[bash]curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer[/bash]

Create/Deploy Laravel project

Follow the guide at How to manage Laravel projects with Git and BitBucket

Trouble-Shooting

1. Receiving locate error, we can fix it as follows

  • First, view the current locale setting

    [bash]locale[/bash]

  • Then, generate the missing locale and reconfigure locales to take notice:

    [bash][email protected]:/home/domain# sudo locale-gen en_US.UTF-8
    Generating locales…
    en_US.UTF-8… done
    Generation complete.
    [email protected]:/home/domain# sudo dpkg-reconfigure locales
    Generating locales…
    en_US.UTF-8… up-to-date
    Generation complete.[/bash]

  • If the above work still does not fix, we need to set some environment

    [bash]$ export LC_TYPE="en_US.UTF-8"
    $ export LC_ALL="en_US.UTF-8"[/bash]

    and then disable

    [bash]AcceptEnv LANG LC_*[/bash]

    in /etc/ssh/sshd_config so that it affects in all ssh sessions

2. add-apt-repository: command not found

Fix it by install software-properties-common package:

[bash]apt-get install software-properties-common[/bash]

3. libboost dependencies:

Error such

[bash] hhvm : Depends: libboost-filesystem1.53.0 but it is not installable
Depends: libboost-program-options1.53.0 but it is not installable
Depends: libboost-system1.53.0 but it is not installable
Depends: libboost-system1.53.0 but it is not installable
Depends: libboost-regex1.53.0 but it is not installable
Depends: libicu48 but it is not installable
Depends: libtasn1-3 but it is not installable
Depends: libboost-thread1.53.0 but it is not installable
E: Unable to correct problems, you have held broken packages.[/bash]

. The above error appeared when building in not up-to-date Ubuntu version (12.10 instead of 13.10 for instance). It is suggested to upgrade to last stable version (13.10) as follows (first try apt-get dist-upgrade if we are on the same major version – 13.x with the latest one)

[bash]apt-get install update-manager-core
apt-get update && apt-get dist-upgrade
do-release-upgrade[/bash]

Follow all screens’ steps to finish the installation before continuing with hhvm

4. Install HHVM on CentOS:

Install as follows:

[bash][[email protected] yum.repos.d]# yum install http://ftp.riken.jp/Linux/fedora/epel/6/i386/epel-release-6-8.noarch.rpm
[[email protected] yum.repos.d]# cd /etc/yum.repos.d
[[email protected] yum.repos.d]# wget http://www.hop5.in/yum/el6/hop5.repo
[[email protected] yum.repos.d]# sudo yum upgrade libgcc –setopt=protected_multilib=false
[[email protected] yum.repos.d]# sudo yum upgrade libstdc++ –setopt=protected_multilib=false
[[email protected] yum.repos.d]# yum install hhvm
[/bash]
For CentOS 7, use [bash]yum install http://dl.fedoraproject.org/pub/epel/beta/7/x86_64/epel-release-7-1.noarch.rpm[/bash]

5. Disabling hhvm

  • Stop service:

    [bash]/etc/init.d/hhvm stop[/bash]

  • Disable auto-start when system is started:

    [bash]update-rc.d hhvm disable[/bash]

Leave a Reply