This will be a tutorial for setting up your own LEMP stack on Debian 7 Wheezy. This might be also used for installing one on Ubuntu but is not guaranteed.

LEMP Stack?

LEMP is almost the same with a LAMP stack but the Web server part is replaced with Nginx. LEMP is an acronym for a software bundle of Linux, Nginx(for E), MySQL and PHP. Sometimes any of this softwares gets replaced like MySQL can sometimes be replaced with MariaDB and PHP either Perl or Python.

Prerequisite

I’ll assume that a Debian 7 Wheezy is already installed and ready to be configured for installing a LEMP stack. Login as a root in your terminal or you can use sudo.

Make Sure Everything Is Up To Date

apt-get update && apt-get upgrade -y

Install MySQL

Install MySQL by typing

apt-get install mysql-server

On process of installing MySQL it should ask you for the root password.

Install Nginx

Nginx will be our web server and to install it type in your terminal

apt-get install nginx

After installing Nginx make sure that it is working correctly by going into your browser and browsing into “localhost”. An Nginx welcome page should appear if everything was installed without problem.

Install PHP

PHP will be our scripting language and to install it type in your terminal

apt-get install php5 php5-fpm php5-mysql

php5 in the list is the scripting language itself and php5-fpm will be used for the communication between our webserver and the translation of the php scripts and php5-mysql is for enabling MySQL support for php5.

Setup php5-fpm With Nginx

Now that we have installed everything we need to setup php5-fpm to communicate with Nginx. Since Nginx is doesnt have something like libapache2-mod-php5 module for Apache we need the php5-fpm to run our php scripts and communicate with Nginx to respond every requests.

Prepare Nginx VirtualHost

To do so we need to edit the file in /etc/nginx/sites-enabled/default. Open the file with

nano /etc/nginx/sites-enabled/default

Add index.php in the index directive

index index.html index.htm index.php;

Find the line where the php config is commented out. It should look like below

#location ~ \.php$ {
# fastcgi_split_path_info ^(.+\.php)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm:
# fastcgi_pass unix:/var/run/php5-fpm.sock;
# fastcgi_index index.php;
# include fastcgi_params;
#}

Removed some of the comments on this section it should look like below

location ~ \.php$ {
 fastcgi_split_path_info ^(.+\.php)(/.+)$;
# # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
# # With php5-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php5-fpm:
 fastcgi_pass unix:/var/run/php5-fpm.sock;
 fastcgi_index index.php;
 include fastcgi_params;
}

Save the file and exit.

Note: You can also change the server_namedirective. By default it is set to localhost but you can set it to your public IP or your domain name.

After editing the default VirtualHost file. Make sure the reload Nginx, so the change we did will be loaded.

service nginx reload

Setup php-fpm

Since Nginx is expecting to communicate with php-fpm in a unix socket located in /var/run/php5-fpm.sock. Declared on this line

fastcgi_pass unix:/var/run/php5-fpm.sock;

from the above VirtualHost. We will need to make sure that php5-fpm is also listening on this unix socket. To check open the file in /etc/php5/fpm/pool.d/www.conf

nano /etc/php5/fpm/pool.d/www.conf

Look for the line with the listen directive and make sure it is set to listen on a unix socket located in /var/run/php5-fpm.sock. It should look like this

listen = /var/run/php5-fpm.sock

If it set to other value make sure to replace it with the above. Save the file and exit.

Make sure to reload the php5-fpm daemon

service php5-fpm reload

Test Out The LEMP Stack

Now that we have configured Nginx and php5-fpm we can test the LEMP stack if it is now working properly.

Create a file called php-info.php in the root directory. You can check where this is in the default VirtualHost file of Nginx. The directive is root and after the scace will be the location of the root directory. The location is usually in /usr/share/nginx/www. Create the file by

nano /usr/share/nginx/www/php-info.php

Put the code below into the file

<? php
 echo phpinfo();
?>

Save and exit.

Now go into your browser and browse into “localhost” or whatever you put in the server_name directive and you should see the php informations if everything was installed successfully

Thats it! Now you have your LEMP stack all setup and ready.

Looking for cool VPS provider? Try Linode(referral link), they are currently my VPS provider.