Installing LEMP stack in Debian 7

By | November 23, 2014

This post describes the process of Installing LEMP stack in Debian 7 with Nginx, MariaDB and PHP5-FPM. A LEMP stack is the platform for hosting php based applications, like a Content Management System (CMS) or a Blog tool.

Nginx is available in the debian official stable repositories, however the nginx version is slightly dated. So in order to make use of newer stable version of the same, I will make use of the official Nginx repository for debian. Again MariaDB is not available in the official stable debian repositories so I have to make use of a third party repository as described in the official MariaDB page.

1) Installing MariaDB

Install the python-software-properties package

sudo apt-get install python-software-properties

Install the apt keys

sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xcbcb082a1bb943db

Add the new repository

sudo add-apt-repository 'deb http://ftp.yz.yamagata-u.ac.jp/pub/dbms/mariadb/repo/5.5/debian wheezy main'

Once the key is imported and the repository added you can install MariaDB

sudo apt-get update
sudo apt-get install mariadb-server

Now we need to secure MariaDB by running the secure installation command. Here we will use a strong Mariadb root password, remove the test database and disable remote login into MariaDB.

mysql_secure_installation

Thats it..!!! We are done..!!!

2) Installing Nginx

Get and install the apt keys for the official nginx repository for debian wheezy

wget http://nginx.org/keys/nginx_signing.key  && sudo apt-key add nginx_signing.key

Add the following lines to the file /etc/apt/sources.list

deb http://nginx.org/packages/debian/ wheezy nginx
deb-src http://nginx.org/packages/debian/ wheezy nginx

Finally run the following command to install nginx

apt-get update
apt-get install nginx

Thats it..!!! Nginx shoud be installed and up and running now. You can check it through your web browser by using your server FQDN or ip address. Here i am using my server FQDN http://www.example.com

Screenshot from 2014-11-23 20:00:01

 

3) Install PHP5-FPM

Install PHP5-FPM and some other useful PHP modules with the following command

 apt-get install php5-fpm php5-gd php5-mysql

Now its time to make a few changes with how php5-fpm works and connect it to nginx. Open the main php-fpm configuration file

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

Find the parameter that sets “cgi.fix_pathinfo”. This will be commented out with a semi-colon (;) and set to “1” by default.

This is an extremely insecure setting because it tells PHP to attempt to execute the closest file it can find if a PHP file does not match exactly. This basically would allow users to craft PHP requests in a way that would allow them to execute scripts that they shouldn’t be allowed to execute.

We will change both of these conditions by uncommenting the line and setting it to “0” like this:

cgi.fix_pathinfo=0

Next navigate inside the directory “/etc/php5/fpm/pool.d”. Open the file “www.conf”

sudo nano www.conf

make changes as follows:

user = nginx
group = nginx

by default user and group vales are pointing to “www-data”. This is debian default setting.

Also the change from “www-data” to “nginx” has to be incorporated in the following lines as well:

listen.owner = nginx
listen.group = nginx

N.B: We made this change because if we don’t then php will run as “www-data” and nginx will run as “nginx“. And that means your LEMP stack is not going to function properly, throwing lot of errors.

Now save the file and restart the php5-fpm service

service php5-fpm restart 

Coming back to nginx now…. The niginx webserver is running as the user nginx. the webroot is “/usr/share/nginx/html” and the main configuration directory is “/etc/nginx“.  Inside “/etc/nginx” we find the file “nginx.conf” which controls the global parameters. Also we will find the directory “conf.d” here. All the sites that are to be served by nginx are defined here with “.conf” extension. Within this “conf.d” directory, there is a sample site file called “default.conf“. We will take it out of the way.

mv default.conf default.conf.original

Now we will create our own site file called mysite.conf

sudo nano mysite.conf

and populate the contents as follows passing the processing of php to php-fpm

server {
 listen 80;
 root /usr/share/nginx/html;
 index index.php index.html index.htm;
 server_name example.com;
 location / {
 try_files $uri $uri/ /index.html;
 }
 error_page 404 /404.html;
 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root /usr/share/nginx/html;
 }
 # pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
 location ~ \.php$ {
 try_files $uri =404;
 fastcgi_pass unix:/var/run/php5-fpm.sock;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include fastcgi_params;
 }
}

Restart nginx

service nginx restart

4) Testing PHP with a php info page

We can quickly see all of the details of the new php configuration.

To set this up, first create a new file:

sudo nano /usr/share/nginx/html/info.php

Add in the following line:

<?php
phpinfo();
?>

Then Save and Exit.

Restart nginx:

sudo service nginx restart

You can see the php-fpm configuration details by visiting http://server-url-or-ip/info.php. It will look something like this:

Screenshot from 2014-11-23 21:30:29

Your LEMP stack is now set up now….!!!!

 5) References

i) For MariaDB installation:

https://downloads.mariadb.org/mariadb/repositories/#mirror=nus&distro=Debian&distro_release=wheezy&version=5.5

ii) For Nginx Installation:

http://nginx.org/en/linux_packages.html

2 thoughts on “Installing LEMP stack in Debian 7

  1. Pingback: Install Wordpress on Debian 7 LEMP Stack - www.rudraraj.net

  2. Pingback: Install Dokuwiki with Nginx and PHP5-FPM Debian 7 - www.rudraraj.net

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.