Chat with us, powered by LiveChat

This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

How to Create a LEMP Stack on Debian Server

October 18, 2016

4 Min Read
Reading Time: 4 minutes

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.

You Might Also Like: Here is a List of Best PHP Servers 

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

apt-get update

Let’s address the elephant in the room now:

Step 1: Installing Nginx

Run the following command to install apache on your server:

apt-get install nginx

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:

apt-get install MySQL-server

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:

apt-get install php5 php-pear php5-MySQL php5-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.

cd /var/www/
mkdir public_html

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

ifconfig eth0 | grep inet | awk '{ print $2 }'

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

vim /etc/nginx/sites-available/default

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:

location ~ \.php$ {

               

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

               fastcgi_index index.php;

               fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

               include fastcgi_params;

               

       }

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:

service nginx start

Step 5: Testing It

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

cd /var/www/public_html/
vim index.php

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:

<?php

phpinfo();

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.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Launch PHP websites without the worry of Server Management.

Pre-Installed Optimized Stack with Git, Composer & SSH

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

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!