Install and Configure LEMP Stack (Linux, Nginx, MySQL, PHP & phpMyAdmin) on Ubuntu 14.04

LEMP Stack

LEMP is an archetypal model of web service solution stacks, named as an acronym of the names of its original four components: the Linux operating system, the Nginx(engine-x) HTTP Server, the MySQL relational database management system (RDBMS), and the PHP programming language. As a solution stack, LEMP is suitable for building dynamic web sites and web applications to increase the ability of the server to scale in response to demand.

Getting Started

I will be installing the required packages on Ubuntu 14.04 LTS (Trusty Tahr). In addition, I’ve had great success on using Digital Ocean‘s Virtual Private Server, they are easy to use and can typically be setup in 55 seconds. I will assume you have your server all setup, and you are ready to begin at the command line, so lets get started.

  • Install Nginx
  • Nginx (pronounced as engine-x) is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server written by Igor Sysoev.

    First lets make sure our server is all up-to-date. Enter the following command in your terminal:

    sudo apt-get update

    Now, install Nginx using this command:

    sudo apt-get install nginx

    Start Nginx service using the command:

    sudo service nginx start
  • Test Nginx
  • Open up your web browser and navigate to http://ip-address/ or http://localhost/. You will see a screen something like below.


  • Configure Nginx
  • Check the No. of CPU(s) in your server using this command:


    Now Open the file /etc/nginx/nginx.conf in any editor:

    sudo nano /etc/nginx/nginx.conf

    Adjust the worker_processes (i.e No. of CPU’s in your server). In my case it’s 1. So I set the worker_processes to 1.

    worker_processes 1;

    Restart Nginx service:

    sudo service nginx restart

    The default vhost is defined in the file /etc/nginx/sites-available/default. And the virtual hosts are defined in server {} containers – let’s modify it as follows:

    Open the file /etc/nginx/sites-available/default in any editor.

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

    Under the server section, set the server_name to FQDN (Fully Qualified Domain Name) or IP address as shown below. Make sure you’ve also added index.php, See the example below:


    server {

    listen 80 default_server;

    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;

    index index.php index.html index.htm;

    # Make site accessible from http://localhost/




       listen 80; -> listen for ipv4

       listen [::]:80 default_server ipv6only=on; -> listen for ipv6

       root /var/www/html; –> document root directory.

       server_name; –> Server FQDN

    Now, scroll down further and find the section #location ~ \.php$. Uncomment and modify the following lines as shown below.


    location ~ \.php$ {

    try_files $uri =404; -> Add this line

    fastcgi_split_path_info ^(.+\.php)(/.+)$;

    #    # NOTE: You should have “cgi.fix_pathinfo = 0;” in php.ini


    #    # With php5-cgi alone:

    #    fastcgi_pass;

    #    # With php5-fpm:

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

    fastcgi_index index.php;

    include fastcgi_params;




    Here, I added an extra line ‘ try_files $uri =404; ‘ to prevent zero day exploits.

    Save and exit the file.

  • Test Nginx Configuration
  • Test the nginx configuration for any syntax errors using command:

    sudo nginx -t

    Sample output:

    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

    nginx: configuration file /etc/nginx/nginx.conf test is successful

    Restart Nginx service:

    sudo service nginx restart
  • Install MySQL
  • MySQL is a relational database management system (RDBMS) that runs as a server providing multi-user access to a number of databases.

    sudo apt-get install mysql-server-5.6 mysql-client-5.6

    During installation, you’ll be asked to setup the MySQL “root” user password. Enter the password and select Ok.

    Re-enter the password.

    MySQL is now installed.

  • Verify MySQL status
  • You can verify the MySQL server status using command:

    sudo service mysql status

    Sample output:

    mysql start/running, process 980

  • Install PHP
  • PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely used open-source general purpose scripting language that is especially suited for web development and can be embedded into HTML.

    Install PHP with following command:

    sudo apt-get install php5 php5-fpm

    Install PHP MySQL library with the following command:

    For MySQL version 5.5 and below.

    sudo apt-get install php5-mysql

    For MySQL version 5.6.

    sudo apt-get install php5-mysqlnd

  • Configure PHP
  • Open php.ini file in any editor:

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

    Find the line “ cgi.fix_pathinfo=1 “, uncomment it and change the value 1 to 0 .




    Now restart PHP-FPM service.

    sudo service php5-fpm restart

    To check whether php5-fpm is running or not. Use the command below:

    sudo service php5-fpm status

    Sample output:

    php5-fpm start/running, process 1124

  • Test PHP
  • Now create the following PHP file in the nginx document root folder /usr/share/nginx/html:

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

    Add the following lines in the file.




    Save and exit the file.

    Navigate to http://server-ip-address/info.php . It will display all the details about PHP such as version, modules, build date and commands, etc.

  • PHP-FPM Use A TCP Connection
  • By default PHP-FPM listens on the socket /var/run/php5-fpm.sock . If you want to make PHP-FPM use a TCP connection, open the file /etc/php5/fpm/pool.d/www.conf .

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

    Modify the listen line to look as follows:


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

    listen =



    This will make PHP-FPM listen on port 9000 on the IP (localhost) . Make sure you use a port that is not in use on your system.

    Save and exit the file. Restart php5-fpm service.

    sudo service php5-fpm restart

    Now open and modify the nginx configuration file:

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

    Find the line fastcgi_pass unix:/var/run/php5-fpm.sock; and change it to fastcgi_pass; as shown below:


    location ~ \.php$ {

    try_files $uri =404;

    fastcgi_split_path_info ^(.+\.php)(/.+)$;

    #    # NOTE: You should have “cgi.fix_pathinfo = 0;” in php.ini


    #    # With php5-cgi alone:


    #    # With php5-fpm:

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

    fastcgi_index index.php;

    include fastcgi_params;



    Save and exit the file. Finally restart nginx service.

    sudo service nginx restart

  • Install phpMyAdmin
  • phpMyAdmin is a free and open source tool written in PHP intended to handle the administration of MySQL with the use of a web browser. It can perform various tasks such as creating, modifying or deleting databases, tables, fields or rows; executing SQL statements; or managing users and permissions.

    It is available in the Official Debian repositories. So install it with command:

    sudo apt-get install phpmyadmin

    Select the web server that should be automatically configured to run phpMyAdmin.

    By default, nginx will not be displayed here. So, select apache or lighttpd, and we will configure phpmyadmin to work with nginx webserver later.

    Select Yes to configure database for phpmyadmin with dbconfig-common.

    Enter password of the database’s administrative user (i.e MySQL root user password).

    Enter MySQL application password for phpmyadmin.

    Re-enter the password.

    phpMyAdmin has been successfully installed now.

    Create a symbolic link between phpMyAdmin and the website root directory. Here our website root document directory is /usr/share/nginx/html/ .

    sudo ln -s /usr/share/phpmyadmin/ /usr/share/nginx/html/

    Restart nginx server.

    sudo service nginx restart

  • Access phpMyAdmin in Web Console
  • Now you can access the phpMyAdmin console by navigating to http://server-ip-address/phpmyadmin from your browser.

    Enter your MySQL username and password which you have given in previous steps.

    You will be redirected to phpMyAdmin main web interface. This is how my phpMyAdmin dashboard looks.

    Now you can manage your MySQL databases from phpMyAdmin web interface.

  • Fix “The mcrypt extension is missing. Please check your PHP configuration.”
  • Install mcrypt extension for PHP with following command:

    sudo apt-get install php5-mcrypt

    Enable the mcrypt extension with following command:

    sudo php5enmod mcrypt

    Restart Nginx service:

    sudo service nginx restart

    Restart PHP-FPM service:

    sudo service php5-fpm restart

That’s it! Congratulation your LEMP server is ready to use.

If I can get my codes to function as I want , I’ve succeeded as a Programmer.
Web Server
Share this Story:
  • facebook
  • twitter
  • gplus

About JiNexus

DevOp (Development and Operations) with a focus on PHP web applications and cloud-based Ubuntu servers.
Experienced in building large custom MVC web applications in PHP, using frameworks like Zend Framework, Apigility, Doctrine ORM, Code Igniter, Phalcon and WordPress. Currently focusing on back-end web applications in PHP, I also like to venture out into the Bootstrap-based front end world using HTML, CSS and JavaScript (jQuery mainly).
Sometimes one needs to experiment with other technologies and I've done this with Python.

"Life is like programming. We fail to compile, because realization comes from warnings and success comes from experience."


  1. BobbuBrowne
    86 days ago

    Hello! Cool post, amazing!!!

Leave a comment