How to Set Up LEMH (Linux, Nginx, MariaDB, HHVM) Stack in Ubuntu Server

If you have administered a Linux server before, you will be familiar with the commonly used LAMP (Linux, Apache, MySQL, PHP) stack. However, Apache, MySQL and PHP can sometimes be resource intensive and may not be the best for your server. In this tutorial, we are going to replace each and every part of the stack, except Linux, with a better alternative – Nginx, MariaDB and HHVM.

1. This tutorial is done on a Ubuntu 14.04 server, though the instructions will work for any Debian-based distro as well.

2. This tutorial only focuses on installing and setting up the LEMH stack. It does not touch on the security aspect of the server.

3. You must already have SSH access to your server (or physical access to your machine) to carry out this tutorial.

Nginx is a lightweight, yet powerful, replacement for Apache. It is an open source reverse proxy server as well as a web server with a strong focus on high concurrency, high performance and low memory usage. Do note that Nginx is not compatible with Apache, so all the Apache configuration, particularly those .htaccess files that you created, won’t work in Nginx. It is also possible to run both Apache and Nginx on the same server, but that will involve some complicated configuration which we won’t be covering here.

Nginx is found in the Ubuntu repository, but it is often outdated. To get the latest stable version, add the following PPA to your system:

The latest version of Nginx as of this post is 1.6.2. Alternatively, if you want to try out the latest developmental version, you can use this PPA instead:

The latest developmental version as of this post is 1.7.8.

To install Nginx, run the command:

nginx-install-command

Once installed, run the following command to make sure it is started:

Next, in your web browser, go to the url “http://123.4.56.789” (replace 123.4.56.789 with your server’s IP address), and you should see the following:

nginx-working

For many years, MySQL is the default database management service for many applications. Since it was acquired by Oracle, many people started to question its “open-source”-ness, and the community (which included the founder of MySQL) decided to create a fork of MySQL and named it MariaDB. MariaDB is a drop-in replacement for MySQL, which means it is fully compatible with MySQL databases and can be used as a total replacement for MySQL.

Interesting fact: MySQL is founded by Michael Widenius and is named after his first daughter, My. MariaDB’s lead developer is also Michael Widenius and is named after his second daughter, Maria.

In your server, if you have previously installed MySQL, it is best to remove MySQL completely before installing MariaDB:

Next, add the PPA for MariaDB:

Note that you can also generate your own PPA here and find the location closest to your server.

Install MariaDB with this command:

During the installation, it will prompt you to set a password for the root user.

mariadb-set-root-password

After the installation, you can start MariaDB with the command:

HHVM is not a replacement for PHP. It 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.

Before we install HHVM, it is important to first install PHP in the server. Instead of installing the full PHP5 package, which includes a bunch of Apache files, we will only install php5-fpm, which will then install the necessary PHP dependencies. Also, we will need php5-fpm as a fallback for HHVM.

php5-installation

Once done, install HHVM with the commands:

hhvm-after-installation-setup

After HHVM is installed, proceed to configure it for Nginx with the command:

hhvm-install-fastcgi

Lastly, restart HHVM

To test if HHVM is working, create a “phpinfo.php” file in the Nginx root folder.

and paste the following line:

Press “Ctrl + o” to save and “Ctrl + x” to exit.

In your browser, load the url: http://123.4.56.789/phpinfo.php (replace 123.4.56.789 with your server’s IP address). You should see just the word “HipHop” to show that HHVM is working.

hhvm-hiphop-screen

Now that you have everything set up, it is time to create a virtual host to run your website.

Create a new config file that holds your website detail:

and paste in the following snippet:

You can change the server_name to point to your own domain and the root folder location where you want the files to be served.

HHVM has this bug of crashing occasionally without restarting itself; this will cause the website to fail with a 500 error. In this case, we will create a fallback system whereby PHP5-FPM will take over when HHVM fails. Notice the location @fallback block in the config snippet above? That is the callback when HHVM fails. To complete the equation, we still need to create the “hhvm-with-fallback.conf” file which is a modified version of the “hhvm.conf” file.

