How to Install and configure (LEMP) stack in 6 steps.

11th March 2015 , Written by

Here is this article you will learn how to install and configure LEMP stack “Linux, nginx, MySQL, PHP”
First of all you will need to know more about LEMP stack. LEMP stack is a group of open source software to get web servers up and running. LEMP Stack used to power many popular web applications. “LEMP” refers to a Linux-based operating system, the Nginx web server, the MySQL database server, and the PHP programing language. It is common to substitute other programing languages like Python, Perl, and even Ruby for PHP. The “LEMP” configuration replaces the Apache web server component with nginx (pronounced “engine x,” providing the “E” in LEMP) to increase the ability of the server to scale in response to demand.

Kindly note the following:

The steps in this article require the user to have a root privileges on the operating system.
This article is installed on VPS with Centos 6.5 x86_64 GNU/Linux.
Nginx version: nginx/1.6.2
PHP 5.3.3
Mysql DB Server version: 5.1.73
Steps of installation:

First Step— “Update”:

As mentioned above, the steps in this article require the user to have a root privileges on the operating system.

First thing to do after installing the operating system is to have your system updated, and this could be done through the following command:

yum -y update

Second step—“Install nginx”

In order to install nginx you have to add nginx yum repository.

Create a file named /etc/yum.repos.d/nginx.repo and paste the configurations below:


name=nginx repo




* Then install nginx using the following command :

yum install nginx

Service nginx start

After installing nginx you can test nginx to verify that everything went as planned by visiting your server’s public IP address in your web browser


You will see the default Nginx web page, It should look something like this:

Third step—Install PHP and PHP modules

*Install the rpmforge and epel repositories:

rpm --import

rpm -Uvh

rpm -Uvh

* Then install php:

yum install php php-fpm php-common php-pecl-apc php-cli php-pear php-pdo php-mysql php-pgsql php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml php-pecl-memcache.x86_64 php-gd php-mbstring php-mcrypt php-xml phpMyAdmin

Fourth step—Configure nginx

* First you will need to add new users and create their directories

adduser site1
passwd site1
cd /home/site1
mkdir {public_html,logs}
touch logs/{error.log,access.log}
chown -R site1:site1 /home/site1
chmod 755 /home/site1

* Create a new virtual host file for your site with the following configuration :-

vim /etc/nginx/conf.d/site1.conf

server {

listen 80;

server_name domain name

#charset koi8-r;

error_log /home/site1/logs/error.log;

access_log /home/site1/logs/access.log;

location / {

root /home/site1/public_html;

index index.php index.html index.htm;

try_files $uri $uri/ index.php;

error_page 404 /404.html;
location = /404.html {
root /home/site1/public_html;
# redirect server error pages to the static page /50x.html
# error_page 500 502 503 504 /50x.html;

location = /50x.html {

root /home/site1/public_html;


# proxy the PHP scripts to Apache listening on

#location ~ \.php$ {

# proxy_pass;

# pass the PHP scripts to FastCGI server listening on

location ~ \.php$ {

root /home/site1/public_html;


fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include fastcgi_params;


location /phpMyAdmin {

root /usr/share;

index index.php index.html index.htm;

location ~ ^/phpMyAdmin/(.+\.php)$ {

try_files $uri =404;

root /usr/share;


fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

include /etc/nginx/fastcgi_params;


location ~* ^/phpMyAdmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {

root /usr/share/;



location /phpmyadmin {

rewrite ^/* /phpMyAdmin last;



Fifth step—Configure php-FPM

With PHP-FPM it’s possible to use different pools for different sites and allocate resources accurately.

An example of configurations for every pool:



listen =

user = site1

group = site1

request_slowlog_timeout = 5s

slowlog = /var/log/php-fpm/slowlog-site.log

listen.allowed_clients =

pm = dynamic

pm.max_children = 5

pm.start_servers = 3

pm.min_spare_servers = 2

pm.max_spare_servers = 4

pm.max_requests = 200

listen.backlog = -1

pm.status_path = /status

request_terminate_timeout = 120s

rlimit_files = 131072

rlimit_core = unlimited

catch_workers_output = yes


step—Install MySQL

Install MySql using the following command:

yum install mysql mysql-server

Run MySQL set up script
sudo /usr/bin/mysql_secure_installation

The prompt will ask you for your current root password.

Type it in.

Enter current password for root (enter for none).

OK, successfully used password, move on…

Then the prompt will ask you if you want to change the root password. Go ahead and choose N and move on to the next steps.

Note :- It’s easy just to say Yes to all the options. At the end, MySQL will reload and implement the new changes.

By default, MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into the production environment.

Remove anonymous users? [Y/n] y


Normally, root should only be allowed to connect from ‘localhost’. This ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y


Note: By default, MySQL comes with a database named ‘test’ that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment.

Remove test database and access to it? [Y/n] y

Drop test database


Remove privileges on test database


Reloading the privilege tables will ensure that all changes made so far will take effect immediately.

Reload privilege tables now? [Y/n] y


Seventh step—Testing Your website:

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

To set this up, first create a new file:

vim /home/site1/public_html/index.php

Add in the following line:


Finally Save and Exit.

Start services

service nginx start
service php-fpm start
service mysqld start

Auto start services when the server starts

chkconfig nginx on
chkconfig php-fpm on
chkconfig mysqld on

You can see php details by visiting http://youripaddress/

The page that you visit should look like this:


This page basically gives you information about your server from the perspective of PHP. It is useful for debugging and to ensure that your settings are being applied correctly.

You probably want to remove this file after testing because it could give information about your server to unauthorized users.

By Now your LEMP stack is now setup and configured on your virtual private server, and you have many choices for what to do next. Basically, you’ve installed a platform that will allow you to install most kinds of web application.

Written By Eng. Mohamed EL Madany