A LEMP stack is a group of open source software commonly used for hosting dynamic websites and web apps. LEMP is an acronym for Linux operating system, Enginx web server, MySQL and PHP. This guide shows you how to install a LEMP stack on an Ubuntu 16.04 server.

It is advisable to update your server before doing any major installation in order to get the latest version of the system packages and its dependencies. Use the command below to update with the Ubuntu package manager apt-get.

sudo apt-get update

Step 1 – Install Nginx Web Server
Issue the following command to install Nginx web server from Ubuntu official repositories using the apt command line:
sudo apt-get install nginx

As a non-root user, you will be required to enter the sudo password for the account in use. Enter the password and continue the installation.

Once it is installed, you can start Nginx on your Cloudwafer VPS by issuing the command:

sudo systemctl start nginx

Check the status of the Nginx Web server with the command below:

sudo systemctl status nginx

To verify that you have correctly installed Nginx web server on your server, visit your server's public IP address in your web browser as shown below. If you see this page, then your Nginx web server is now correctly installed.

Step2: Install the MySQL Database (MariaDB)
MariaDB is a community-developed fork of the MySQL relational database management system intended to remain free under the GNU GPL. To install the MariaDB-server package, type the command below:

sudo apt install mariadb-server mariadb-client

After completing the installation, issue the following command to secure our database which locks down access to our database removing the anonymous user, deleting test database and disallows remotely root logins.

sudo mysql_secure_installation

You will be asked to set a root password if you don’t have one, followed by a series of questions which you should answer Y (yes) or press ENTER to accept the default value which is Yes.

After successfully completing this, our database system is now set up and we can move on to the next step in our installation.

Step 3: Install the PHPMyAdmin
Issue the following command to install PHPMyAdmin, a web panel frontend used to administer MySQL databases from the Ubuntu official repositories using the apt command line:

sudo apt-get install php php-mysql php-fpm

After completing the installation, start and check if php7.0-fpm has been properly installed by issuing the below command:
systemctl start php7.0-fpm

systemctl status php7.0-fpm

Step 3: Configuring the PHP Processor

Our PHP components have been successfully installed, the next step is to make some little configuration changes to make our setup more secure.

  • Open the main php-fpm configuration file with root privileges: sudo nano /etc/php/7.0/fpm/php.ini

  • Locate cgi.fix_pathinfo (Ctrl+W allows you to search using nano), this will be commented out with a semicolon (;) and set to "1" by default.

  • Change the value to 0 and uncomment the line by removing the semi-colon. i.e cgi.fix_pathinfo=0

Ensure you save the file before closing then proceed to restart the PHP processor by issuing the command:

sudo systemctl restart php7.0-fpm

  • Next step is to configure Nginx to use the PHP processor. This is done on the server block level by opening the default Nginx server block configuration file with the command:

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

  • Add index.php as the first value of our index directive as shown below index index.php index index.html index.htm index.nginx-debian.html;
  • Modify the server_name directive to point to your server's domain name or public IP address.
  • Ensure you save the file before closing then test your configuration file for syntax errors by typing: sudo nginx -t

If any errors are reported, recheck your file before continuing then proceed to reload Nginx to make the necessary changes:

sudo systemctl reload nginx

Step 3: Create a PHP File to Test Configuration
We are going to create a phpinfo() file to test our configuration. phpinfo() is also a valuable debugging tool, as it contains all EGPCS (Environment, GET, POST, Cookie, and Server) data. This file outputs a large amount of information, such as:

PHP version  
Information about PHP compilation options and extensions
Server information and environment (if compiled as a module)
PHP environment
OS version information, paths, master and local values of configuration options
HTTP headers
PHP license

Open a new file called info.php within your document root in your text editor:

sudo nano /var/www/html/info.php

Type the following code into the blank file:

<?php phpinfo(); ?>

When you are finished, save and close the file.

Visit this page in your web browser by typing your server's domain name or public IP address followed by /info.php:


Note: It is advisable to delete this info.php file as it contains vital information about your server that can be viewed by anyone on the web. You can do that with this command: sudo rm /var/www/html/info.php