Installing LEMP (Linux, nGinx, MySQL, PHP) Stack on Ubuntu 14.04

This article explains the steps to install the LEMP software bundle on Ubuntu 14.04.

Blue Light Tech

LEMP

LEMP is a variant form of the LAMP software stack, used for building highly dynamic web applications and web sites. The Apache component of LAMP is replaced by Nginx in LEMP. The components of the LEMP bundle are as follows:

  • Linux operating system
  • Nginx Web Server
  • MySQL database
  • PHP (sometimes substituted by Python or Perl)

Ubuntu

Ubuntu is a Linux-based operating system and uses the Unity desktop. After installing LEMP on Ubuntu, you can start using it for required purposes.

Ubuntu has a Package Manager, apt. The package manager maintains most of the software required for Ubuntu. You can download the latest LEMP packages using apt.

Prerequisites

The prerequisites to successfully install LEMP on Ubuntu 14.04 are as follows:

Setting up Non-Root User Account

Root user has higher privileges and permissions which this installation does not require. To reduce the risk which may come with unwanted permissions, a non-root user with some root privileges is used for the installation.

To create a non-root user for the installation, perform the following:

  1. Open the terminal and log in as root user on Ubuntu server.
  2. Type the command
    adduser <username>

    Replace <username> with a name for the user. For example – tester
  3. Type a password for the user.
  4. Retype the password.

    The figure shows the password being updated for the new user.

  1. Type answers for the questions that appear on the terminal.
    These questions are optional and if you do not wish to provide answers, press the Enter
    The system asks for confirmation on the information provided.
  2. Press y to confirm the information.
  3. To add the user to the sudo group, run the command
    gpasswd –a <username> sudo

    Replace <username> with a name for the user. The user is now successfully added to the sudo group.

Installing LEMP

LEMP installation comprises of the following tasks:

  1. Installing Nginx Web Server
  2. Installing MySQL
    1. Creating Directory and securing the database
  3. Installing PHP
    1. Installing php5-fpm
    2. Configuring PHP Processor
  4. Configuring Nginx to Use the PHP Processor
  5. Verifying the Installation

Installing Nginx Web Server

To install Nginx Web Server on Ubuntu 14.04, perform the following:

  1. Log in to the terminal as the user performing the installation.
  2. To get an update of the packages, run the command -
    sudo apt-get update

    The system will prompt you to provide the password for the user.
  3. Type the password for the user account.
    The system grabs the updates and displays the message as shown in the following figure.
  4. To install the Nginx package, run the command
    sudo apt-get install nginx

    The system asks for confirmation of the installation.
  5. Press y to confirm the installation.

Installing MySQL

To install MySQL database on Ubuntu 14.04, perform the following:

  1. Log in to the terminal as the user performing the installation.
  2. Run the command
    sudo apt-get install mysql-server

    The system asks for confirmation of the installation.
  3. Press y to confirm the installation.
    System prompts you to provide a root password for the database.
  4. Type a strong password for MySQL root user.
  5. Retype the password to confirm.
    The MySQL database will now be created.

Creating Directory and Securing the Database

After installing the database, you must create a directory structure for the database to store information and ensure that the database is made secure.

Perform the following:

  1. To create the database directory, run the command
    sudo mysql_install_db
  2. To secure the database, perform the following:
    1. Run the command
      sudo mysql_secure_installation
    2. Type the MySQL root password provided during the database installation.
      System asks if you want to change the root password.
    3. Press n if you do not want to change the MySQL root password.
      System prompts some more questions.
    4. Press Enter for next four questions –
      • Remove anonymous users?
      • Disallow root login remotely?
      • Remove test database and access to it?
      • Reload privilege tables now?

System shows the Installation Secure message. The database is now secure for storing data.

Installing PHP

PHP acts as the link between Nginx web server and MySQL database to generate dynamic content. Nginx, unlike some other web servers, does not contain native PHP processing. php5-fpm is a fastCGI process manager which can act as the PHP processor. This module must be installed on the Ubuntu server.

Installing php5-fpm

To install php5-fpm on Ubuntu 14.04, perform the following:

  1. Log in to the terminal as the user performing the installation.
  2. Run the command
    sudo apt-get install php5-fpm php5-mysql

    The system asks for confirmation on the installation.
  3. Press y to confirm the installation.PHP is successfully installed on the system.

