php-doc-en/reference/mongodb/tutorial/hhvm.xml

302 lines
8.6 KiB
XML
Raw Normal View History

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 336263 $ -->
<section xml:id="mongodb.tutorial.install.hhvm" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<title>Installation 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 ito
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
<emphasis>source</emphasis> into <code>/usr/local/hhvm/3.9.1</code>
(with the binary being at
<code>/usr/local/hhvm/3.9.1/bin/hhvm</code>).
</para>
<section xml:id="mongodb.tutorial.install.hhvm.nginx">
<title>NGINX</title>
<para>
We simply install NGINX by running <code>apt-get install nginx-full</code>. If this
fails to start, it is likely that you already have Apache running on the
same port. If that happens, you will see the following lines in
<code>/var/log/nginx/error.log</code>:
</para>
<programlisting role="shell">
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>
If this happens, simple remove Apache by running <code>apt-get remove
apache2</code>.
</para>
</section>
<section xml:id="mongodb.tutorial.install.hhvm.hhvm">
<title>HHVM</title>
<para>
I have installed HHVM from a source build, mostly because I needed one with
some of my own patches and debug symbols. The folks at Facebook also provide
pre-built packages, which is probably what you *want* to use in production and
development. You can find them at the
<link xlink:href="&url.facebook.hhvm.prebuild;">HHVM Wiki</link>.
</para>
<para>
You need to install 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>
Because I installed from source, I had to do some extra work. I had to create
<code>/var/run/hhvm</code>:
<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>
<para>
I also had to start HHVM as <code>www-data</code> user. For now, I am
running it in the foreground in <emphasis>server</emphasis> mode as
follows:
</para>
<programlisting role="shell">
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 xml:id="mongodb.tutorial.install.hhvm.nginxandhhvm">
<title>Making NGINX talk to HHVM</title>
<para>
Once HHVM runs, we need to tell NGINX how to talk to HHVM for
<code>.php</code> files. Although this is perhaps not the most clean way
of doing this, you can add the following snippet to
<code>/etc/nginx/sites-enabled/default</code>, just after the
<code>location / { … }</code> section:
</para>
<programlisting>
<![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 the snippet, you should restart NGINX:
</para>
<programlisting role="php">
<![CDATA[
sudo service nginx restart
]]>
</programlisting>
<para>
Just to see that it all works now, we will create a project directory and
in there, we place a ``index.php`` file with a ``phpinfo()`` file:
</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: <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:
<programlisting role="php">
<![CDATA[
<?php
phpinfo();
?>
]]>
</programlisting>
</para>
</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 xml:id="mongodb.tutorial.install.hhvm.driver">
<title>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 take the
following steps:
<itemizedlist>
<listitem>
<para>
Download the latest driver from
<code>https://github.com/mongodb-labs/mongo-hhvm-driver-prototype/releases</code>.
At the moment, there is only <link
xlink:href="&url.mongodb.hhvm.release;">hhvm-mongodb-1.0beta1.tgz</link>
so we will be using this one in the examples.
</para>
</listitem>
<listitem>
<para>
Unpack the archive: <code>tar -xvzf hhvm-mongodb-1.0beta1.tgz</code>
</para>
</listitem>
<listitem>
<para>
<code>cd</code> into the newly created directory: <code>cd hhvm-mongodb-1.0beta1</code>
</para>
</listitem>
<listitem>
<para>
Generate the configure files for the bundled libraries. For this to work,
you need to have the <code>automake</code>, <code>autoconf</code> and <code>libtool</code> packages
installed (through <code>apt-get</code>).
<programlisting>
<![CDATA[
cd libbson; ./autogen.sh; cd ..
cd libmongoc; ./autogen.sh; cd ..
]]>
</programlisting>
</para>
</listitem>
<listitem>
<para>
Generate the Makefile: <code>hphpize &amp;&amp; cmake .</code>
</para>
</listitem>
<listitem>
<para>
Build the driver: <code>make</code>
</para>
</listitem>
<listitem>
<para>
Install the driver: <code>sudo make install</code>. This last step tells
you were it has installed the binary file <code>mongodb.so</code>. In my
case, it showed: <code>Installing:
/usr/local/hhvm/3.9.1/lib/hhvm/extensions/20150212/mongodb.so</code>
</para>
</listitem>
</itemizedlist>
</para>
<para>
Now that the driver is installed, you need to enable it in HHVM. In order
to do this, you need to add the following lines to
<code>/etc/hhvm/php.ini</code>, swapping out my directory name for the
that showed when running <code>make install</code>:
<programlisting role="shell">
<![CDATA[
hhvm.dynamic_extension_path=/usr/local/hhvm/3.9.1/lib/hhvm/extensions/20150212
hhvm.dynamic_extensions[mongodb]=mongodb.so
]]>
</programlisting>
After you have done that, you need to stop HHVM by pressing Ctrl-C in the
shell running HHVM, and then start it again as above:
<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>
<para>
In order to test that it works, we edit our <code>index.php</code> file,
and replace its contents with:
<programlisting role="php">
<![CDATA[
<?php
var_dump(phpversion("mongodb"));
?>
]]>
</programlisting>
</para>
<para>
This should output something like:
<programlisting>
string(9) "1.0beta1"
</programlisting>
</para>
</section>
<section>
<title>Further reading</title>
<para>
Continue this tutorial by jumping to <xref
linkend="mongodb.tutorial.library"/>
</para>
</section>
</section>
<!-- 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
-->