Cloud Servers Help

Build a LEMP stack (Linux, NGINX, MySQL, PHP) - Debian 7

Difficulty: 3
Time: 20 minutes

To build a dynamic web application, you need what has been coined a “stack” - which is developer lingo for an integrated set of software that has all of the components your application needs.

Most stacks have the same types of component, and differ mainly in which pieces of software they use for those components. For example, we’ll look at LEMP (which this article covers):

Component LAMP Software
Operating system Linux (e.g. Ubuntu)
Web server NGINX (pronounced engine-x)
Database MySQL
Programming language PHP

If you're astute, maybe you noticed that LEMP is basically an acronym for the software the stack uses (LNMP is difficult to pronounce in English, so they used “E” for NGINX because of its pronunciation).

In this guide, we'll walk you through installing all of these components (except for Linux, which is already installed as your OS when you create the server).

Install and start NGINX

  1. Update apt installation package. The NGINX installation package may have been included with your OS. If not, or if you don't know, type:
    sudo apt-get update
  2. Install NGINX:
    sudo apt-get install nginx
  3. Start the NGINX service:
    sudo service nginx start
  4. Verify that NGINX is installed by going to:
    http://your server's IP address

    GoDaddy Cloud Server customers can use Find your server's IP.

A Welcome to nginx! page displays.

Install MySQL

  1. Install MySQL:
    sudo apt-get install mysql-server php5-mysql
  2. Enter a root password, and then press enter.

    It is possible to not enter a password here. However, we cannot urge you strongly enough to enter a strong password here. Without one your server becomes incredibly vulnerable.

  3. Activate MySQL:
    sudo mysql_install_db
  4. Set MySQL to start on server boot:
    sudo update-rc.d mysql defaults
  5. Secure your database server:
    sudo /usr/bin/mysql_secure_installation
    Answer the following questions when prompted.
    Question Suggested Answer Why/Why not?
    Enter current password for root (enter for none): Enter the password you created earlier. MySQL needs this password to have sufficient access to system resources, so that it can function as a database server.
    Do you want to change the root password? n Unless you want to change what you just did.
    Remove anonymous users? y The anonymous user allows anyone to login without requiring an account created by an admin. A hacker attempting to compromise your database server may try logging in as this user.
    Disallow root login remotely? y It is more secure to create a non-root user with admin permissions. Answer "no," if you need more flexibility to manage your database server.
    Remove test database and access to it? y If you have a reason to keep a test database, answer "no."
    Reload privilege tables now? y Why wait?

Install and configure PHP

  1. Install PHP:
    sudo apt-get install php5-fpm
  2. Open the php.ini file in vim:
    sudo vim /etc/php5/fpm/php.ini
  3. Find the cgi.fix_pathinfo directive. Most of the file is commented out and it will be about halfway through the file under a section called Paths and Directories.
  4. Uncomment the line and set it as follows:
    cgi.fix_pathinfo=0
  5. Save and close the file:
    :wq!
  6. Restart PHP:
    sudo service php5-fpm restart

Configure NGINX for PHP

All of your LEMP stack components are now installed. You need only make NGINX aware that a FastCGI server exists to process PHP requests.

  1. Open the nginx default config file for editing:
    sudo vim /etc/nginx/sites-available/default
  2. Find the server context, which looks something like this:

    server {
        #listen   80;
        #listen   [::]:80 default_server ipv6only=on;
        root /usr/share/nginx/www;
        index index.html index.htm;
        server_name localhost;
  3. Uncomment the listen parameter, and edit the index and server_name parameters. Your server name should be either your server's IP address or a domain name you want to use. When complete, it should look like the following (changes highlighted in blue):
    server {
        listen   80;
        root /usr/share/nginx/www;
        index index.php index.html index.htm;
        server_name coolexample.com;
  4. Find the location / {} context, which will look like this (comments are removed from the example):
    location / {
            try_files $uri $uri/ /index.html;
            }
  5. Immediately beneath the closing bracket of the location / {} context, uncomment the following lines (if these lines do not exist in your example file, you will need to create them manually):
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/www;
    }
  6. Uncomment and edit the location ~ \.php$ context, so that it looks like this:
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
  7. Save and close the file:
    :wq!
  8. Restart NGINX:
    sudo service nginx restart

If NGINX fails to restart, check your edits and make sure all of your braces ({}) are closed and that each directive ends with a semi-colon (;).

Verify PHP is running

  1. Create a test file in your web root directory:
    sudo vim /usr/share/nginx/www/info.php
  2. Give this file the following contents:
    <?php
    phpinfo();
    ?>
  3. Go to:
    http://your server's IP address/info.php

A page full of statistics with a PHP logo in the upper right-hand corner will be displayed.

Next steps

Now that you have a LEMP stack, we recommend setting up NGINX server blocks for your websites.


Was This Article Helpful?
Thank You For Your Feedback
Glad we helped! Anything more we can do for you?
Sorry about that. How can we be more helpful?