Paste the following snippet:

Save and exit the file.

Lastly, test Nginx settings with

and if everything is fine, restart Nginx.

That’s it. You have completed the LEMH stack setup on your Linux server.

28 comments

  1. Thank you for this wonderful tutorial, I want to try but wanted to used MySQL instead of MariaDB.
    Can you guide what changes require to install mysql and configure it ?

    • That is very easy. You just need to skip over the MariaDB installation part and replace with sudo apt-get install mysql-server. This will install MySQL.

  2. Ah cool that fall back. I have tried hhvm several times, but every time it could crash it after some load tests.
    Do you have any idea what this error is? And is there a good way to restart hhvm after this crash?

  3. Hello Damien,
    I was installing LEMH, I have installed nginx and MariaDB successfully, I was expecting that you will show us to install latest version of PHP (5.6.5). Can you guide on how to install latest version on PHP with nginx on ubuntu ?
    I tried to follow some tutorial, they suggested to use PPA https://launchpad.net/~ondrej/+archive/ubuntu/php5-5.6 , But it seems that it is also installing default apache files.

    So how to acheive this ? I mean how to have latest version of PHP with nginx and configured with hhvm ?

    Thank you
    Sanjay

    • Assuming you have already installed PHP, Nginx, and HHVM, all you need to do is to add the repository:

      sudo add-apt-repository ppa:ondrej/php5-5.6

      and update/upgrade your system. It should upgrade your current version of PHP to 5.6.5 (or later).

        • Alternatively, you can just install “php5-fpm”. It will grab and install
          only its dependencies, which is php5. It won’t install apache.

  4. I have installed step-by-step but i get error 403 Forbidden
    I execute like domain.tld/index.php works but not without index.php

    • In the virtual host file, add “index.php” beside “index.htm”, so it become:

      index index.html index.htm index.php;

  5. Hi,

    Have a LEMP environment is better than a LAMP (apache2, MySQL, PHP-FPM) environment ? I have a big webapp who need lot of resource and the Time To First Byte (TTFB is a bit long, about 600ms) so I’m trying to find a solution for improve that. I think HHVM is the good way, right ?

    • Apache is known for its power and Nginx for speed. For a small simple site, there isn’t really much difference between the two of them, but for heavy application, Apache will slow down under heavy load. As for HHVM, my
      experience is that it is fast, but unstable. If you opt for stability, stay with PHP (or PHP-FPM for Nginx).

  6. Damien,
    I can’t seem to get php or hhvm to interpret and render the phpinfo.php file, instead the browser asks me if i want to download the phpinfo.php text file.
    I have installed hhvm and php5-fpm and restarted both services but although the domains serve static html pages they won’t read and serve the phpinfo.php file. I’m guessing the php interpreter isn’t working.
    I am attempting to setup three virtual hosts configurations below:
    server {
    listen 80;
    server_name mydomain1.com http://www.mydomain1.com;
    root /srv/www/mydomain1;
    index index.php index.html index.htm;
    location / {
    try_files $uri $uri/ =404;
    }
    server {
    listen 80;
    server_name mydomain2.com http://www.mydomain2.com;
    root /srv/www/mydomain2;
    index index.php index.html index.htm;
    location / {
    try_files $uri $uri/ =404;
    }
    server {
    listen 80;
    server_name mydomain3.com http://www.mydomain3.com;
    root /srv/www/mydomain3;
    index index.php index.html index.htm;
    location / {
    try_files $uri $uri/ =404;
    }

    }

    Any ideas?

  7. I figured out the problem.
    I needed to do include the hhvm.conf in the server block as below
    server {
    etc same as before..
    include hhvm.conf;
    }
    This worked!

Comments are closed.

Sponsored Stories