📜 ⬆️ ⬇️

HHVM, Nginx and PHP (as well as Laravel)

HHVM + Nginx + PHP + Laravel A lot of people were interested in installing HHVM on Nginx for use with Laravel. Let's get started.

HHVM (or HipHop Virtual Machine) is a virtual machine designed to run programs written in PHP. Uses JIT compilation technology to increase bytecode execution speed.

Introduction

As in most of my articles, I will use Ubuntu 12.04 LTS as a server to install our good. However, we can easily install it all on a Mac using Brew ( nginx and hhvm ). Information on installing HHVM on other platforms (including newer versions of Ubuntu) can be found here .
')
Well, let's go!

Installation Basics

First, establish the basic things necessary for our purpose:

$ sudo apt-get update $ sudo apt-get install -y unzip vim git-core curl wget build-essential python-software-properties 


Nginx installation

Next, install Nginx. Why does he go first? Since installing the hhvm-fastcgi package , it [the package] will change some Nginx configurations if it detects it.

 $ sudo apt-get install -y nginx 


Installing HHVM FastCGI

Judging by the HHVM blog, we can install HHVM with FastCGI. The following code will install HHVM and configure it to work with FastCGI.

 $ echo deb http://dl.hhvm.com/ubuntu precise main | sudo tee /etc/apt/sources.list.d/hhvm.list $ sudo apt-get update $ sudo apt-get install -y --force-yes hhvm-fastcgi 

Note: I added the --force-yes flag to fix some dependency problems.

HHVM Setup

HHVM is essentially a native version of PHP, so we don’t need to install PHP separately. After installation, you can use HHVM as normal PHP. For example, you can execute php files:

 $ hhvm some_file.php 

Since we probably have Composer and PHPUnit, which assume accessibility from the command line, we can create a php symlink on hhvm:

 $ sudo ln -s `which hhvm` /usr/local/bin/php 

Now we can use PHP as usual!

 $ php -v HipHop VM v2.3.2 (rel) Compiler: tags/HHVM-2.3.2-0-gf951cb8d8812c59344d5322454853b584b668636 Repo schema: 5b5a4fc9cde5a5d014d1dfdb491bf74e4e700131 


HHVM Fast-CGI

I use Vagrant and I want the / vagrant directory to be my root directory. To change the root, we need to configure both HHVM and Nginx.

Let's start with HHVM. File to edit: /etc/hhvm/server.hdf Here is a one-line command to execute:

 # Change doc root from /var/www/ to /vagrant/ $ sudo sed -i 's/SourceRoot = \/var\/www\//SourceRoot = \/vagrant\//' /etc/hhvm/server.hdf 

Then restart HHVM to apply the changes:

 $ sudo service hhvm-fastcgi restart 


Configuring Nginx

Now configure Nginx. I will create a new configuration file called vagrant:

 # Create and edit our new configuration file $ sudo vim /etc/nginx/sites-available/vagrant 

Here you can see the final content of this file:

 // File /etc/nginx/sites-available/vagrant server { root /vagrant; index index.html index.htm index.php; server_name localhost; # 192.168.33.10.xip.io if you are using Vaprobash include hhvm.conf; # Include HHVM's configuration file for Nginx location / { try_files $uri $uri/ /index.php?q=$uri&$args; } location ~ /\.ht { deny all; } } 


Two important notes

First, we do not define a location block. Installing Nginx to HHVM represents the ability of HHVM to see Nginx and create the file /etc/nginx/hhvm.conf for you. The hhvm.conf file that we connect already has a location block needed by PHP.
Secondly, the hhvm.conf file assumes the root directory / var / www. This overwrites the root / vagrant setting. We need to change this so that our vagrant file defines the root directory by deleting the root directive in the hhvm.conf file:

 $ sudo vim /etc/nginx/hhvm.conf # Then comment out the line 'root /var/www' and save 

The /etc/nginx/hhvm.conf file itself:

 location ~ \.php$ { # root /var/www fastcgi_keep_conn on; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name; include fastcgi_params; } 

Now let's turn on the virtual host we just created:

 $ sudo ln -s /etc/nginx/sites-available/vagrant /etc/nginx/sites-enabled/vagrant 


Now you have to restart HHVM-FastCGI and Nginx for the changes to take effect. But first, create a test php file.

Some PHP

Create a simple test file:

 $ vim /vagrant/index.php 

Something simple like this:

 <?php echo phpinfo(); // Expected output: HipHop 

Things are easy. Just restart hhvm-fastcgi and nginx. After that, you will be able to see index.php when connecting to the server’s IP address (or to 192.168.33.10.xip.io, if you use Vaprobash ) in the browser.

 $ sudo service hhvm-fastcgi restart $ sudo service nginx reload 

You should see “HipHop” in the browser.

Laravel

In our case, the installation of Laravel is no different from the usual installation thereof. Since we simlink PHP, everything should work like a clock. Let's try.

Composer installation

This will be an ordinary global installation:

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


Install laravel

Now we have the opportunity to create a new laravel project. I will call it hhlaravel:

 # Move to /vagrant so we install Laravel # into /vagrant/hhlaravel $ cd /vagrant $ composer create-project laravel/laravel hhlaravel 

You can drink tea or coffee until all dependencies are established :)

Last step: configure Laravel

Let's do a little tweak and set the root directory to / public:

 # Fast CGI document root $ sudo sed -i 's/SourceRoot = \/vagrant\//SourceRoot = \/vagrant\/hhlaravel\/public\//' /etc/hhvm/server.hdf # Nginx document root sudo sed -i 's/root \/vagrant;/root \/vagrant\/hhlaravel\/public;/' /etc/nginx/sites-available/vagrant # Reload configuration $ sudo service hhvm-fastcgi restart $ sudo service nginx reload 

All is ready! You can check the performance using a browser.

Warnings and notes

You will want to pay much more attention to the error logs. By default, HHVM does not display any errors in the browser. Check the log of laravel:

 # Show last 50 lines written out to laravel log $ tail -n 50 -f /vagrant/hhlaravel/app/logs/laravel.log 

Check the HHVM log:

 $tail -n 50 -f /var/log/hhvm/error.log 

You can also get the stack trace to your logs by changing the server configuration .

Edit /etc/hhvm/server.hdf and add the directives InjectedStackTrace and NativeStackTrace.
 Log { Level = Warning AlwaysLogUnhandledExceptions = true RuntimeErrorReportingLevel = 8191 UseLogFile = true UseSyslog = false File = /var/log/hhvm/error.log InjectedStackTrace = true NativeStackTrace = true Access { * { File = /var/log/hhvm/access.log Format = %h %l %u % t \"%r\" %>s %b } } } 

And finally, you can add some output to the browser window by adding Debug settings to /etc/hhvm/server.hdf:

 Debug { FullBacktrace = true ServerStackTrace = true ServerErrorMessage = true TranslateSource = true } 

However, remember that error logs will have more detailed information about the error. There is an article about using remote debugger for HHVM (I haven't experienced it myself).

Source: https://habr.com/ru/post/208778/


All Articles