Configuring PHP Processor

After installing the processor package, the PHP configuration must be updated to make it secure.

To configure the PHP Processor, perform the following:

  1. Log in to the terminal as the user performing the installation.
  2. Run the command
    sudo nano /etc/php5/fpm/php.ini
    to open the php5-fpm configuration file.
  3. Search for the parameter fix_pathinfo. To search, press Esc key twice followed by W. The Search bar appears. Type cgi.fix_pathinfo and press Enter key.
    The terminal displays the searched item in the file as shown in the following figure.
  4. Remove the commenting at the beginning of the line.
  5. Replace 1 with 0
    The edited section should look as shown in the following figure.
  6. Press Ctrl+X to save and exit the file.
  7. Press y to confirm saving the file.
  8. Restart PHP processor to register the update. Run the command
    sudo service php5-fpm restart

Configuring Nginx to Use the PHP Processor

After installing the PHP processor Nginx must be configured to use the updated PHP processor. For this, the Nginx server block configuration must be updated.

  1. Log in to the terminal as the user performing the installation.
  2. Run the command
    sudo nano /etc/nginx/sites-available/default
    to open the Nginx server block configuration.
  3. Search for listen 80 default_server. To search, press Esc key twice followed by W. The Search bar appears. Type the value and press Enter
    The terminal displays the searched item in the file as shown in the following figure.
  4. Make the following changes to the file:
    1. Add index.php as the first value to the index directive. This allows PHP index files to be served first when directory requests are made.
    2. Update server_name directive to server_domain_name_or_IP. This makes the configuration to point to the server domain name or public IP address.
    3. Add try_files and fastcgi_param directives.
    4. Uncomment some lines. Refer the following figure for more information.
    5. Press Ctrl+X to save and exit the file.
    6. Press y to confirm saving the file.
  5. Restart Nginx to register the update. Run the command
    sudo service nginx restart

After completing these steps, LEMP installation is complete on the system. You can start using it for required purposes.

Verifying the Installation

To verify if the installation is successful, you can create a test PHP file to return server information.

Perform the following:

  1. Log in to the terminal.
  2. Run the command
    sudo nano /usr/share/nginx/html/info.php
    to create the info.php file.
  3. Type the following into the file.
    <?php
    phpinfo();
    ?>
  4. Save and close the file.
  5. Type http://<server_domain_name_or_IP>/info.php in your browser’s address bar.
    Replace <server_domain_name_or_IP> with the domain name or public IP address of your server.
    You will see a PHP web page showing information about your server. This shows that your installation is successful.

Note: Remove the info.php file by using the command

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


12 Comments


Judi - 12/05/2018 13:15:33

2DCmAh https://www.genericpharmacydrug.com


Judi - 12/05/2018 12:51:44

87DwVU https://www.genericpharmacydrug.com


mike11 - 18/04/2018 04:16:18

iEGHnq https://www.genericpharmacydrug.com


mike11 - 18/04/2018 02:24:34

BKjPgm https://www.genericpharmacydrug.com


GoldenTabs - 08/01/2018 13:47:55

edMMVx https://goldentabs.com/


JimmiXzSq - 18/05/2017 17:38:07

9dyZ9S http://www.LnAJ7K8QSpkiStk3sLL0hQP6MO2wQ8gO.com


Barnypok - 02/04/2017 07:13:04

ibq6q9 http://www.LnAJ7K8QSpkiStk3sLL0hQP6MO2wQ8gO.com


Barnypok - 02/04/2017 01:24:05

jEEbtw http://www.LnAJ7K8QSpkiStk3sLL0hQP6MO2wQ8gO.com


Barnypok - 01/04/2017 01:26:31

LRjODg http://www.LnAJ7K8QSpkiStk3sLL0hQP6MO2wQ8gO.com


JimmiXzSw - 19/02/2017 07:46:54

zP4uwG http://www.FyLitCl7Pf7ojQdDUOLQOuaxTXbj5iNG.com


Adi - 20/12/2015 22:20:02

I really wish there were more arciltes like this on the web.


Alex - 06/11/2015 08:37:53

Good article, very clear steps shown which is always nice to see! Would've been nice to read the reasons to choose nginx over apache though.