php-doc-en/reference/mongodb/tutorial/hhvm.xml
2016-01-25 18:52:08 +00:00

249 lines
7 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 336263 $ -->
<chapter xml:id="mongodb.tutorial.hhvm" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Getting Started with HHVM</title>
<para>
In this section you will learn how to set-up NGINX with HHVM and MongoDB.
However, this tutorial is not meant to be an all inclusive guide on how to
run NGINX and HHVM in a production environment.
</para>
<para>
In this tutorial, we will be using a Debian system with NGINX installed
through <code>apt-get</code> and HHVM installed from source or pre-built
packages into <code>/usr/local/hhvm/3.9.1</code> (with the binary located at
<code>/usr/local/hhvm/3.9.1/bin/hhvm</code>).
</para>
<section>
<title>Installing NGINX</title>
<para>
We simply install NGINX by running <code>apt-get install nginx-full</code>.
If NGINX fails to start after installation, it is likely that you already
have another web server (e.g. Apache) running on the same port. In that case,
you will see the following lines in <code>/var/log/nginx/error.log</code>:
</para>
<programlisting role="shell">
<![CDATA[
2015/09/29 10:19:27 [emerg] 22445#22445: bind() to 0.0.0.0:80 failed (98:Address already in use)
2015/09/29 10:19:27 [emerg] 22445#22445: bind() to [::]:80 failed (98: Address already in use)
]]>
</programlisting>
<para>
To resolve this, you can either change the default port for NGINX or Apache,
stop the Apache process with <code>service apache2 stop</code>, or remove
Apache entirely with <code>apt-get remove apache2</code>.
</para>
</section>
<section>
<title>Installing HHVM</title>
<para>
This tutorial is written from the perspective of an extension developer, so
we've installed HHVM from source to faciliate patch development and ensure
that debug symbols are available. That said, the folks at Facebook also
provide pre-built packages, which is probably what <emphasis>you</emphasis>
will want to use in production and development. You can find installation
instructions for these packages on the
<link xlink:href="&url.facebook.hhvm.prebuild;">HHVM Wiki</link>.
</para>
<para>
You will need to install both the the <code>hhvm</code>
<emphasis>and</emphasis> <code>hhvm-dev</code> packages. The latter is needed
so that we can compile the MongoDB HHVM extension later on.
</para>
<para>
If you are compiling HHVM from source, you will need to create
<code>/var/run/hhvm</code>:
</para>
<programlisting role="shell">
<![CDATA[
sudo mkdir -p /var/run/hhvm
sudo chown www-data.www-data /var/run/hhvm
sudo mkdir /etc/hhvm
sudo touch /etc/hhvm/php.ini
# So that you don't have to ``sudo`` to edit the file
sudo chown derick /etc/hhvm/php.ini
# To see whether it actually works
echo "date.timezone=Europe/London" >> /etc/hhvm/php.ini
]]>
</programlisting>
<para>
HHVM should be started as the <code>www-data</code> user. For the purposes of
this tutorial, we can run it in the foreground in <emphasis>server</emphasis>
mode as follows:
</para>
<programlisting role="shell">
<![CDATA[
sudo -u www-data -s /usr/local/hhvm/3.9.1/bin/hhvm \
--mode server \
-vServer.Type=fastcgi \
-vServer.FileSocket=/var/run/hhvm/sock
]]>
</programlisting>
</section>
<section>
<title>Making NGINX Talk to HHVM</title>
<para>
Once HHVM runs, we need to tell NGINX how to talk to HHVM for executing
<code>.php</code> files. Although this is perhaps not the cleanest approach,
you can add the following snippet to
<code>/etc/nginx/sites-enabled/default</code>, just after the
<code>location / { … }</code> section:
</para>
<programlisting role="conf">
<![CDATA[
location ~ \.php$ {
fastcgi_pass unix:/var/run/hhvm/sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
]]>
</programlisting>
<para>
After adding that snippet, you should restart NGINX:
</para>
<programlisting role="shell">
<![CDATA[
sudo service nginx restart
]]>
</programlisting>
<para>
To confirm that everything works so far, we will create a project directory
with an <code>index.php</code> file that calls <code>phpinfo()</code>:
</para>
<para>
<itemizedlist>
<listitem>
<para>
Create the project directory:
<code>sudo mkdir -p /var/www/html/my-first-project</code>
</para>
</listitem>
<listitem>
<para>
Change permissions to your user and the <code>www-data</code> group:
<code>sudo chown derick.www-data /var/www/html/my-first-project</code>
</para>
</listitem>
<listitem>
<para>
Create the file <code>/var/www/html/my-first-project/index.php</code>.
From now on, I will <emphasis>not</emphasis> include the full path
<code>/var/www/html/my-first-project/</code> when I mention file names.
Put the following content in this file:
</para>
<programlisting role="php">
<![CDATA[
<?php
phpinfo();
?>
]]>
</programlisting>
</listitem>
</itemizedlist>
</para>
<para>
Now, in your browser, request the page
<code>http://gargleblaster/my-first-project/index.php</code> (but adjust the
hostname). This should then show a page starting with "HHVM Version 3.9.1"
followed by several tables with information.
</para>
</section>
<section>
<title>Installing the MongoDB Driver for HHVM</title>
<para>
The MongoDB driver is the part that links up the PHP in HHVM with the
database server. To install and register the driver with HHVM, you need to
follow the steps in <xref linkend="mongodb.installation.hhvm"/>.
</para>
<para>
After you have installed and enabled the extension, HHVM will need to be
restarted. If you have HHVM running in the shell from earlier, stop it with
Ctrl-C and restart it again as above:
</para>
<programlisting role="shell">
<![CDATA[
sudo -u www-data -s /usr/local/hhvm/3.9.1/bin/hhvm \
--mode server \
-vServer.Type=fastcgi \
-vServer.FileSocket=/var/run/hhvm/sock
]]>
</programlisting>
<para>
In order to test that the driver is loaded, edit the <code>index.php</code>
file and replace its contents with:
</para>
<programlisting role="php">
<![CDATA[
<?php
var_dump(phpversion("mongodb"));
?>
]]>
</programlisting>
<para>This should output something similar to:</para>
<programlisting role="text">
<![CDATA[
string(5) "x.y.z"
]]>
</programlisting>
</section>
<section>
<title>Further Reading</title>
<para>
Continue this tutorial by jumping to
<xref linkend="mongodb.tutorial.library"/>.
</para>
</section>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->