How to Create a LEMP Stack on Debian Server

by Ahmed Khan  October 18, 2016

In a previous article on server administration I created a LAMP stack on Debian. In this tutorial, we are going to create LEMP stack. This is yet another type of stack which consists of Linux, Nginx, MySQL, PHP-FPM and PHP. It’s used by many developers around the world to speed up their websites.

Nginx is a reverse proxy server for the HTTP, HTTPS, SMTP, POP3, and IMAP protocol. It is used as a load balancer and also as an HTTP cache. On Cloudways ThunderStack, Nginx is used as an HTTP cache. Nginx doesn’t compile PHP directly like Apache; it uses PHP-FPM to compile PHP codes.

installing-lemp-stack-on-debian-banner

In case you already knew what is LEMP and all that and just want to know how to setup LEMP stack, let’s jump right into it. For many developers, the first challenge is how to install LEMP on Debian 9.

Before you create a LEMP stack though, open your SSH terminal and run the following command to update all the packages:

Let’s address the elephant in the room now:

Step 1: Installing Nginx

Run the following command to install apache on your server:

install nginx

Type “y” when it prompts and hit Enter to continue installation. We are going to configure Nginx once we install PHP and PHP-FPM.

Step 2: Installing MySQL

Now type the following command to install MySQL:

Once installation is finished it will ask you to set your root password.

installing mysql server

Set your password and press enter. Now run the following command to complete MySQL installation:

MySQL_secure_installation

Once you hit enter, the prompt will ask you to enter the root password then hit enter again.

  • Type n in order to not change root password and hit enter,
  • then type y to remove anonymous users and press enter.
  • Type n if you want to disallow root login remotely and hit enter again.
  • Now type y to remove test tables and databases and… press enter, then type y again and hit enter for one last time before you have to press it again later.

mysql installation setting

Once the whole process is complete MySQL will be installed on your server.

Step 3: Installing PHP

Type the following command to install PHP along with PHP-FPM:

Wait for the installation to complete. Now let’s configure Nginx to compile PHP and run on your server IP.

Step 4: Configuring Nginx

First type the following commands to navigate to www folder and create a public_html folder inside it.

Before setting up Nginx, type the following command to get your server IP if you don’t already know it:

Save the IP somewhere and type the following command to open Nginx default configuration file:

Now press “i” to edit the document. On the root, add /public_html after www. On server_name type your Server IP after a space. Now replace the code inside .php$ with the following:

Your default Nginx Conf file will look like this:

nginx setting

Now save the file by pressing Esc and type “:wq”. Type the following command to start Nginx server:

Step 5: Testing It

It’s time to test things now. Type the following command:

Navigate to public_html folder and create a new index.php file.

php info file

Now press “i” to start editing the file and paste the following code in it:

Now press Esc to save the file and type “:wq”. Now type your server IP on your browser and hit enter. If you find the following page then Nginx and PHP have been successfully configured on your server.

Conclusion:

We learned how to create a LEMP stack on debian server and also configure Nginx to compile PHP codes. If you have any queries, feel free to leave your comments below.

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

About Ahmed Khan

Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works. You can email him at ahmed.khan@cloudways.com

Stay Connected:

You Might Also Like...