How to Install Linux, Nginx, MySQL, PHP (LEMP) Stack on Debian 7

Introduction

 

LEMP is an open source software configuration used in web server and it stands for Linux, Nginx, MySQL, and PHP. This tutorial describes how to install LEMP stack on Debian 7. The only difference between LAMP and LEMP stack is the web server in which the stack runs.

Since the server is already set up with Debian, the Linux part of the stack is taken care of. This tutorial will show you how to install nginx, mysql and php-fpm, configure as well as verify them.
 

Update Apt-Get

 

We will use apt-get package manager to install and update all required software packages and server programs. We need to do a complete update before getting down to the installation process:

sudo apt-get update

The above command does a thorough update.
 

Installation of MySQL

 

Let’s start with MySQL installation process. It is used for storing, organizing and retrieving data.

Open your terminal and enter the following command.

sudo apt-get install mysql-server

During the installation, the system will ask for MySQL root password. In case if you missed it, you can always set the password while program is installing.

After installation completes, we need to activate it using the command:

sudo mysql_install_db

Now, you can start running the secure installation processes.

sudo /usr/bin/mysql_secure_installation

While the secure install scrip is running, the system will ask for your MySQL root password. You will be able to set the MySQL root password here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

You will be asked whether you want to change the root password. You can choose N as the root password should already be set.
 

Installation and Configuration of Nginx on VPS

 

Once the installation of MySQL is completed, you can now move on to installing nginx on your VPS.

sudo apt-get install nginx

Since nginx does not start on its own, you need to get it running:

sudo service nginx start

Now, you can verify the nginx installation by directing your web browser to your IP address. Get your VPS IP address using:

ifconfig eth0 | grep inet | awk '{ print $2 }'

 

Configuration
 

Open the default virtual host file:

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

You need to make the following changes:

  1. Add index.php to the index line.
  2. Replace the server_name from localhost to domain name or IP address.
  3. Changes in “location ~ \.php$ {“ section.

 

After the necessary changes, it should look like this:

[...]server {
        listen   80;
              root /usr/share/nginx/www;
        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/www;
        }

         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        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;
                        }
}
[...]

 

Installing PHP

 

Let’s go ahead and install php-fpm using apt-get.

sudo apt-get install php5-fpm php5-mysql

Now, open the php configuration file and update it with one change.

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

Search for the line ‘cgi.fix_pathinfo=1’ and replace 1 to 0.

cgi.fix_pathinfo=0

This is to ensure that the php interpreter processes only the exact file path. After making the changes, save and exit the file.

Now, open up the php5-fpm configuration file:

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

Replace the line ‘listen = 127.0.0.1:9000’ to ‘/var/run/php5-fpm.sock’ :

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

Once you are done with the changes, restart the php-fpm:

sudo service php5-fpm restart

 

Creating a PHP Info Page

 

Now that the LEMP stack is completely installed on your system, you may have to verify it by creating a simple php info page.

For that, you need to create a new blank file and paste the following code.

sudo nano /usr/share/nginx/www/info.php
<?
phpphpinfo();
?>

Save and exit the file.

Restart nginx using:

sudo service nginx restart

Visit your php info page by going to your web address, http://youripaddress/info.php.

You will be able to view the nginx and php-fpm configuration details too by visiting it.

LEMP stack is now successfully installed on your VPS.

 

support2 has written 111 articles

Leave a Reply