Welcome to our tutorial on installing LEMP on your brand-new Ubuntu 14.04 Stack. We’ll show you how to get up and running with an extremely popular web server stack. LEMP stands for Linux, Nginx (pronounced Engine-X), MariaDB, and PHP. We’ve taken care of the Linux part by choosing to run Ubuntu, so we just have to install the remaining components and we should be ready to run a highly-concurrent web server scripted with PHP using MariaDB as the database. Before we begin, you should have a non-root user configured with sudo privileges. If you haven’t done so, check out our “Getting Started with Your Stack” tutorial.

Installing nginx

Let’s begin by updating our package repositories to make sure we have the most up to date packages available to us.

sudo apt-get update

Now we can install nginx by running the following command

sudo apt-get install nginx

We’ll start the nginx process by running the daemon using the init service on Ubuntu

sudo service nginx start

If everything works perfectly, you should be able to point your browser to your Stack’s IP address and see the default nginx index page. At this point we have our web server set up and are ready to move on.

Screen Shot 2015-07-30 at 1.20.24 PM

Install MariaDB

We can now install the MariaDB server and configure it to our liking. Run the following command:

sudo apt-get install mariadb-server libmariadbd-dev

You will be prompted to enter a password to use as the root password for the database. You should choose a good password and keep it safe for further reference.

Once this has completed there is additional configuration to remove sample databases and users. This is highly recommended for databases that will be used for production. Run the following command

sudo mysql_secure_installation

You will be prompted to enter the root password you set during installation. After this, you should just hit enter to continue through the recommended configuration settings. After you’ve completed these steps, you should have a working MariaDB installation. Enter mysql -u root -p to enter your password and verify that everything worked correctly. We can move on and install PHP now.

Installing PHP

Since Nginx does not contain native PHP processing like some other web servers, we will need to install php5-fpm, which stands for “fastCGI process manager”. We will tell Nginx to pass PHP requests to this software for processing.

sudo apt-get install php5-fpm php5-mysql

Open up the PHP configuration file in your favourite command line text editor and make the following change. We’ll be using nano.

sudo nano /etc/php5/fpm/php.ini

Find the line that says ‘cgi.fix_pathinfo’ and change its value to be 0. It should look like the following:


By default, if an exact match for some request cannot be routed, PHP will attempt to resolve the path by accepting the closest possible match. For production-grade servers, this is not advised as it allows attackers to try many different paths to try and expose a part of your server. Once you’ve made the changes type the following to restart the PHP daemon

sudo service php5-fpm restart

We need to make a few changes to how nginx processes PHP files. Open up the following configuration file using nano:

sudo nano /etc/nginx/sites-available/default

First we’ll need to tell nginx to look for index.php before any other file when a client makes a request to the DocumentRoot. We also need to modify the server_name directive to point to our server’s domain name or public IP address. These settings correspond to the lines beginning with ‘index’ and ‘root’ in the configuration file. The actual configuration file includes some commented out lines that define error processing routines. We will uncomment those to include that functionality. For the actual PHP processing, we will need to uncomment a portion of another section. We will also need to add a try_files directive to make sure Nginx doesn’t pass bad requests to our PHP processor. Below is what your configuration file should look like following all of the above changes:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
	root /usr/share/nginx/html;
    index index.php index.html index.htm;
	server_name YOUR_IP_HERE;
	location / {
        try_files $uri $uri/ =404;
	error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
	location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;

Replace the ‘YOUR_IP_HERE’ with your Stack’s public IP address and press CTRL-O, Enter/Return, followed by CTRL-X to write the changes to the file and exit.

Final Words

That’s it! You’re ready to begin developing a website using all of the technologies we installed on your Stack today. Check out our other tutorials in our Community Section to see how to perform various other server administration and development tasks. From all of us at Stack Harbor, ahoy!