How to Setup a LEMP Stack on Ubuntu 14.04 LTS

Last week I covered setting up a LAMP server on Ubuntu 14.04 LTS but for my money, it’s all about LEMP stacks. The “E” actually stands for Nginx (pronouced Engine X) and it’s an altnerative to the Apache web server that is built for speed and has a very low memory footprint. I usually use DigitalOcean to spin up a new server, but today I will be switching it up and using Linode as they recently started to offer hourly billing.

As always, I recommend making sure that your system is up to date. Even though Ubuntu 14.04 LTS is a very new release, there can be new packages available. As time goes on, there will be more and more updates, often times the updates are related to security making them very mandatory even for the casual server admin. To update your server, log in and run:

sudo apt-get update && apt-get dist-upgrade

Once your upgrade is completed, we will proceed to installing the EMP portion of our LEMP stack. To do so, you will need to run:

sudo apt-get install nginx php5-fpm php5-cli php5-mysql mysql-server

That command will install a bare bones LEMP server (including command-line PHP). Depending on your needs, you can install additional libraries as well as different servers (for instance PostgreSQL). Don’t forget to set a password on MySQL. It’s optional, but highly recommended.

At the time of Ubuntu 14.04 LTS’ release and the time of this writing, PHP is at version 5.5.x and that’s what is being shipped by Canonical with this release (albeit slightly behind the latest revision). In addition to PHP being on the latest stable branch, a version of nginx is also being shipped that is on the latest stable branch of 1.4.x. Not quite bleeding edge, but a far cry from the highly dated packages shipped with Ubuntu 12.04 LTS.

Now that all of our software is installed, let’s point our web browser to the IP address (or domain if applicable) of our server. Upon doing so, you should be greeting with a “Welcome to nginx!” page. It’s actually not even remotely as sophisticated as the Apache welcome page distributed by Debian / Ubuntu but it serves it’s purpose.

We’ve confirmed that nginx is installed properly, but what about PHP? First, let us create a file that will call phpinfo(), you can do so by issuing the following command:

echo '<?php phpinfo(); ?>' > /usr/share/nginx/html/phpinfo.php

If we were working with Apache, I’d immediately direct you to the file, but if I were to do that, nginx would serve the file to you as a download instead of processing it as a PHP script. To enable PHP processing, we will need to edit /etc/nginx/sites-available/default with our favorite editor (mine is still vim 😉

When editing the file, you will want to jump to line 54 which starts with location ~. .php$ { and uncomment the entire block of the directive. Once you have it uncommented, you will want to re-comment line 59 – fastcgi_pass 127.0.0.1:9000; so we do not have two active fastcgi_pass lines. Upon doing so, save your file and restart nginx by running:

sudo service nginx restart

Once nginx is restarted, you can now point your browser to the same IP (or domain) as before with the inclusion of /phpinfo.php as part of the URL. If you did everything correctly, you will be presented with the PHP info page and you will be ready to hack away on your brand new LEMP server!

Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.


If you found this article helpful, please consider buying me a coffee.