How to Setup a LEMP Stack on Ubuntu 12.04 LTS

Don’t get me wrong, there’s nothing wrong with a good ol’ LAMP stack (Linux, Apache, MySQL and PHP) but in my experience, Apache doesn’t necessarily scale all that well with PHP. It’s really not Apache’s fault, the fact is that most of the time servers are configured to use the PHP Module for Apache. The downside of using this module is that the module is loaded every time the server is accessed and memory usage can get clogged up pretty quick under higher volume.

For this post I’m going to discuss swapping out Apache for Nginx (pronounced Engine-X) which is not only faster and has a smaller memory footprint than Apache, but it also requires that PHP be run via FastCGI. You very well can maximize your Apache performance by opting to use FastCGI instead of the PHP Module (I’ll save that for another post).

Just like when we set up our LAMP server, I’m going to spin up a new Ubuntu 12.04 LTS x32 droplet with DigitalOcean (yes, an referral link ;). I always rely on Long Term Support releases when dealing with Ubuntu, but this guide should work fine on 12.10 and beyond.

I always like to make sure that I’m working with an up to date system, so the first thing we should do is:

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

Depending on how far behind your install disk / image was, this may take a couple of minutes and you may be required to reboot. The L is all ready to go, let’s go ahead and get our EMP on. The next step will install the required applications and the only prompt you should receive is regarding setting the MySQL password. You can omit the password, but I wouldn’t recommend it.

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

As of 12.04, php-fpm (the PHP FastCGI Process Manager) is part of the Canonical repository which cuts down our set up time down a bit. In previous versions of Ubuntu you would have to install an alternate FPM like spawn-fcgi and manually set up a wrapper and init script for it. I also included the package for the Hardened-PHP Project as I feel it’s a mandatory component to every server running PHP.

Once everything is done installing, you can try to access your new web server by pointing your browser to either the server’s IP address or a domain name you may already have pointed there. You will be greeted with a friendly:

Welcome to nginx!

Now that everything is up and running, are you ready to start hacking some PHP? Not quite yet my friend, unlike Apache’s PHP module, Nginx does require a bit more configuration to get PHP up and running. First thing we’ll need to do is to configure Nginx to route requests to PHP scripts to our FastCGI Process Manager. I’m going to walk you though doing this on the default Nginx site. Open up the file /etc/nginx/sites-enabled/default and jump down to the last uncommented }. Just above that (you should be inside the server block) you’ll want to add the following:

location ~ .php$
{
	try_files $uri =404
	include /etc/nginx/fastcgi_params
	fastcgi_pass 127.0.0.1:9000
	fastcgi_index index.php
	fastcgi_param SCRIPT_FILENAME /usr/share/nginx/www$fastcgi_script_name
}

The gist of that location directive is that it’s telling Nginx that when it sees a request for a PHP file, pass it along to the FastCGI Process Manager for handling. Once you have that added to the default site’s configuration file, you’ll need to reload Nginx so the settings take effect.

sudo /etc/init.d/nginx reload

Now we’re all set to test that PHP is working properly by creating a test script. With your favorite text editor, open up /usr/share/nginx/www/phpinfo.php and enter the following:

<?php
phpinfo
?>

Jump back over to your web browser and add /phpinfo.php to the server’s IP address or hostname and you should see that familiar PHP Info page. Now you’re ready to get to hacking!

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.